[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
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 /** 11 * Core wrapper object for a Requests_Response for standardisation. 12 * 13 * @since 4.6.0 14 * 15 * @see WP_HTTP_Response 16 */ 17 class WP_HTTP_Requests_Response extends WP_HTTP_Response { 18 /** 19 * Requests Response object. 20 * 21 * @since 4.6.0 22 * @var Requests_Response 23 */ 24 protected $response; 25 26 /** 27 * Filename the response was saved to. 28 * 29 * @since 4.6.0 30 * @var string|null 31 */ 32 protected $filename; 33 34 /** 35 * Constructor. 36 * 37 * @since 4.6.0 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 /** 48 * Retrieves the response object for the request. 49 * 50 * @since 4.6.0 51 * 52 * @return Requests_Response HTTP response. 53 */ 54 public function get_response_object() { 55 return $this->response; 56 } 57 58 /** 59 * Retrieves headers associated with the response. 60 * 61 * @since 4.6.0 62 * 63 * @return \Requests_Utility_CaseInsensitiveDictionary Map of header name to header value. 64 */ 65 public function get_headers() { 66 // Ensure headers remain case-insensitive. 67 $converted = new Requests_Utility_CaseInsensitiveDictionary(); 68 69 foreach ( $this->response->headers->getAll() as $key => $value ) { 70 if ( count( $value ) === 1 ) { 71 $converted[ $key ] = $value[0]; 72 } else { 73 $converted[ $key ] = $value; 74 } 75 } 76 77 return $converted; 78 } 79 80 /** 81 * Sets all header values. 82 * 83 * @since 4.6.0 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 /** 92 * Sets a single HTTP header. 93 * 94 * @since 4.6.0 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 106 $this->response->headers[ $key ] = $value; 107 } 108 109 /** 110 * Retrieves the HTTP return code for the response. 111 * 112 * @since 4.6.0 113 * 114 * @return int The 3-digit HTTP status code. 115 */ 116 public function get_status() { 117 return $this->response->status_code; 118 } 119 120 /** 121 * Sets the 3-digit HTTP status code. 122 * 123 * @since 4.6.0 124 * 125 * @param int $code HTTP status. 126 */ 127 public function set_status( $code ) { 128 $this->response->status_code = absint( $code ); 129 } 130 131 /** 132 * Retrieves the response data. 133 * 134 * @since 4.6.0 135 * 136 * @return string Response data. 137 */ 138 public function get_data() { 139 return $this->response->body; 140 } 141 142 /** 143 * Sets the response data. 144 * 145 * @since 4.6.0 146 * 147 * @param string $data Response data. 148 */ 149 public function set_data( $data ) { 150 $this->response->body = $data; 151 } 152 153 /** 154 * Retrieves cookies from the response. 155 * 156 * @since 4.6.0 157 * 158 * @return WP_HTTP_Cookie[] List of cookie objects. 159 */ 160 public function get_cookies() { 161 $cookies = array(); 162 foreach ( $this->response->cookies as $cookie ) { 163 $cookies[] = new WP_Http_Cookie( 164 array( 165 'name' => $cookie->name, 166 'value' => urldecode( $cookie->value ), 167 'expires' => isset( $cookie->attributes['expires'] ) ? $cookie->attributes['expires'] : null, 168 'path' => isset( $cookie->attributes['path'] ) ? $cookie->attributes['path'] : null, 169 'domain' => isset( $cookie->attributes['domain'] ) ? $cookie->attributes['domain'] : null, 170 'host_only' => isset( $cookie->flags['host-only'] ) ? $cookie->flags['host-only'] : null, 171 ) 172 ); 173 } 174 175 return $cookies; 176 } 177 178 /** 179 * Converts the object to a WP_Http response array. 180 * 181 * @since 4.6.0 182 * 183 * @return array WP_Http response array, per WP_Http::request(). 184 */ 185 public function to_array() { 186 return array( 187 'headers' => $this->get_headers(), 188 'body' => $this->get_data(), 189 'response' => array( 190 'code' => $this->get_status(), 191 'message' => get_status_header_desc( $this->get_status() ), 192 ), 193 'cookies' => $this->get_cookies(), 194 'filename' => $this->filename, 195 ); 196 } 197 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Dec 24 01:00:02 2024 | Cross-referenced by PHPXref 0.7.1 |