| @@ -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,28 +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. If it's not a string, which can happen with | |
| 206 | - * not well-formed data coming from Jetpack sites, we still consider it a truthy value. | |
| 207 | - * | |
| 208 | - * @param mixed $value true, 1, "1", "t", and "true" (case insensitive) are truthy, everything else isn't. | |
| 209 | - * @return bool | |
| 210 | - */ | |
| 211 | - public static function is_truthy( $value ) { | |
| 212 | - if ( true === $value ) { | |
| 213 | - return true; | |
| 214 | - } | |
| 215 | - | |
| 216 | - if ( 1 === $value ) { | |
| 217 | - return true; | |
| 218 | - } | |
| 219 | - | |
| 220 | - if ( ! is_string( $value ) ) { | |
| 221 | - return false; | |
| 222 | - } | |
| 223 | - | |
| 62 | + static function is_truthy( $value ) { | |
| 224 | 63 | switch ( strtolower( (string) $value ) ) { |
| 225 | 64 | case '1': |
| 226 | 65 | case 't': |
| 227 | 66 | case 'true': |
| @@ -230,27 +69,9 @@ | ||
| 230 | 69 | |
| 231 | 70 | return false; |
| 232 | 71 | } |
| 233 | 72 | |
| 234 | - /** | |
| 235 | - * Determine if a string is falsey. | |
| 236 | - * | |
| 237 | - * @param mixed $value false, 0, "0", "f", and "false" (case insensitive) are falsey, everything else isn't. | |
| 238 | - * @return bool | |
| 239 | - */ | |
| 240 | - public static function is_falsy( $value ) { | |
| 241 | - if ( false === $value ) { | |
| 242 | - return true; | |
| 243 | - } | |
| 244 | - | |
| 245 | - if ( 0 === $value ) { | |
| 246 | - return true; | |
| 247 | - } | |
| 248 | - | |
| 249 | - if ( ! is_string( $value ) ) { | |
| 250 | - return false; | |
| 251 | - } | |
| 252 | - | |
| 73 | + static function is_falsy( $value ) { | |
| 253 | 74 | switch ( strtolower( (string) $value ) ) { |
| 254 | 75 | case '0': |
| 255 | 76 | case 'f': |
| 256 | 77 | case 'false': |
| @@ -259,36 +80,20 @@ | ||
| 259 | 80 | |
| 260 | 81 | return false; |
| 261 | 82 | } |
| 262 | 83 | |
| 263 | - /** | |
| 264 | - * Constructor. | |
| 265 | - * | |
| 266 | - * @todo This should be private. | |
| 267 | - * @param string|null $method As for `$this->setup_inputs()`. | |
| 268 | - * @param string|null $url As for `$this->setup_inputs()`. | |
| 269 | - * @param string|null $post_body As for `$this->setup_inputs()`. | |
| 270 | - */ | |
| 271 | - public function __construct( $method = null, $url = null, $post_body = null ) { | |
| 272 | - $this->setup_inputs( $method, $url, $post_body ); | |
| 84 | + function __construct( ...$args ) { | |
| 85 | + call_user_func_array( array( $this, 'setup_inputs' ), $args ); | |
| 273 | 86 | } |
| 274 | 87 | |
| 275 | - /** | |
| 276 | - * Setup inputs. | |
| 277 | - * | |
| 278 | - * @param string|null $method Request HTTP method. Fetched from `$_SERVER` if null. | |
| 279 | - * @param string|null $url URL requested. Determined from `$_SERVER` if null. | |
| 280 | - * @param string|null $post_body POST body. Read from `php://input` if null and method is POST. | |
| 281 | - */ | |
| 282 | - public function setup_inputs( $method = null, $url = null, $post_body = null ) { | |
| 283 | - if ( $method === null ) { | |
| 284 | - $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'] ); | |
| 285 | 91 | } else { |
| 286 | 92 | $this->method = strtoupper( $method ); |
| 287 | 93 | } |
| 288 | - if ( $url === null ) { | |
| 289 | - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sniff misses the esc_url_raw. | |
| 290 | - $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'] ); | |
| 291 | 96 | } else { |
| 292 | 97 | $this->url = $url; |
| 293 | 98 | } |
| 294 | 99 | |
| @@ -300,28 +105,27 @@ | ||
| 300 | 105 | if ( ! empty( $parsed['query'] ) ) { |
| 301 | 106 | wp_parse_str( $parsed['query'], $this->query ); |
| 302 | 107 | } |
| 303 | 108 | |
| 304 | - if ( ! empty( $_SERVER['HTTP_ACCEPT'] ) ) { | |
| 305 | - $this->accept = filter_var( wp_unslash( $_SERVER['HTTP_ACCEPT'] ) ); | |
| 109 | + if ( isset( $_SERVER['HTTP_ACCEPT'] ) && $_SERVER['HTTP_ACCEPT'] ) { | |
| 110 | + $this->accept = $_SERVER['HTTP_ACCEPT']; | |
| 306 | 111 | } |
| 307 | 112 | |
| 308 | 113 | if ( 'POST' === $this->method ) { |
| 309 | - if ( $post_body === null ) { | |
| 114 | + if ( is_null( $post_body ) ) { | |
| 310 | 115 | $this->post_body = file_get_contents( 'php://input' ); |
| 311 | 116 | |
| 312 | - if ( ! empty( $_SERVER['HTTP_CONTENT_TYPE'] ) ) { | |
| 313 | - $this->content_type = filter_var( wp_unslash( $_SERVER['HTTP_CONTENT_TYPE'] ) ); | |
| 314 | - } elseif ( ! empty( $_SERVER['CONTENT_TYPE'] ) ) { | |
| 315 | - $this->content_type = filter_var( wp_unslash( $_SERVER['CONTENT_TYPE'] ) ); | |
| 316 | - } elseif ( isset( $this->post_body[0] ) && '{' === $this->post_body[0] ) { | |
| 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']; | |
| 121 | + } elseif ( '{' === $this->post_body[0] ) { | |
| 317 | 122 | $this->content_type = 'application/json'; |
| 318 | 123 | } else { |
| 319 | 124 | $this->content_type = 'application/x-www-form-urlencoded'; |
| 320 | 125 | } |
| 321 | 126 | |
| 322 | - if ( str_starts_with( strtolower( $this->content_type ), 'multipart/' ) ) { | |
| 323 | - // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 127 | + if ( 0 === strpos( strtolower( $this->content_type ), 'multipart/' ) ) { | |
| 324 | 128 | $this->post_body = http_build_query( stripslashes_deep( $_POST ) ); |
| 325 | 129 | $this->files = $_FILES; |
| 326 | 130 | $this->content_type = 'multipart/form-data'; |
| 327 | 131 | } |
| @@ -326,9 +130,9 @@ | ||
| 326 | 130 | $this->content_type = 'multipart/form-data'; |
| 327 | 131 | } |
| 328 | 132 | } else { |
| 329 | 133 | $this->post_body = $post_body; |
| 330 | - $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'; | |
| 331 | 135 | } |
| 332 | 136 | } else { |
| 333 | 137 | $this->post_body = null; |
| 334 | 138 | $this->content_type = null; |
| @@ -333,19 +137,13 @@ | ||
| 333 | 137 | $this->post_body = null; |
| 334 | 138 | $this->content_type = null; |
| 335 | 139 | } |
| 336 | 140 | |
| 337 | - $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--'; | |
| 338 | 142 | } |
| 339 | 143 | |
| 340 | - /** | |
| 341 | - * Initialize. | |
| 342 | - * | |
| 343 | - * @return null|WP_Error (although this implementation always returns null) | |
| 344 | - */ | |
| 345 | - public function initialize() { | |
| 144 | + function initialize() { | |
| 346 | 145 | $this->token_details['blog_id'] = Jetpack_Options::get_option( 'id' ); |
| 347 | - return null; | |
| 348 | 146 | } |
| 349 | 147 | |
| 350 | 148 | /** |
| 351 | 149 | * Checks if the current request is authorized with a blog token. |
| @@ -380,22 +178,16 @@ | ||
| 380 | 178 | |
| 381 | 179 | return true; |
| 382 | 180 | } |
| 383 | 181 | |
| 384 | - /** | |
| 385 | - * Serve. | |
| 386 | - * | |
| 387 | - * @param bool $exit Whether to exit. | |
| 388 | - * @return string|null Content type (assuming it didn't exit), or null in certain error cases. | |
| 389 | - */ | |
| 390 | - public function serve( $exit = true ) { | |
| 391 | - ini_set( 'display_errors', false ); // phpcs:ignore WordPress.PHP.IniSet.display_errors_Blacklisted | |
| 182 | + function serve( $exit = true ) { | |
| 183 | + ini_set( 'display_errors', false ); | |
| 392 | 184 | |
| 393 | 185 | $this->exit = (bool) $exit; |
| 394 | 186 | |
| 395 | 187 | // This was causing problems with Jetpack, but is necessary for wpcom |
| 396 | 188 | // @see https://github.com/Automattic/jetpack/pull/2603 |
| 397 | - // @see r124548-wpcom . | |
| 189 | + // @see r124548-wpcom | |
| 398 | 190 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 399 | 191 | add_filter( 'home_url', array( $this, 'ensure_http_scheme_of_home_url' ), 10, 3 ); |
| 400 | 192 | } |
| 401 | 193 | |
| @@ -403,9 +195,9 @@ | ||
| 403 | 195 | |
| 404 | 196 | add_filter( 'comment_edit_pre', array( $this, 'comment_edit_pre' ) ); |
| 405 | 197 | |
| 406 | 198 | $initialization = $this->initialize(); |
| 407 | - if ( 'OPTIONS' === $this->method ) { | |
| 199 | + if ( 'OPTIONS' == $this->method ) { | |
| 408 | 200 | /** |
| 409 | 201 | * Fires before the page output. |
| 410 | 202 | * Can be used to specify custom header options. |
| 411 | 203 | * |
| @@ -421,14 +213,13 @@ | ||
| 421 | 213 | $this->output_error( $initialization ); |
| 422 | 214 | return; |
| 423 | 215 | } |
| 424 | 216 | |
| 425 | - // Normalize path and extract API version. | |
| 217 | + // Normalize path and extract API version | |
| 426 | 218 | $this->path = untrailingslashit( $this->path ); |
| 427 | - if ( preg_match( '#^/rest/v(\d+(\.\d+)*)#', $this->path, $matches ) ) { | |
| 428 | - $this->path = substr( $this->path, strlen( $matches[0] ) ); | |
| 429 | - $this->version = $matches[1]; | |
| 430 | - } | |
| 219 | + preg_match( '#^/rest/v(\d+(\.\d+)*)#', $this->path, $matches ); | |
| 220 | + $this->path = substr( $this->path, strlen( $matches[0] ) ); | |
| 221 | + $this->version = $matches[1]; | |
| 431 | 222 | |
| 432 | 223 | $allowed_methods = array( 'GET', 'POST' ); |
| 433 | 224 | $four_oh_five = false; |
| 434 | 225 | |
| @@ -437,17 +228,17 @@ | ||
| 437 | 228 | |
| 438 | 229 | if ( $is_help ) { |
| 439 | 230 | $origin = get_http_origin(); |
| 440 | 231 | |
| 441 | - if ( ! empty( $origin ) && 'GET' === $this->method ) { | |
| 232 | + if ( ! empty( $origin ) && 'GET' == $this->method ) { | |
| 442 | 233 | header( 'Access-Control-Allow-Origin: ' . esc_url_raw( $origin ) ); |
| 443 | 234 | } |
| 444 | 235 | |
| 445 | 236 | $this->path = substr( rtrim( $this->path, '/' ), 0, -5 ); |
| 446 | - // Show help for all matching endpoints regardless of method. | |
| 237 | + // Show help for all matching endpoints regardless of method | |
| 447 | 238 | $methods = $allowed_methods; |
| 448 | 239 | $find_all_matching_endpoints = true; |
| 449 | - // 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 | |
| 450 | 241 | $depth = substr_count( $this->path, '/' ) + 1; |
| 451 | 242 | if ( false !== stripos( $this->accept, 'javascript' ) || false !== stripos( $this->accept, 'json' ) ) { |
| 452 | 243 | $help_content_type = 'json'; |
| 453 | 244 | } else { |
| @@ -452,30 +243,30 @@ | ||
| 452 | 243 | $help_content_type = 'json'; |
| 453 | 244 | } else { |
| 454 | 245 | $help_content_type = 'html'; |
| 455 | 246 | } |
| 456 | - } elseif ( in_array( $this->method, $allowed_methods, true ) ) { | |
| 457 | - // Only serve requested method. | |
| 458 | - $methods = array( $this->method ); | |
| 459 | - $find_all_matching_endpoints = false; | |
| 460 | 247 | } else { |
| 461 | - // We don't allow this requested method - find matching endpoints and send 405. | |
| 462 | - $methods = $allowed_methods; | |
| 463 | - $find_all_matching_endpoints = true; | |
| 464 | - $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 | + } | |
| 465 | 258 | } |
| 466 | 259 | |
| 467 | - // Find which endpoint to serve. | |
| 260 | + // Find which endpoint to serve | |
| 468 | 261 | $found = false; |
| 469 | 262 | foreach ( $this->endpoints as $endpoint_path_versions => $endpoints_by_method ) { |
| 470 | - // @todo Determine if anything depends on this being serialized rather than e.g. JSON. | |
| 471 | - // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize -- Legacy, possibly depended on elsewhere. | |
| 472 | 263 | $endpoint_path_versions = unserialize( $endpoint_path_versions ); |
| 473 | 264 | $endpoint_path = $endpoint_path_versions[0]; |
| 474 | 265 | $endpoint_min_version = $endpoint_path_versions[1]; |
| 475 | 266 | $endpoint_max_version = $endpoint_path_versions[2]; |
| 476 | 267 | |
| 477 | - // Make sure max_version is not less than min_version. | |
| 268 | + // Make sure max_version is not less than min_version | |
| 478 | 269 | if ( version_compare( $endpoint_max_version, $endpoint_min_version, '<' ) ) { |
| 479 | 270 | $endpoint_max_version = $endpoint_min_version; |
| 480 | 271 | } |
| 481 | 272 | |
| @@ -483,16 +274,16 @@ | ||
| 483 | 274 | if ( ! isset( $endpoints_by_method[ $method ] ) ) { |
| 484 | 275 | continue; |
| 485 | 276 | } |
| 486 | 277 | |
| 487 | - // Normalize. | |
| 278 | + // Normalize | |
| 488 | 279 | $endpoint_path = untrailingslashit( $endpoint_path ); |
| 489 | 280 | if ( $is_help ) { |
| 490 | - // Truncate path at help depth. | |
| 491 | - $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 ) ); | |
| 492 | 283 | } |
| 493 | 284 | |
| 494 | - // Generate regular expression from sprintf(). | |
| 285 | + // Generate regular expression from sprintf() | |
| 495 | 286 | $endpoint_path_regex = str_replace( array( '%s', '%d' ), array( '([^/?&]+)', '(\d+)' ), $endpoint_path ); |
| 496 | 287 | |
| 497 | 288 | if ( ! preg_match( "#^$endpoint_path_regex\$#", $this->path, $path_pieces ) ) { |
| 498 | 289 | // This endpoint does not match the requested path. |
| @@ -508,9 +299,9 @@ | ||
| 508 | 299 | |
| 509 | 300 | if ( $find_all_matching_endpoints ) { |
| 510 | 301 | $matching_endpoints[] = array( $endpoints_by_method[ $method ], $path_pieces ); |
| 511 | 302 | } else { |
| 512 | - // The method parameters are now in $path_pieces. | |
| 303 | + // The method parameters are now in $path_pieces | |
| 513 | 304 | $endpoint = $endpoints_by_method[ $method ]; |
| 514 | 305 | break 2; |
| 515 | 306 | } |
| 516 | 307 | } |
| @@ -525,9 +316,9 @@ | ||
| 525 | 316 | foreach ( $matching_endpoints as $matching_endpoint ) { |
| 526 | 317 | $allowed_methods[] = $matching_endpoint[0]->method; |
| 527 | 318 | } |
| 528 | 319 | |
| 529 | - header( 'Allow: ' . strtoupper( implode( ',', array_unique( $allowed_methods ) ) ) ); | |
| 320 | + header( 'Allow: ' . strtoupper( join( ',', array_unique( $allowed_methods ) ) ) ); | |
| 530 | 321 | return $this->output( |
| 531 | 322 | 405, |
| 532 | 323 | array( |
| 533 | 324 | 'error' => 'not_allowed', |
| @@ -585,28 +376,14 @@ | ||
| 585 | 376 | |
| 586 | 377 | return $this->output( $output_status_code, $response, 'application/json', $this->extra_headers ); |
| 587 | 378 | } |
| 588 | 379 | |
| 589 | - /** | |
| 590 | - * Process a request. | |
| 591 | - * | |
| 592 | - * @param WPCOM_JSON_API_Endpoint $endpoint Endpoint. | |
| 593 | - * @param array $path_pieces Path pieces. | |
| 594 | - * @return array|WP_Error Return value from the endpoint's callback. | |
| 595 | - */ | |
| 596 | - public function process_request( WPCOM_JSON_API_Endpoint $endpoint, $path_pieces ) { | |
| 380 | + function process_request( WPCOM_JSON_API_Endpoint $endpoint, $path_pieces ) { | |
| 597 | 381 | $this->endpoint = $endpoint; |
| 598 | 382 | return call_user_func_array( array( $endpoint, 'callback' ), $path_pieces ); |
| 599 | 383 | } |
| 600 | 384 | |
| 601 | - /** | |
| 602 | - * Output a response or error without exiting. | |
| 603 | - * | |
| 604 | - * @param int $status_code HTTP status code. | |
| 605 | - * @param mixed $response Response data. | |
| 606 | - * @param string $content_type Content type of the response. | |
| 607 | - */ | |
| 608 | - public function output_early( $status_code, $response = null, $content_type = 'application/json' ) { | |
| 385 | + function output_early( $status_code, $response = null, $content_type = 'application/json' ) { | |
| 609 | 386 | $exit = $this->exit; |
| 610 | 387 | $this->exit = false; |
| 611 | 388 | if ( is_wp_error( $response ) ) { |
| 612 | 389 | $this->output_error( $response ); |
| @@ -618,30 +395,14 @@ | ||
| 618 | 395 | $this->finish_request(); |
| 619 | 396 | } |
| 620 | 397 | } |
| 621 | 398 | |
| 622 | - /** | |
| 623 | - * Set output status code. | |
| 624 | - * | |
| 625 | - * @param int $code HTTP status code. | |
| 626 | - */ | |
| 627 | - public function set_output_status_code( $code = 200 ) { | |
| 399 | + function set_output_status_code( $code = 200 ) { | |
| 628 | 400 | $this->output_status_code = $code; |
| 629 | 401 | } |
| 630 | 402 | |
| 631 | - /** | |
| 632 | - * Output a response. | |
| 633 | - * | |
| 634 | - * @param int $status_code HTTP status code. | |
| 635 | - * @param mixed $response Response data. | |
| 636 | - * @param string $content_type Content type of the response. | |
| 637 | - * @param array $extra Additional HTTP headers. | |
| 638 | - * @return string Content type (assuming it didn't exit). | |
| 639 | - */ | |
| 640 | - public function output( $status_code, $response = null, $content_type = 'application/json', $extra = array() ) { | |
| 641 | - $status_code = (int) $status_code; | |
| 642 | - | |
| 643 | - // 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 | |
| 644 | 405 | if ( $this->did_output ) { |
| 645 | 406 | if ( $this->exit ) { |
| 646 | 407 | exit; |
| 647 | 408 | } |
| @@ -649,9 +410,9 @@ | ||
| 649 | 410 | } |
| 650 | 411 | $this->did_output = true; |
| 651 | 412 | |
| 652 | 413 | // 400s and 404s are allowed for all origins |
| 653 | - if ( 404 === $status_code || 400 === $status_code ) { | |
| 414 | + if ( 404 == $status_code || 400 == $status_code ) { | |
| 654 | 415 | header( 'Access-Control-Allow-Origin: *' ); |
| 655 | 416 | } |
| 656 | 417 | |
| 657 | 418 | /* Add headers for form submission from <amp-form/> */ |
| @@ -659,9 +420,10 @@ | ||
| 659 | 420 | header( 'Access-Control-Allow-Origin: ' . wp_unslash( $this->amp_source_origin ) ); |
| 660 | 421 | header( 'Access-Control-Allow-Credentials: true' ); |
| 661 | 422 | } |
| 662 | 423 | |
| 663 | - if ( $response === null ) { | |
| 424 | + | |
| 425 | + if ( is_null( $response ) ) { | |
| 664 | 426 | $response = new stdClass(); |
| 665 | 427 | } |
| 666 | 428 | |
| 667 | 429 | if ( 'text/plain' === $content_type || |
| @@ -670,9 +432,9 @@ | ||
| 670 | 432 | header( 'Content-Type: ' . $content_type ); |
| 671 | 433 | foreach ( $extra as $key => $value ) { |
| 672 | 434 | header( "$key: $value" ); |
| 673 | 435 | } |
| 674 | - echo $response; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 436 | + echo $response; | |
| 675 | 437 | if ( $this->exit ) { |
| 676 | 438 | exit; |
| 677 | 439 | } |
| 678 | 440 | |
| @@ -715,13 +477,13 @@ | ||
| 715 | 477 | |
| 716 | 478 | if ( $callback ) { |
| 717 | 479 | // Mitigate Rosetta Flash [1] by setting the Content-Type-Options: nosniff header |
| 718 | 480 | // and by prepending the JSONP response with a JS comment. |
| 719 | - // [1] <https://blog.miki.it/2014/7/8/abusing-jsonp-with-rosetta-flash/index.html>. | |
| 720 | - 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("; | |
| 721 | 483 | |
| 722 | 484 | } |
| 723 | - 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 ); | |
| 724 | 486 | if ( $callback ) { |
| 725 | 487 | echo ');'; |
| 726 | 488 | } |
| 727 | 489 | |
| @@ -731,19 +493,13 @@ | ||
| 731 | 493 | |
| 732 | 494 | return $content_type; |
| 733 | 495 | } |
| 734 | 496 | |
| 735 | - /** | |
| 736 | - * Serialize an error. | |
| 737 | - * | |
| 738 | - * @param WP_Error $error Error. | |
| 739 | - * @return array with 'status_code' and 'errors' data. | |
| 740 | - */ | |
| 741 | 497 | public static function serializable_error( $error ) { |
| 742 | 498 | |
| 743 | 499 | $status_code = $error->get_error_data(); |
| 744 | 500 | |
| 745 | - if ( is_array( $status_code ) && isset( $status_code['status_code'] ) ) { | |
| 501 | + if ( is_array( $status_code ) ) { | |
| 746 | 502 | $status_code = $status_code['status_code']; |
| 747 | 503 | } |
| 748 | 504 | |
| 749 | 505 | if ( ! $status_code ) { |
| @@ -753,10 +509,9 @@ | ||
| 753 | 509 | 'error' => $error->get_error_code(), |
| 754 | 510 | 'message' => $error->get_error_message(), |
| 755 | 511 | ); |
| 756 | 512 | |
| 757 | - $additional_data = $error->get_error_data( 'additional_data' ); | |
| 758 | - if ( $additional_data ) { | |
| 513 | + if ( $additional_data = $error->get_error_data( 'additional_data' ) ) { | |
| 759 | 514 | $response['data'] = $additional_data; |
| 760 | 515 | } |
| 761 | 516 | |
| 762 | 517 | return array( |
| @@ -764,27 +519,15 @@ | ||
| 764 | 519 | 'errors' => $response, |
| 765 | 520 | ); |
| 766 | 521 | } |
| 767 | 522 | |
| 768 | - /** | |
| 769 | - * Output an error. | |
| 770 | - * | |
| 771 | - * @param WP_Error $error Error. | |
| 772 | - * @return string Content type (assuming it didn't exit). | |
| 773 | - */ | |
| 774 | - public function output_error( $error ) { | |
| 775 | - $error_response = static::serializable_error( $error ); | |
| 523 | + function output_error( $error ) { | |
| 524 | + $error_response = $this->serializable_error( $error ); | |
| 776 | 525 | |
| 777 | 526 | return $this->output( $error_response['status_code'], $error_response['errors'] ); |
| 778 | 527 | } |
| 779 | 528 | |
| 780 | - /** | |
| 781 | - * Filter fields in a response. | |
| 782 | - * | |
| 783 | - * @param array|object $response Response. | |
| 784 | - * @return array|object Filtered response. | |
| 785 | - */ | |
| 786 | - public function filter_fields( $response ) { | |
| 529 | + function filter_fields( $response ) { | |
| 787 | 530 | if ( empty( $this->query['fields'] ) || ( is_array( $response ) && ! empty( $response['error'] ) ) || ! empty( $this->endpoint->custom_fields_filtering ) ) { |
| 788 | 531 | return $response; |
| 789 | 532 | } |
| 790 | 533 | |
| @@ -822,9 +565,8 @@ | ||
| 822 | 565 | |
| 823 | 566 | foreach ( $response[ $key_to_filter ] as $key => $values ) { |
| 824 | 567 | if ( is_object( $values ) ) { |
| 825 | 568 | if ( is_object( $response[ $key_to_filter ] ) ) { |
| 826 | - // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.Found -- False positive. | |
| 827 | 569 | $response[ $key_to_filter ]->$key = (object) array_intersect_key( ( (array) $values ), array_flip( $fields ) ); |
| 828 | 570 | } elseif ( is_array( $response[ $key_to_filter ] ) ) { |
| 829 | 571 | $response[ $key_to_filter ][ $key ] = (object) array_intersect_key( ( (array) $values ), array_flip( $fields ) ); |
| 830 | 572 | } |
| @@ -847,19 +589,9 @@ | ||
| 847 | 589 | |
| 848 | 590 | return $response; |
| 849 | 591 | } |
| 850 | 592 | |
| 851 | - /** | |
| 852 | - * Filter for `home_url`. | |
| 853 | - * | |
| 854 | - * If `$original_scheme` is null, turns an https URL to http. | |
| 855 | - * | |
| 856 | - * @param string $url The complete home URL including scheme and path. | |
| 857 | - * @param string $path Path relative to the home URL. Blank string if no path is specified. | |
| 858 | - * @param string|null $original_scheme Scheme to give the home URL context. Accepts 'http', 'https', 'relative', 'rest', or null. | |
| 859 | - * @return string URL. | |
| 860 | - */ | |
| 861 | - public function ensure_http_scheme_of_home_url( $url, $path, $original_scheme ) { | |
| 593 | + function ensure_http_scheme_of_home_url( $url, $path, $original_scheme ) { | |
| 862 | 594 | if ( $original_scheme ) { |
| 863 | 595 | return $url; |
| 864 | 596 | } |
| 865 | 597 | |
| @@ -865,66 +597,31 @@ | ||
| 865 | 597 | |
| 866 | 598 | return preg_replace( '#^https:#', 'http:', $url ); |
| 867 | 599 | } |
| 868 | 600 | |
| 869 | - /** | |
| 870 | - * Decode HTML special characters in comment content. | |
| 871 | - * | |
| 872 | - * @param string $comment_content Comment content. | |
| 873 | - * @return string | |
| 874 | - */ | |
| 875 | - public function comment_edit_pre( $comment_content ) { | |
| 601 | + function comment_edit_pre( $comment_content ) { | |
| 876 | 602 | return htmlspecialchars_decode( $comment_content, ENT_QUOTES ); |
| 877 | 603 | } |
| 878 | 604 | |
| 879 | - /** | |
| 880 | - * JSON encode. | |
| 881 | - * | |
| 882 | - * @param mixed $data Data. | |
| 883 | - * @return string|false | |
| 884 | - */ | |
| 885 | - public function json_encode( $data ) { | |
| 605 | + function json_encode( $data ) { | |
| 886 | 606 | return wp_json_encode( $data ); |
| 887 | 607 | } |
| 888 | 608 | |
| 889 | - /** | |
| 890 | - * Test if a string ends with a string. | |
| 891 | - * | |
| 892 | - * @param string $haystack String to check. | |
| 893 | - * @param string $needle Suffix to check. | |
| 894 | - * @return bool | |
| 895 | - */ | |
| 896 | - public function ends_with( $haystack, $needle ) { | |
| 897 | - return substr( $haystack, -strlen( $needle ) ) === $needle; | |
| 609 | + function ends_with( $haystack, $needle ) { | |
| 610 | + return $needle === substr( $haystack, -strlen( $needle ) ); | |
| 898 | 611 | } |
| 899 | 612 | |
| 900 | - /** | |
| 901 | - * Returns the site's blog_id in the WP.com ecosystem | |
| 902 | - * | |
| 903 | - * @return int | |
| 904 | - */ | |
| 905 | - 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() { | |
| 906 | 615 | return $this->token_details['blog_id']; |
| 907 | 616 | } |
| 908 | 617 | |
| 909 | - /** | |
| 910 | - * Returns the site's local blog_id. | |
| 911 | - * | |
| 912 | - * @param int $blog_id Blog ID. | |
| 913 | - * @return int | |
| 914 | - */ | |
| 915 | - 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 ) { | |
| 916 | 620 | return $GLOBALS['blog_id']; |
| 917 | 621 | } |
| 918 | 622 | |
| 919 | - /** | |
| 920 | - * Switch to blog and validate user. | |
| 921 | - * | |
| 922 | - * @param int $blog_id Blog ID. | |
| 923 | - * @param bool $verify_token_for_blog Whether to verify the token. | |
| 924 | - * @return int Blog ID. | |
| 925 | - */ | |
| 926 | - 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 ) { | |
| 927 | 624 | if ( $this->is_restricted_blog( $blog_id ) ) { |
| 928 | 625 | return new WP_Error( 'unauthorized', 'User cannot access this restricted blog', 403 ); |
| 929 | 626 | } |
| 930 | 627 | /** |
| @@ -931,9 +628,9 @@ | ||
| 931 | 628 | * If this is a private site we check for 2 things: |
| 932 | 629 | * 1. In case of user based authentication, we need to check if the logged-in user has the 'read' capability. |
| 933 | 630 | * 2. In case of site based authentication, make sure the endpoint accepts it. |
| 934 | 631 | */ |
| 935 | - if ( ( new Status() )->is_private_site() && | |
| 632 | + if ( -1 === (int) get_option( 'blog_public' ) && | |
| 936 | 633 | ! current_user_can( 'read' ) && |
| 937 | 634 | ! $this->endpoint->accepts_site_based_authentication() |
| 938 | 635 | ) { |
| 939 | 636 | return new WP_Error( 'unauthorized', 'User cannot access this private blog.', 403 ); |
| @@ -941,15 +638,10 @@ | ||
| 941 | 638 | |
| 942 | 639 | return $blog_id; |
| 943 | 640 | } |
| 944 | 641 | |
| 945 | - /** | |
| 946 | - * Returns true if the specified blog ID is a restricted blog | |
| 947 | - * | |
| 948 | - * @param int $blog_id Blog ID. | |
| 949 | - * @return bool | |
| 950 | - */ | |
| 951 | - 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 ) { | |
| 952 | 644 | /** |
| 953 | 645 | * Filters all REST API access and return a 403 unauthorized response for all Restricted blog IDs. |
| 954 | 646 | * |
| 955 | 647 | * @module json-api |
| @@ -958,73 +650,32 @@ | ||
| 958 | 650 | * |
| 959 | 651 | * @param array $array Array of Blog IDs. |
| 960 | 652 | */ |
| 961 | 653 | $restricted_blog_ids = apply_filters( 'wpcom_json_api_restricted_blog_ids', array() ); |
| 962 | - 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 ); | |
| 963 | 655 | } |
| 964 | 656 | |
| 965 | - /** | |
| 966 | - * Post like count. | |
| 967 | - * | |
| 968 | - * @param int $blog_id Blog ID. | |
| 969 | - * @param int $post_id Post ID. | |
| 970 | - * @return int | |
| 971 | - */ | |
| 972 | - public function post_like_count( $blog_id, $post_id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 657 | + function post_like_count( $blog_id, $post_id ) { | |
| 973 | 658 | return 0; |
| 974 | 659 | } |
| 975 | 660 | |
| 976 | - /** | |
| 977 | - * Is liked? | |
| 978 | - * | |
| 979 | - * @param int $blog_id Blog ID. | |
| 980 | - * @param int $post_id Post ID. | |
| 981 | - * @return bool | |
| 982 | - */ | |
| 983 | - public function is_liked( $blog_id, $post_id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 661 | + function is_liked( $blog_id, $post_id ) { | |
| 984 | 662 | return false; |
| 985 | 663 | } |
| 986 | 664 | |
| 987 | - /** | |
| 988 | - * Is reblogged? | |
| 989 | - * | |
| 990 | - * @param int $blog_id Blog ID. | |
| 991 | - * @param int $post_id Post ID. | |
| 992 | - * @return bool | |
| 993 | - */ | |
| 994 | - public function is_reblogged( $blog_id, $post_id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 665 | + function is_reblogged( $blog_id, $post_id ) { | |
| 995 | 666 | return false; |
| 996 | 667 | } |
| 997 | 668 | |
| 998 | - /** | |
| 999 | - * Is following? | |
| 1000 | - * | |
| 1001 | - * @param int $blog_id Blog ID. | |
| 1002 | - * @return bool | |
| 1003 | - */ | |
| 1004 | - public function is_following( $blog_id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 669 | + function is_following( $blog_id ) { | |
| 1005 | 670 | return false; |
| 1006 | 671 | } |
| 1007 | 672 | |
| 1008 | - /** | |
| 1009 | - * Add global ID. | |
| 1010 | - * | |
| 1011 | - * @param int $blog_id Blog ID. | |
| 1012 | - * @param int $post_id Post ID. | |
| 1013 | - * @return string | |
| 1014 | - */ | |
| 1015 | - 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 ) { | |
| 1016 | 674 | return ''; |
| 1017 | 675 | } |
| 1018 | 676 | |
| 1019 | - /** | |
| 1020 | - * Get avatar URL. | |
| 1021 | - * | |
| 1022 | - * @param string $email Email. | |
| 1023 | - * @param array $avatar_size Args for `get_avatar_url()`. | |
| 1024 | - * @return string|false | |
| 1025 | - */ | |
| 1026 | - public function get_avatar_url( $email, $avatar_size = null ) { | |
| 677 | + function get_avatar_url( $email, $avatar_size = null ) { | |
| 1027 | 678 | if ( function_exists( 'wpcom_get_avatar_url' ) ) { |
| 1028 | 679 | return null === $avatar_size |
| 1029 | 680 | ? wpcom_get_avatar_url( $email ) |
| 1030 | 681 | : wpcom_get_avatar_url( $email, $avatar_size ); |
| @@ -1035,11 +686,11 @@ | ||
| 1035 | 686 | } |
| 1036 | 687 | } |
| 1037 | 688 | |
| 1038 | 689 | /** |
| 1039 | - * 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. | |
| 1040 | 691 | * |
| 1041 | - * @param int $post_id Post ID. | |
| 692 | + * @param $post_id int Post ID. | |
| 1042 | 693 | * @return array Array of counts, matching the output of https://developer.wordpress.org/reference/functions/get_comment_count/. |
| 1043 | 694 | */ |
| 1044 | 695 | public function wp_count_comments( $post_id ) { |
| 1045 | 696 | global $wpdb; |
| @@ -1052,44 +703,30 @@ | ||
| 1052 | 703 | 'all' => 0, |
| 1053 | 704 | ); |
| 1054 | 705 | |
| 1055 | 706 | /** |
| 1056 | - * Exclude certain comment types from comment counts in the REST API. | |
| 1057 | - * | |
| 1058 | - * @since 6.9.0 | |
| 1059 | - * @deprecated 11.1 | |
| 1060 | - * @module json-api | |
| 1061 | - * | |
| 1062 | - * @param array Array of comment types to exclude (default: 'order_note', 'webhook_delivery', 'review', 'action_log') | |
| 1063 | - */ | |
| 1064 | - $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 | |
| 1065 | - | |
| 1066 | - /** | |
| 1067 | - * Include certain comment types in comment counts in the REST API. | |
| 1068 | - * Note: the default array of comment types includes an empty string, | |
| 1069 | - * to support comments posted before WP 5.5, that used an empty string as comment type. | |
| 1070 | - * | |
| 1071 | - * @since 11.1 | |
| 1072 | - * @module json-api | |
| 1073 | - * | |
| 1074 | - * @param array Array of comment types to include (default: 'comment', 'pingback', 'trackback') | |
| 1075 | - */ | |
| 1076 | - $include = apply_filters( | |
| 1077 | - 'jetpack_api_include_comment_types_count', | |
| 1078 | - 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' ) | |
| 1079 | 717 | ); |
| 1080 | 718 | |
| 1081 | - if ( empty( $include ) ) { | |
| 719 | + if ( empty( $exclude ) ) { | |
| 1082 | 720 | return wp_count_comments( $post_id ); |
| 1083 | 721 | } |
| 1084 | 722 | |
| 1085 | - array_walk( $include, 'esc_sql' ); | |
| 723 | + array_walk( $exclude, 'esc_sql' ); | |
| 1086 | 724 | $where = sprintf( |
| 1087 | - "WHERE comment_type IN ( '%s' )", | |
| 1088 | - implode( "','", $include ) | |
| 725 | + "WHERE comment_type NOT IN ( '%s' )", | |
| 726 | + implode( "','", $exclude ) | |
| 1089 | 727 | ); |
| 1090 | 728 | |
| 1091 | - // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- `$where` is built with escaping just above. | |
| 1092 | 729 | $count = $wpdb->get_results( |
| 1093 | 730 | "SELECT comment_approved, COUNT(*) AS num_comments |
| 1094 | 731 | FROM $wpdb->comments |
| 1095 | 732 | {$where} |
| @@ -1095,9 +732,8 @@ | ||
| 1095 | 732 | {$where} |
| 1096 | 733 | GROUP BY comment_approved |
| 1097 | 734 | " |
| 1098 | 735 | ); |
| 1099 | - // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared | |
| 1100 | 736 | |
| 1101 | 737 | $approved = array( |
| 1102 | 738 | '0' => 'moderated', |
| 1103 | 739 | '1' => 'approved', |
| @@ -1105,9 +741,9 @@ | ||
| 1105 | 741 | 'trash' => 'trash', |
| 1106 | 742 | 'post-trashed' => 'post-trashed', |
| 1107 | 743 | ); |
| 1108 | 744 | |
| 1109 | - // <https://developer.wordpress.org/reference/functions/get_comment_count/#source> | |
| 745 | + // https://developer.wordpress.org/reference/functions/get_comment_count/#source | |
| 1110 | 746 | foreach ( $count as $row ) { |
| 1111 | 747 | if ( ! in_array( $row->comment_approved, array( 'post-trashed', 'trash', 'spam' ), true ) ) { |
| 1112 | 748 | $counts['all'] += $row->num_comments; |
| 1113 | 749 | $counts['total_comments'] += $row->num_comments; |
| @@ -1130,15 +766,15 @@ | ||
| 1130 | 766 | return $counts; |
| 1131 | 767 | } |
| 1132 | 768 | |
| 1133 | 769 | /** |
| 1134 | - * Traps `wp_die()` calls and outputs a JSON response instead. | |
| 770 | + * traps `wp_die()` calls and outputs a JSON response instead. | |
| 1135 | 771 | * The result is always output, never returned. |
| 1136 | 772 | * |
| 1137 | 773 | * @param string|null $error_code Call with string to start the trapping. Call with null to stop. |
| 1138 | 774 | * @param int $http_status HTTP status code, 400 by default. |
| 1139 | 775 | */ |
| 1140 | - public function trap_wp_die( $error_code = null, $http_status = 400 ) { | |
| 776 | + function trap_wp_die( $error_code = null, $http_status = 400 ) { | |
| 1141 | 777 | // Determine the filter name; based on the conditionals inside the wp_die function. |
| 1142 | 778 | if ( wp_is_json_request() ) { |
| 1143 | 779 | $die_handler = 'wp_die_json_handler'; |
| 1144 | 780 | } elseif ( wp_is_jsonp_request() ) { |
| @@ -1148,11 +784,11 @@ | ||
| 1148 | 784 | } else { |
| 1149 | 785 | $die_handler = 'wp_die_handler'; |
| 1150 | 786 | } |
| 1151 | 787 | |
| 1152 | - if ( $error_code === null ) { | |
| 788 | + if ( is_null( $error_code ) ) { | |
| 1153 | 789 | $this->trapped_error = null; |
| 1154 | - // Stop trapping. | |
| 790 | + // Stop trapping | |
| 1155 | 791 | remove_filter( $die_handler, array( $this, 'wp_die_handler_callback' ) ); |
| 1156 | 792 | return; |
| 1157 | 793 | } |
| 1158 | 794 | |
| @@ -1160,10 +796,12 @@ | ||
| 1160 | 796 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 1161 | 797 | if ( ! defined( 'REST_API_REQUEST' ) || ! REST_API_REQUEST ) { |
| 1162 | 798 | return; |
| 1163 | 799 | } |
| 1164 | - } elseif ( ! defined( 'XMLRPC_REQUEST' ) || ! XMLRPC_REQUEST ) { | |
| 1165 | - return; | |
| 800 | + } else { | |
| 801 | + if ( ! defined( 'XMLRPC_REQUEST' ) || ! XMLRPC_REQUEST ) { | |
| 802 | + return; | |
| 803 | + } | |
| 1166 | 804 | } |
| 1167 | 805 | |
| 1168 | 806 | $this->trapped_error = array( |
| 1169 | 807 | 'status' => $http_status, |
| @@ -1169,30 +807,17 @@ | ||
| 1169 | 807 | 'status' => $http_status, |
| 1170 | 808 | 'code' => $error_code, |
| 1171 | 809 | 'message' => '', |
| 1172 | 810 | ); |
| 1173 | - // Start trapping. | |
| 811 | + // Start trapping | |
| 1174 | 812 | add_filter( $die_handler, array( $this, 'wp_die_handler_callback' ) ); |
| 1175 | 813 | } |
| 1176 | 814 | |
| 1177 | - /** | |
| 1178 | - * Filter function for `wp_die_handler` and similar filters. | |
| 1179 | - * | |
| 1180 | - * @return callable | |
| 1181 | - */ | |
| 1182 | - public function wp_die_handler_callback() { | |
| 815 | + function wp_die_handler_callback() { | |
| 1183 | 816 | return array( $this, 'wp_die_handler' ); |
| 1184 | 817 | } |
| 1185 | 818 | |
| 1186 | - /** | |
| 1187 | - * Handler for `wp_die` calls. | |
| 1188 | - * | |
| 1189 | - * @param string|WP_Error $message As for `wp_die()`. | |
| 1190 | - * @param string|int $title As for `wp_die()`. | |
| 1191 | - * @param string|array|int $args As for `wp_die()`. | |
| 1192 | - * @return never | |
| 1193 | - */ | |
| 1194 | - public function wp_die_handler( $message, $title = '', $args = array() ) { | |
| 819 | + function wp_die_handler( $message, $title = '', $args = array() ) { | |
| 1195 | 820 | // Allow wp_die calls to override HTTP status code... |
| 1196 | 821 | $args = wp_parse_args( |
| 1197 | 822 | $args, |
| 1198 | 823 | array( |
| @@ -1200,9 +825,9 @@ | ||
| 1200 | 825 | ) |
| 1201 | 826 | ); |
| 1202 | 827 | |
| 1203 | 828 | // ... unless it's 500 |
| 1204 | - if ( 500 !== (int) $args['response'] ) { | |
| 829 | + if ( (int) $args['response'] !== 500 ) { | |
| 1205 | 830 | $this->trapped_error['status'] = $args['response']; |
| 1206 | 831 | } |
| 1207 | 832 | |
| 1208 | 833 | if ( $title ) { |
| @@ -1221,17 +846,14 @@ | ||
| 1221 | 846 | break; |
| 1222 | 847 | } |
| 1223 | 848 | |
| 1224 | 849 | // We still want to exit so that code execution stops where it should. |
| 1225 | - // Attach the JSON output to the WordPress shutdown handler. | |
| 850 | + // Attach the JSON output to the WordPress shutdown handler | |
| 1226 | 851 | add_action( 'shutdown', array( $this, 'output_trapped_error' ), 0 ); |
| 1227 | 852 | exit; |
| 1228 | 853 | } |
| 1229 | 854 | |
| 1230 | - /** | |
| 1231 | - * Output the trapped error. | |
| 1232 | - */ | |
| 1233 | - public function output_trapped_error() { | |
| 855 | + function output_trapped_error() { | |
| 1234 | 856 | $this->exit = false; // We're already exiting once. Don't do it twice. |
| 1235 | 857 | $this->output( |
| 1236 | 858 | $this->trapped_error['status'], |
| 1237 | 859 | (object) array( |
| @@ -1240,12 +862,9 @@ | ||
| 1240 | 862 | ) |
| 1241 | 863 | ); |
| 1242 | 864 | } |
| 1243 | 865 | |
| 1244 | - /** | |
| 1245 | - * Finish the request. | |
| 1246 | - */ | |
| 1247 | - public function finish_request() { | |
| 866 | + function finish_request() { | |
| 1248 | 867 | if ( function_exists( 'fastcgi_finish_request' ) ) { |
| 1249 | 868 | return fastcgi_finish_request(); |
| 1250 | 869 | } |
| 1251 | 870 | } |