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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. * @return \Requests_Utility_CaseInsensitiveDictionary Map of header name to header value.
  59. */
  60. public function get_headers() {
  61. // Ensure headers remain case-insensitive.
  62. $converted = new Requests_Utility_CaseInsensitiveDictionary();
  63. foreach ( $this->response->headers->getAll() as $key => $value ) {
  64. if ( count( $value ) === 1 ) {
  65. $converted[ $key ] = $value[0];
  66. } else {
  67. $converted[ $key ] = $value;
  68. }
  69. }
  70. return $converted;
  71. }
  72. /**
  73. * Sets all header values.
  74. *
  75. * @since 4.6.0
  76. *
  77. * @param array $headers Map of header name to header value.
  78. */
  79. public function set_headers( $headers ) {
  80. $this->response->headers = new Requests_Response_Headers( $headers );
  81. }
  82. /**
  83. * Sets a single HTTP header.
  84. *
  85. * @since 4.6.0
  86. *
  87. * @param string $key Header name.
  88. * @param string $value Header value.
  89. * @param bool $replace Optional. Whether to replace an existing header of the same name.
  90. * Default true.
  91. */
  92. public function header( $key, $value, $replace = true ) {
  93. if ( $replace ) {
  94. unset( $this->response->headers[ $key ] );
  95. }
  96. $this->response->headers[ $key ] = $value;
  97. }
  98. /**
  99. * Retrieves the HTTP return code for the response.
  100. *
  101. * @since 4.6.0
  102. *
  103. * @return int The 3-digit HTTP status code.
  104. */
  105. public function get_status() {
  106. return $this->response->status_code;
  107. }
  108. /**
  109. * Sets the 3-digit HTTP status code.
  110. *
  111. * @since 4.6.0
  112. *
  113. * @param int $code HTTP status.
  114. */
  115. public function set_status( $code ) {
  116. $this->response->status_code = absint( $code );
  117. }
  118. /**
  119. * Retrieves the response data.
  120. *
  121. * @since 4.6.0
  122. *
  123. * @return string Response data.
  124. */
  125. public function get_data() {
  126. return $this->response->body;
  127. }
  128. /**
  129. * Sets the response data.
  130. *
  131. * @since 4.6.0
  132. *
  133. * @param string $data Response data.
  134. */
  135. public function set_data( $data ) {
  136. $this->response->body = $data;
  137. }
  138. /**
  139. * Retrieves cookies from the response.
  140. *
  141. * @since 4.6.0
  142. *
  143. * @return WP_HTTP_Cookie[] List of cookie objects.
  144. */
  145. public function get_cookies() {
  146. $cookies = array();
  147. foreach ( $this->response->cookies as $cookie ) {
  148. $cookies[] = new WP_Http_Cookie(
  149. array(
  150. 'name' => $cookie->name,
  151. 'value' => urldecode( $cookie->value ),
  152. 'expires' => isset( $cookie->attributes['expires'] ) ? $cookie->attributes['expires'] : null,
  153. 'path' => isset( $cookie->attributes['path'] ) ? $cookie->attributes['path'] : null,
  154. 'domain' => isset( $cookie->attributes['domain'] ) ? $cookie->attributes['domain'] : null,
  155. 'host_only' => isset( $cookie->flags['host-only'] ) ? $cookie->flags['host-only'] : null,
  156. )
  157. );
  158. }
  159. return $cookies;
  160. }
  161. /**
  162. * Converts the object to a WP_Http response array.
  163. *
  164. * @since 4.6.0
  165. *
  166. * @return array WP_Http response array, per WP_Http::request().
  167. */
  168. public function to_array() {
  169. return array(
  170. 'headers' => $this->get_headers(),
  171. 'body' => $this->get_data(),
  172. 'response' => array(
  173. 'code' => $this->get_status(),
  174. 'message' => get_status_header_desc( $this->get_status() ),
  175. ),
  176. 'cookies' => $this->get_cookies(),
  177. 'filename' => $this->filename,
  178. );
  179. }
  180. }