class-wp-http-requests-response.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * HTTP API: WP_HTTP_Requests_Response class
  4. *
  5. * @package WordPress
  6. * @subpackage HTTP
  7. * @since 4.6.0
  8. */
  9. /**
  10. * Core wrapper object for a Requests_Response for standardisation.
  11. *
  12. * @since 4.6.0
  13. *
  14. * @see WP_HTTP_Response
  15. */
  16. class WP_HTTP_Requests_Response extends WP_HTTP_Response {
  17. /**
  18. * Requests Response object.
  19. *
  20. * @since 4.6.0
  21. * @var Requests_Response
  22. */
  23. protected $response;
  24. /**
  25. * Filename the response was saved to.
  26. *
  27. * @since 4.6.0
  28. * @var string|null
  29. */
  30. protected $filename;
  31. /**
  32. * Constructor.
  33. *
  34. * @since 4.6.0
  35. *
  36. * @param Requests_Response $response HTTP response.
  37. * @param string $filename Optional. File name. Default empty.
  38. */
  39. public function __construct( Requests_Response $response, $filename = '' ) {
  40. $this->response = $response;
  41. $this->filename = $filename;
  42. }
  43. /**
  44. * Retrieves the response object for the request.
  45. *
  46. * @since 4.6.0
  47. *
  48. * @return Requests_Response HTTP response.
  49. */
  50. public function get_response_object() {
  51. return $this->response;
  52. }
  53. /**
  54. * Retrieves headers associated with the response.
  55. *
  56. * @since 4.6.0
  57. *
  58. * @see \Requests_Utility_CaseInsensitiveDictionary
  59. *
  60. * @return \Requests_Utility_CaseInsensitiveDictionary Map of header name to header value.
  61. */
  62. public function get_headers() {
  63. // Ensure headers remain case-insensitive.
  64. $converted = new Requests_Utility_CaseInsensitiveDictionary();
  65. foreach ( $this->response->headers->getAll() as $key => $value ) {
  66. if ( count( $value ) === 1 ) {
  67. $converted[ $key ] = $value[0];
  68. } else {
  69. $converted[ $key ] = $value;
  70. }
  71. }
  72. return $converted;
  73. }
  74. /**
  75. * Sets all header values.
  76. *
  77. * @since 4.6.0
  78. *
  79. * @param array $headers Map of header name to header value.
  80. */
  81. public function set_headers( $headers ) {
  82. $this->response->headers = new Requests_Response_Headers( $headers );
  83. }
  84. /**
  85. * Sets a single HTTP header.
  86. *
  87. * @since 4.6.0
  88. *
  89. * @param string $key Header name.
  90. * @param string $value Header value.
  91. * @param bool $replace Optional. Whether to replace an existing header of the same name.
  92. * Default true.
  93. */
  94. public function header( $key, $value, $replace = true ) {
  95. if ( $replace ) {
  96. unset( $this->response->headers[ $key ] );
  97. }
  98. $this->response->headers[ $key ] = $value;
  99. }
  100. /**
  101. * Retrieves the HTTP return code for the response.
  102. *
  103. * @since 4.6.0
  104. *
  105. * @return int The 3-digit HTTP status code.
  106. */
  107. public function get_status() {
  108. return $this->response->status_code;
  109. }
  110. /**
  111. * Sets the 3-digit HTTP status code.
  112. *
  113. * @since 4.6.0
  114. *
  115. * @param int $code HTTP status.
  116. */
  117. public function set_status( $code ) {
  118. $this->response->status_code = absint( $code );
  119. }
  120. /**
  121. * Retrieves the response data.
  122. *
  123. * @since 4.6.0
  124. *
  125. * @return mixed Response data.
  126. */
  127. public function get_data() {
  128. return $this->response->body;
  129. }
  130. /**
  131. * Sets the response data.
  132. *
  133. * @since 4.6.0
  134. *
  135. * @param mixed $data Response data.
  136. */
  137. public function set_data( $data ) {
  138. $this->response->body = $data;
  139. }
  140. /**
  141. * Retrieves cookies from the response.
  142. *
  143. * @since 4.6.0
  144. *
  145. * @return WP_HTTP_Cookie[] List of cookie objects.
  146. */
  147. public function get_cookies() {
  148. $cookies = array();
  149. foreach ( $this->response->cookies as $cookie ) {
  150. $cookies[] = new WP_Http_Cookie( array(
  151. 'name' => $cookie->name,
  152. 'value' => urldecode( $cookie->value ),
  153. 'expires' => isset( $cookie->attributes['expires'] ) ? $cookie->attributes['expires'] : null,
  154. 'path' => isset( $cookie->attributes['path'] ) ? $cookie->attributes['path'] : null,
  155. 'domain' => isset( $cookie->attributes['domain'] ) ? $cookie->attributes['domain'] : null,
  156. ));
  157. }
  158. return $cookies;
  159. }
  160. /**
  161. * Converts the object to a WP_Http response array.
  162. *
  163. * @since 4.6.0
  164. *
  165. * @return array WP_Http response array, per WP_Http::request().
  166. */
  167. public function to_array() {
  168. return array(
  169. 'headers' => $this->get_headers(),
  170. 'body' => $this->get_data(),
  171. 'response' => array(
  172. 'code' => $this->get_status(),
  173. 'message' => get_status_header_desc( $this->get_status() ),
  174. ),
  175. 'cookies' => $this->get_cookies(),
  176. 'filename' => $this->filename,
  177. );
  178. }
  179. }