You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

210 lines
4.4 KiB

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