| @@ -4,8 +4,12 @@ | ||
| 4 | 4 | * |
| 5 | 5 | * @package automattic/jetpack |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 9 | + exit( 0 ); | |
| 10 | +} | |
| 11 | + | |
| 8 | 12 | require_once __DIR__ . '/../class.json-api.php'; |
| 9 | 13 | |
| 10 | 14 | /** |
| 11 | 15 | * Base class for WPCOM_JSON_API_Links. |
| @@ -75,8 +79,10 @@ | ||
| 75 | 79 | private function __clone() { } |
| 76 | 80 | |
| 77 | 81 | /** |
| 78 | 82 | * Overriding PHP's default __wakeup method to prvent unserializing of the instance, and return an error message. |
| 83 | + * | |
| 84 | + * @return never | |
| 79 | 85 | */ |
| 80 | 86 | public function __wakeup() { |
| 81 | 87 | die( "Please don't __wakeup WPCOM_JSON_API_Links" ); |
| 82 | 88 | } |
| @@ -286,12 +292,13 @@ | ||
| 286 | 292 | */ |
| 287 | 293 | public function get_closest_version_of_endpoint( $template_path, $path, $request_method = 'GET' ) { |
| 288 | 294 | $closest_endpoint_cache_by_version = & $this->closest_endpoint_cache_by_version; |
| 289 | 295 | |
| 290 | - $closest_endpoint_cache = & $closest_endpoint_cache_by_version[ $this->api->version ]; | |
| 296 | + $api_version = $this->api->version ?? ''; | |
| 297 | + $closest_endpoint_cache = & $closest_endpoint_cache_by_version[ $api_version ]; | |
| 291 | 298 | if ( ! $closest_endpoint_cache ) { |
| 292 | - $closest_endpoint_cache_by_version[ $this->api->version ] = array(); | |
| 293 | - $closest_endpoint_cache = & $closest_endpoint_cache_by_version[ $this->api->version ]; | |
| 299 | + $closest_endpoint_cache_by_version[ $api_version ] = array(); | |
| 300 | + $closest_endpoint_cache = & $closest_endpoint_cache_by_version[ $api_version ]; | |
| 294 | 301 | } |
| 295 | 302 | |
| 296 | 303 | if ( ! isset( $closest_endpoint_cache[ $template_path ] ) ) { |
| 297 | 304 | $closest_endpoint_cache[ $template_path ] = array(); |
| @@ -309,12 +316,12 @@ | ||
| 309 | 316 | |
| 310 | 317 | $matches_by_version = & $this->matches_by_version; |
| 311 | 318 | |
| 312 | 319 | // try to match out of saved matches. |
| 313 | - if ( ! isset( $matches_by_version[ $this->api->version ] ) ) { | |
| 314 | - $matches_by_version[ $this->api->version ] = array(); | |
| 320 | + if ( ! isset( $matches_by_version[ $api_version ] ) ) { | |
| 321 | + $matches_by_version[ $api_version ] = array(); | |
| 315 | 322 | } |
| 316 | - foreach ( $matches_by_version[ $this->api->version ] as $match ) { | |
| 323 | + foreach ( $matches_by_version[ $api_version ] as $match ) { | |
| 317 | 324 | $regex = $match->regex; |
| 318 | 325 | if ( preg_match( "#^$regex\$#", $path ) ) { |
| 319 | 326 | $closest_endpoint_cache[ $template_path ][ $request_method ] = $match->version; |
| 320 | 327 | return $match->version; |
| @@ -339,9 +346,9 @@ | ||
| 339 | 346 | if ( ! in_array( $request_method, $endpoint['request_methods'], true ) ) { |
| 340 | 347 | continue; |
| 341 | 348 | } |
| 342 | 349 | |
| 343 | - $endpoint_path = untrailingslashit( $endpoint['path'] ); | |
| 350 | + $endpoint_path = untrailingslashit( (string) $endpoint['path'] ); | |
| 344 | 351 | $endpoint_path_regex = str_replace( array( '%s', '%d' ), array( '([^/?&]+)', '(\d+)' ), $endpoint_path ); |
| 345 | 352 | |
| 346 | 353 | if ( ! preg_match( "#^$endpoint_path_regex\$#", $path ) ) { |
| 347 | 354 | continue; |
| @@ -375,9 +382,9 @@ | ||
| 375 | 382 | |
| 376 | 383 | // If the endpoint version is less than the requested endpoint version, return the max version found. |
| 377 | 384 | if ( ! empty( $max_version_found ) ) { |
| 378 | 385 | array_push( |
| 379 | - $matches_by_version[ $this->api->version ], | |
| 386 | + $matches_by_version[ $api_version ], | |
| 380 | 387 | (object) $max_version_found |
| 381 | 388 | ); |
| 382 | 389 | $closest_endpoint_cache[ $template_path ][ $request_method ] = $max_version_found['version']; |
| 383 | 390 | return $max_version_found['version']; |
| @@ -410,9 +417,9 @@ | ||
| 410 | 417 | // The key contains a serialized path, min_version and max_version. |
| 411 | 418 | list( $path, $min_version, $max_version ) = unserialize( $key ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize -- Legacy, see serialization at class.json-api.php. |
| 412 | 419 | |
| 413 | 420 | // Grab the last component of the relative path to use as the top-level key. |
| 414 | - $last_path_segment = $this->get_last_segment_of_relative_path( $path ); | |
| 421 | + $last_path_segment = $this->get_last_segment_of_relative_path( $path ) ?? ''; | |
| 415 | 422 | |
| 416 | 423 | $endpoint_path_versions[ $last_path_segment ][] = array( |
| 417 | 424 | 'path' => $path, |
| 418 | 425 | 'min_version' => $min_version, |