| @@ -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. |
| @@ -288,12 +292,13 @@ | ||
| 288 | 292 | */ |
| 289 | 293 | public function get_closest_version_of_endpoint( $template_path, $path, $request_method = 'GET' ) { |
| 290 | 294 | $closest_endpoint_cache_by_version = & $this->closest_endpoint_cache_by_version; |
| 291 | 295 | |
| 292 | - $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 ]; | |
| 293 | 298 | if ( ! $closest_endpoint_cache ) { |
| 294 | - $closest_endpoint_cache_by_version[ $this->api->version ] = array(); | |
| 295 | - $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 ]; | |
| 296 | 301 | } |
| 297 | 302 | |
| 298 | 303 | if ( ! isset( $closest_endpoint_cache[ $template_path ] ) ) { |
| 299 | 304 | $closest_endpoint_cache[ $template_path ] = array(); |
| @@ -311,12 +316,12 @@ | ||
| 311 | 316 | |
| 312 | 317 | $matches_by_version = & $this->matches_by_version; |
| 313 | 318 | |
| 314 | 319 | // try to match out of saved matches. |
| 315 | - if ( ! isset( $matches_by_version[ $this->api->version ] ) ) { | |
| 316 | - $matches_by_version[ $this->api->version ] = array(); | |
| 320 | + if ( ! isset( $matches_by_version[ $api_version ] ) ) { | |
| 321 | + $matches_by_version[ $api_version ] = array(); | |
| 317 | 322 | } |
| 318 | - foreach ( $matches_by_version[ $this->api->version ] as $match ) { | |
| 323 | + foreach ( $matches_by_version[ $api_version ] as $match ) { | |
| 319 | 324 | $regex = $match->regex; |
| 320 | 325 | if ( preg_match( "#^$regex\$#", $path ) ) { |
| 321 | 326 | $closest_endpoint_cache[ $template_path ][ $request_method ] = $match->version; |
| 322 | 327 | return $match->version; |
| @@ -341,9 +346,9 @@ | ||
| 341 | 346 | if ( ! in_array( $request_method, $endpoint['request_methods'], true ) ) { |
| 342 | 347 | continue; |
| 343 | 348 | } |
| 344 | 349 | |
| 345 | - $endpoint_path = untrailingslashit( $endpoint['path'] ); | |
| 350 | + $endpoint_path = untrailingslashit( (string) $endpoint['path'] ); | |
| 346 | 351 | $endpoint_path_regex = str_replace( array( '%s', '%d' ), array( '([^/?&]+)', '(\d+)' ), $endpoint_path ); |
| 347 | 352 | |
| 348 | 353 | if ( ! preg_match( "#^$endpoint_path_regex\$#", $path ) ) { |
| 349 | 354 | continue; |
| @@ -377,9 +382,9 @@ | ||
| 377 | 382 | |
| 378 | 383 | // If the endpoint version is less than the requested endpoint version, return the max version found. |
| 379 | 384 | if ( ! empty( $max_version_found ) ) { |
| 380 | 385 | array_push( |
| 381 | - $matches_by_version[ $this->api->version ], | |
| 386 | + $matches_by_version[ $api_version ], | |
| 382 | 387 | (object) $max_version_found |
| 383 | 388 | ); |
| 384 | 389 | $closest_endpoint_cache[ $template_path ][ $request_method ] = $max_version_found['version']; |
| 385 | 390 | return $max_version_found['version']; |
| @@ -412,9 +417,9 @@ | ||
| 412 | 417 | // The key contains a serialized path, min_version and max_version. |
| 413 | 418 | list( $path, $min_version, $max_version ) = unserialize( $key ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize -- Legacy, see serialization at class.json-api.php. |
| 414 | 419 | |
| 415 | 420 | // Grab the last component of the relative path to use as the top-level key. |
| 416 | - $last_path_segment = $this->get_last_segment_of_relative_path( $path ); | |
| 421 | + $last_path_segment = $this->get_last_segment_of_relative_path( $path ) ?? ''; | |
| 417 | 422 | |
| 418 | 423 | $endpoint_path_versions[ $last_path_segment ][] = array( |
| 419 | 424 | 'path' => $path, |
| 420 | 425 | 'min_version' => $min_version, |