Completed
Push — master ( a9f7c3...bf825c )
by Marin
06:41
created

class.json-api-endpoints.php (17 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
use Automattic\Jetpack\Connection\Client;
4
5
require_once dirname( __FILE__ ) . '/json-api-config.php';
6
require_once dirname( __FILE__ ) . '/sal/class.json-api-links.php';
7
require_once dirname( __FILE__ ) . '/sal/class.json-api-metadata.php';
8
require_once dirname( __FILE__ ) . '/sal/class.json-api-date.php';
9
10
// Endpoint
11
abstract class WPCOM_JSON_API_Endpoint {
12
	// The API Object
13
	public $api;
14
15
	// The link-generating utility class
16
	public $links;
17
18
	public $pass_wpcom_user_details = false;
19
20
	// One liner.
21
	public $description;
22
23
	// Object Grouping For Documentation (Users, Posts, Comments)
24
	public $group;
25
26
	// Stats extra value to bump
27
	public $stat;
28
29
	// HTTP Method
30
	public $method = 'GET';
31
32
	// Minimum version of the api for which to serve this endpoint
33
	public $min_version = '0';
34
35
	// Maximum version of the api for which to serve this endpoint
36
	public $max_version = WPCOM_JSON_API__CURRENT_VERSION;
37
38
	// Path at which to serve this endpoint: sprintf() format.
39
	public $path = '';
40
41
	// Identifiers to fill sprintf() formatted $path
42
	public $path_labels = array();
43
44
	// Accepted query parameters
45
	public $query = array(
46
		// Parameter name
47
		'context'       => array(
48
			// Default value => description
49
			'display' => 'Formats the output as HTML for display.  Shortcodes are parsed, paragraph tags are added, etc..',
50
			// Other possible values => description
51
			'edit'    => 'Formats the output for editing.  Shortcodes are left unparsed, significant whitespace is kept, etc..',
52
		),
53
		'http_envelope' => array(
54
			'false' => '',
55
			'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.',
56
		),
57
		'pretty'        => array(
58
			'false' => '',
59
			'true'  => 'Output pretty JSON',
60
		),
61
		'meta'          => "(string) Optional. Loads data from the endpoints found in the 'meta' part of the response. Comma-separated list. Example: meta=site,likes",
62
		'fields'        => '(string) Optional. Returns specified fields only. Comma-separated list. Example: fields=ID,title',
63
		// Parameter name => description (default value is empty)
64
		'callback'      => '(string) An optional JSONP callback function.',
65
	);
66
67
	// Response format
68
	public $response_format = array();
69
70
	// Request format
71
	public $request_format = array();
72
73
	// Is this endpoint still in testing phase?  If so, not available to the public.
74
	public $in_testing = false;
75
76
	// Is this endpoint still allowed if the site in question is flagged?
77
	public $allowed_if_flagged = false;
78
79
	// Is this endpoint allowed if the site is red flagged?
80
	public $allowed_if_red_flagged = false;
81
82
	// Is this endpoint allowed if the site is deleted?
83
	public $allowed_if_deleted = false;
84
85
	/**
86
	 * @var string Version of the API
87
	 */
88
	public $version = '';
89
90
	/**
91
	 * @var string Example request to make
92
	 */
93
	public $example_request = '';
94
95
	/**
96
	 * @var string Example request data (for POST methods)
97
	 */
98
	public $example_request_data = '';
99
100
	/**
101
	 * @var string Example response from $example_request
102
	 */
103
	public $example_response = '';
104
105
	/**
106
	 * @var bool Set to true if the endpoint implements its own filtering instead of the standard `fields` query method
107
	 */
108
	public $custom_fields_filtering = false;
109
110
	/**
111
	 * @var bool Set to true if the endpoint accepts all cross origin requests. You probably should not set this flag.
112
	 */
113
	public $allow_cross_origin_request = false;
114
115
	/**
116
	 * @var bool Set to true if the endpoint can recieve unauthorized POST requests.
117
	 */
118
	public $allow_unauthorized_request = false;
119
120
	/**
121
	 * @var bool Set to true if the endpoint should accept site based (not user based) authentication.
122
	 */
123
	public $allow_jetpack_site_auth = false;
124
125
	/**
126
	 * @var bool Set to true if the endpoint should accept auth from an upload token.
127
	 */
128
	public $allow_upload_token_auth = false;
129
130
	function __construct( $args ) {
131
		$defaults = array(
132
			'in_testing'                 => false,
133
			'allowed_if_flagged'         => false,
134
			'allowed_if_red_flagged'     => false,
135
			'allowed_if_deleted'         => false,
136
			'description'                => '',
137
			'group'                      => '',
138
			'method'                     => 'GET',
139
			'path'                       => '/',
140
			'min_version'                => '0',
141
			'max_version'                => WPCOM_JSON_API__CURRENT_VERSION,
142
			'force'                      => '',
143
			'deprecated'                 => false,
144
			'new_version'                => WPCOM_JSON_API__CURRENT_VERSION,
145
			'jp_disabled'                => false,
146
			'path_labels'                => array(),
147
			'request_format'             => array(),
148
			'response_format'            => array(),
149
			'query_parameters'           => array(),
150
			'version'                    => 'v1',
151
			'example_request'            => '',
152
			'example_request_data'       => '',
153
			'example_response'           => '',
154
			'required_scope'             => '',
155
			'pass_wpcom_user_details'    => false,
156
			'custom_fields_filtering'    => false,
157
			'allow_cross_origin_request' => false,
158
			'allow_unauthorized_request' => false,
159
			'allow_jetpack_site_auth'    => false,
160
			'allow_upload_token_auth'    => false,
161
		);
162
163
		$args = wp_parse_args( $args, $defaults );
164
165
		$this->in_testing = $args['in_testing'];
166
167
		$this->allowed_if_flagged     = $args['allowed_if_flagged'];
168
		$this->allowed_if_red_flagged = $args['allowed_if_red_flagged'];
169
		$this->allowed_if_deleted     = $args['allowed_if_deleted'];
170
171
		$this->description = $args['description'];
172
		$this->group       = $args['group'];
173
		$this->stat        = $args['stat'];
174
		$this->force       = $args['force'];
175
		$this->jp_disabled = $args['jp_disabled'];
176
177
		$this->method      = $args['method'];
178
		$this->path        = $args['path'];
179
		$this->path_labels = $args['path_labels'];
180
		$this->min_version = $args['min_version'];
181
		$this->max_version = $args['max_version'];
182
		$this->deprecated  = $args['deprecated'];
183
		$this->new_version = $args['new_version'];
184
185
		// Ensure max version is not less than min version
186
		if ( version_compare( $this->min_version, $this->max_version, '>' ) ) {
187
			$this->max_version = $this->min_version;
188
		}
189
190
		$this->pass_wpcom_user_details = $args['pass_wpcom_user_details'];
191
		$this->custom_fields_filtering = (bool) $args['custom_fields_filtering'];
192
193
		$this->allow_cross_origin_request = (bool) $args['allow_cross_origin_request'];
194
		$this->allow_unauthorized_request = (bool) $args['allow_unauthorized_request'];
195
		$this->allow_jetpack_site_auth    = (bool) $args['allow_jetpack_site_auth'];
196
		$this->allow_upload_token_auth    = (bool) $args['allow_upload_token_auth'];
197
198
		$this->version = $args['version'];
199
200
		$this->required_scope = $args['required_scope'];
201
202 View Code Duplication
		if ( $this->request_format ) {
203
			$this->request_format = array_filter( array_merge( $this->request_format, $args['request_format'] ) );
204
		} else {
205
			$this->request_format = $args['request_format'];
206
		}
207
208 View Code Duplication
		if ( $this->response_format ) {
209
			$this->response_format = array_filter( array_merge( $this->response_format, $args['response_format'] ) );
210
		} else {
211
			$this->response_format = $args['response_format'];
212
		}
213
214
		if ( false === $args['query_parameters'] ) {
215
			$this->query = array();
216
		} elseif ( is_array( $args['query_parameters'] ) ) {
217
			$this->query = array_filter( array_merge( $this->query, $args['query_parameters'] ) );
218
		}
219
220
		$this->api   = WPCOM_JSON_API::init(); // Auto-add to WPCOM_JSON_API
221
		$this->links = WPCOM_JSON_API_Links::getInstance();
222
223
		/** Example Request/Response */
224
225
		// Examples for endpoint documentation request
226
		$this->example_request      = $args['example_request'];
227
		$this->example_request_data = $args['example_request_data'];
228
		$this->example_response     = $args['example_response'];
229
230
		$this->api->add( $this );
231
	}
232
233
	// Get all query args.  Prefill with defaults
234
	function query_args( $return_default_values = true, $cast_and_filter = true ) {
235
		$args = array_intersect_key( $this->api->query, $this->query );
236
237
		if ( ! $cast_and_filter ) {
238
			return $args;
239
		}
240
241
		return $this->cast_and_filter( $args, $this->query, $return_default_values );
242
	}
243
244
	// Get POST body data
245
	function input( $return_default_values = true, $cast_and_filter = true ) {
246
		$input        = trim( $this->api->post_body );
247
		$content_type = $this->api->content_type;
248
		if ( $content_type ) {
249
			list ( $content_type ) = explode( ';', $content_type );
250
		}
251
		$content_type = trim( $content_type );
252
		switch ( $content_type ) {
253
			case 'application/json':
254
			case 'application/x-javascript':
255
			case 'text/javascript':
256
			case 'text/x-javascript':
257
			case 'text/x-json':
258
			case 'text/json':
259
				$return = json_decode( $input, true );
260
261
				if ( function_exists( 'json_last_error' ) ) {
262
					if ( JSON_ERROR_NONE !== json_last_error() ) { // phpcs:ignore PHPCompatibility
263
						return null;
264
					}
265
				} else {
266
					if ( is_null( $return ) && json_encode( null ) !== $input ) {
267
						return null;
268
					}
269
				}
270
271
				break;
272
			case 'multipart/form-data':
273
				$return = array_merge( stripslashes_deep( $_POST ), $_FILES );
274
				break;
275
			case 'application/x-www-form-urlencoded':
276
				// attempt JSON first, since probably a curl command
277
				$return = json_decode( $input, true );
278
279
				if ( is_null( $return ) ) {
280
					wp_parse_str( $input, $return );
281
				}
282
283
				break;
284
			default:
285
				wp_parse_str( $input, $return );
286
				break;
287
		}
288
289
		if ( isset( $this->api->query['force'] )
290
			&& 'secure' === $this->api->query['force']
291
			&& isset( $return['secure_key'] ) ) {
292
			$this->api->post_body      = $this->get_secure_body( $return['secure_key'] );
293
			$this->api->query['force'] = false;
294
			return $this->input( $return_default_values, $cast_and_filter );
295
		}
296
297
		if ( $cast_and_filter ) {
298
			$return = $this->cast_and_filter( $return, $this->request_format, $return_default_values );
299
		}
300
		return $return;
301
	}
302
303
304
	protected function get_secure_body( $secure_key ) {
305
		$response = Client::wpcom_json_api_request_as_blog(
306
			sprintf( '/sites/%d/secure-request', Jetpack_Options::get_option( 'id' ) ),
307
			'1.1',
308
			array( 'method' => 'POST' ),
309
			array( 'secure_key' => $secure_key )
310
		);
311
		if ( 200 !== $response['response']['code'] ) {
312
			return null;
313
		}
314
		return json_decode( $response['body'], true );
315
	}
316
317
	function cast_and_filter( $data, $documentation, $return_default_values = false, $for_output = false ) {
318
		$return_as_object = false;
319
		if ( is_object( $data ) ) {
320
			// @todo this should probably be a deep copy if $data can ever have nested objects
321
			$data             = (array) $data;
322
			$return_as_object = true;
323
		} elseif ( ! is_array( $data ) ) {
324
			return $data;
325
		}
326
327
		$boolean_arg = array( 'false', 'true' );
328
		$naeloob_arg = array( 'true', 'false' );
329
330
		$return = array();
331
332
		foreach ( $documentation as $key => $description ) {
333
			if ( is_array( $description ) ) {
334
				// String or boolean array keys only
335
				$whitelist = array_keys( $description );
336
337
				if ( $whitelist === $boolean_arg || $whitelist === $naeloob_arg ) {
338
					// Truthiness
339
					if ( isset( $data[ $key ] ) ) {
340
						$return[ $key ] = (bool) WPCOM_JSON_API::is_truthy( $data[ $key ] );
341
					} elseif ( $return_default_values ) {
342
						$return[ $key ] = $whitelist === $naeloob_arg; // Default to true for naeloob_arg and false for boolean_arg.
343
					}
344
				} elseif ( isset( $data[ $key ] ) && isset( $description[ $data[ $key ] ] ) ) {
345
					// String Key
346
					$return[ $key ] = (string) $data[ $key ];
347
				} elseif ( $return_default_values ) {
348
					// Default value
349
					$return[ $key ] = (string) current( $whitelist );
350
				}
351
352
				continue;
353
			}
354
355
			$types = $this->parse_types( $description );
356
			$type  = array_shift( $types );
357
358
			// Explicit default - string and int only for now.  Always set these reguardless of $return_default_values
359
			if ( isset( $type['default'] ) ) {
360
				if ( ! isset( $data[ $key ] ) ) {
361
					$data[ $key ] = $type['default'];
362
				}
363
			}
364
365
			if ( ! isset( $data[ $key ] ) ) {
366
				continue;
367
			}
368
369
			$this->cast_and_filter_item( $return, $type, $key, $data[ $key ], $types, $for_output );
370
		}
371
372
		if ( $return_as_object ) {
373
			return (object) $return;
374
		}
375
376
		return $return;
377
	}
378
379
	/**
380
	 * Casts $value according to $type.
381
	 * Handles fallbacks for certain values of $type when $value is not that $type
382
	 * Currently, only handles fallback between string <-> array (two way), from string -> false (one way), and from object -> false (one way),
383
	 * and string -> object (one way)
384
	 *
385
	 * Handles "child types" - array:URL, object:category
386
	 * array:URL means an array of URLs
387
	 * object:category means a hash of categories
388
	 *
389
	 * Handles object typing - object>post means an object of type post
390
	 */
391
	function cast_and_filter_item( &$return, $type, $key, $value, $types = array(), $for_output = false ) {
392
		if ( is_string( $type ) ) {
393
			$type = compact( 'type' );
394
		}
395
396
		switch ( $type['type'] ) {
397
			case 'false':
398
				$return[ $key ] = false;
399
				break;
400
			case 'url':
401
				if ( is_object( $value ) && isset( $value->url ) && false !== strpos( $value->url, 'https://videos.files.wordpress.com/' ) ) {
402
					$value = $value->url;
403
				}
404
				// Check for string since esc_url_raw() expects one.
405
				if ( ! is_string( $value ) ) {
406
					break;
407
				}
408
				$return[ $key ] = (string) esc_url_raw( $value );
409
				break;
410
			case 'string':
411
				// Fallback string -> array, or for string -> object
412
				if ( is_array( $value ) || is_object( $value ) ) {
413 View Code Duplication
					if ( ! empty( $types[0] ) ) {
414
						$next_type = array_shift( $types );
415
						return $this->cast_and_filter_item( $return, $next_type, $key, $value, $types, $for_output );
416
					}
417
				}
418
419
				// Fallback string -> false
420 View Code Duplication
				if ( ! is_string( $value ) ) {
421
					if ( ! empty( $types[0] ) && 'false' === $types[0]['type'] ) {
422
						$next_type = array_shift( $types );
423
						return $this->cast_and_filter_item( $return, $next_type, $key, $value, $types, $for_output );
424
					}
425
				}
426
				$return[ $key ] = (string) $value;
427
				break;
428
			case 'html':
429
				$return[ $key ] = (string) $value;
430
				break;
431
			case 'safehtml':
432
				$return[ $key ] = wp_kses( (string) $value, wp_kses_allowed_html() );
433
				break;
434
			case 'zip':
435
			case 'media':
436
				if ( is_array( $value ) ) {
437
					if ( isset( $value['name'] ) && is_array( $value['name'] ) ) {
438
						// It's a $_FILES array
439
						// Reformat into array of $_FILES items
440
						$files = array();
441
442
						foreach ( $value['name'] as $k => $v ) {
443
							$files[ $k ] = array();
444
							foreach ( array_keys( $value ) as $file_key ) {
445
								$files[ $k ][ $file_key ] = $value[ $file_key ][ $k ];
446
							}
447
						}
448
449
						$return[ $key ] = $files;
450
						break;
451
					}
452
				} else {
453
					// no break - treat as 'array'
454
				}
455
				// nobreak
456
			case 'array':
457
				// Fallback array -> string
458 View Code Duplication
				if ( is_string( $value ) ) {
459
					if ( ! empty( $types[0] ) ) {
460
						$next_type = array_shift( $types );
461
						return $this->cast_and_filter_item( $return, $next_type, $key, $value, $types, $for_output );
462
					}
463
				}
464
465 View Code Duplication
				if ( isset( $type['children'] ) ) {
466
					$children = array();
467
					foreach ( (array) $value as $k => $child ) {
468
						$this->cast_and_filter_item( $children, $type['children'], $k, $child, array(), $for_output );
469
					}
470
					$return[ $key ] = (array) $children;
471
					break;
472
				}
473
474
				$return[ $key ] = (array) $value;
475
				break;
476
			case 'iso 8601 datetime':
477
			case 'datetime':
478
				// (string)s
479
				$dates = $this->parse_date( (string) $value );
480
				if ( $for_output ) {
481
					$return[ $key ] = $this->format_date( $dates[1], $dates[0] );
482
				} else {
483
					list( $return[ $key ], $return[ "{$key}_gmt" ] ) = $dates;
484
				}
485
				break;
486
			case 'float':
487
				$return[ $key ] = (float) $value;
488
				break;
489
			case 'int':
490
			case 'integer':
491
				$return[ $key ] = (int) $value;
492
				break;
493
			case 'bool':
494
			case 'boolean':
495
				$return[ $key ] = (bool) WPCOM_JSON_API::is_truthy( $value );
496
				break;
497
			case 'object':
498
				// Fallback object -> false
499 View Code Duplication
				if ( is_scalar( $value ) || is_null( $value ) ) {
500
					if ( ! empty( $types[0] ) && 'false' === $types[0]['type'] ) {
501
						return $this->cast_and_filter_item( $return, 'false', $key, $value, $types, $for_output );
502
					}
503
				}
504
505 View Code Duplication
				if ( isset( $type['children'] ) ) {
506
					$children = array();
507
					foreach ( (array) $value as $k => $child ) {
508
						$this->cast_and_filter_item( $children, $type['children'], $k, $child, array(), $for_output );
509
					}
510
					$return[ $key ] = (object) $children;
511
					break;
512
				}
513
514
				if ( isset( $type['subtype'] ) ) {
515
					return $this->cast_and_filter_item( $return, $type['subtype'], $key, $value, $types, $for_output );
516
				}
517
518
				$return[ $key ] = (object) $value;
519
				break;
520
			case 'post':
521
				$return[ $key ] = (object) $this->cast_and_filter( $value, $this->post_object_format, false, $for_output );
522
				break;
523
			case 'comment':
524
				$return[ $key ] = (object) $this->cast_and_filter( $value, $this->comment_object_format, false, $for_output );
525
				break;
526
			case 'tag':
527
			case 'category':
528
				$docs = array(
529
					'ID'          => '(int)',
530
					'name'        => '(string)',
531
					'slug'        => '(string)',
532
					'description' => '(HTML)',
533
					'post_count'  => '(int)',
534
					'feed_url'    => '(string)',
535
					'meta'        => '(object)',
536
				);
537
				if ( 'category' === $type['type'] ) {
538
					$docs['parent'] = '(int)';
539
				}
540
				$return[ $key ] = (object) $this->cast_and_filter( $value, $docs, false, $for_output );
541
				break;
542
			case 'post_reference':
543 View Code Duplication
			case 'comment_reference':
544
				$docs           = array(
545
					'ID'    => '(int)',
546
					'type'  => '(string)',
547
					'title' => '(string)',
548
					'link'  => '(URL)',
549
				);
550
				$return[ $key ] = (object) $this->cast_and_filter( $value, $docs, false, $for_output );
551
				break;
552 View Code Duplication
			case 'geo':
553
				$docs           = array(
554
					'latitude'  => '(float)',
555
					'longitude' => '(float)',
556
					'address'   => '(string)',
557
				);
558
				$return[ $key ] = (object) $this->cast_and_filter( $value, $docs, false, $for_output );
559
				break;
560
			case 'author':
561
				$docs           = array(
562
					'ID'             => '(int)',
563
					'user_login'     => '(string)',
564
					'login'          => '(string)',
565
					'email'          => '(string|false)',
566
					'name'           => '(string)',
567
					'first_name'     => '(string)',
568
					'last_name'      => '(string)',
569
					'nice_name'      => '(string)',
570
					'URL'            => '(URL)',
571
					'avatar_URL'     => '(URL)',
572
					'profile_URL'    => '(URL)',
573
					'is_super_admin' => '(bool)',
574
					'roles'          => '(array:string)',
575
					'ip_address'     => '(string|false)',
576
				);
577
				$return[ $key ] = (object) $this->cast_and_filter( $value, $docs, false, $for_output );
578
				break;
579 View Code Duplication
			case 'role':
580
				$docs           = array(
581
					'name'         => '(string)',
582
					'display_name' => '(string)',
583
					'capabilities' => '(object:boolean)',
584
				);
585
				$return[ $key ] = (object) $this->cast_and_filter( $value, $docs, false, $for_output );
586
				break;
587
			case 'attachment':
588
				$docs           = array(
589
					'ID'        => '(int)',
590
					'URL'       => '(URL)',
591
					'guid'      => '(string)',
592
					'mime_type' => '(string)',
593
					'width'     => '(int)',
594
					'height'    => '(int)',
595
					'duration'  => '(int)',
596
				);
597
				$return[ $key ] = (object) $this->cast_and_filter(
598
					$value,
599
					/**
600
					* Filter the documentation returned for a post attachment.
601
					*
602
					* @module json-api
603
					*
604
					* @since 1.9.0
605
					*
606
					* @param array $docs Array of documentation about a post attachment.
607
					*/
608
					apply_filters( 'wpcom_json_api_attachment_cast_and_filter', $docs ),
609
					false,
610
					$for_output
611
				);
612
				break;
613
			case 'metadata':
614
				$docs           = array(
615
					'id'             => '(int)',
616
					'key'            => '(string)',
617
					'value'          => '(string|false|float|int|array|object)',
618
					'previous_value' => '(string)',
619
					'operation'      => '(string)',
620
				);
621
				$return[ $key ] = (object) $this->cast_and_filter(
622
					$value,
623
					/** This filter is documented in class.json-api-endpoints.php */
624
					apply_filters( 'wpcom_json_api_attachment_cast_and_filter', $docs ),
625
					false,
626
					$for_output
627
				);
628
				break;
629
			case 'plugin':
630
				$docs           = array(
631
					'id'           => '(safehtml) The plugin\'s ID',
632
					'slug'         => '(safehtml) The plugin\'s Slug',
633
					'active'       => '(boolean)  The plugin status.',
634
					'update'       => '(object)   The plugin update info.',
635
					'name'         => '(safehtml) The name of the plugin.',
636
					'plugin_url'   => '(url)      Link to the plugin\'s web site.',
637
					'version'      => '(safehtml) The plugin version number.',
638
					'description'  => '(safehtml) Description of what the plugin does and/or notes from the author',
639
					'author'       => '(safehtml) The plugin author\'s name',
640
					'author_url'   => '(url)      The plugin author web site address',
641
					'network'      => '(boolean)  Whether the plugin can only be activated network wide.',
642
					'autoupdate'   => '(boolean)  Whether the plugin is auto updated',
643
					'log'          => '(array:safehtml) An array of update log strings.',
644
					'action_links' => '(array) An array of action links that the plugin uses.',
645
				);
646
				$return[ $key ] = (object) $this->cast_and_filter(
647
					$value,
648
					/**
649
					* Filter the documentation returned for a plugin.
650
					*
651
					* @module json-api
652
					*
653
					* @since 3.1.0
654
					*
655
					* @param array $docs Array of documentation about a plugin.
656
					*/
657
					apply_filters( 'wpcom_json_api_plugin_cast_and_filter', $docs ),
658
					false,
659
					$for_output
660
				);
661
				break;
662
			case 'plugin_v1_2':
663
				$docs           = class_exists( 'Jetpack_JSON_API_Get_Plugins_v1_2_Endpoint' )
664
				? Jetpack_JSON_API_Get_Plugins_v1_2_Endpoint::$_response_format
665
				: Jetpack_JSON_API_Plugins_Endpoint::$_response_format_v1_2;
666
				$return[ $key ] = (object) $this->cast_and_filter(
667
					$value,
668
					/**
669
					* Filter the documentation returned for a plugin.
670
					*
671
					* @module json-api
672
					*
673
					* @since 3.1.0
674
					*
675
					* @param array $docs Array of documentation about a plugin.
676
					*/
677
					apply_filters( 'wpcom_json_api_plugin_cast_and_filter', $docs ),
678
					false,
679
					$for_output
680
				);
681
				break;
682
			case 'file_mod_capabilities':
683
				$docs           = array(
684
					'reasons_modify_files_unavailable' => '(array) The reasons why files can\'t be modified',
685
					'reasons_autoupdate_unavailable'   => '(array) The reasons why autoupdates aren\'t allowed',
686
					'modify_files'                     => '(boolean) true if files can be modified',
687
					'autoupdate_files'                 => '(boolean) true if autoupdates are allowed',
688
				);
689
				$return[ $key ] = (array) $this->cast_and_filter( $value, $docs, false, $for_output );
690
				break;
691
			case 'jetpackmodule':
692
				$docs           = array(
693
					'id'          => '(string)   The module\'s ID',
694
					'active'      => '(boolean)  The module\'s status.',
695
					'name'        => '(string)   The module\'s name.',
696
					'description' => '(safehtml) The module\'s description.',
697
					'sort'        => '(int)      The module\'s display order.',
698
					'introduced'  => '(string)   The Jetpack version when the module was introduced.',
699
					'changed'     => '(string)   The Jetpack version when the module was changed.',
700
					'free'        => '(boolean)  The module\'s Free or Paid status.',
701
					'module_tags' => '(array)    The module\'s tags.',
702
					'override'    => '(string)   The module\'s override. Empty if no override, otherwise \'active\' or \'inactive\'',
703
				);
704
				$return[ $key ] = (object) $this->cast_and_filter(
705
					$value,
706
					/** This filter is documented in class.json-api-endpoints.php */
707
					apply_filters( 'wpcom_json_api_plugin_cast_and_filter', $docs ),
708
					false,
709
					$for_output
710
				);
711
				break;
712
			case 'sharing_button':
713
				$docs           = array(
714
					'ID'         => '(string)',
715
					'name'       => '(string)',
716
					'URL'        => '(string)',
717
					'icon'       => '(string)',
718
					'enabled'    => '(bool)',
719
					'visibility' => '(string)',
720
				);
721
				$return[ $key ] = (array) $this->cast_and_filter( $value, $docs, false, $for_output );
722
				break;
723
			case 'sharing_button_service':
724
				$docs           = array(
725
					'ID'               => '(string) The service identifier',
726
					'name'             => '(string) The service name',
727
					'class_name'       => '(string) Class name for custom style sharing button elements',
728
					'genericon'        => '(string) The Genericon unicode character for the custom style sharing button icon',
729
					'preview_smart'    => '(string) An HTML snippet of a rendered sharing button smart preview',
730
					'preview_smart_js' => '(string) An HTML snippet of the page-wide initialization scripts used for rendering the sharing button smart preview',
731
				);
732
				$return[ $key ] = (array) $this->cast_and_filter( $value, $docs, false, $for_output );
733
				break;
734
			case 'site_keyring':
735
				$docs           = array(
736
					'keyring_id'       => '(int) Keyring ID',
737
					'service'          => '(string) The service name',
738
					'external_user_id' => '(string) External user id for the service',
739
				);
740
				$return[ $key ] = (array) $this->cast_and_filter( $value, $docs, false, $for_output );
741
				break;
742
			case 'taxonomy':
743
				$docs           = array(
744
					'name'         => '(string) The taxonomy slug',
745
					'label'        => '(string) The taxonomy human-readable name',
746
					'labels'       => '(object) Mapping of labels for the taxonomy',
747
					'description'  => '(string) The taxonomy description',
748
					'hierarchical' => '(bool) Whether the taxonomy is hierarchical',
749
					'public'       => '(bool) Whether the taxonomy is public',
750
					'capabilities' => '(object) Mapping of current user capabilities for the taxonomy',
751
				);
752
				$return[ $key ] = (array) $this->cast_and_filter( $value, $docs, false, $for_output );
753
				break;
754
755
			default:
756
				$method_name = $type['type'] . '_docs';
757
				if ( method_exists( 'WPCOM_JSON_API_Jetpack_Overrides', $method_name ) ) {
758
					$docs = WPCOM_JSON_API_Jetpack_Overrides::$method_name();
759
				}
760
761
				if ( ! empty( $docs ) ) {
762
					$return[ $key ] = (object) $this->cast_and_filter(
763
						$value,
764
						/** This filter is documented in class.json-api-endpoints.php */
765
						apply_filters( 'wpcom_json_api_plugin_cast_and_filter', $docs ),
766
						false,
767
						$for_output
768
					);
769
				} else {
770
					trigger_error( "Unknown API casting type {$type['type']}", E_USER_WARNING );
771
				}
772
		}
773
	}
774
775
	function parse_types( $text ) {
776
		if ( ! preg_match( '#^\(([^)]+)\)#', ltrim( $text ), $matches ) ) {
777
			return 'none';
778
		}
779
780
		$types  = explode( '|', strtolower( $matches[1] ) );
781
		$return = array();
782
		foreach ( $types as $type ) {
783
			foreach ( array(
784
				':' => 'children',
785
				'>' => 'subtype',
786
				'=' => 'default',
787
			) as $operator => $meaning ) {
788
				if ( false !== strpos( $type, $operator ) ) {
789
					$item     = explode( $operator, $type, 2 );
790
					$return[] = array(
791
						'type'   => $item[0],
792
						$meaning => $item[1],
793
					);
794
					continue 2;
795
				}
796
			}
797
			$return[] = compact( 'type' );
798
		}
799
800
		return $return;
801
	}
802
803
	/**
804
	 * Checks if the endpoint is publicly displayable
805
	 */
806
	function is_publicly_documentable() {
807
		return '__do_not_document' !== $this->group && true !== $this->in_testing;
808
	}
809
810
	/**
811
	 * Auto generates documentation based on description, method, path, path_labels, and query parameters.
812
	 * Echoes HTML.
813
	 */
814
	function document( $show_description = true ) {
815
		global $wpdb;
816
		$original_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : 'unset';
817
		unset( $GLOBALS['post'] );
818
819
		$doc = $this->generate_documentation();
820
821
		if ( $show_description ) :
822
			?>
823
<caption>
824
	<h1><?php echo wp_kses_post( $doc['method'] ); ?> <?php echo wp_kses_post( $doc['path_labeled'] ); ?></h1>
825
	<p><?php echo wp_kses_post( $doc['description'] ); ?></p>
826
</caption>
827
828
<?php endif; ?>
829
830
		<?php if ( true === $this->deprecated ) { ?>
831
<p><strong>This endpoint is deprecated in favor of version <?php echo floatval( $this->new_version ); ?></strong></p>
832
<?php } ?>
833
834
<section class="resource-info">
835
	<h2 id="apidoc-resource-info">Resource Information</h2>
836
837
	<table class="api-doc api-doc-resource-parameters api-doc-resource">
838
839
	<thead>
840
		<tr>
841
			<th class="api-index-title" scope="column">&nbsp;</th>
842
			<th class="api-index-title" scope="column">&nbsp;</th>
843
		</tr>
844
	</thead>
845
	<tbody>
846
847
		<tr class="api-index-item">
848
			<th scope="row" class="parameter api-index-item-title">Method</th>
849
			<td class="type api-index-item-title"><?php echo wp_kses_post( $doc['method'] ); ?></td>
850
		</tr>
851
852
		<tr class="api-index-item">
853
			<th scope="row" class="parameter api-index-item-title">URL</th>
854
			<?php
855
			$version = WPCOM_JSON_API__CURRENT_VERSION;
856
			if ( ! empty( $this->max_version ) ) {
857
				$version = $this->max_version;
858
			}
859
			?>
860
			<td class="type api-index-item-title">https://public-api.wordpress.com/rest/v<?php echo floatval( $version ); ?><?php echo wp_kses_post( $doc['path_labeled'] ); ?></td>
861
		</tr>
862
863
		<tr class="api-index-item">
864
			<th scope="row" class="parameter api-index-item-title">Requires authentication?</th>
865
			<?php
866
			$requires_auth = $wpdb->get_row( $wpdb->prepare( 'SELECT requires_authentication FROM rest_api_documentation WHERE `version` = %s AND `path` = %s AND `method` = %s LIMIT 1', $version, untrailingslashit( $doc['path_labeled'] ), $doc['method'] ) );
867
			?>
868
			<td class="type api-index-item-title"><?php echo ( true === (bool) $requires_auth->requires_authentication ? 'Yes' : 'No' ); ?></td>
869
		</tr>
870
871
	</tbody>
872
	</table>
873
874
</section>
875
876
		<?php
877
878
		foreach ( array(
879
			'path'     => 'Method Parameters',
880
			'query'    => 'Query Parameters',
881
			'body'     => 'Request Parameters',
882
			'response' => 'Response Parameters',
883
		) as $doc_section_key => $label ) :
884
			$doc_section = 'response' === $doc_section_key ? $doc['response']['body'] : $doc['request'][ $doc_section_key ];
885
			if ( ! $doc_section ) {
886
				continue;
887
			}
888
889
			$param_label = strtolower( str_replace( ' ', '-', $label ) );
890
			?>
891
892
<section class="<?php echo $param_label; ?>">
893
894
<h2 id="apidoc-<?php echo esc_attr( $doc_section_key ); ?>"><?php echo wp_kses_post( $label ); ?></h2>
895
896
<table class="api-doc api-doc-<?php echo $param_label; ?>-parameters api-doc-<?php echo strtolower( str_replace( ' ', '-', $doc['group'] ) ); ?>">
897
898
<thead>
899
	<tr>
900
		<th class="api-index-title" scope="column">Parameter</th>
901
		<th class="api-index-title" scope="column">Type</th>
902
		<th class="api-index-title" scope="column">Description</th>
903
	</tr>
904
</thead>
905
<tbody>
906
907
			<?php foreach ( $doc_section as $key => $item ) : ?>
908
909
	<tr class="api-index-item">
910
		<th scope="row" class="parameter api-index-item-title"><?php echo wp_kses_post( $key ); ?></th>
911
		<td class="type api-index-item-title"><?php echo wp_kses_post( $item['type'] ); // @todo auto-link? ?></td>
912
		<td class="description api-index-item-body">
913
				<?php
914
915
				$this->generate_doc_description( $item['description'] );
916
917
				?>
918
		</td>
919
	</tr>
920
921
			<?php endforeach; ?>
922
</tbody>
923
</table>
924
</section>
925
<?php endforeach; ?>
926
927
		<?php
928
		if ( 'unset' !== $original_post ) {
929
			$GLOBALS['post'] = $original_post;
930
		}
931
	}
932
933
	function add_http_build_query_to_php_content_example( $matches ) {
934
		$trimmed_match = ltrim( $matches[0] );
935
		$pad           = substr( $matches[0], 0, -1 * strlen( $trimmed_match ) );
936
		$pad           = ltrim( $pad, ' ' );
937
		$return        = '  ' . str_replace( "\n", "\n  ", $matches[0] );
938
		return " http_build_query({$return}{$pad})";
939
	}
940
941
	/**
942
	 * Recursively generates the <dl>'s to document item descriptions.
943
	 * Echoes HTML.
944
	 */
945
	function generate_doc_description( $item ) {
946
		if ( is_array( $item ) ) :
947
			?>
948
949
		<dl>
950
			<?php	foreach ( $item as $description_key => $description_value ) : ?>
951
952
			<dt><?php echo wp_kses_post( $description_key . ':' ); ?></dt>
953
			<dd><?php $this->generate_doc_description( $description_value ); ?></dd>
954
955
			<?php	endforeach; ?>
956
957
		</dl>
958
959
			<?php
960
		else :
961
			echo wp_kses_post( $item );
962
		endif;
963
	}
964
965
	/**
966
	 * Auto generates documentation based on description, method, path, path_labels, and query parameters.
967
	 * Echoes HTML.
968
	 */
969
	function generate_documentation() {
970
		$format       = str_replace( '%d', '%s', $this->path );
971
		$path_labeled = $format;
972
		if ( ! empty( $this->path_labels ) ) {
973
			$path_labeled = vsprintf( $format, array_keys( $this->path_labels ) );
974
		}
975
		$boolean_arg = array( 'false', 'true' );
976
		$naeloob_arg = array( 'true', 'false' );
977
978
		$doc = array(
979
			'description'  => $this->description,
980
			'method'       => $this->method,
981
			'path_format'  => $this->path,
982
			'path_labeled' => $path_labeled,
983
			'group'        => $this->group,
984
			'request'      => array(
985
				'path'  => array(),
986
				'query' => array(),
987
				'body'  => array(),
988
			),
989
			'response'     => array(
990
				'body' => array(),
991
			),
992
		);
993
994
		foreach ( array(
995
			'path_labels'     => 'path',
996
			'query'           => 'query',
997
			'request_format'  => 'body',
998
			'response_format' => 'body',
999
		) as $_property => $doc_item ) {
1000
			foreach ( (array) $this->$_property as $key => $description ) {
1001
				if ( is_array( $description ) ) {
1002
					$description_keys = array_keys( $description );
1003
					if ( $boolean_arg === $description_keys || $naeloob_arg === $description_keys ) {
1004
						$type = '(bool)';
1005
					} else {
1006
						$type = '(string)';
1007
					}
1008
1009
					if ( 'response_format' !== $_property ) {
1010
						// hack - don't show "(default)" in response format
1011
						reset( $description );
1012
						$description_key                 = key( $description );
1013
						$description[ $description_key ] = "(default) {$description[$description_key]}";
1014
					}
1015
				} else {
1016
					$types   = $this->parse_types( $description );
1017
					$type    = array();
1018
					$default = '';
1019
1020
					if ( 'none' == $types ) {
1021
						$types           = array();
1022
						$types[]['type'] = 'none';
1023
					}
1024
1025
					foreach ( $types as $type_array ) {
1026
						$type[] = $type_array['type'];
1027
						if ( isset( $type_array['default'] ) ) {
1028
							$default = $type_array['default'];
1029
							if ( 'string' === $type_array['type'] ) {
1030
								$default = "'$default'";
1031
							}
1032
						}
1033
					}
1034
					$type                       = '(' . join( '|', $type ) . ')';
1035
					$noop                       = ''; // skip an index in list below
1036
					list( $noop, $description ) = explode( ')', $description, 2 );
1037
					$description                = trim( $description );
1038
					if ( $default ) {
1039
						$description .= " Default: $default.";
1040
					}
1041
				}
1042
1043
				$item = compact( 'type', 'description' );
1044
1045
				if ( 'response_format' === $_property ) {
1046
					$doc['response'][ $doc_item ][ $key ] = $item;
1047
				} else {
1048
					$doc['request'][ $doc_item ][ $key ] = $item;
1049
				}
1050
			}
1051
		}
1052
1053
		return $doc;
1054
	}
1055
1056
	function user_can_view_post( $post_id ) {
1057
		$post = get_post( $post_id );
1058
		if ( ! $post || is_wp_error( $post ) ) {
1059
			return false;
1060
		}
1061
1062 View Code Duplication
		if ( 'inherit' === $post->post_status ) {
1063
			$parent_post     = get_post( $post->post_parent );
1064
			$post_status_obj = get_post_status_object( $parent_post->post_status );
1065
		} else {
1066
			$post_status_obj = get_post_status_object( $post->post_status );
1067
		}
1068
1069
		if ( ! $post_status_obj->public ) {
1070
			if ( is_user_logged_in() ) {
1071
				if ( $post_status_obj->protected ) {
1072
					if ( ! current_user_can( 'edit_post', $post->ID ) ) {
1073
						return new WP_Error( 'unauthorized', 'User cannot view post', 403 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unauthorized'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
1074
					}
1075
				} elseif ( $post_status_obj->private ) {
1076
					if ( ! current_user_can( 'read_post', $post->ID ) ) {
1077
						return new WP_Error( 'unauthorized', 'User cannot view post', 403 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unauthorized'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
1078
					}
1079
				} elseif ( in_array( $post->post_status, array( 'inherit', 'trash' ) ) ) {
1080
					if ( ! current_user_can( 'edit_post', $post->ID ) ) {
1081
						return new WP_Error( 'unauthorized', 'User cannot view post', 403 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unauthorized'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
1082
					}
1083
				} elseif ( 'auto-draft' === $post->post_status ) {
1084
					// allow auto-drafts
1085
				} else {
1086
					return new WP_Error( 'unauthorized', 'User cannot view post', 403 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unauthorized'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
1087
				}
1088
			} else {
1089
				return new WP_Error( 'unauthorized', 'User cannot view post', 403 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unauthorized'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
1090
			}
1091
		}
1092
1093 View Code Duplication
		if (
1094
			-1 == get_option( 'blog_public' ) &&
1095
			/**
1096
			 * Filter access to a specific post.
1097
			 *
1098
			 * @module json-api
1099
			 *
1100
			 * @since 3.4.0
1101
			 *
1102
			 * @param bool current_user_can( 'read_post', $post->ID ) Can the current user access the post.
1103
			 * @param WP_Post $post Post data.
1104
			 */
1105
			! apply_filters(
1106
				'wpcom_json_api_user_can_view_post',
1107
				current_user_can( 'read_post', $post->ID ),
1108
				$post
1109
			)
1110
		) {
1111
			return new WP_Error(
1112
				'unauthorized',
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unauthorized'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
1113
				'User cannot view post',
1114
				array(
1115
					'status_code' => 403,
1116
					'error'       => 'private_blog',
1117
				)
1118
			);
1119
		}
1120
1121 View Code Duplication
		if ( strlen( $post->post_password ) && ! current_user_can( 'edit_post', $post->ID ) ) {
1122
			return new WP_Error(
1123
				'unauthorized',
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unauthorized'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
1124
				'User cannot view password protected post',
1125
				array(
1126
					'status_code' => 403,
1127
					'error'       => 'password_protected',
1128
				)
1129
			);
1130
		}
1131
1132
		return true;
1133
	}
1134
1135
	/**
1136
	 * Returns author object.
1137
	 *
1138
	 * @param object $author user ID, user row, WP_User object, comment row, post row
1139
	 * @param bool   $show_email_and_ip output the author's email address and IP address?
1140
	 *
1141
	 * @return object
1142
	 */
1143
	function get_author( $author, $show_email_and_ip = false ) {
1144
		$ip_address = isset( $author->comment_author_IP ) ? $author->comment_author_IP : '';
1145
1146
		if ( isset( $author->comment_author_email ) ) {
1147
			$ID          = 0;
1148
			$login       = '';
1149
			$email       = $author->comment_author_email;
1150
			$name        = $author->comment_author;
1151
			$first_name  = '';
1152
			$last_name   = '';
1153
			$URL         = $author->comment_author_url;
1154
			$avatar_URL  = $this->api->get_avatar_url( $author );
1155
			$profile_URL = 'https://en.gravatar.com/' . md5( strtolower( trim( $email ) ) );
1156
			$nice        = '';
1157
			$site_id     = -1;
1158
1159
			// Comment author URLs and Emails are sent through wp_kses() on save, which replaces "&" with "&amp;"
1160
			// "&" is the only email/URL character altered by wp_kses()
1161
			foreach ( array( 'email', 'URL' ) as $field ) {
1162
				$$field = str_replace( '&amp;', '&', $$field );
1163
			}
1164
		} else {
1165
			if ( isset( $author->user_id ) && $author->user_id ) {
1166
				$author = $author->user_id;
1167
			} elseif ( isset( $author->user_email ) ) {
1168
				$author = $author->ID;
1169
			} elseif ( isset( $author->post_author ) ) {
1170
				// then $author is a Post Object.
1171
				if ( 0 == $author->post_author ) {
1172
					return null;
1173
				}
1174
				/**
1175
				 * Filter whether the current site is a Jetpack site.
1176
				 *
1177
				 * @module json-api
1178
				 *
1179
				 * @since 3.3.0
1180
				 *
1181
				 * @param bool false Is the current site a Jetpack site. Default to false.
1182
				 * @param int get_current_blog_id() Blog ID.
1183
				 */
1184
				$is_jetpack = true === apply_filters( 'is_jetpack_site', false, get_current_blog_id() );
1185
				$post_id    = $author->ID;
1186
				if ( $is_jetpack && ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
1187
					$ID         = get_post_meta( $post_id, '_jetpack_post_author_external_id', true );
1188
					$email      = get_post_meta( $post_id, '_jetpack_author_email', true );
1189
					$login      = '';
1190
					$name       = get_post_meta( $post_id, '_jetpack_author', true );
1191
					$first_name = '';
1192
					$last_name  = '';
1193
					$URL        = '';
1194
					$nice       = '';
1195
				} else {
1196
					$author = $author->post_author;
1197
				}
1198
			}
1199
1200
			if ( ! isset( $ID ) ) {
1201
				$user = get_user_by( 'id', $author );
1202
				if ( ! $user || is_wp_error( $user ) ) {
1203
					trigger_error( 'Unknown user', E_USER_WARNING );
1204
1205
					return null;
1206
				}
1207
				$ID         = $user->ID;
1208
				$email      = $user->user_email;
1209
				$login      = $user->user_login;
1210
				$name       = $user->display_name;
1211
				$first_name = $user->first_name;
1212
				$last_name  = $user->last_name;
1213
				$URL        = $user->user_url;
1214
				$nice       = $user->user_nicename;
1215
			}
1216
			if ( defined( 'IS_WPCOM' ) && IS_WPCOM && ! $is_jetpack ) {
1217
				$active_blog = get_active_blog_for_user( $ID );
1218
				$site_id     = $active_blog->blog_id;
1219
				if ( $site_id > -1 ) {
1220
					$site_visible = (
1221
						-1 != $active_blog->public ||
1222
						is_private_blog_user( $site_id, get_current_user_id() )
1223
					);
1224
				}
1225
				$profile_URL = "https://en.gravatar.com/{$login}";
1226
			} else {
1227
				$profile_URL = 'https://en.gravatar.com/' . md5( strtolower( trim( $email ) ) );
1228
				$site_id     = -1;
1229
			}
1230
1231
			$avatar_URL = $this->api->get_avatar_url( $email );
1232
		}
1233
1234
		if ( $show_email_and_ip ) {
1235
			$email      = (string) $email;
1236
			$ip_address = (string) $ip_address;
1237
		} else {
1238
			$email      = false;
1239
			$ip_address = false;
1240
		}
1241
1242
		$author = array(
1243
			'ID'          => (int) $ID,
1244
			'login'       => (string) $login,
1245
			'email'       => $email, // (string|bool)
1246
			'name'        => (string) $name,
1247
			'first_name'  => (string) $first_name,
1248
			'last_name'   => (string) $last_name,
1249
			'nice_name'   => (string) $nice,
1250
			'URL'         => (string) esc_url_raw( $URL ),
1251
			'avatar_URL'  => (string) esc_url_raw( $avatar_URL ),
1252
			'profile_URL' => (string) esc_url_raw( $profile_URL ),
1253
			'ip_address'  => $ip_address, // (string|bool)
1254
		);
1255
1256
		if ( $site_id > -1 ) {
1257
			$author['site_ID']      = (int) $site_id;
1258
			$author['site_visible'] = $site_visible;
1259
		}
1260
1261
		return (object) $author;
1262
	}
1263
1264
	function get_media_item( $media_id ) {
1265
		$media_item = get_post( $media_id );
1266
1267
		if ( ! $media_item || is_wp_error( $media_item ) ) {
1268
			return new WP_Error( 'unknown_media', 'Unknown Media', 404 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unknown_media'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
1269
		}
1270
1271
		$response = array(
1272
			'id'          => strval( $media_item->ID ),
1273
			'date'        => (string) $this->format_date( $media_item->post_date_gmt, $media_item->post_date ),
1274
			'parent'      => $media_item->post_parent,
1275
			'link'        => wp_get_attachment_url( $media_item->ID ),
1276
			'title'       => $media_item->post_title,
1277
			'caption'     => $media_item->post_excerpt,
1278
			'description' => $media_item->post_content,
1279
			'metadata'    => wp_get_attachment_metadata( $media_item->ID ),
1280
		);
1281
1282
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM && is_array( $response['metadata'] ) && ! empty( $response['metadata']['file'] ) ) {
1283
			remove_filter( '_wp_relative_upload_path', 'wpcom_wp_relative_upload_path', 10 );
1284
			$response['metadata']['file'] = _wp_relative_upload_path( $response['metadata']['file'] );
1285
			add_filter( '_wp_relative_upload_path', 'wpcom_wp_relative_upload_path', 10, 2 );
1286
		}
1287
1288
		$response['meta'] = (object) array(
1289
			'links' => (object) array(
1290
				'self' => (string) $this->links->get_media_link( $this->api->get_blog_id_for_output(), $media_id ),
1291
				'help' => (string) $this->links->get_media_link( $this->api->get_blog_id_for_output(), $media_id, 'help' ),
1292
				'site' => (string) $this->links->get_site_link( $this->api->get_blog_id_for_output() ),
1293
			),
1294
		);
1295
1296
		return (object) $response;
1297
	}
1298
1299
	function get_media_item_v1_1( $media_id, $media_item = null, $file = null ) {
1300
1301
		if ( ! $media_item ) {
1302
			$media_item = get_post( $media_id );
1303
		}
1304
1305
		if ( ! $media_item || is_wp_error( $media_item ) ) {
1306
			return new WP_Error( 'unknown_media', 'Unknown Media', 404 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unknown_media'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
1307
		}
1308
1309
		$attachment_file = get_attached_file( $media_item->ID );
1310
1311
		$file      = basename( $attachment_file ? $attachment_file : $file );
1312
		$file_info = pathinfo( $file );
1313
		$ext       = isset( $file_info['extension'] ) ? $file_info['extension'] : null;
1314
1315
		$response = array(
1316
			'ID'          => $media_item->ID,
1317
			'URL'         => wp_get_attachment_url( $media_item->ID ),
1318
			'guid'        => $media_item->guid,
1319
			'date'        => (string) $this->format_date( $media_item->post_date_gmt, $media_item->post_date ),
1320
			'post_ID'     => $media_item->post_parent,
1321
			'author_ID'   => (int) $media_item->post_author,
1322
			'file'        => $file,
1323
			'mime_type'   => $media_item->post_mime_type,
1324
			'extension'   => $ext,
1325
			'title'       => $media_item->post_title,
1326
			'caption'     => $media_item->post_excerpt,
1327
			'description' => $media_item->post_content,
1328
			'alt'         => get_post_meta( $media_item->ID, '_wp_attachment_image_alt', true ),
1329
			'icon'        => wp_mime_type_icon( $media_item->ID ),
1330
			'thumbnails'  => array(),
1331
		);
1332
1333
		if ( in_array( $ext, array( 'jpg', 'jpeg', 'png', 'gif' ) ) ) {
1334
			$metadata = wp_get_attachment_metadata( $media_item->ID );
1335 View Code Duplication
			if ( isset( $metadata['height'], $metadata['width'] ) ) {
1336
				$response['height'] = $metadata['height'];
1337
				$response['width']  = $metadata['width'];
1338
			}
1339
1340
			if ( isset( $metadata['sizes'] ) ) {
1341
				/**
1342
				 * Filter the thumbnail sizes available for each attachment ID.
1343
				 *
1344
				 * @module json-api
1345
				 *
1346
				 * @since 3.9.0
1347
				 *
1348
				 * @param array $metadata['sizes'] Array of thumbnail sizes available for a given attachment ID.
1349
				 * @param string $media_id Attachment ID.
1350
				 */
1351
				$sizes = apply_filters( 'rest_api_thumbnail_sizes', $metadata['sizes'], $media_item->ID );
1352 View Code Duplication
				if ( is_array( $sizes ) ) {
1353
					foreach ( $sizes as $size => $size_details ) {
1354
						$response['thumbnails'][ $size ] = dirname( $response['URL'] ) . '/' . $size_details['file'];
1355
					}
1356
					/**
1357
					 * Filter the thumbnail URLs for attachment files.
1358
					 *
1359
					 * @module json-api
1360
					 *
1361
					 * @since 7.1.0
1362
					 *
1363
					 * @param array $metadata['sizes'] Array with thumbnail sizes as keys and URLs as values.
1364
					 */
1365
					$response['thumbnails'] = apply_filters( 'rest_api_thumbnail_size_urls', $response['thumbnails'] );
1366
				}
1367
			}
1368
1369
			if ( isset( $metadata['image_meta'] ) ) {
1370
				$response['exif'] = $metadata['image_meta'];
1371
			}
1372
		}
1373
1374 View Code Duplication
		if ( in_array( $ext, array( 'mp3', 'm4a', 'wav', 'ogg' ) ) ) {
1375
			$metadata           = wp_get_attachment_metadata( $media_item->ID );
1376
			$response['length'] = $metadata['length'];
1377
			$response['exif']   = $metadata;
1378
		}
1379
1380
		$is_video = false;
1381
1382
		if (
1383
			in_array( $ext, array( 'ogv', 'mp4', 'mov', 'wmv', 'avi', 'mpg', '3gp', '3g2', 'm4v' ) )
1384
			||
1385
			$response['mime_type'] === 'video/videopress'
1386
		) {
1387
			$is_video = true;
1388
		}
1389
1390
		if ( $is_video ) {
1391
			$metadata = wp_get_attachment_metadata( $media_item->ID );
1392
1393 View Code Duplication
			if ( isset( $metadata['height'], $metadata['width'] ) ) {
1394
				$response['height'] = $metadata['height'];
1395
				$response['width']  = $metadata['width'];
1396
			}
1397
1398
			if ( isset( $metadata['length'] ) ) {
1399
				$response['length'] = $metadata['length'];
1400
			}
1401
1402
			// add VideoPress info
1403
			if ( function_exists( 'video_get_info_by_blogpostid' ) ) {
1404
				$info = video_get_info_by_blogpostid( $this->api->get_blog_id_for_output(), $media_item->ID );
1405
1406
				// If we failed to get VideoPress info, but it exists in the meta data (for some reason)
1407
				// then let's use that.
1408
				if ( false === $info && isset( $metadata['videopress'] ) ) {
1409
					$info = (object) $metadata['videopress'];
1410
				}
1411
1412
				// Thumbnails
1413 View Code Duplication
				if ( function_exists( 'video_format_done' ) && function_exists( 'video_image_url_by_guid' ) ) {
1414
					$response['thumbnails'] = array(
1415
						'fmt_hd'  => '',
1416
						'fmt_dvd' => '',
1417
						'fmt_std' => '',
1418
					);
1419
					foreach ( $response['thumbnails'] as $size => $thumbnail_url ) {
1420
						if ( video_format_done( $info, $size ) ) {
1421
							$response['thumbnails'][ $size ] = video_image_url_by_guid( $info->guid, $size );
1422
						} else {
1423
							unset( $response['thumbnails'][ $size ] );
1424
						}
1425
					}
1426
				}
1427
1428
				// If we didn't get VideoPress information (for some reason) then let's
1429
				// not try and include it in the response.
1430
				if ( isset( $info->guid ) ) {
1431
					$response['videopress_guid']            = $info->guid;
1432
					$response['videopress_processing_done'] = true;
1433
					if ( '0000-00-00 00:00:00' === $info->finish_date_gmt ) {
1434
						$response['videopress_processing_done'] = false;
1435
					}
1436
				}
1437
			}
1438
		}
1439
1440
		$response['thumbnails'] = (object) $response['thumbnails'];
1441
1442
		$response['meta'] = (object) array(
1443
			'links' => (object) array(
1444
				'self' => (string) $this->links->get_media_link( $this->api->get_blog_id_for_output(), $media_item->ID ),
1445
				'help' => (string) $this->links->get_media_link( $this->api->get_blog_id_for_output(), $media_item->ID, 'help' ),
1446
				'site' => (string) $this->links->get_site_link( $this->api->get_blog_id_for_output() ),
1447
			),
1448
		);
1449
1450
		// add VideoPress link to the meta
1451
		if ( isset( $response['videopress_guid'] ) ) {
1452 View Code Duplication
			if ( function_exists( 'video_get_info_by_blogpostid' ) ) {
1453
				$response['meta']->links->videopress = (string) $this->links->get_link( '/videos/%s', $response['videopress_guid'], '' );
1454
			}
1455
		}
1456
1457 View Code Duplication
		if ( $media_item->post_parent > 0 ) {
1458
			$response['meta']->links->parent = (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $media_item->post_parent );
1459
		}
1460
1461
		return (object) $response;
1462
	}
1463
1464
	function get_taxonomy( $taxonomy_id, $taxonomy_type, $context ) {
1465
1466
		$taxonomy = get_term_by( 'slug', $taxonomy_id, $taxonomy_type );
1467
		// keep updating this function
1468
		if ( ! $taxonomy || is_wp_error( $taxonomy ) ) {
1469
			return new WP_Error( 'unknown_taxonomy', 'Unknown taxonomy', 404 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unknown_taxonomy'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
1470
		}
1471
1472
		return $this->format_taxonomy( $taxonomy, $taxonomy_type, $context );
1473
	}
1474
1475
	function format_taxonomy( $taxonomy, $taxonomy_type, $context ) {
1476
		// Permissions
1477 View Code Duplication
		switch ( $context ) {
1478
			case 'edit':
1479
				$tax = get_taxonomy( $taxonomy_type );
1480
				if ( ! current_user_can( $tax->cap->edit_terms ) ) {
1481
					return new WP_Error( 'unauthorized', 'User cannot edit taxonomy', 403 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unauthorized'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
1482
				}
1483
				break;
1484
			case 'display':
1485
				if ( -1 == get_option( 'blog_public' ) && ! current_user_can( 'read' ) ) {
1486
					return new WP_Error( 'unauthorized', 'User cannot view taxonomy', 403 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unauthorized'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
1487
				}
1488
				break;
1489
			default:
1490
				return new WP_Error( 'invalid_context', 'Invalid API CONTEXT', 400 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'invalid_context'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
1491
		}
1492
1493
		$response                = array();
1494
		$response['ID']          = (int) $taxonomy->term_id;
1495
		$response['name']        = (string) $taxonomy->name;
1496
		$response['slug']        = (string) $taxonomy->slug;
1497
		$response['description'] = (string) $taxonomy->description;
1498
		$response['post_count']  = (int) $taxonomy->count;
1499
		$response['feed_url']    = get_term_feed_link( $taxonomy->term_id, $taxonomy_type );
1500
1501
		if ( is_taxonomy_hierarchical( $taxonomy_type ) ) {
1502
			$response['parent'] = (int) $taxonomy->parent;
1503
		}
1504
1505
		$response['meta'] = (object) array(
1506
			'links' => (object) array(
1507
				'self' => (string) $this->links->get_taxonomy_link( $this->api->get_blog_id_for_output(), $taxonomy->slug, $taxonomy_type ),
1508
				'help' => (string) $this->links->get_taxonomy_link( $this->api->get_blog_id_for_output(), $taxonomy->slug, $taxonomy_type, 'help' ),
1509
				'site' => (string) $this->links->get_site_link( $this->api->get_blog_id_for_output() ),
1510
			),
1511
		);
1512
1513
		return (object) $response;
1514
	}
1515
1516
	/**
1517
	 * Returns ISO 8601 formatted datetime: 2011-12-08T01:15:36-08:00
1518
	 *
1519
	 * @param $date_gmt (string) GMT datetime string.
1520
	 * @param $date (string) Optional.  Used to calculate the offset from GMT.
1521
	 *
1522
	 * @return string
1523
	 */
1524
	function format_date( $date_gmt, $date = null ) {
1525
		return WPCOM_JSON_API_Date::format_date( $date_gmt, $date );
1526
	}
1527
1528
	/**
1529
	 * Parses a date string and returns the local and GMT representations
1530
	 * of that date & time in 'YYYY-MM-DD HH:MM:SS' format without
1531
	 * timezones or offsets. If the parsed datetime was not localized to a
1532
	 * particular timezone or offset we will assume it was given in GMT
1533
	 * relative to now and will convert it to local time using either the
1534
	 * timezone set in the options table for the blog or the GMT offset.
1535
	 *
1536
	 * @param datetime string
1537
	 *
1538
	 * @return array( $local_time_string, $gmt_time_string )
1539
	 */
1540
	function parse_date( $date_string ) {
1541
		$date_string_info = date_parse( $date_string );
1542
		if ( is_array( $date_string_info ) && 0 === $date_string_info['error_count'] ) {
1543
			// Check if it's already localized. Can't just check is_localtime because date_parse('oppossum') returns true; WTF, PHP.
1544
			if ( isset( $date_string_info['zone'] ) && true === $date_string_info['is_localtime'] ) {
1545
				$dt_local = clone $dt_utc = new DateTime( $date_string );
1546
				$dt_utc->setTimezone( new DateTimeZone( 'UTC' ) );
1547
				return array(
1548
					(string) $dt_local->format( 'Y-m-d H:i:s' ),
1549
					(string) $dt_utc->format( 'Y-m-d H:i:s' ),
1550
				);
1551
			}
1552
1553
			// It's parseable but no TZ info so assume UTC
1554
			$dt_local = clone $dt_utc = new DateTime( $date_string, new DateTimeZone( 'UTC' ) );
1555
		} else {
1556
			// Could not parse time, use now in UTC
1557
			$dt_local = clone $dt_utc = new DateTime( 'now', new DateTimeZone( 'UTC' ) );
1558
		}
1559
1560
		// First try to use timezone as it's daylight savings aware.
1561
		$timezone_string = get_option( 'timezone_string' );
1562
		if ( $timezone_string ) {
1563
			$tz = timezone_open( $timezone_string );
1564
			if ( $tz ) {
1565
				$dt_local->setTimezone( $tz );
1566
				return array(
1567
					(string) $dt_local->format( 'Y-m-d H:i:s' ),
1568
					(string) $dt_utc->format( 'Y-m-d H:i:s' ),
1569
				);
1570
			}
1571
		}
1572
1573
		// Fallback to GMT offset (in hours)
1574
		// NOTE: TZ of $dt_local is still UTC, we simply modified the timestamp with an offset.
1575
		$gmt_offset_seconds = intval( get_option( 'gmt_offset' ) * 3600 );
1576
		$dt_local->modify( "+{$gmt_offset_seconds} seconds" );
1577
		return array(
1578
			(string) $dt_local->format( 'Y-m-d H:i:s' ),
1579
			(string) $dt_utc->format( 'Y-m-d H:i:s' ),
1580
		);
1581
	}
1582
1583
	// Load the functions.php file for the current theme to get its post formats, CPTs, etc.
1584
	function load_theme_functions() {
1585
		// bail if we've done this already (can happen when calling /batch endpoint)
1586
		if ( defined( 'REST_API_THEME_FUNCTIONS_LOADED' ) ) {
1587
			return;
1588
		}
1589
1590
		// VIP context loading is handled elsewhere, so bail to prevent
1591
		// duplicate loading. See `switch_to_blog_and_validate_user()`
1592
		if ( function_exists( 'wpcom_is_vip' ) && wpcom_is_vip() ) {
1593
			return;
1594
		}
1595
1596
		define( 'REST_API_THEME_FUNCTIONS_LOADED', true );
1597
1598
		// the theme info we care about is found either within functions.php or one of the jetpack files.
1599
		$function_files = array( '/functions.php', '/inc/jetpack.compat.php', '/inc/jetpack.php', '/includes/jetpack.compat.php' );
1600
1601
		$copy_dirs = array( get_template_directory() );
1602
1603
		// Is this a child theme? Load the child theme's functions file.
1604
		if ( get_stylesheet_directory() !== get_template_directory() && wpcom_is_child_theme() ) {
1605
			foreach ( $function_files as $function_file ) {
1606
				if ( file_exists( get_stylesheet_directory() . $function_file ) ) {
1607
					require_once get_stylesheet_directory() . $function_file;
1608
				}
1609
			}
1610
			$copy_dirs[] = get_stylesheet_directory();
1611
		}
1612
1613
		foreach ( $function_files as $function_file ) {
1614
			if ( file_exists( get_template_directory() . $function_file ) ) {
1615
				require_once get_template_directory() . $function_file;
1616
			}
1617
		}
1618
1619
		// add inc/wpcom.php and/or includes/wpcom.php
1620
		wpcom_load_theme_compat_file();
1621
1622
		// Enable including additional directories or files in actions to be copied
1623
		$copy_dirs = apply_filters( 'restapi_theme_action_copy_dirs', $copy_dirs );
1624
1625
		// since the stuff we care about (CPTS, post formats, are usually on setup or init hooks, we want to load those)
1626
		$this->copy_hooks( 'after_setup_theme', 'restapi_theme_after_setup_theme', $copy_dirs );
1627
1628
		/**
1629
		 * Fires functions hooked onto `after_setup_theme` by the theme for the purpose of the REST API.
1630
		 *
1631
		 * The REST API does not load the theme when processing requests.
1632
		 * To enable theme-based functionality, the API will load the '/functions.php',
1633
		 * '/inc/jetpack.compat.php', '/inc/jetpack.php', '/includes/jetpack.compat.php files
1634
		 * of the theme (parent and child) and copy functions hooked onto 'after_setup_theme' within those files.
1635
		 *
1636
		 * @module json-api
1637
		 *
1638
		 * @since 3.2.0
1639
		 */
1640
		do_action( 'restapi_theme_after_setup_theme' );
1641
		$this->copy_hooks( 'init', 'restapi_theme_init', $copy_dirs );
1642
1643
		/**
1644
		 * Fires functions hooked onto `init` by the theme for the purpose of the REST API.
1645
		 *
1646
		 * The REST API does not load the theme when processing requests.
1647
		 * To enable theme-based functionality, the API will load the '/functions.php',
1648
		 * '/inc/jetpack.compat.php', '/inc/jetpack.php', '/includes/jetpack.compat.php files
1649
		 * of the theme (parent and child) and copy functions hooked onto 'init' within those files.
1650
		 *
1651
		 * @module json-api
1652
		 *
1653
		 * @since 3.2.0
1654
		 */
1655
		do_action( 'restapi_theme_init' );
1656
	}
1657
1658
	function copy_hooks( $from_hook, $to_hook, $base_paths ) {
1659
		global $wp_filter;
1660
		foreach ( $wp_filter as $hook => $actions ) {
1661
1662
			if ( $from_hook != $hook ) {
1663
				continue;
1664
			}
1665
			if ( ! has_action( $hook ) ) {
1666
				continue;
1667
			}
1668
1669
			foreach ( $actions as $priority => $callbacks ) {
1670
				foreach ( $callbacks as $callback_key => $callback_data ) {
1671
					$callback = $callback_data['function'];
1672
1673
					// use reflection api to determine filename where function is defined
1674
					$reflection = $this->get_reflection( $callback );
1675
1676
					if ( false !== $reflection ) {
1677
						$file_name = $reflection->getFileName();
1678
						foreach ( $base_paths as $base_path ) {
1679
1680
							// only copy hooks with functions which are part of the specified files
1681
							if ( 0 === strpos( $file_name, $base_path ) ) {
1682
								add_action(
1683
									$to_hook,
1684
									$callback_data['function'],
1685
									$priority,
1686
									$callback_data['accepted_args']
1687
								);
1688
							}
1689
						}
1690
					}
1691
				}
1692
			}
1693
		}
1694
	}
1695
1696
	function get_reflection( $callback ) {
1697
		if ( is_array( $callback ) ) {
1698
			list( $class, $method ) = $callback;
1699
			return new ReflectionMethod( $class, $method );
1700
		}
1701
1702
		if ( is_string( $callback ) && strpos( $callback, '::' ) !== false ) {
1703
			list( $class, $method ) = explode( '::', $callback );
1704
			return new ReflectionMethod( $class, $method );
1705
		}
1706
1707
		if ( version_compare( PHP_VERSION, '5.3.0', '>=' ) && method_exists( $callback, '__invoke' ) ) {
1708
			return new ReflectionMethod( $callback, '__invoke' );
1709
		}
1710
1711
		if ( is_string( $callback ) && strpos( $callback, '::' ) == false && function_exists( $callback ) ) {
1712
			return new ReflectionFunction( $callback );
1713
		}
1714
1715
		return false;
1716
	}
1717
1718
	/**
1719
	 * Check whether a user can view or edit a post type
1720
	 *
1721
	 * @param string $post_type              post type to check
1722
	 * @param string $context                'display' or 'edit'
1723
	 * @return bool
1724
	 */
1725 View Code Duplication
	function current_user_can_access_post_type( $post_type, $context = 'display' ) {
1726
		$post_type_object = get_post_type_object( $post_type );
1727
		if ( ! $post_type_object ) {
1728
			return false;
1729
		}
1730
1731
		switch ( $context ) {
1732
			case 'edit':
1733
				return current_user_can( $post_type_object->cap->edit_posts );
1734
			case 'display':
1735
				return $post_type_object->public || current_user_can( $post_type_object->cap->read_private_posts );
1736
			default:
1737
				return false;
1738
		}
1739
	}
1740
1741 View Code Duplication
	function is_post_type_allowed( $post_type ) {
1742
		// if the post type is empty, that's fine, WordPress will default to post
1743
		if ( empty( $post_type ) ) {
1744
			return true;
1745
		}
1746
1747
		// allow special 'any' type
1748
		if ( 'any' == $post_type ) {
1749
			return true;
1750
		}
1751
1752
		// check for allowed types
1753
		if ( in_array( $post_type, $this->_get_whitelisted_post_types() ) ) {
1754
			return true;
1755
		}
1756
1757
		if ( $post_type_object = get_post_type_object( $post_type ) ) {
1758
			if ( ! empty( $post_type_object->show_in_rest ) ) {
1759
				return $post_type_object->show_in_rest;
1760
			}
1761
			if ( ! empty( $post_type_object->publicly_queryable ) ) {
1762
				return $post_type_object->publicly_queryable;
1763
			}
1764
		}
1765
1766
		return ! empty( $post_type_object->public );
1767
	}
1768
1769
	/**
1770
	 * Gets the whitelisted post types that JP should allow access to.
1771
	 *
1772
	 * @return array Whitelisted post types.
1773
	 */
1774 View Code Duplication
	protected function _get_whitelisted_post_types() {
1775
		$allowed_types = array( 'post', 'page', 'revision' );
1776
1777
		/**
1778
		 * Filter the post types Jetpack has access to, and can synchronize with WordPress.com.
1779
		 *
1780
		 * @module json-api
1781
		 *
1782
		 * @since 2.2.3
1783
		 *
1784
		 * @param array $allowed_types Array of whitelisted post types. Default to `array( 'post', 'page', 'revision' )`.
1785
		 */
1786
		$allowed_types = apply_filters( 'rest_api_allowed_post_types', $allowed_types );
1787
1788
		return array_unique( $allowed_types );
1789
	}
1790
1791
	function handle_media_creation_v1_1( $media_files, $media_urls, $media_attrs = array(), $force_parent_id = false ) {
1792
1793
		add_filter( 'upload_mimes', array( $this, 'allow_video_uploads' ) );
1794
1795
		$media_ids             = $errors = array();
1796
		$user_can_upload_files = current_user_can( 'upload_files' ) || $this->api->is_authorized_with_upload_token();
0 ignored issues
show
The method is_authorized_with_upload_token() does not seem to exist on object<WPCOM_JSON_API>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1797
		$media_attrs           = array_values( $media_attrs ); // reset the keys
1798
		$i                     = 0;
1799
1800
		if ( ! empty( $media_files ) ) {
1801
			$this->api->trap_wp_die( 'upload_error' );
1802
			foreach ( $media_files as $media_item ) {
1803
				$_FILES['.api.media.item.'] = $media_item;
1804 View Code Duplication
				if ( ! $user_can_upload_files ) {
1805
					$media_id = new WP_Error( 'unauthorized', 'User cannot upload media.', 403 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unauthorized'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
1806
				} else {
1807
					if ( $force_parent_id ) {
1808
						$parent_id = absint( $force_parent_id );
1809
					} elseif ( ! empty( $media_attrs[ $i ] ) && ! empty( $media_attrs[ $i ]['parent_id'] ) ) {
1810
						$parent_id = absint( $media_attrs[ $i ]['parent_id'] );
1811
					} else {
1812
						$parent_id = 0;
1813
					}
1814
					$media_id = media_handle_upload( '.api.media.item.', $parent_id );
1815
				}
1816
				if ( is_wp_error( $media_id ) ) {
1817
					$errors[ $i ]['file']    = $media_item['name'];
1818
					$errors[ $i ]['error']   = $media_id->get_error_code();
1819
					$errors[ $i ]['message'] = $media_id->get_error_message();
1820
				} else {
1821
					$media_ids[ $i ] = $media_id;
1822
				}
1823
1824
				$i++;
1825
			}
1826
			$this->api->trap_wp_die( null );
1827
			unset( $_FILES['.api.media.item.'] );
1828
		}
1829
1830
		if ( ! empty( $media_urls ) ) {
1831
			foreach ( $media_urls as $url ) {
1832 View Code Duplication
				if ( ! $user_can_upload_files ) {
1833
					$media_id = new WP_Error( 'unauthorized', 'User cannot upload media.', 403 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unauthorized'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
1834
				} else {
1835
					if ( $force_parent_id ) {
1836
						$parent_id = absint( $force_parent_id );
1837
					} elseif ( ! empty( $media_attrs[ $i ] ) && ! empty( $media_attrs[ $i ]['parent_id'] ) ) {
1838
						$parent_id = absint( $media_attrs[ $i ]['parent_id'] );
1839
					} else {
1840
						$parent_id = 0;
1841
					}
1842
					$media_id = $this->handle_media_sideload( $url, $parent_id );
1843
				}
1844
				if ( is_wp_error( $media_id ) ) {
1845
					$errors[ $i ] = array(
1846
						'file'    => $url,
1847
						'error'   => $media_id->get_error_code(),
1848
						'message' => $media_id->get_error_message(),
1849
					);
1850
				} elseif ( ! empty( $media_id ) ) {
1851
					$media_ids[ $i ] = $media_id;
1852
				}
1853
1854
				$i++;
1855
			}
1856
		}
1857
1858
		if ( ! empty( $media_attrs ) ) {
1859
			foreach ( $media_ids as $index => $media_id ) {
1860
				if ( empty( $media_attrs[ $index ] ) ) {
1861
					continue;
1862
				}
1863
1864
				$attrs  = $media_attrs[ $index ];
1865
				$insert = array();
1866
1867
				// Attributes: Title, Caption, Description
1868
1869
				if ( isset( $attrs['title'] ) ) {
1870
					$insert['post_title'] = $attrs['title'];
1871
				}
1872
1873
				if ( isset( $attrs['caption'] ) ) {
1874
					$insert['post_excerpt'] = $attrs['caption'];
1875
				}
1876
1877
				if ( isset( $attrs['description'] ) ) {
1878
					$insert['post_content'] = $attrs['description'];
1879
				}
1880
1881
				if ( ! empty( $insert ) ) {
1882
					$insert['ID'] = $media_id;
1883
					wp_update_post( (object) $insert );
1884
				}
1885
1886
				// Attributes: Alt
1887
1888 View Code Duplication
				if ( isset( $attrs['alt'] ) ) {
1889
					$alt = wp_strip_all_tags( $attrs['alt'], true );
1890
					update_post_meta( $media_id, '_wp_attachment_image_alt', $alt );
1891
				}
1892
1893
				// Attributes: Artist, Album
1894
1895
				$id3_meta = array();
1896
1897 View Code Duplication
				foreach ( array( 'artist', 'album' ) as $key ) {
1898
					if ( isset( $attrs[ $key ] ) ) {
1899
						$id3_meta[ $key ] = wp_strip_all_tags( $attrs[ $key ], true );
1900
					}
1901
				}
1902
1903
				if ( ! empty( $id3_meta ) ) {
1904
					// Before updating metadata, ensure that the item is audio
1905
					$item = $this->get_media_item_v1_1( $media_id );
1906
					if ( 0 === strpos( $item->mime_type, 'audio/' ) ) {
1907
						wp_update_attachment_metadata( $media_id, $id3_meta );
1908
					}
1909
				}
1910
			}
1911
		}
1912
1913
		return array(
1914
			'media_ids' => $media_ids,
1915
			'errors'    => $errors,
1916
		);
1917
1918
	}
1919
1920
	function handle_media_sideload( $url, $parent_post_id = 0, $type = 'any' ) {
1921
		if ( ! function_exists( 'download_url' ) || ! function_exists( 'media_handle_sideload' ) ) {
1922
			return false;
1923
		}
1924
1925
		// if we didn't get a URL, let's bail
1926
		$parsed = @parse_url( $url );
1927
		if ( empty( $parsed ) ) {
1928
			return false;
1929
		}
1930
1931
		$tmp = download_url( $url );
1932
		if ( is_wp_error( $tmp ) ) {
1933
			return $tmp;
1934
		}
1935
1936
		// First check to see if we get a mime-type match by file, otherwise, check to
1937
		// see if WordPress supports this file as an image. If neither, then it is not supported.
1938 View Code Duplication
		if ( ! $this->is_file_supported_for_sideloading( $tmp ) || 'image' === $type && ! file_is_displayable_image( $tmp ) ) {
1939
			@unlink( $tmp );
1940
			return new WP_Error( 'invalid_input', 'Invalid file type.', 403 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'invalid_input'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
1941
		}
1942
1943
		// emulate a $_FILES entry
1944
		$file_array = array(
1945
			'name'     => basename( parse_url( $url, PHP_URL_PATH ) ),
1946
			'tmp_name' => $tmp,
1947
		);
1948
1949
		$id = media_handle_sideload( $file_array, $parent_post_id );
1950
		if ( file_exists( $tmp ) ) {
1951
			@unlink( $tmp );
1952
		}
1953
1954
		if ( is_wp_error( $id ) ) {
1955
			return $id;
1956
		}
1957
1958
		if ( ! $id || ! is_int( $id ) ) {
1959
			return false;
1960
		}
1961
1962
		return $id;
1963
	}
1964
1965
	/**
1966
	 * Checks that the mime type of the specified file is among those in a filterable list of mime types.
1967
	 *
1968
	 * @param string $file Path to file to get its mime type.
1969
	 *
1970
	 * @return bool
1971
	 */
1972 View Code Duplication
	protected function is_file_supported_for_sideloading( $file ) {
1973
		if ( class_exists( 'finfo' ) ) { // php 5.3+
1974
			// phpcs:ignore PHPCompatibility.PHP.NewClasses.finfoFound
1975
			$finfo = new finfo( FILEINFO_MIME );
1976
			$mime  = explode( '; ', $finfo->file( $file ) );
1977
			$type  = $mime[0];
1978
1979
		} elseif ( function_exists( 'mime_content_type' ) ) { // PHP 5.2
1980
			$type = mime_content_type( $file );
1981
1982
		} else {
1983
			return false;
1984
		}
1985
1986
		/**
1987
		 * Filter the list of supported mime types for media sideloading.
1988
		 *
1989
		 * @since 4.0.0
1990
		 *
1991
		 * @module json-api
1992
		 *
1993
		 * @param array $supported_mime_types Array of the supported mime types for media sideloading.
1994
		 */
1995
		$supported_mime_types = apply_filters(
1996
			'jetpack_supported_media_sideload_types',
1997
			array(
1998
				'image/png',
1999
				'image/jpeg',
2000
				'image/gif',
2001
				'image/bmp',
2002
				'video/quicktime',
2003
				'video/mp4',
2004
				'video/mpeg',
2005
				'video/ogg',
2006
				'video/3gpp',
2007
				'video/3gpp2',
2008
				'video/h261',
2009
				'video/h262',
2010
				'video/h264',
2011
				'video/x-msvideo',
2012
				'video/x-ms-wmv',
2013
				'video/x-ms-asf',
2014
			)
2015
		);
2016
2017
		// If the type returned was not an array as expected, then we know we don't have a match.
2018
		if ( ! is_array( $supported_mime_types ) ) {
2019
			return false;
2020
		}
2021
2022
		return in_array( $type, $supported_mime_types );
2023
	}
2024
2025
	function allow_video_uploads( $mimes ) {
2026
		// if we are on Jetpack, bail - Videos are already allowed
2027
		if ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) {
2028
			return $mimes;
2029
		}
2030
2031
		// extra check that this filter is only ever applied during REST API requests
2032
		if ( ! defined( 'REST_API_REQUEST' ) || ! REST_API_REQUEST ) {
2033
			return $mimes;
2034
		}
2035
2036
		// bail early if they already have the upgrade..
2037
		if ( get_option( 'video_upgrade' ) == '1' ) {
2038
			return $mimes;
2039
		}
2040
2041
		// lets whitelist to only specific clients right now
2042
		$clients_allowed_video_uploads = array();
2043
		/**
2044
		 * Filter the list of whitelisted video clients.
2045
		 *
2046
		 * @module json-api
2047
		 *
2048
		 * @since 3.2.0
2049
		 *
2050
		 * @param array $clients_allowed_video_uploads Array of whitelisted Video clients.
2051
		 */
2052
		$clients_allowed_video_uploads = apply_filters( 'rest_api_clients_allowed_video_uploads', $clients_allowed_video_uploads );
2053
		if ( ! in_array( $this->api->token_details['client_id'], $clients_allowed_video_uploads ) ) {
2054
			return $mimes;
2055
		}
2056
2057
		$mime_list = wp_get_mime_types();
2058
2059
		$video_exts = explode( ' ', get_site_option( 'video_upload_filetypes', false, false ) );
2060
		/**
2061
		 * Filter the video filetypes allowed on the site.
2062
		 *
2063
		 * @module json-api
2064
		 *
2065
		 * @since 3.2.0
2066
		 *
2067
		 * @param array $video_exts Array of video filetypes allowed on the site.
2068
		 */
2069
		$video_exts  = apply_filters( 'video_upload_filetypes', $video_exts );
2070
		$video_mimes = array();
2071
2072
		if ( ! empty( $video_exts ) ) {
2073
			foreach ( $video_exts as $ext ) {
2074
				foreach ( $mime_list as $ext_pattern => $mime ) {
2075
					if ( $ext != '' && strpos( $ext_pattern, $ext ) !== false ) {
2076
						$video_mimes[ $ext_pattern ] = $mime;
2077
					}
2078
				}
2079
			}
2080
2081
			$mimes = array_merge( $mimes, $video_mimes );
2082
		}
2083
2084
		return $mimes;
2085
	}
2086
2087
	function is_current_site_multi_user() {
2088
		$users = wp_cache_get( 'site_user_count', 'WPCOM_JSON_API_Endpoint' );
2089
		if ( false === $users ) {
2090
			$user_query = new WP_User_Query(
2091
				array(
2092
					'blog_id' => get_current_blog_id(),
2093
					'fields'  => 'ID',
2094
				)
2095
			);
2096
			$users      = (int) $user_query->get_total();
2097
			wp_cache_set( 'site_user_count', $users, 'WPCOM_JSON_API_Endpoint', DAY_IN_SECONDS );
2098
		}
2099
		return $users > 1;
2100
	}
2101
2102
	function allows_cross_origin_requests() {
2103
		return 'GET' == $this->method || $this->allow_cross_origin_request;
2104
	}
2105
2106
	function allows_unauthorized_requests( $origin, $complete_access_origins ) {
2107
		return 'GET' == $this->method || ( $this->allow_unauthorized_request && in_array( $origin, $complete_access_origins ) );
2108
	}
2109
2110
	function get_platform() {
2111
		return wpcom_get_sal_platform( $this->api->token_details );
2112
	}
2113
2114
	/**
2115
	 * Allows the endpoint to perform logic to allow it to decide whether-or-not it should force a
2116
	 * response from the WPCOM API, or potentially go to the Jetpack blog.
2117
	 *
2118
	 * Override this method if you want to do something different.
2119
	 *
2120
	 * @param  int $blog_id
2121
	 * @return bool
2122
	 */
2123
	function force_wpcom_request( $blog_id ) {
2124
		return false;
2125
	}
2126
2127
	/**
2128
	 * Return endpoint response
2129
	 *
2130
	 * @param ... determined by ->$path
2131
	 *
2132
	 * @return
2133
	 *  falsy: HTTP 500, no response body
2134
	 *  WP_Error( $error_code, $error_message, $http_status_code ): HTTP $status_code, json_encode( array( 'error' => $error_code, 'message' => $error_message ) ) response body
2135
	 *  $data: HTTP 200, json_encode( $data ) response body
2136
	 */
2137
	abstract function callback( $path = '' );
2138
2139
2140
}
2141
2142
require_once dirname( __FILE__ ) . '/json-endpoints.php';
2143