PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 9.2
Jetpack – WP Security, Backup, Speed, & Growth v9.2
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
← All changes | class.json-api.php +134 -499 12.3.29.2 View file →
@@ -1,194 +1,52 @@
1 -<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 -/**
3 - * Jetpack JSON API.
4 - *
5 - * @package automattic/jetpack
6 - */
1 +<?php
7 2
8 -use Automattic\Jetpack\Status;
3 +defined( 'WPCOM_JSON_API__DEBUG' ) or define( 'WPCOM_JSON_API__DEBUG', false );
9 4
10 -if ( ! defined( 'WPCOM_JSON_API__DEBUG' ) ) {
11 - define( 'WPCOM_JSON_API__DEBUG', false );
12 -}
5 +require_once dirname( __FILE__ ) . '/sal/class.json-api-platform.php';
13 6
14 -require_once __DIR__ . '/sal/class.json-api-platform.php';
15 -
16 -/**
17 - * Jetpack JSON API.
18 - */
19 7 class WPCOM_JSON_API {
20 - /**
21 - * Static instance.
22 - *
23 - * @todo This should be private.
24 - * @var self|null
25 - */
26 - public static $self = null;
8 + static $self = null;
27 9
28 - /**
29 - * Registered endpoints.
30 - *
31 - * @var WPCOM_JSON_API_Endpoint[]
32 - */
33 10 public $endpoints = array();
34 11
35 - /**
36 - * Endpoint being processed.
37 - *
38 - * @var WPCOM_JSON_API_Endpoint
39 - */
40 - public $endpoint = null;
41 -
42 - /**
43 - * Token details.
44 - *
45 - * @var array
46 - */
47 12 public $token_details = array();
48 13
49 - /**
50 - * Request HTTP method.
51 - *
52 - * @var string
53 - */
54 - public $method = '';
55 -
56 - /**
57 - * Request URL.
58 - *
59 - * @var string
60 - */
61 - public $url = '';
62 -
63 - /**
64 - * Path part of the request URL.
65 - *
66 - * @var string
67 - */
68 - public $path = '';
69 -
70 - /**
71 - * Version extracted from the request URL.
72 - *
73 - * @var string|null
74 - */
75 - public $version = null;
76 -
77 - /**
78 - * Parsed query data.
79 - *
80 - * @var array
81 - */
82 - public $query = array();
83 -
84 - /**
85 - * Post body, if the request is a POST.
86 - *
87 - * @var string|null
88 - */
89 - public $post_body = null;
90 -
91 - /**
92 - * Copy of `$_FILES` if the request is a POST.
93 - *
94 - * @var null|array
95 - */
96 - public $files = null;
97 -
98 - /**
99 - * Content type of the request.
100 - *
101 - * @var string|null
102 - */
14 + public $method = '';
15 + public $url = '';
16 + public $path = '';
17 + public $version = null;
18 + public $query = array();
19 + public $post_body = null;
20 + public $files = null;
103 21 public $content_type = null;
22 + public $accept = '';
104 23
105 - /**
106 - * Value of `$_SERVER['HTTP_ACCEPT']`, if any
107 - *
108 - * @var string
109 - */
110 - public $accept = '';
111 -
112 - /**
113 - * Value of `$_SERVER['HTTPS']`, or "--UNset--" if unset.
114 - *
115 - * @var string
116 - */
117 - public $_server_https; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
118 -
119 - /**
120 - * Whether to exit after serving a response.
121 - *
122 - * @var bool
123 - */
124 - public $exit = true;
125 -
126 - /**
127 - * Public API scheme.
128 - *
129 - * @var string
130 - */
24 + public $_server_https;
25 + public $exit = true;
131 26 public $public_api_scheme = 'https';
132 27
133 - /**
134 - * Output status code.
135 - *
136 - * @var int
137 - */
138 28 public $output_status_code = 200;
139 29
140 - /**
141 - * Trapped error.
142 - *
143 - * @var null|array
144 - */
145 30 public $trapped_error = null;
31 + public $did_output = false;
146 32
147 - /**
148 - * Whether output has been done.
149 - *
150 - * @var bool
151 - */
152 - public $did_output = false;
153 -
154 - /**
155 - * Extra HTTP headers.
156 - *
157 - * @var string
158 - */
159 33 public $extra_headers = array();
160 34
161 - /**
162 - * AMP source origin.
163 - *
164 - * @var string
165 - */
166 35 public $amp_source_origin = null;
167 36
168 37 /**
169 - * Initialize.
170 - *
171 - * @param string|null $method As for `$this->setup_inputs()`.
172 - * @param string|null $url As for `$this->setup_inputs()`.
173 - * @param string|null $post_body As for `$this->setup_inputs()`.
174 38 * @return WPCOM_JSON_API instance
175 39 */
176 - public static function init( $method = null, $url = null, $post_body = null ) {
40 + static function init( $method = null, $url = null, $post_body = null ) {
177 41 if ( ! self::$self ) {
178 - self::$self = new static( $method, $url, $post_body );
42 + $class = function_exists( 'get_called_class' ) ? get_called_class() : __CLASS__; // phpcs:ignore PHPCompatibility.PHP.NewFunctions.get_called_classFound
43 + self::$self = new $class( $method, $url, $post_body );
179 44 }
180 45 return self::$self;
181 46 }
182 47
183 - /**
184 - * Add an endpoint.
185 - *
186 - * @param WPCOM_JSON_API_Endpoint $endpoint Endpoint to add.
187 - */
188 - public function add( WPCOM_JSON_API_Endpoint $endpoint ) {
189 - // @todo Determine if anything depends on this being serialized rather than e.g. JSON.
190 - // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- Legacy, possibly depended on elsewhere.
48 + function add( WPCOM_JSON_API_Endpoint $endpoint ) {
191 49 $path_versions = serialize(
192 50 array(
193 51 $endpoint->path,
194 52 $endpoint->min_version,
@@ -200,15 +58,9 @@
200 58 }
201 59 $this->endpoints[ $path_versions ][ $endpoint->method ] = $endpoint;
202 60 }
203 61
204 - /**
205 - * Determine if a string is truthy.
206 - *
207 - * @param string $value "1", "t", and "true" (case insensitive) are falsey, everything else isn't.
208 - * @return bool
209 - */
210 - public static function is_truthy( $value ) {
62 + static function is_truthy( $value ) {
211 63 switch ( strtolower( (string) $value ) ) {
212 64 case '1':
213 65 case 't':
214 66 case 'true':
@@ -217,15 +69,9 @@
217 69
218 70 return false;
219 71 }
220 72
221 - /**
222 - * Determine if a string is falsey.
223 - *
224 - * @param string $value "0", "f", and "false" (case insensitive) are falsey, everything else isn't.
225 - * @return bool
226 - */
227 - public static function is_falsy( $value ) {
73 + static function is_falsy( $value ) {
228 74 switch ( strtolower( (string) $value ) ) {
229 75 case '0':
230 76 case 'f':
231 77 case 'false':
@@ -234,36 +80,20 @@
234 80
235 81 return false;
236 82 }
237 83
238 - /**
239 - * Constructor.
240 - *
241 - * @todo This should be private.
242 - * @param string|null $method As for `$this->setup_inputs()`.
243 - * @param string|null $url As for `$this->setup_inputs()`.
244 - * @param string|null $post_body As for `$this->setup_inputs()`.
245 - */
246 - public function __construct( $method = null, $url = null, $post_body = null ) {
247 - $this->setup_inputs( $method, $url, $post_body );
84 + function __construct( ...$args ) {
85 + call_user_func_array( array( $this, 'setup_inputs' ), $args );
248 86 }
249 87
250 - /**
251 - * Setup inputs.
252 - *
253 - * @param string|null $method Request HTTP method. Fetched from `$_SERVER` if null.
254 - * @param string|null $url URL requested. Determined from `$_SERVER` if null.
255 - * @param string|null $post_body POST body. Read from `php://input` if null and method is POST.
256 - */
257 - public function setup_inputs( $method = null, $url = null, $post_body = null ) {
258 - if ( $method === null ) {
259 - $this->method = isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( filter_var( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) : '';
88 + function setup_inputs( $method = null, $url = null, $post_body = null ) {
89 + if ( is_null( $method ) ) {
90 + $this->method = strtoupper( $_SERVER['REQUEST_METHOD'] );
260 91 } else {
261 92 $this->method = strtoupper( $method );
262 93 }
263 - if ( $url === null ) {
264 - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sniff misses the esc_url_raw.
265 - $this->url = esc_url_raw( set_url_scheme( 'http://' . ( isset( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : '' ) . ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '' ) ) );
94 + if ( is_null( $url ) ) {
95 + $this->url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
266 96 } else {
267 97 $this->url = $url;
268 98 }
269 99
@@ -275,20 +105,20 @@
275 105 if ( ! empty( $parsed['query'] ) ) {
276 106 wp_parse_str( $parsed['query'], $this->query );
277 107 }
278 108
279 - if ( ! empty( $_SERVER['HTTP_ACCEPT'] ) ) {
280 - $this->accept = filter_var( wp_unslash( $_SERVER['HTTP_ACCEPT'] ) );
109 + if ( isset( $_SERVER['HTTP_ACCEPT'] ) && $_SERVER['HTTP_ACCEPT'] ) {
110 + $this->accept = $_SERVER['HTTP_ACCEPT'];
281 111 }
282 112
283 113 if ( 'POST' === $this->method ) {
284 - if ( $post_body === null ) {
114 + if ( is_null( $post_body ) ) {
285 115 $this->post_body = file_get_contents( 'php://input' );
286 116
287 - if ( ! empty( $_SERVER['HTTP_CONTENT_TYPE'] ) ) {
288 - $this->content_type = filter_var( wp_unslash( $_SERVER['HTTP_CONTENT_TYPE'] ) );
289 - } elseif ( ! empty( $_SERVER['CONTENT_TYPE'] ) ) {
290 - $this->content_type = filter_var( wp_unslash( $_SERVER['CONTENT_TYPE'] ) );
117 + if ( isset( $_SERVER['HTTP_CONTENT_TYPE'] ) && $_SERVER['HTTP_CONTENT_TYPE'] ) {
118 + $this->content_type = $_SERVER['HTTP_CONTENT_TYPE'];
119 + } elseif ( isset( $_SERVER['CONTENT_TYPE'] ) && $_SERVER['CONTENT_TYPE'] ) {
120 + $this->content_type = $_SERVER['CONTENT_TYPE'];
291 121 } elseif ( '{' === $this->post_body[0] ) {
292 122 $this->content_type = 'application/json';
293 123 } else {
294 124 $this->content_type = 'application/x-www-form-urlencoded';
@@ -294,9 +124,8 @@
294 124 $this->content_type = 'application/x-www-form-urlencoded';
295 125 }
296 126
297 127 if ( 0 === strpos( strtolower( $this->content_type ), 'multipart/' ) ) {
298 - // phpcs:ignore WordPress.Security.NonceVerification.Missing
299 128 $this->post_body = http_build_query( stripslashes_deep( $_POST ) );
300 129 $this->files = $_FILES;
301 130 $this->content_type = 'multipart/form-data';
302 131 }
@@ -301,9 +130,9 @@
301 130 $this->content_type = 'multipart/form-data';
302 131 }
303 132 } else {
304 133 $this->post_body = $post_body;
305 - $this->content_type = isset( $this->post_body[0] ) && '{' === $this->post_body[0] ? 'application/json' : 'application/x-www-form-urlencoded';
134 + $this->content_type = '{' === isset( $this->post_body[0] ) && $this->post_body[0] ? 'application/json' : 'application/x-www-form-urlencoded';
306 135 }
307 136 } else {
308 137 $this->post_body = null;
309 138 $this->content_type = null;
@@ -308,19 +137,13 @@
308 137 $this->post_body = null;
309 138 $this->content_type = null;
310 139 }
311 140
312 - $this->_server_https = array_key_exists( 'HTTPS', $_SERVER ) ? filter_var( wp_unslash( $_SERVER['HTTPS'] ) ) : '--UNset--';
141 + $this->_server_https = array_key_exists( 'HTTPS', $_SERVER ) ? $_SERVER['HTTPS'] : '--UNset--';
313 142 }
314 143
315 - /**
316 - * Initialize.
317 - *
318 - * @return null|WP_Error (although this implementation always returns null)
319 - */
320 - public function initialize() {
144 + function initialize() {
321 145 $this->token_details['blog_id'] = Jetpack_Options::get_option( 'id' );
322 - return null;
323 146 }
324 147
325 148 /**
326 149 * Checks if the current request is authorized with a blog token.
@@ -355,22 +178,16 @@
355 178
356 179 return true;
357 180 }
358 181
359 - /**
360 - * Serve.
361 - *
362 - * @param bool $exit Whether to exit.
363 - * @return string|null Content type (assuming it didn't exit), or null in certain error cases.
364 - */
365 - public function serve( $exit = true ) {
366 - ini_set( 'display_errors', false ); // phpcs:ignore WordPress.PHP.IniSet.display_errors_Blacklisted
182 + function serve( $exit = true ) {
183 + ini_set( 'display_errors', false );
367 184
368 185 $this->exit = (bool) $exit;
369 186
370 187 // This was causing problems with Jetpack, but is necessary for wpcom
371 188 // @see https://github.com/Automattic/jetpack/pull/2603
372 - // @see r124548-wpcom .
189 + // @see r124548-wpcom
373 190 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
374 191 add_filter( 'home_url', array( $this, 'ensure_http_scheme_of_home_url' ), 10, 3 );
375 192 }
376 193
@@ -378,9 +195,9 @@
378 195
379 196 add_filter( 'comment_edit_pre', array( $this, 'comment_edit_pre' ) );
380 197
381 198 $initialization = $this->initialize();
382 - if ( 'OPTIONS' === $this->method ) {
199 + if ( 'OPTIONS' == $this->method ) {
383 200 /**
384 201 * Fires before the page output.
385 202 * Can be used to specify custom header options.
386 203 *
@@ -396,9 +213,9 @@
396 213 $this->output_error( $initialization );
397 214 return;
398 215 }
399 216
400 - // Normalize path and extract API version.
217 + // Normalize path and extract API version
401 218 $this->path = untrailingslashit( $this->path );
402 219 preg_match( '#^/rest/v(\d+(\.\d+)*)#', $this->path, $matches );
403 220 $this->path = substr( $this->path, strlen( $matches[0] ) );
404 221 $this->version = $matches[1];
@@ -411,17 +228,17 @@
411 228
412 229 if ( $is_help ) {
413 230 $origin = get_http_origin();
414 231
415 - if ( ! empty( $origin ) && 'GET' === $this->method ) {
232 + if ( ! empty( $origin ) && 'GET' == $this->method ) {
416 233 header( 'Access-Control-Allow-Origin: ' . esc_url_raw( $origin ) );
417 234 }
418 235
419 236 $this->path = substr( rtrim( $this->path, '/' ), 0, -5 );
420 - // Show help for all matching endpoints regardless of method.
237 + // Show help for all matching endpoints regardless of method
421 238 $methods = $allowed_methods;
422 239 $find_all_matching_endpoints = true;
423 - // How deep to truncate each endpoint's path to see if it matches this help request.
240 + // How deep to truncate each endpoint's path to see if it matches this help request
424 241 $depth = substr_count( $this->path, '/' ) + 1;
425 242 if ( false !== stripos( $this->accept, 'javascript' ) || false !== stripos( $this->accept, 'json' ) ) {
426 243 $help_content_type = 'json';
427 244 } else {
@@ -426,30 +243,30 @@
426 243 $help_content_type = 'json';
427 244 } else {
428 245 $help_content_type = 'html';
429 246 }
430 - } elseif ( in_array( $this->method, $allowed_methods, true ) ) {
431 - // Only serve requested method.
432 - $methods = array( $this->method );
433 - $find_all_matching_endpoints = false;
434 247 } else {
435 - // We don't allow this requested method - find matching endpoints and send 405.
436 - $methods = $allowed_methods;
437 - $find_all_matching_endpoints = true;
438 - $four_oh_five = true;
248 + if ( in_array( $this->method, $allowed_methods ) ) {
249 + // Only serve requested method
250 + $methods = array( $this->method );
251 + $find_all_matching_endpoints = false;
252 + } else {
253 + // We don't allow this requested method - find matching endpoints and send 405
254 + $methods = $allowed_methods;
255 + $find_all_matching_endpoints = true;
256 + $four_oh_five = true;
257 + }
439 258 }
440 259
441 - // Find which endpoint to serve.
260 + // Find which endpoint to serve
442 261 $found = false;
443 262 foreach ( $this->endpoints as $endpoint_path_versions => $endpoints_by_method ) {
444 - // @todo Determine if anything depends on this being serialized rather than e.g. JSON.
445 - // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize -- Legacy, possibly depended on elsewhere.
446 263 $endpoint_path_versions = unserialize( $endpoint_path_versions );
447 264 $endpoint_path = $endpoint_path_versions[0];
448 265 $endpoint_min_version = $endpoint_path_versions[1];
449 266 $endpoint_max_version = $endpoint_path_versions[2];
450 267
451 - // Make sure max_version is not less than min_version.
268 + // Make sure max_version is not less than min_version
452 269 if ( version_compare( $endpoint_max_version, $endpoint_min_version, '<' ) ) {
453 270 $endpoint_max_version = $endpoint_min_version;
454 271 }
455 272
@@ -457,16 +274,16 @@
457 274 if ( ! isset( $endpoints_by_method[ $method ] ) ) {
458 275 continue;
459 276 }
460 277
461 - // Normalize.
278 + // Normalize
462 279 $endpoint_path = untrailingslashit( $endpoint_path );
463 280 if ( $is_help ) {
464 - // Truncate path at help depth.
465 - $endpoint_path = implode( '/', array_slice( explode( '/', $endpoint_path ), 0, $depth ) );
281 + // Truncate path at help depth
282 + $endpoint_path = join( '/', array_slice( explode( '/', $endpoint_path ), 0, $depth ) );
466 283 }
467 284
468 - // Generate regular expression from sprintf().
285 + // Generate regular expression from sprintf()
469 286 $endpoint_path_regex = str_replace( array( '%s', '%d' ), array( '([^/?&]+)', '(\d+)' ), $endpoint_path );
470 287
471 288 if ( ! preg_match( "#^$endpoint_path_regex\$#", $this->path, $path_pieces ) ) {
472 289 // This endpoint does not match the requested path.
@@ -482,9 +299,9 @@
482 299
483 300 if ( $find_all_matching_endpoints ) {
484 301 $matching_endpoints[] = array( $endpoints_by_method[ $method ], $path_pieces );
485 302 } else {
486 - // The method parameters are now in $path_pieces.
303 + // The method parameters are now in $path_pieces
487 304 $endpoint = $endpoints_by_method[ $method ];
488 305 break 2;
489 306 }
490 307 }
@@ -499,9 +316,9 @@
499 316 foreach ( $matching_endpoints as $matching_endpoint ) {
500 317 $allowed_methods[] = $matching_endpoint[0]->method;
501 318 }
502 319
503 - header( 'Allow: ' . strtoupper( implode( ',', array_unique( $allowed_methods ) ) ) );
320 + header( 'Allow: ' . strtoupper( join( ',', array_unique( $allowed_methods ) ) ) );
504 321 return $this->output(
505 322 405,
506 323 array(
507 324 'error' => 'not_allowed',
@@ -559,28 +376,14 @@
559 376
560 377 return $this->output( $output_status_code, $response, 'application/json', $this->extra_headers );
561 378 }
562 379
563 - /**
564 - * Process a request.
565 - *
566 - * @param WPCOM_JSON_API_Endpoint $endpoint Endpoint.
567 - * @param array $path_pieces Path pieces.
568 - * @return array|WP_Error Return value from the endpoint's callback.
569 - */
570 - public function process_request( WPCOM_JSON_API_Endpoint $endpoint, $path_pieces ) {
380 + function process_request( WPCOM_JSON_API_Endpoint $endpoint, $path_pieces ) {
571 381 $this->endpoint = $endpoint;
572 382 return call_user_func_array( array( $endpoint, 'callback' ), $path_pieces );
573 383 }
574 384
575 - /**
576 - * Output a response or error without exiting.
577 - *
578 - * @param int $status_code HTTP status code.
579 - * @param mixed $response Response data.
580 - * @param string $content_type Content type of the response.
581 - */
582 - public function output_early( $status_code, $response = null, $content_type = 'application/json' ) {
385 + function output_early( $status_code, $response = null, $content_type = 'application/json' ) {
583 386 $exit = $this->exit;
584 387 $this->exit = false;
585 388 if ( is_wp_error( $response ) ) {
586 389 $this->output_error( $response );
@@ -592,30 +395,14 @@
592 395 $this->finish_request();
593 396 }
594 397 }
595 398
596 - /**
597 - * Set output status code.
598 - *
599 - * @param int $code HTTP status code.
600 - */
601 - public function set_output_status_code( $code = 200 ) {
399 + function set_output_status_code( $code = 200 ) {
602 400 $this->output_status_code = $code;
603 401 }
604 402
605 - /**
606 - * Output a response.
607 - *
608 - * @param int $status_code HTTP status code.
609 - * @param mixed $response Response data.
610 - * @param string $content_type Content type of the response.
611 - * @param array $extra Additional HTTP headers.
612 - * @return string Content type (assuming it didn't exit).
613 - */
614 - public function output( $status_code, $response = null, $content_type = 'application/json', $extra = array() ) {
615 - $status_code = (int) $status_code;
616 -
617 - // In case output() was called before the callback returned.
403 + function output( $status_code, $response = null, $content_type = 'application/json', $extra = array() ) {
404 + // In case output() was called before the callback returned
618 405 if ( $this->did_output ) {
619 406 if ( $this->exit ) {
620 407 exit;
621 408 }
@@ -623,9 +410,9 @@
623 410 }
624 411 $this->did_output = true;
625 412
626 413 // 400s and 404s are allowed for all origins
627 - if ( 404 === $status_code || 400 === $status_code ) {
414 + if ( 404 == $status_code || 400 == $status_code ) {
628 415 header( 'Access-Control-Allow-Origin: *' );
629 416 }
630 417
631 418 /* Add headers for form submission from <amp-form/> */
@@ -633,9 +420,10 @@
633 420 header( 'Access-Control-Allow-Origin: ' . wp_unslash( $this->amp_source_origin ) );
634 421 header( 'Access-Control-Allow-Credentials: true' );
635 422 }
636 423
637 - if ( $response === null ) {
424 +
425 + if ( is_null( $response ) ) {
638 426 $response = new stdClass();
639 427 }
640 428
641 429 if ( 'text/plain' === $content_type ||
@@ -644,9 +432,9 @@
644 432 header( 'Content-Type: ' . $content_type );
645 433 foreach ( $extra as $key => $value ) {
646 434 header( "$key: $value" );
647 435 }
648 - echo $response; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
436 + echo $response;
649 437 if ( $this->exit ) {
650 438 exit;
651 439 }
652 440
@@ -689,13 +477,13 @@
689 477
690 478 if ( $callback ) {
691 479 // Mitigate Rosetta Flash [1] by setting the Content-Type-Options: nosniff header
692 480 // and by prepending the JSONP response with a JS comment.
693 - // [1] <https://blog.miki.it/2014/7/8/abusing-jsonp-with-rosetta-flash/index.html>.
694 - echo "/**/$callback("; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- This is JSONP output, not HTML.
481 + // [1] https://blog.miki.it/2014/7/8/abusing-jsonp-with-rosetta-flash/index.html
482 + echo "/**/$callback(";
695 483
696 484 }
697 - echo $this->json_encode( $response ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- This is JSON or JSONP output, not HTML.
485 + echo $this->json_encode( $response );
698 486 if ( $callback ) {
699 487 echo ');';
700 488 }
701 489
@@ -705,14 +493,8 @@
705 493
706 494 return $content_type;
707 495 }
708 496
709 - /**
710 - * Serialize an error.
711 - *
712 - * @param WP_Error $error Error.
713 - * @return array with 'status_code' and 'errors' data.
714 - */
715 497 public static function serializable_error( $error ) {
716 498
717 499 $status_code = $error->get_error_data();
718 500
@@ -727,10 +509,9 @@
727 509 'error' => $error->get_error_code(),
728 510 'message' => $error->get_error_message(),
729 511 );
730 512
731 - $additional_data = $error->get_error_data( 'additional_data' );
732 - if ( $additional_data ) {
513 + if ( $additional_data = $error->get_error_data( 'additional_data' ) ) {
733 514 $response['data'] = $additional_data;
734 515 }
735 516
736 517 return array(
@@ -738,27 +519,15 @@
738 519 'errors' => $response,
739 520 );
740 521 }
741 522
742 - /**
743 - * Output an error.
744 - *
745 - * @param WP_Error $error Error.
746 - * @return string Content type (assuming it didn't exit).
747 - */
748 - public function output_error( $error ) {
749 - $error_response = static::serializable_error( $error );
523 + function output_error( $error ) {
524 + $error_response = $this->serializable_error( $error );
750 525
751 526 return $this->output( $error_response['status_code'], $error_response['errors'] );
752 527 }
753 528
754 - /**
755 - * Filter fields in a response.
756 - *
757 - * @param array|object $response Response.
758 - * @return array|object Filtered response.
759 - */
760 - public function filter_fields( $response ) {
529 + function filter_fields( $response ) {
761 530 if ( empty( $this->query['fields'] ) || ( is_array( $response ) && ! empty( $response['error'] ) ) || ! empty( $this->endpoint->custom_fields_filtering ) ) {
762 531 return $response;
763 532 }
764 533
@@ -796,9 +565,8 @@
796 565
797 566 foreach ( $response[ $key_to_filter ] as $key => $values ) {
798 567 if ( is_object( $values ) ) {
799 568 if ( is_object( $response[ $key_to_filter ] ) ) {
800 - // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.Found -- False positive.
801 569 $response[ $key_to_filter ]->$key = (object) array_intersect_key( ( (array) $values ), array_flip( $fields ) );
802 570 } elseif ( is_array( $response[ $key_to_filter ] ) ) {
803 571 $response[ $key_to_filter ][ $key ] = (object) array_intersect_key( ( (array) $values ), array_flip( $fields ) );
804 572 }
@@ -821,19 +589,9 @@
821 589
822 590 return $response;
823 591 }
824 592
825 - /**
826 - * Filter for `home_url`.
827 - *
828 - * If `$original_scheme` is null, turns an https URL to http.
829 - *
830 - * @param string $url The complete home URL including scheme and path.
831 - * @param string $path Path relative to the home URL. Blank string if no path is specified.
832 - * @param string|null $original_scheme Scheme to give the home URL context. Accepts 'http', 'https', 'relative', 'rest', or null.
833 - * @return string URL.
834 - */
835 - public function ensure_http_scheme_of_home_url( $url, $path, $original_scheme ) {
593 + function ensure_http_scheme_of_home_url( $url, $path, $original_scheme ) {
836 594 if ( $original_scheme ) {
837 595 return $url;
838 596 }
839 597
@@ -839,66 +597,31 @@
839 597
840 598 return preg_replace( '#^https:#', 'http:', $url );
841 599 }
842 600
843 - /**
844 - * Decode HTML special characters in comment content.
845 - *
846 - * @param string $comment_content Comment content.
847 - * @return string
848 - */
849 - public function comment_edit_pre( $comment_content ) {
601 + function comment_edit_pre( $comment_content ) {
850 602 return htmlspecialchars_decode( $comment_content, ENT_QUOTES );
851 603 }
852 604
853 - /**
854 - * JSON encode.
855 - *
856 - * @param mixed $data Data.
857 - * @return string|false
858 - */
859 - public function json_encode( $data ) {
605 + function json_encode( $data ) {
860 606 return wp_json_encode( $data );
861 607 }
862 608
863 - /**
864 - * Test if a string ends with a string.
865 - *
866 - * @param string $haystack String to check.
867 - * @param string $needle Suffix to check.
868 - * @return bool
869 - */
870 - public function ends_with( $haystack, $needle ) {
871 - return substr( $haystack, -strlen( $needle ) ) === $needle;
609 + function ends_with( $haystack, $needle ) {
610 + return $needle === substr( $haystack, -strlen( $needle ) );
872 611 }
873 612
874 - /**
875 - * Returns the site's blog_id in the WP.com ecosystem
876 - *
877 - * @return int
878 - */
879 - public function get_blog_id_for_output() {
613 + // Returns the site's blog_id in the WP.com ecosystem
614 + function get_blog_id_for_output() {
880 615 return $this->token_details['blog_id'];
881 616 }
882 617
883 - /**
884 - * Returns the site's local blog_id.
885 - *
886 - * @param int $blog_id Blog ID.
887 - * @return int
888 - */
889 - public function get_blog_id( $blog_id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
618 + // Returns the site's local blog_id
619 + function get_blog_id( $blog_id ) {
890 620 return $GLOBALS['blog_id'];
891 621 }
892 622
893 - /**
894 - * Switch to blog and validate user.
895 - *
896 - * @param int $blog_id Blog ID.
897 - * @param bool $verify_token_for_blog Whether to verify the token.
898 - * @return int Blog ID.
899 - */
900 - public function switch_to_blog_and_validate_user( $blog_id = 0, $verify_token_for_blog = true ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
623 + function switch_to_blog_and_validate_user( $blog_id = 0, $verify_token_for_blog = true ) {
901 624 if ( $this->is_restricted_blog( $blog_id ) ) {
902 625 return new WP_Error( 'unauthorized', 'User cannot access this restricted blog', 403 );
903 626 }
904 627 /**
@@ -905,9 +628,9 @@
905 628 * If this is a private site we check for 2 things:
906 629 * 1. In case of user based authentication, we need to check if the logged-in user has the 'read' capability.
907 630 * 2. In case of site based authentication, make sure the endpoint accepts it.
908 631 */
909 - if ( ( new Status() )->is_private_site() &&
632 + if ( -1 === (int) get_option( 'blog_public' ) &&
910 633 ! current_user_can( 'read' ) &&
911 634 ! $this->endpoint->accepts_site_based_authentication()
912 635 ) {
913 636 return new WP_Error( 'unauthorized', 'User cannot access this private blog.', 403 );
@@ -915,15 +638,10 @@
915 638
916 639 return $blog_id;
917 640 }
918 641
919 - /**
920 - * Returns true if the specified blog ID is a restricted blog
921 - *
922 - * @param int $blog_id Blog ID.
923 - * @return bool
924 - */
925 - public function is_restricted_blog( $blog_id ) {
642 + // Returns true if the specified blog ID is a restricted blog
643 + function is_restricted_blog( $blog_id ) {
926 644 /**
927 645 * Filters all REST API access and return a 403 unauthorized response for all Restricted blog IDs.
928 646 *
929 647 * @module json-api
@@ -932,73 +650,32 @@
932 650 *
933 651 * @param array $array Array of Blog IDs.
934 652 */
935 653 $restricted_blog_ids = apply_filters( 'wpcom_json_api_restricted_blog_ids', array() );
936 - return true === in_array( $blog_id, $restricted_blog_ids ); // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict -- I don't trust filters to return the right types.
654 + return true === in_array( $blog_id, $restricted_blog_ids );
937 655 }
938 656
939 - /**
940 - * Post like count.
941 - *
942 - * @param int $blog_id Blog ID.
943 - * @param int $post_id Post ID.
944 - * @return int
945 - */
946 - public function post_like_count( $blog_id, $post_id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
657 + function post_like_count( $blog_id, $post_id ) {
947 658 return 0;
948 659 }
949 660
950 - /**
951 - * Is liked?
952 - *
953 - * @param int $blog_id Blog ID.
954 - * @param int $post_id Post ID.
955 - * @return bool
956 - */
957 - public function is_liked( $blog_id, $post_id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
661 + function is_liked( $blog_id, $post_id ) {
958 662 return false;
959 663 }
960 664
961 - /**
962 - * Is reblogged?
963 - *
964 - * @param int $blog_id Blog ID.
965 - * @param int $post_id Post ID.
966 - * @return bool
967 - */
968 - public function is_reblogged( $blog_id, $post_id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
665 + function is_reblogged( $blog_id, $post_id ) {
969 666 return false;
970 667 }
971 668
972 - /**
973 - * Is following?
974 - *
975 - * @param int $blog_id Blog ID.
976 - * @return bool
977 - */
978 - public function is_following( $blog_id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
669 + function is_following( $blog_id ) {
979 670 return false;
980 671 }
981 672
982 - /**
983 - * Add global ID.
984 - *
985 - * @param int $blog_id Blog ID.
986 - * @param int $post_id Post ID.
987 - * @return string
988 - */
989 - public function add_global_ID( $blog_id, $post_id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable, WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
673 + function add_global_ID( $blog_id, $post_id ) {
990 674 return '';
991 675 }
992 676
993 - /**
994 - * Get avatar URL.
995 - *
996 - * @param string $email Email.
997 - * @param array $avatar_size Args for `get_avatar_url()`.
998 - * @return string|false
999 - */
1000 - public function get_avatar_url( $email, $avatar_size = null ) {
677 + function get_avatar_url( $email, $avatar_size = null ) {
1001 678 if ( function_exists( 'wpcom_get_avatar_url' ) ) {
1002 679 return null === $avatar_size
1003 680 ? wpcom_get_avatar_url( $email )
1004 681 : wpcom_get_avatar_url( $email, $avatar_size );
@@ -1009,11 +686,11 @@
1009 686 }
1010 687 }
1011 688
1012 689 /**
1013 - * Counts the number of comments on a site, including certain comment types.
690 + * Counts the number of comments on a site, excluding certain comment types.
1014 691 *
1015 - * @param int $post_id Post ID.
692 + * @param $post_id int Post ID.
1016 693 * @return array Array of counts, matching the output of https://developer.wordpress.org/reference/functions/get_comment_count/.
1017 694 */
1018 695 public function wp_count_comments( $post_id ) {
1019 696 global $wpdb;
@@ -1026,44 +703,30 @@
1026 703 'all' => 0,
1027 704 );
1028 705
1029 706 /**
1030 - * Exclude certain comment types from comment counts in the REST API.
1031 - *
1032 - * @since 6.9.0
1033 - * @deprecated 11.1
1034 - * @module json-api
1035 - *
1036 - * @param array Array of comment types to exclude (default: 'order_note', 'webhook_delivery', 'review', 'action_log')
1037 - */
1038 - $exclude = apply_filters_deprecated( 'jetpack_api_exclude_comment_types_count', array( 'order_note', 'webhook_delivery', 'review', 'action_log' ), 'jetpack-11.1', 'jetpack_api_include_comment_types_count' ); // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
1039 -
1040 - /**
1041 - * Include certain comment types in comment counts in the REST API.
1042 - * Note: the default array of comment types includes an empty string,
1043 - * to support comments posted before WP 5.5, that used an empty string as comment type.
1044 - *
1045 - * @since 11.1
1046 - * @module json-api
1047 - *
1048 - * @param array Array of comment types to include (default: 'comment', 'pingback', 'trackback')
1049 - */
1050 - $include = apply_filters(
1051 - 'jetpack_api_include_comment_types_count',
1052 - array( 'comment', 'pingback', 'trackback', '' )
707 + * Exclude certain comment types from comment counts in the REST API.
708 + *
709 + * @since 6.9.0
710 + * @module json-api
711 + *
712 + * @param array Array of comment types to exclude (default: 'order_note', 'webhook_delivery', 'review', 'action_log')
713 + */
714 + $exclude = apply_filters(
715 + 'jetpack_api_exclude_comment_types_count',
716 + array( 'order_note', 'webhook_delivery', 'review', 'action_log' )
1053 717 );
1054 718
1055 - if ( empty( $include ) ) {
719 + if ( empty( $exclude ) ) {
1056 720 return wp_count_comments( $post_id );
1057 721 }
1058 722
1059 - array_walk( $include, 'esc_sql' );
723 + array_walk( $exclude, 'esc_sql' );
1060 724 $where = sprintf(
1061 - "WHERE comment_type IN ( '%s' )",
1062 - implode( "','", $include )
725 + "WHERE comment_type NOT IN ( '%s' )",
726 + implode( "','", $exclude )
1063 727 );
1064 728
1065 - // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- `$where` is built with escaping just above.
1066 729 $count = $wpdb->get_results(
1067 730 "SELECT comment_approved, COUNT(*) AS num_comments
1068 731 FROM $wpdb->comments
1069 732 {$where}
@@ -1069,9 +732,8 @@
1069 732 {$where}
1070 733 GROUP BY comment_approved
1071 734 "
1072 735 );
1073 - // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
1074 736
1075 737 $approved = array(
1076 738 '0' => 'moderated',
1077 739 '1' => 'approved',
@@ -1079,9 +741,9 @@
1079 741 'trash' => 'trash',
1080 742 'post-trashed' => 'post-trashed',
1081 743 );
1082 744
1083 - // <https://developer.wordpress.org/reference/functions/get_comment_count/#source>
745 + // https://developer.wordpress.org/reference/functions/get_comment_count/#source
1084 746 foreach ( $count as $row ) {
1085 747 if ( ! in_array( $row->comment_approved, array( 'post-trashed', 'trash', 'spam' ), true ) ) {
1086 748 $counts['all'] += $row->num_comments;
1087 749 $counts['total_comments'] += $row->num_comments;
@@ -1104,30 +766,19 @@
1104 766 return $counts;
1105 767 }
1106 768
1107 769 /**
1108 - * Traps `wp_die()` calls and outputs a JSON response instead.
770 + * traps `wp_die()` calls and outputs a JSON response instead.
1109 771 * The result is always output, never returned.
1110 772 *
1111 773 * @param string|null $error_code Call with string to start the trapping. Call with null to stop.
1112 774 * @param int $http_status HTTP status code, 400 by default.
1113 775 */
1114 - public function trap_wp_die( $error_code = null, $http_status = 400 ) {
1115 - // Determine the filter name; based on the conditionals inside the wp_die function.
1116 - if ( wp_is_json_request() ) {
1117 - $die_handler = 'wp_die_json_handler';
1118 - } elseif ( wp_is_jsonp_request() ) {
1119 - $die_handler = 'wp_die_jsonp_handler';
1120 - } elseif ( wp_is_xml_request() ) {
1121 - $die_handler = 'wp_die_xml_handler';
1122 - } else {
1123 - $die_handler = 'wp_die_handler';
1124 - }
1125 -
1126 - if ( $error_code === null ) {
776 + function trap_wp_die( $error_code = null, $http_status = 400 ) {
777 + if ( is_null( $error_code ) ) {
1127 778 $this->trapped_error = null;
1128 - // Stop trapping.
1129 - remove_filter( $die_handler, array( $this, 'wp_die_handler_callback' ) );
779 + // Stop trapping
780 + remove_filter( 'wp_die_handler', array( $this, 'wp_die_handler_callback' ) );
1130 781 return;
1131 782 }
1132 783
1133 784 // If API called via PHP, bail: don't do our custom wp_die(). Do the normal wp_die().
@@ -1134,10 +785,12 @@
1134 785 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
1135 786 if ( ! defined( 'REST_API_REQUEST' ) || ! REST_API_REQUEST ) {
1136 787 return;
1137 788 }
1138 - } elseif ( ! defined( 'XMLRPC_REQUEST' ) || ! XMLRPC_REQUEST ) {
1139 - return;
789 + } else {
790 + if ( ! defined( 'XMLRPC_REQUEST' ) || ! XMLRPC_REQUEST ) {
791 + return;
792 + }
1140 793 }
1141 794
1142 795 $this->trapped_error = array(
1143 796 'status' => $http_status,
@@ -1143,29 +796,17 @@
1143 796 'status' => $http_status,
1144 797 'code' => $error_code,
1145 798 'message' => '',
1146 799 );
1147 - // Start trapping.
1148 - add_filter( $die_handler, array( $this, 'wp_die_handler_callback' ) );
800 + // Start trapping
801 + add_filter( 'wp_die_handler', array( $this, 'wp_die_handler_callback' ) );
1149 802 }
1150 803
1151 - /**
1152 - * Filter function for `wp_die_handler` and similar filters.
1153 - *
1154 - * @return callable
1155 - */
1156 - public function wp_die_handler_callback() {
804 + function wp_die_handler_callback() {
1157 805 return array( $this, 'wp_die_handler' );
1158 806 }
1159 807
1160 - /**
1161 - * Handler for `wp_die` calls.
1162 - *
1163 - * @param string|WP_Error $message As for `wp_die()`.
1164 - * @param string|int $title As for `wp_die()`.
1165 - * @param string|array|int $args As for `wp_die()`.
1166 - */
1167 - public function wp_die_handler( $message, $title = '', $args = array() ) {
808 + function wp_die_handler( $message, $title = '', $args = array() ) {
1168 809 // Allow wp_die calls to override HTTP status code...
1169 810 $args = wp_parse_args(
1170 811 $args,
1171 812 array(
@@ -1173,9 +814,9 @@
1173 814 )
1174 815 );
1175 816
1176 817 // ... unless it's 500
1177 - if ( 500 !== (int) $args['response'] ) {
818 + if ( (int) $args['response'] !== 500 ) {
1178 819 $this->trapped_error['status'] = $args['response'];
1179 820 }
1180 821
1181 822 if ( $title ) {
@@ -1194,17 +835,14 @@
1194 835 break;
1195 836 }
1196 837
1197 838 // We still want to exit so that code execution stops where it should.
1198 - // Attach the JSON output to the WordPress shutdown handler.
839 + // Attach the JSON output to the WordPress shutdown handler
1199 840 add_action( 'shutdown', array( $this, 'output_trapped_error' ), 0 );
1200 841 exit;
1201 842 }
1202 843
1203 - /**
1204 - * Output the trapped error.
1205 - */
1206 - public function output_trapped_error() {
844 + function output_trapped_error() {
1207 845 $this->exit = false; // We're already exiting once. Don't do it twice.
1208 846 $this->output(
1209 847 $this->trapped_error['status'],
1210 848 (object) array(
@@ -1213,12 +851,9 @@
1213 851 )
1214 852 );
1215 853 }
1216 854
1217 - /**
1218 - * Finish the request.
1219 - */
1220 - public function finish_request() {
855 + function finish_request() {
1221 856 if ( function_exists( 'fastcgi_finish_request' ) ) {
1222 857 return fastcgi_finish_request();
1223 858 }
1224 859 }