Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like WPCOM_JSON_API_Endpoint often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WPCOM_JSON_API_Endpoint, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | abstract class WPCOM_JSON_API_Endpoint { |
||
7 | // The API Object |
||
8 | public $api; |
||
9 | |||
10 | public $pass_wpcom_user_details = false; |
||
11 | public $can_use_user_details_instead_of_blog_membership = false; |
||
12 | |||
13 | // One liner. |
||
14 | public $description; |
||
15 | |||
16 | // Object Grouping For Documentation (Users, Posts, Comments) |
||
17 | public $group; |
||
18 | |||
19 | // Stats extra value to bump |
||
20 | public $stat; |
||
21 | |||
22 | // HTTP Method |
||
23 | public $method = 'GET'; |
||
24 | |||
25 | // Minimum version of the api for which to serve this endpoint |
||
26 | public $min_version = '0'; |
||
27 | |||
28 | // Maximum version of the api for which to serve this endpoint |
||
29 | public $max_version = WPCOM_JSON_API__CURRENT_VERSION; |
||
30 | |||
31 | // Path at which to serve this endpoint: sprintf() format. |
||
32 | public $path = ''; |
||
33 | |||
34 | // Identifiers to fill sprintf() formatted $path |
||
35 | public $path_labels = array(); |
||
36 | |||
37 | // Accepted query parameters |
||
38 | public $query = array( |
||
39 | // Parameter name |
||
40 | 'context' => array( |
||
41 | // Default value => description |
||
42 | 'display' => 'Formats the output as HTML for display. Shortcodes are parsed, paragraph tags are added, etc..', |
||
43 | // Other possible values => description |
||
44 | 'edit' => 'Formats the output for editing. Shortcodes are left unparsed, significant whitespace is kept, etc..', |
||
45 | ), |
||
46 | 'http_envelope' => array( |
||
47 | 'false' => '', |
||
48 | 'true' => 'Some environments (like in-browser JavaScript or Flash) block or divert responses with a non-200 HTTP status code. Setting this parameter will force the HTTP status code to always be 200. The JSON response is wrapped in an "envelope" containing the "real" HTTP status code and headers.', |
||
49 | ), |
||
50 | 'pretty' => array( |
||
51 | 'false' => '', |
||
52 | 'true' => 'Output pretty JSON', |
||
53 | ), |
||
54 | 'meta' => "(string) Optional. Loads data from the endpoints found in the 'meta' part of the response. Comma-separated list. Example: meta=site,likes", |
||
55 | 'fields' => '(string) Optional. Returns specified fields only. Comma-separated list. Example: fields=ID,title', |
||
56 | // Parameter name => description (default value is empty) |
||
57 | 'callback' => '(string) An optional JSONP callback function.', |
||
58 | ); |
||
59 | |||
60 | // Response format |
||
61 | public $response_format = array(); |
||
62 | |||
63 | // Request format |
||
64 | public $request_format = array(); |
||
65 | |||
66 | // Is this endpoint still in testing phase? If so, not available to the public. |
||
67 | public $in_testing = false; |
||
68 | |||
69 | // Is this endpoint still allowed if the site in question is flagged? |
||
70 | public $allowed_if_flagged = false; |
||
71 | |||
72 | /** |
||
73 | * @var string Version of the API |
||
74 | */ |
||
75 | public $version = ''; |
||
76 | |||
77 | /** |
||
78 | * @var string Example request to make |
||
79 | */ |
||
80 | public $example_request = ''; |
||
81 | |||
82 | /** |
||
83 | * @var string Example request data (for POST methods) |
||
84 | */ |
||
85 | public $example_request_data = ''; |
||
86 | |||
87 | /** |
||
88 | * @var string Example response from $example_request |
||
89 | */ |
||
90 | public $example_response = ''; |
||
91 | |||
92 | /** |
||
93 | * @var bool Set to true if the endpoint implements its own filtering instead of the standard `fields` query method |
||
94 | */ |
||
95 | public $custom_fields_filtering = false; |
||
96 | |||
97 | /** |
||
98 | * @var bool Set to true if the endpoint accepts all cross origin requests. You probably should not set this flag. |
||
99 | */ |
||
100 | public $allow_cross_origin_request = false; |
||
101 | |||
102 | /** |
||
103 | * @var bool Set to true if the endpoint can recieve unauthorized POST requests. |
||
104 | */ |
||
105 | public $allow_unauthorized_request = false; |
||
106 | |||
107 | /** |
||
108 | * @var bool Set to true if the endpoint should accept site based (not user based) authentication. |
||
109 | */ |
||
110 | public $allow_jetpack_site_auth = false; |
||
111 | |||
112 | function __construct( $args ) { |
||
113 | $defaults = array( |
||
114 | 'in_testing' => false, |
||
115 | 'allowed_if_flagged' => false, |
||
116 | 'description' => '', |
||
117 | 'group' => '', |
||
118 | 'method' => 'GET', |
||
119 | 'path' => '/', |
||
120 | 'min_version' => '0', |
||
121 | 'max_version' => WPCOM_JSON_API__CURRENT_VERSION, |
||
122 | 'force' => '', |
||
123 | 'deprecated' => false, |
||
124 | 'new_version' => WPCOM_JSON_API__CURRENT_VERSION, |
||
125 | 'jp_disabled' => false, |
||
126 | 'path_labels' => array(), |
||
127 | 'request_format' => array(), |
||
128 | 'response_format' => array(), |
||
129 | 'query_parameters' => array(), |
||
130 | 'version' => 'v1', |
||
131 | 'example_request' => '', |
||
132 | 'example_request_data' => '', |
||
133 | 'example_response' => '', |
||
134 | 'required_scope' => '', |
||
135 | 'pass_wpcom_user_details' => false, |
||
136 | 'can_use_user_details_instead_of_blog_membership' => false, |
||
137 | 'custom_fields_filtering' => false, |
||
138 | 'allow_cross_origin_request' => false, |
||
139 | 'allow_unauthorized_request' => false, |
||
140 | 'allow_jetpack_site_auth' => false, |
||
141 | ); |
||
142 | |||
143 | $args = wp_parse_args( $args, $defaults ); |
||
144 | |||
145 | $this->in_testing = $args['in_testing']; |
||
|
|||
146 | |||
147 | $this->allowed_if_flagged = $args['allowed_if_flagged']; |
||
148 | |||
149 | $this->description = $args['description']; |
||
150 | $this->group = $args['group']; |
||
151 | $this->stat = $args['stat']; |
||
152 | $this->force = $args['force']; |
||
153 | $this->jp_disabled = $args['jp_disabled']; |
||
154 | |||
155 | $this->method = $args['method']; |
||
156 | $this->path = $args['path']; |
||
157 | $this->path_labels = $args['path_labels']; |
||
158 | $this->min_version = $args['min_version']; |
||
159 | $this->max_version = $args['max_version']; |
||
160 | $this->deprecated = $args['deprecated']; |
||
161 | $this->new_version = $args['new_version']; |
||
162 | |||
163 | $this->pass_wpcom_user_details = $args['pass_wpcom_user_details']; |
||
164 | $this->custom_fields_filtering = (bool) $args['custom_fields_filtering']; |
||
165 | $this->can_use_user_details_instead_of_blog_membership = $args['can_use_user_details_instead_of_blog_membership']; |
||
166 | |||
167 | $this->allow_cross_origin_request = (bool) $args['allow_cross_origin_request']; |
||
168 | $this->allow_unauthorized_request = (bool) $args['allow_unauthorized_request']; |
||
169 | $this->allow_jetpack_site_auth = (bool) $args['allow_jetpack_site_auth']; |
||
170 | |||
171 | $this->version = $args['version']; |
||
172 | |||
173 | $this->required_scope = $args['required_scope']; |
||
174 | |||
175 | View Code Duplication | if ( $this->request_format ) { |
|
176 | $this->request_format = array_filter( array_merge( $this->request_format, $args['request_format'] ) ); |
||
177 | } else { |
||
178 | $this->request_format = $args['request_format']; |
||
179 | } |
||
180 | |||
181 | View Code Duplication | if ( $this->response_format ) { |
|
182 | $this->response_format = array_filter( array_merge( $this->response_format, $args['response_format'] ) ); |
||
183 | } else { |
||
184 | $this->response_format = $args['response_format']; |
||
185 | } |
||
186 | |||
187 | if ( false === $args['query_parameters'] ) { |
||
188 | $this->query = array(); |
||
189 | } elseif ( is_array( $args['query_parameters'] ) ) { |
||
190 | $this->query = array_filter( array_merge( $this->query, $args['query_parameters'] ) ); |
||
191 | } |
||
192 | |||
193 | $this->api = WPCOM_JSON_API::init(); // Auto-add to WPCOM_JSON_API |
||
194 | |||
195 | /** Example Request/Response ******************************************/ |
||
196 | |||
197 | // Examples for endpoint documentation request |
||
198 | $this->example_request = $args['example_request']; |
||
199 | $this->example_request_data = $args['example_request_data']; |
||
200 | $this->example_response = $args['example_response']; |
||
201 | |||
202 | $this->api->add( $this ); |
||
203 | } |
||
204 | |||
205 | // Get all query args. Prefill with defaults |
||
206 | function query_args( $return_default_values = true, $cast_and_filter = true ) { |
||
207 | $args = array_intersect_key( $this->api->query, $this->query ); |
||
208 | |||
209 | if ( !$cast_and_filter ) { |
||
210 | return $args; |
||
211 | } |
||
212 | |||
213 | return $this->cast_and_filter( $args, $this->query, $return_default_values ); |
||
214 | } |
||
215 | |||
216 | // Get POST body data |
||
217 | function input( $return_default_values = true, $cast_and_filter = true ) { |
||
218 | $input = trim( $this->api->post_body ); |
||
219 | $content_type = $this->api->content_type; |
||
220 | if ( $content_type ) { |
||
221 | list ( $content_type ) = explode( ';', $content_type ); |
||
222 | } |
||
223 | $content_type = trim( $content_type ); |
||
224 | switch ( $content_type ) { |
||
225 | case 'application/json' : |
||
226 | case 'application/x-javascript' : |
||
227 | case 'text/javascript' : |
||
228 | case 'text/x-javascript' : |
||
229 | case 'text/x-json' : |
||
230 | case 'text/json' : |
||
231 | $return = json_decode( $input, true ); |
||
232 | |||
233 | if ( function_exists( 'json_last_error' ) ) { |
||
234 | if ( JSON_ERROR_NONE !== json_last_error() ) { |
||
235 | return null; |
||
236 | } |
||
237 | } else { |
||
238 | if ( is_null( $return ) && json_encode( null ) !== $input ) { |
||
239 | return null; |
||
240 | } |
||
241 | } |
||
242 | |||
243 | break; |
||
244 | case 'multipart/form-data' : |
||
245 | $return = array_merge( stripslashes_deep( $_POST ), $_FILES ); |
||
246 | break; |
||
247 | case 'application/x-www-form-urlencoded' : |
||
248 | //attempt JSON first, since probably a curl command |
||
249 | $return = json_decode( $input, true ); |
||
250 | |||
251 | if ( is_null( $return ) ) { |
||
252 | wp_parse_str( $input, $return ); |
||
253 | } |
||
254 | |||
255 | break; |
||
256 | default : |
||
257 | wp_parse_str( $input, $return ); |
||
258 | break; |
||
259 | } |
||
260 | |||
261 | if ( !$cast_and_filter ) { |
||
262 | return $return; |
||
263 | } |
||
264 | |||
265 | return $this->cast_and_filter( $return, $this->request_format, $return_default_values ); |
||
266 | } |
||
267 | |||
268 | function cast_and_filter( $data, $documentation, $return_default_values = false, $for_output = false ) { |
||
269 | $return_as_object = false; |
||
270 | if ( is_object( $data ) ) { |
||
271 | // @todo this should probably be a deep copy if $data can ever have nested objects |
||
272 | $data = (array) $data; |
||
273 | $return_as_object = true; |
||
274 | } elseif ( !is_array( $data ) ) { |
||
275 | return $data; |
||
276 | } |
||
277 | |||
278 | $boolean_arg = array( 'false', 'true' ); |
||
279 | $naeloob_arg = array( 'true', 'false' ); |
||
280 | |||
281 | $return = array(); |
||
282 | |||
283 | foreach ( $documentation as $key => $description ) { |
||
284 | if ( is_array( $description ) ) { |
||
285 | // String or boolean array keys only |
||
286 | $whitelist = array_keys( $description ); |
||
287 | |||
288 | if ( $whitelist === $boolean_arg || $whitelist === $naeloob_arg ) { |
||
289 | // Truthiness |
||
290 | if ( isset( $data[$key] ) ) { |
||
291 | $return[$key] = (bool) WPCOM_JSON_API::is_truthy( $data[$key] ); |
||
292 | } elseif ( $return_default_values ) { |
||
293 | $return[$key] = $whitelist === $naeloob_arg; // Default to true for naeloob_arg and false for boolean_arg. |
||
294 | } |
||
295 | } elseif ( isset( $data[$key] ) && isset( $description[$data[$key]] ) ) { |
||
296 | // String Key |
||
297 | $return[$key] = (string) $data[$key]; |
||
298 | } elseif ( $return_default_values ) { |
||
299 | // Default value |
||
300 | $return[$key] = (string) current( $whitelist ); |
||
301 | } |
||
302 | |||
303 | continue; |
||
304 | } |
||
305 | |||
306 | $types = $this->parse_types( $description ); |
||
307 | $type = array_shift( $types ); |
||
308 | |||
309 | // Explicit default - string and int only for now. Always set these reguardless of $return_default_values |
||
310 | if ( isset( $type['default'] ) ) { |
||
311 | if ( !isset( $data[$key] ) ) { |
||
312 | $data[$key] = $type['default']; |
||
313 | } |
||
314 | } |
||
315 | |||
316 | if ( !isset( $data[$key] ) ) { |
||
317 | continue; |
||
318 | } |
||
319 | |||
320 | $this->cast_and_filter_item( $return, $type, $key, $data[$key], $types, $for_output ); |
||
321 | } |
||
322 | |||
323 | if ( $return_as_object ) { |
||
324 | return (object) $return; |
||
325 | } |
||
326 | |||
327 | return $return; |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * Casts $value according to $type. |
||
332 | * Handles fallbacks for certain values of $type when $value is not that $type |
||
333 | * Currently, only handles fallback between string <-> array (two way), from string -> false (one way), and from object -> false (one way) |
||
334 | * |
||
335 | * Handles "child types" - array:URL, object:category |
||
336 | * array:URL means an array of URLs |
||
337 | * object:category means a hash of categories |
||
338 | * |
||
339 | * Handles object typing - object>post means an object of type post |
||
340 | */ |
||
341 | function cast_and_filter_item( &$return, $type, $key, $value, $types = array(), $for_output = false ) { |
||
342 | if ( is_string( $type ) ) { |
||
343 | $type = compact( 'type' ); |
||
344 | } |
||
345 | |||
346 | switch ( $type['type'] ) { |
||
347 | case 'false' : |
||
348 | $return[$key] = false; |
||
349 | break; |
||
350 | case 'url' : |
||
351 | $return[$key] = (string) esc_url_raw( $value ); |
||
352 | break; |
||
353 | case 'string' : |
||
354 | // Fallback string -> array, or string -> object |
||
355 | if ( is_array( $value ) || is_object( $value ) ) { |
||
356 | View Code Duplication | if ( !empty( $types[0] ) ) { |
|
357 | $next_type = array_shift( $types ); |
||
358 | return $this->cast_and_filter_item( $return, $next_type, $key, $value, $types, $for_output ); |
||
359 | } |
||
360 | } |
||
361 | |||
362 | // Fallback string -> false |
||
363 | View Code Duplication | if ( !is_string( $value ) ) { |
|
364 | if ( !empty( $types[0] ) && 'false' === $types[0]['type'] ) { |
||
365 | $next_type = array_shift( $types ); |
||
366 | return $this->cast_and_filter_item( $return, $next_type, $key, $value, $types, $for_output ); |
||
367 | } |
||
368 | } |
||
369 | $return[$key] = (string) $value; |
||
370 | break; |
||
371 | case 'html' : |
||
372 | $return[$key] = (string) $value; |
||
373 | break; |
||
374 | case 'safehtml' : |
||
375 | $return[$key] = wp_kses( (string) $value, wp_kses_allowed_html() ); |
||
376 | break; |
||
377 | case 'media' : |
||
378 | if ( is_array( $value ) ) { |
||
379 | if ( isset( $value['name'] ) ) { |
||
380 | // It's a $_FILES array |
||
381 | // Reformat into array of $_FILES items |
||
382 | |||
383 | $files = array(); |
||
384 | foreach ( $value['name'] as $k => $v ) { |
||
385 | $files[$k] = array(); |
||
386 | foreach ( array_keys( $value ) as $file_key ) { |
||
387 | $files[$k][$file_key] = $value[$file_key][$k]; |
||
388 | } |
||
389 | } |
||
390 | |||
391 | $return[$key] = $files; |
||
392 | break; |
||
393 | } |
||
394 | } else { |
||
395 | // no break - treat as 'array' |
||
396 | } |
||
397 | // nobreak |
||
398 | case 'array' : |
||
399 | // Fallback array -> string |
||
400 | View Code Duplication | if ( is_string( $value ) ) { |
|
401 | if ( !empty( $types[0] ) ) { |
||
402 | $next_type = array_shift( $types ); |
||
403 | return $this->cast_and_filter_item( $return, $next_type, $key, $value, $types, $for_output ); |
||
404 | } |
||
405 | } |
||
406 | |||
407 | View Code Duplication | if ( isset( $type['children'] ) ) { |
|
408 | $children = array(); |
||
409 | foreach ( (array) $value as $k => $child ) { |
||
410 | $this->cast_and_filter_item( $children, $type['children'], $k, $child, array(), $for_output ); |
||
411 | } |
||
412 | $return[$key] = (array) $children; |
||
413 | break; |
||
414 | } |
||
415 | |||
416 | $return[$key] = (array) $value; |
||
417 | break; |
||
418 | case 'iso 8601 datetime' : |
||
419 | case 'datetime' : |
||
420 | // (string)s |
||
421 | $dates = $this->parse_date( (string) $value ); |
||
422 | if ( $for_output ) { |
||
423 | $return[$key] = $this->format_date( $dates[1], $dates[0] ); |
||
424 | } else { |
||
425 | list( $return[$key], $return["{$key}_gmt"] ) = $dates; |
||
426 | } |
||
427 | break; |
||
428 | case 'float' : |
||
429 | $return[$key] = (float) $value; |
||
430 | break; |
||
431 | case 'int' : |
||
432 | case 'integer' : |
||
433 | $return[$key] = (int) $value; |
||
434 | break; |
||
435 | case 'bool' : |
||
436 | case 'boolean' : |
||
437 | $return[$key] = (bool) WPCOM_JSON_API::is_truthy( $value ); |
||
438 | break; |
||
439 | case 'object' : |
||
440 | // Fallback object -> false |
||
441 | View Code Duplication | if ( is_scalar( $value ) || is_null( $value ) ) { |
|
442 | if ( !empty( $types[0] ) && 'false' === $types[0]['type'] ) { |
||
443 | return $this->cast_and_filter_item( $return, 'false', $key, $value, $types, $for_output ); |
||
444 | } |
||
445 | } |
||
446 | |||
447 | View Code Duplication | if ( isset( $type['children'] ) ) { |
|
448 | $children = array(); |
||
449 | foreach ( (array) $value as $k => $child ) { |
||
450 | $this->cast_and_filter_item( $children, $type['children'], $k, $child, array(), $for_output ); |
||
451 | } |
||
452 | $return[$key] = (object) $children; |
||
453 | break; |
||
454 | } |
||
455 | |||
456 | if ( isset( $type['subtype'] ) ) { |
||
457 | return $this->cast_and_filter_item( $return, $type['subtype'], $key, $value, $types, $for_output ); |
||
458 | } |
||
459 | |||
460 | $return[$key] = (object) $value; |
||
461 | break; |
||
462 | case 'post' : |
||
463 | $return[$key] = (object) $this->cast_and_filter( $value, $this->post_object_format, false, $for_output ); |
||
464 | break; |
||
465 | case 'comment' : |
||
466 | $return[$key] = (object) $this->cast_and_filter( $value, $this->comment_object_format, false, $for_output ); |
||
467 | break; |
||
468 | case 'tag' : |
||
469 | case 'category' : |
||
470 | $docs = array( |
||
471 | 'ID' => '(int)', |
||
472 | 'name' => '(string)', |
||
473 | 'slug' => '(string)', |
||
474 | 'description' => '(HTML)', |
||
475 | 'post_count' => '(int)', |
||
476 | 'meta' => '(object)', |
||
477 | ); |
||
478 | if ( 'category' === $type['type'] ) { |
||
479 | $docs['parent'] = '(int)'; |
||
480 | } |
||
481 | $return[$key] = (object) $this->cast_and_filter( $value, $docs, false, $for_output ); |
||
482 | break; |
||
483 | case 'post_reference' : |
||
484 | View Code Duplication | case 'comment_reference' : |
|
485 | $docs = array( |
||
486 | 'ID' => '(int)', |
||
487 | 'type' => '(string)', |
||
488 | 'title' => '(string)', |
||
489 | 'link' => '(URL)', |
||
490 | ); |
||
491 | $return[$key] = (object) $this->cast_and_filter( $value, $docs, false, $for_output ); |
||
492 | break; |
||
493 | View Code Duplication | case 'geo' : |
|
494 | $docs = array( |
||
495 | 'latitude' => '(float)', |
||
496 | 'longitude' => '(float)', |
||
497 | 'address' => '(string)', |
||
498 | ); |
||
499 | $return[$key] = (object) $this->cast_and_filter( $value, $docs, false, $for_output ); |
||
500 | break; |
||
501 | case 'author' : |
||
502 | $docs = array( |
||
503 | 'ID' => '(int)', |
||
504 | 'user_login' => '(string)', |
||
505 | 'login' => '(string)', |
||
506 | 'email' => '(string|false)', |
||
507 | 'name' => '(string)', |
||
508 | 'first_name' => '(string)', |
||
509 | 'last_name' => '(string)', |
||
510 | 'nice_name' => '(string)', |
||
511 | 'URL' => '(URL)', |
||
512 | 'avatar_URL' => '(URL)', |
||
513 | 'profile_URL' => '(URL)', |
||
514 | 'is_super_admin' => '(bool)', |
||
515 | 'roles' => '(array:string)' |
||
516 | ); |
||
517 | $return[$key] = (object) $this->cast_and_filter( $value, $docs, false, $for_output ); |
||
518 | break; |
||
519 | View Code Duplication | case 'role' : |
|
520 | $docs = array( |
||
521 | 'name' => '(string)', |
||
522 | 'display_name' => '(string)', |
||
523 | 'capabilities' => '(object:boolean)', |
||
524 | ); |
||
525 | $return[$key] = (object) $this->cast_and_filter( $value, $docs, false, $for_output ); |
||
526 | break; |
||
527 | case 'attachment' : |
||
528 | $docs = array( |
||
529 | 'ID' => '(int)', |
||
530 | 'URL' => '(URL)', |
||
531 | 'guid' => '(string)', |
||
532 | 'mime_type' => '(string)', |
||
533 | 'width' => '(int)', |
||
534 | 'height' => '(int)', |
||
535 | 'duration' => '(int)', |
||
536 | ); |
||
537 | $return[$key] = (object) $this->cast_and_filter( |
||
538 | $value, |
||
539 | /** |
||
540 | * Filter the documentation returned for a post attachment. |
||
541 | * |
||
542 | * @module json-api |
||
543 | * |
||
544 | * @since 1.9.0 |
||
545 | * |
||
546 | * @param array $docs Array of documentation about a post attachment. |
||
547 | */ |
||
548 | apply_filters( 'wpcom_json_api_attachment_cast_and_filter', $docs ), |
||
549 | false, |
||
550 | $for_output |
||
551 | ); |
||
552 | break; |
||
553 | case 'metadata' : |
||
554 | $docs = array( |
||
555 | 'id' => '(int)', |
||
556 | 'key' => '(string)', |
||
557 | 'value' => '(string|false|float|int|array|object)', |
||
558 | 'previous_value' => '(string)', |
||
559 | 'operation' => '(string)', |
||
560 | ); |
||
561 | $return[$key] = (object) $this->cast_and_filter( |
||
562 | $value, |
||
563 | /** This filter is documented in class.json-api-endpoints.php */ |
||
564 | apply_filters( 'wpcom_json_api_attachment_cast_and_filter', $docs ), |
||
565 | false, |
||
566 | $for_output |
||
567 | ); |
||
568 | break; |
||
569 | case 'plugin' : |
||
570 | $docs = array( |
||
571 | 'id' => '(safehtml) The plugin\'s ID', |
||
572 | 'slug' => '(safehtml) The plugin\'s Slug', |
||
573 | 'active' => '(boolean) The plugin status.', |
||
574 | 'update' => '(object) The plugin update info.', |
||
575 | 'name' => '(safehtml) The name of the plugin.', |
||
576 | 'plugin_url' => '(url) Link to the plugin\'s web site.', |
||
577 | 'version' => '(safehtml) The plugin version number.', |
||
578 | 'description' => '(safehtml) Description of what the plugin does and/or notes from the author', |
||
579 | 'author' => '(safehtml) The plugin author\'s name', |
||
580 | 'author_url' => '(url) The plugin author web site address', |
||
581 | 'network' => '(boolean) Whether the plugin can only be activated network wide.', |
||
582 | 'autoupdate' => '(boolean) Whether the plugin is auto updated', |
||
583 | 'log' => '(array:safehtml) An array of update log strings.', |
||
584 | ); |
||
585 | $return[$key] = (object) $this->cast_and_filter( |
||
586 | $value, |
||
587 | /** |
||
588 | * Filter the documentation returned for a plugin. |
||
589 | * |
||
590 | * @module json-api |
||
591 | * |
||
592 | * @since 3.1.0 |
||
593 | * |
||
594 | * @param array $docs Array of documentation about a plugin. |
||
595 | */ |
||
596 | apply_filters( 'wpcom_json_api_plugin_cast_and_filter', $docs ), |
||
597 | false, |
||
598 | $for_output |
||
599 | ); |
||
600 | break; |
||
601 | case 'jetpackmodule' : |
||
602 | $docs = array( |
||
603 | 'id' => '(string) The module\'s ID', |
||
604 | 'active' => '(boolean) The module\'s status.', |
||
605 | 'name' => '(string) The module\'s name.', |
||
606 | 'description' => '(safehtml) The module\'s description.', |
||
607 | 'sort' => '(int) The module\'s display order.', |
||
608 | 'introduced' => '(string) The Jetpack version when the module was introduced.', |
||
609 | 'changed' => '(string) The Jetpack version when the module was changed.', |
||
610 | 'free' => '(boolean) The module\'s Free or Paid status.', |
||
611 | 'module_tags' => '(array) The module\'s tags.' |
||
612 | ); |
||
613 | $return[$key] = (object) $this->cast_and_filter( |
||
614 | $value, |
||
615 | /** This filter is documented in class.json-api-endpoints.php */ |
||
616 | apply_filters( 'wpcom_json_api_plugin_cast_and_filter', $docs ), |
||
617 | false, |
||
618 | $for_output |
||
619 | ); |
||
620 | break; |
||
621 | case 'sharing_button' : |
||
622 | $docs = array( |
||
623 | 'ID' => '(string)', |
||
624 | 'name' => '(string)', |
||
625 | 'URL' => '(string)', |
||
626 | 'icon' => '(string)', |
||
627 | 'enabled' => '(bool)', |
||
628 | 'visibility' => '(string)', |
||
629 | ); |
||
630 | $return[$key] = (array) $this->cast_and_filter( $value, $docs, false, $for_output ); |
||
631 | break; |
||
632 | case 'sharing_button_service': |
||
633 | $docs = array( |
||
634 | 'ID' => '(string) The service identifier', |
||
635 | 'name' => '(string) The service name', |
||
636 | 'class_name' => '(string) Class name for custom style sharing button elements', |
||
637 | 'genericon' => '(string) The Genericon unicode character for the custom style sharing button icon', |
||
638 | 'preview_smart' => '(string) An HTML snippet of a rendered sharing button smart preview', |
||
639 | 'preview_smart_js' => '(string) An HTML snippet of the page-wide initialization scripts used for rendering the sharing button smart preview' |
||
640 | ); |
||
641 | $return[$key] = (array) $this->cast_and_filter( $value, $docs, false, $for_output ); |
||
642 | break; |
||
643 | |||
644 | default : |
||
645 | $method_name = $type['type'] . '_docs'; |
||
646 | if ( method_exists( WPCOM_JSON_API_Jetpack_Overrides, $method_name ) ) { |
||
647 | $docs = WPCOM_JSON_API_Jetpack_Overrides::$method_name(); |
||
648 | } |
||
649 | |||
650 | if ( ! empty( $docs ) ) { |
||
651 | $return[$key] = (object) $this->cast_and_filter( |
||
652 | $value, |
||
653 | /** This filter is documented in class.json-api-endpoints.php */ |
||
654 | apply_filters( 'wpcom_json_api_plugin_cast_and_filter', $docs ), |
||
655 | false, |
||
656 | $for_output |
||
657 | ); |
||
658 | } else { |
||
659 | trigger_error( "Unknown API casting type {$type['type']}", E_USER_WARNING ); |
||
660 | } |
||
661 | } |
||
662 | } |
||
663 | |||
664 | function parse_types( $text ) { |
||
665 | if ( !preg_match( '#^\(([^)]+)\)#', ltrim( $text ), $matches ) ) { |
||
666 | return 'none'; |
||
667 | } |
||
668 | |||
669 | $types = explode( '|', strtolower( $matches[1] ) ); |
||
670 | $return = array(); |
||
671 | foreach ( $types as $type ) { |
||
672 | foreach ( array( ':' => 'children', '>' => 'subtype', '=' => 'default' ) as $operator => $meaning ) { |
||
673 | if ( false !== strpos( $type, $operator ) ) { |
||
674 | $item = explode( $operator, $type, 2 ); |
||
675 | $return[] = array( 'type' => $item[0], $meaning => $item[1] ); |
||
676 | continue 2; |
||
677 | } |
||
678 | } |
||
679 | $return[] = compact( 'type' ); |
||
680 | } |
||
681 | |||
682 | return $return; |
||
683 | } |
||
684 | |||
685 | /** |
||
686 | * Checks if the endpoint is publicly displayable |
||
687 | */ |
||
688 | function is_publicly_documentable() { |
||
691 | |||
692 | /** |
||
693 | * Auto generates documentation based on description, method, path, path_labels, and query parameters. |
||
694 | * Echoes HTML. |
||
695 | */ |
||
696 | function document( $show_description = true ) { |
||
697 | global $wpdb; |
||
812 | |||
813 | function add_http_build_query_to_php_content_example( $matches ) { |
||
820 | |||
821 | /** |
||
822 | * Recursively generates the <dl>'s to document item descriptions. |
||
823 | * Echoes HTML. |
||
824 | */ |
||
825 | function generate_doc_description( $item ) { |
||
843 | |||
844 | /** |
||
845 | * Auto generates documentation based on description, method, path, path_labels, and query parameters. |
||
846 | * Echoes HTML. |
||
847 | */ |
||
848 | function generate_documentation() { |
||
929 | |||
930 | function user_can_view_post( $post_id ) { |
||
994 | |||
995 | /** |
||
996 | * Returns author object. |
||
997 | * |
||
998 | * @param $author user ID, user row, WP_User object, comment row, post row |
||
999 | * @param $show_email output the author's email address? |
||
1000 | * |
||
1001 | * @return (object) |
||
1002 | */ |
||
1003 | function get_author( $author, $show_email = false ) { |
||
1106 | |||
1107 | function get_media_item( $media_id ) { |
||
1140 | |||
1141 | function get_media_item_v1_1( $media_id ) { |
||
1262 | |||
1263 | function get_taxonomy( $taxonomy_id, $taxonomy_type, $context ) { |
||
1273 | |||
1274 | function format_taxonomy( $taxonomy, $taxonomy_type, $context ) { |
||
1311 | |||
1312 | /** |
||
1313 | * Returns ISO 8601 formatted datetime: 2011-12-08T01:15:36-08:00 |
||
1314 | * |
||
1315 | * @param $date_gmt (string) GMT datetime string. |
||
1316 | * @param $date (string) Optional. Used to calculate the offset from GMT. |
||
1317 | * |
||
1318 | * @return string |
||
1319 | */ |
||
1320 | function format_date( $date_gmt, $date = null ) { |
||
1363 | |||
1364 | /** |
||
1365 | * Parses a date string and returns the local and GMT representations |
||
1366 | * of that date & time in 'YYYY-MM-DD HH:MM:SS' format without |
||
1367 | * timezones or offsets. If the parsed datetime was not localized to a |
||
1368 | * particular timezone or offset we will assume it was given in GMT |
||
1369 | * relative to now and will convert it to local time using either the |
||
1370 | * timezone set in the options table for the blog or the GMT offset. |
||
1371 | * |
||
1372 | * @param datetime string |
||
1373 | * |
||
1374 | * @return array( $local_time_string, $gmt_time_string ) |
||
1375 | */ |
||
1376 | function parse_date( $date_string ) { |
||
1418 | |||
1419 | // Load the functions.php file for the current theme to get its post formats, CPTs, etc. |
||
1420 | function load_theme_functions() { |
||
1486 | |||
1487 | function copy_hooks( $from_hook, $to_hook, $base_paths ) { |
||
1508 | |||
1509 | function get_reflection( $callback ) { |
||
1530 | |||
1531 | /** |
||
1532 | * Try to find the closest supported version of an endpoint to the current endpoint |
||
1533 | * |
||
1534 | * For example, if we were looking at the path /animals/panda: |
||
1535 | * - if the current endpoint is v1.3 and there is a v1.3 of /animals/%s available, we return 1.3 |
||
1536 | * - if the current endpoint is v1.3 and there is no v1.3 of /animals/%s known, we fall back to the |
||
1537 | * maximum available version of /animals/%s, e.g. 1.1 |
||
1538 | * |
||
1539 | * This method is used in get_link() to construct meta links for API responses. |
||
1540 | * |
||
1541 | * @param $path string The current endpoint path, relative to the version |
||
1542 | * @param $method string Request method used to access the endpoint path |
||
1543 | * @return string The current version, or otherwise the maximum version available |
||
1544 | */ |
||
1545 | function get_closest_version_of_endpoint( $path, $request_method = 'GET' ) { |
||
1600 | |||
1601 | /** |
||
1602 | * Get an array of endpoint paths with their associated versions |
||
1603 | * |
||
1604 | * The result is cached for 30 minutes. |
||
1605 | * |
||
1606 | * @return array Array of endpoint paths, min_versions and max_versions, keyed by last segment of path |
||
1607 | **/ |
||
1608 | protected function get_endpoint_path_versions() { |
||
1647 | |||
1648 | /** |
||
1649 | * Grab the last segment of a relative path |
||
1650 | * |
||
1651 | * @param string $path Path |
||
1652 | * @return string Last path segment |
||
1653 | */ |
||
1654 | protected function get_last_segment_of_relative_path( $path) { |
||
1663 | |||
1664 | /** |
||
1665 | * Generate a URL to an endpoint |
||
1666 | * |
||
1667 | * Used to construct meta links in API responses |
||
1668 | * |
||
1669 | * @param mixed $args Optional arguments to be appended to URL |
||
1670 | * @return string Endpoint URL |
||
1671 | **/ |
||
1672 | function get_link() { |
||
1705 | |||
1706 | function get_me_link( $path = '' ) { |
||
1709 | |||
1710 | function get_taxonomy_link( $blog_id, $taxonomy_id, $taxonomy_type, $path = '' ) { |
||
1716 | |||
1717 | function get_media_link( $blog_id, $media_id, $path = '' ) { |
||
1720 | |||
1721 | function get_site_link( $blog_id, $path = '' ) { |
||
1724 | |||
1725 | function get_post_link( $blog_id, $post_id, $path = '' ) { |
||
1728 | |||
1729 | function get_comment_link( $blog_id, $comment_id, $path = '' ) { |
||
1732 | |||
1733 | function get_publicize_connection_link( $blog_id, $publicize_connection_id, $path = '' ) { |
||
1736 | |||
1737 | function get_publicize_connections_link( $keyring_token_id, $path = '' ) { |
||
1740 | |||
1741 | function get_keyring_connection_link( $keyring_token_id, $path = '' ) { |
||
1744 | |||
1745 | function get_external_service_link( $external_service, $path = '' ) { |
||
1748 | |||
1749 | |||
1750 | /** |
||
1751 | * Check whether a user can view or edit a post type |
||
1752 | * @param string $post_type post type to check |
||
1753 | * @param string $context 'display' or 'edit' |
||
1754 | * @return bool |
||
1755 | */ |
||
1756 | function current_user_can_access_post_type( $post_type, $context='display' ) { |
||
1771 | |||
1772 | function is_post_type_allowed( $post_type ) { |
||
1787 | |||
1788 | /** |
||
1789 | * Gets the whitelisted post types that JP should allow access to. |
||
1790 | * |
||
1791 | * @return array Whitelisted post types. |
||
1792 | */ |
||
1793 | protected function _get_whitelisted_post_types() { |
||
1809 | |||
1810 | function handle_media_creation_v1_1( $media_files, $media_urls, $media_attrs = array(), $force_parent_id = false ) { |
||
1906 | |||
1907 | function handle_media_sideload( $url, $parent_post_id = 0 ) { |
||
1945 | |||
1946 | function allow_video_uploads( $mimes ) { |
||
2006 | |||
2007 | function is_current_site_multi_user() { |
||
2019 | |||
2020 | function allows_cross_origin_requests() { |
||
2023 | |||
2024 | function allows_unauthorized_requests( $origin, $complete_access_origins ) { |
||
2027 | |||
2028 | /** |
||
2029 | * Return endpoint response |
||
2030 | * |
||
2031 | * @param ... determined by ->$path |
||
2032 | * |
||
2033 | * @return |
||
2034 | * falsy: HTTP 500, no response body |
||
2035 | * WP_Error( $error_code, $error_message, $http_status_code ): HTTP $status_code, json_encode( array( 'error' => $error_code, 'message' => $error_message ) ) response body |
||
2036 | * $data: HTTP 200, json_encode( $data ) response body |
||
2037 | */ |
||
2038 | abstract function callback( $path = '' ); |
||
2039 | |||
2040 | |||
2041 | } |
||
2042 | |||
2044 |
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.