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