|
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
|
|
|
* @return array Map of header name to header value. |
|
69
|
|
|
*/ |
|
70
|
|
|
public function get_headers() { |
|
71
|
|
|
// Ensure headers remain case-insensitive |
|
72
|
|
|
$converted = new Requests_Utility_CaseInsensitiveDictionary(); |
|
73
|
|
|
|
|
74
|
|
|
foreach ( $this->response->headers->getAll() as $key => $value ) { |
|
75
|
|
|
if ( count( $value ) === 1 ) { |
|
76
|
|
|
$converted[ $key ] = $value[0]; |
|
77
|
|
|
} |
|
78
|
|
|
else { |
|
79
|
|
|
$converted[ $key ] = $value; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $converted; |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Sets all header values. |
|
88
|
|
|
* |
|
89
|
|
|
* @since 4.6.0 |
|
90
|
|
|
* @access public |
|
91
|
|
|
* |
|
92
|
|
|
* @param array $headers Map of header name to header value. |
|
93
|
|
|
*/ |
|
94
|
|
|
public function set_headers( $headers ) { |
|
95
|
|
|
$this->response->headers = new Requests_Response_Headers( $headers ); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Sets a single HTTP header. |
|
100
|
|
|
* |
|
101
|
|
|
* @since 4.6.0 |
|
102
|
|
|
* @access public |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $key Header name. |
|
105
|
|
|
* @param string $value Header value. |
|
106
|
|
|
* @param bool $replace Optional. Whether to replace an existing header of the same name. |
|
107
|
|
|
* Default true. |
|
108
|
|
|
*/ |
|
109
|
|
|
public function header( $key, $value, $replace = true ) { |
|
110
|
|
|
if ( $replace ) { |
|
111
|
|
|
unset( $this->response->headers[ $key ] ); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$this->response->headers[ $key ] = $value; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Retrieves the HTTP return code for the response. |
|
119
|
|
|
* |
|
120
|
|
|
* @since 4.6.0 |
|
121
|
|
|
* @access public |
|
122
|
|
|
* |
|
123
|
|
|
* @return int The 3-digit HTTP status code. |
|
124
|
|
|
*/ |
|
125
|
|
|
public function get_status() { |
|
126
|
|
|
return $this->response->status_code; |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Sets the 3-digit HTTP status code. |
|
131
|
|
|
* |
|
132
|
|
|
* @since 4.6.0 |
|
133
|
|
|
* @access public |
|
134
|
|
|
* |
|
135
|
|
|
* @param int $code HTTP status. |
|
136
|
|
|
*/ |
|
137
|
|
|
public function set_status( $code ) { |
|
138
|
|
|
$this->response->status_code = absint( $code ); |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Retrieves the response data. |
|
143
|
|
|
* |
|
144
|
|
|
* @since 4.6.0 |
|
145
|
|
|
* @access public |
|
146
|
|
|
* |
|
147
|
|
|
* @return mixed Response data. |
|
148
|
|
|
*/ |
|
149
|
|
|
public function get_data() { |
|
150
|
|
|
return $this->response->body; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Sets the response data. |
|
155
|
|
|
* |
|
156
|
|
|
* @since 4.6.0 |
|
157
|
|
|
* @access public |
|
158
|
|
|
* |
|
159
|
|
|
* @param mixed $data Response data. |
|
160
|
|
|
*/ |
|
161
|
|
|
public function set_data( $data ) { |
|
162
|
|
|
$this->response->body = $data; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Retrieves cookies from the response. |
|
167
|
|
|
* |
|
168
|
|
|
* @since 4.6.0 |
|
169
|
|
|
* @access public |
|
170
|
|
|
* |
|
171
|
|
|
* @return WP_HTTP_Cookie[] List of cookie objects. |
|
172
|
|
|
*/ |
|
173
|
|
|
public function get_cookies() { |
|
174
|
|
|
$cookies = array(); |
|
175
|
|
|
foreach ( $this->response->cookies as $cookie ) { |
|
176
|
|
|
$cookies[] = new WP_Http_Cookie( array( |
|
177
|
|
|
'name' => $cookie->name, |
|
178
|
|
|
'value' => urldecode( $cookie->value ), |
|
179
|
|
|
'expires' => isset( $cookie->attributes['expires'] ) ? $cookie->attributes['expires'] : null, |
|
180
|
|
|
'path' => isset( $cookie->attributes['path'] ) ? $cookie->attributes['path'] : null, |
|
181
|
|
|
'domain' => isset( $cookie->attributes['domain'] ) ? $cookie->attributes['domain'] : null, |
|
182
|
|
|
)); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return $cookies; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Converts the object to a WP_Http response array. |
|
190
|
|
|
* |
|
191
|
|
|
* @since 4.6.0 |
|
192
|
|
|
* @access public |
|
193
|
|
|
* |
|
194
|
|
|
* @return array WP_Http response array, per WP_Http::request(). |
|
195
|
|
|
*/ |
|
196
|
|
|
public function to_array() { |
|
197
|
|
|
return array( |
|
198
|
|
|
'headers' => $this->get_headers(), |
|
199
|
|
|
'body' => $this->get_data(), |
|
200
|
|
|
'response' => array( |
|
201
|
|
|
'code' => $this->get_status(), |
|
202
|
|
|
'message' => get_status_header_desc( $this->get_status() ), |
|
|
|
|
|
|
203
|
|
|
), |
|
204
|
|
|
'cookies' => $this->get_cookies(), |
|
205
|
|
|
'filename' => $this->filename, |
|
206
|
|
|
); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.