1 | <?php |
||
17 | class WP_REST_Response extends WP_HTTP_Response { |
||
18 | |||
19 | /** |
||
20 | * Links related to the response. |
||
21 | * |
||
22 | * @since 4.4.0 |
||
23 | * @access protected |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $links = array(); |
||
27 | |||
28 | /** |
||
29 | * The route that was to create the response. |
||
30 | * |
||
31 | * @since 4.4.0 |
||
32 | * @access protected |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $matched_route = ''; |
||
36 | |||
37 | /** |
||
38 | * The handler that was used to create the response. |
||
39 | * |
||
40 | * @since 4.4.0 |
||
41 | * @access protected |
||
42 | * @var null|array |
||
43 | */ |
||
44 | protected $matched_handler = null; |
||
45 | |||
46 | /** |
||
47 | * Adds a link to the response. |
||
48 | * |
||
49 | * @internal The $rel parameter is first, as this looks nicer when sending multiple. |
||
50 | * |
||
51 | * @since 4.4.0 |
||
52 | * @access public |
||
53 | * |
||
54 | * @link http://tools.ietf.org/html/rfc5988 |
||
55 | * @link http://www.iana.org/assignments/link-relations/link-relations.xml |
||
56 | * |
||
57 | * @param string $rel Link relation. Either an IANA registered type, |
||
58 | * or an absolute URL. |
||
59 | * @param string $href Target URI for the link. |
||
60 | * @param array $attributes Optional. Link parameters to send along with the URL. Default empty array. |
||
61 | */ |
||
62 | public function add_link( $rel, $href, $attributes = array() ) { |
||
77 | |||
78 | /** |
||
79 | * Removes a link from the response. |
||
80 | * |
||
81 | * @since 4.4.0 |
||
82 | * @access public |
||
83 | * |
||
84 | * @param string $rel Link relation. Either an IANA registered type, or an absolute URL. |
||
85 | * @param string $href Optional. Only remove links for the relation matching the given href. |
||
86 | * Default null. |
||
87 | */ |
||
88 | public function remove_link( $rel, $href = null ) { |
||
103 | |||
104 | /** |
||
105 | * Adds multiple links to the response. |
||
106 | * |
||
107 | * Link data should be an associative array with link relation as the key. |
||
108 | * The value can either be an associative array of link attributes |
||
109 | * (including `href` with the URL for the response), or a list of these |
||
110 | * associative arrays. |
||
111 | * |
||
112 | * @since 4.4.0 |
||
113 | * @access public |
||
114 | * |
||
115 | * @param array $links Map of link relation to list of links. |
||
116 | */ |
||
117 | public function add_links( $links ) { |
||
129 | |||
130 | /** |
||
131 | * Retrieves links for the response. |
||
132 | * |
||
133 | * @since 4.4.0 |
||
134 | * @access public |
||
135 | * |
||
136 | * @return array List of links. |
||
137 | */ |
||
138 | public function get_links() { |
||
141 | |||
142 | /** |
||
143 | * Sets a single link header. |
||
144 | * |
||
145 | * @internal The $rel parameter is first, as this looks nicer when sending multiple. |
||
146 | * |
||
147 | * @since 4.4.0 |
||
148 | * @access public |
||
149 | * |
||
150 | * @link http://tools.ietf.org/html/rfc5988 |
||
151 | * @link http://www.iana.org/assignments/link-relations/link-relations.xml |
||
152 | * |
||
153 | * @param string $rel Link relation. Either an IANA registered type, or an absolute URL. |
||
154 | * @param string $link Target IRI for the link. |
||
155 | * @param array $other Optional. Other parameters to send, as an assocative array. |
||
156 | * Default empty array. |
||
157 | */ |
||
158 | public function link_header( $rel, $link, $other = array() ) { |
||
169 | |||
170 | /** |
||
171 | * Retrieves the route that was used. |
||
172 | * |
||
173 | * @since 4.4.0 |
||
174 | * @access public |
||
175 | * |
||
176 | * @return string The matched route. |
||
177 | */ |
||
178 | public function get_matched_route() { |
||
181 | |||
182 | /** |
||
183 | * Sets the route (regex for path) that caused the response. |
||
184 | * |
||
185 | * @since 4.4.0 |
||
186 | * @access public |
||
187 | * |
||
188 | * @param string $route Route name. |
||
189 | */ |
||
190 | public function set_matched_route( $route ) { |
||
193 | |||
194 | /** |
||
195 | * Retrieves the handler that was used to generate the response. |
||
196 | * |
||
197 | * @since 4.4.0 |
||
198 | * @access public |
||
199 | * |
||
200 | * @return null|array The handler that was used to create the response. |
||
201 | */ |
||
202 | public function get_matched_handler() { |
||
205 | |||
206 | /** |
||
207 | * Retrieves the handler that was responsible for generating the response. |
||
208 | * |
||
209 | * @since 4.4.0 |
||
210 | * @access public |
||
211 | * |
||
212 | * @param array $handler The matched handler. |
||
213 | */ |
||
214 | public function set_matched_handler( $handler ) { |
||
217 | |||
218 | /** |
||
219 | * Checks if the response is an error, i.e. >= 400 response code. |
||
220 | * |
||
221 | * @since 4.4.0 |
||
222 | * @access public |
||
223 | * |
||
224 | * @return bool Whether the response is an error. |
||
225 | */ |
||
226 | public function is_error() { |
||
229 | |||
230 | /** |
||
231 | * Retrieves a WP_Error object from the response. |
||
232 | * |
||
233 | * @since 4.4.0 |
||
234 | * @access public |
||
235 | * |
||
236 | * @return WP_Error|null WP_Error or null on not an errored response. |
||
237 | */ |
||
238 | public function as_error() { |
||
259 | |||
260 | /** |
||
261 | * Retrieves the CURIEs (compact URIs) used for relations. |
||
262 | * |
||
263 | * @since 4.5.0 |
||
264 | * @access public |
||
265 | * |
||
266 | * @return array Compact URIs. |
||
267 | */ |
||
268 | public function get_curies() { |
||
269 | $curies = array( |
||
270 | array( |
||
271 | 'name' => 'wp', |
||
272 | 'href' => 'https://api.w.org/{rel}', |
||
273 | 'templated' => true, |
||
274 | ), |
||
275 | ); |
||
276 | |||
277 | /** |
||
278 | * Filter extra CURIEs available on API responses. |
||
279 | * |
||
280 | * CURIEs allow a shortened version of URI relations. This allows a more |
||
281 | * usable form for custom relations than using the full URI. These work |
||
282 | * similarly to how XML namespaces work. |
||
283 | * |
||
284 | * Registered CURIES need to specify a name and URI template. This will |
||
285 | * automatically transform URI relations into their shortened version. |
||
286 | * The shortened relation follows the format `{name}:{rel}`. `{rel}` in |
||
287 | * the URI template will be replaced with the `{rel}` part of the |
||
288 | * shortened relation. |
||
289 | * |
||
290 | * For example, a CURIE with name `example` and URI template |
||
291 | * `http://w.org/{rel}` would transform a `http://w.org/term` relation |
||
292 | * into `example:term`. |
||
293 | * |
||
294 | * Well-behaved clients should expand and normalise these back to their |
||
295 | * full URI relation, however some naive clients may not resolve these |
||
296 | * correctly, so adding new CURIEs may break backwards compatibility. |
||
297 | * |
||
298 | * @since 4.5.0 |
||
299 | * |
||
300 | * @param array $additional Additional CURIEs to register with the API. |
||
301 | */ |
||
302 | $additional = apply_filters( 'rest_response_link_curies', array() ); |
||
303 | return array_merge( $curies, $additional ); |
||
304 | } |
||
305 | } |
||
306 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: