1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* HTTP API: WP_HTTP_Response class |
4
|
|
|
* |
5
|
|
|
* @package WordPress |
6
|
|
|
* @subpackage HTTP |
7
|
|
|
* @since 4.4.0 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Core class used to prepare HTTP responses. |
12
|
|
|
* |
13
|
|
|
* @since 4.4.0 |
14
|
|
|
*/ |
15
|
|
|
class WP_HTTP_Response { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Response data. |
19
|
|
|
* |
20
|
|
|
* @since 4.4.0 |
21
|
|
|
* @access public |
22
|
|
|
* @var mixed |
23
|
|
|
*/ |
24
|
|
|
public $data; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Response headers. |
28
|
|
|
* |
29
|
|
|
* @since 4.4.0 |
30
|
|
|
* @access public |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
public $headers; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Response status. |
37
|
|
|
* |
38
|
|
|
* @since 4.4.0 |
39
|
|
|
* @access public |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
public $status; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor. |
46
|
|
|
* |
47
|
|
|
* @since 4.4.0 |
48
|
|
|
* @access public |
49
|
|
|
* |
50
|
|
|
* @param mixed $data Response data. Default null. |
51
|
|
|
* @param int $status Optional. HTTP status code. Default 200. |
52
|
|
|
* @param array $headers Optional. HTTP header map. Default empty array. |
53
|
|
|
*/ |
54
|
|
|
public function __construct( $data = null, $status = 200, $headers = array() ) { |
55
|
|
|
$this->data = $data; |
56
|
|
|
$this->set_status( $status ); |
57
|
|
|
$this->set_headers( $headers ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Retrieves headers associated with the response. |
62
|
|
|
* |
63
|
|
|
* @since 4.4.0 |
64
|
|
|
* @access public |
65
|
|
|
* |
66
|
|
|
* @return array Map of header name to header value. |
67
|
|
|
*/ |
68
|
|
|
public function get_headers() { |
69
|
|
|
return $this->headers; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Sets all header values. |
74
|
|
|
* |
75
|
|
|
* @since 4.4.0 |
76
|
|
|
* @access public |
77
|
|
|
* |
78
|
|
|
* @param array $headers Map of header name to header value. |
79
|
|
|
*/ |
80
|
|
|
public function set_headers( $headers ) { |
81
|
|
|
$this->headers = $headers; |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Sets a single HTTP header. |
86
|
|
|
* |
87
|
|
|
* @since 4.4.0 |
88
|
|
|
* @access public |
89
|
|
|
* |
90
|
|
|
* @param string $key Header name. |
91
|
|
|
* @param string $value Header value. |
92
|
|
|
* @param bool $replace Optional. Whether to replace an existing header of the same name. |
93
|
|
|
* Default true. |
94
|
|
|
*/ |
95
|
|
|
public function header( $key, $value, $replace = true ) { |
96
|
|
|
if ( $replace || ! isset( $this->headers[ $key ] ) ) { |
97
|
|
|
$this->headers[ $key ] = $value; |
98
|
|
|
} else { |
99
|
|
|
$this->headers[ $key ] .= ', ' . $value; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Retrieves the HTTP return code for the response. |
105
|
|
|
* |
106
|
|
|
* @since 4.4.0 |
107
|
|
|
* @access public |
108
|
|
|
* |
109
|
|
|
* @return int The 3-digit HTTP status code. |
110
|
|
|
*/ |
111
|
|
|
public function get_status() { |
112
|
|
|
return $this->status; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Sets the 3-digit HTTP status code. |
117
|
|
|
* |
118
|
|
|
* @since 4.4.0 |
119
|
|
|
* @access public |
120
|
|
|
* |
121
|
|
|
* @param int $code HTTP status. |
122
|
|
|
*/ |
123
|
|
|
public function set_status( $code ) { |
124
|
|
|
$this->status = absint( $code ); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Retrieves the response data. |
129
|
|
|
* |
130
|
|
|
* @since 4.4.0 |
131
|
|
|
* @access public |
132
|
|
|
* |
133
|
|
|
* @return mixed Response data. |
134
|
|
|
*/ |
135
|
|
|
public function get_data() { |
136
|
|
|
return $this->data; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Sets the response data. |
141
|
|
|
* |
142
|
|
|
* @since 4.4.0 |
143
|
|
|
* @access public |
144
|
|
|
* |
145
|
|
|
* @param mixed $data Response data. |
146
|
|
|
*/ |
147
|
|
|
public function set_data( $data ) { |
148
|
|
|
$this->data = $data; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Retrieves the response data for JSON serialization. |
153
|
|
|
* |
154
|
|
|
* It is expected that in most implementations, this will return the same as get_data(), |
155
|
|
|
* however this may be different if you want to do custom JSON data handling. |
156
|
|
|
* |
157
|
|
|
* @since 4.4.0 |
158
|
|
|
* @access public |
159
|
|
|
* |
160
|
|
|
* @return mixed Any JSON-serializable value. |
161
|
|
|
*/ |
162
|
|
|
public function jsonSerialize() { |
163
|
|
|
return $this->get_data(); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..