From 16784a5ea7d4a8de688b986bcb05afee877cc3ab Mon Sep 17 00:00:00 2001 From: TimothyBlynJacobs Date: Tue, 21 Jul 2020 21:22:03 +0000 Subject: [PATCH] REST API: Optimize rest_filter_response_by_context performance. In [47758] a new function `rest_filter_response_by_context` was introduced to expand the JSON schema features supported by the context filtering mechanism. This commit improves the performance of that function by eliminating repetitive comparisons and loops. Additionally, it improves multi-type support for object + array types. Fixes #50700. Props dlh. Built from https://develop.svn.wordpress.org/trunk@48555 git-svn-id: http://core.svn.wordpress.org/trunk@48317 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/rest-api.php | 25 ++++++++++++++++++++++--- wp-includes/version.php | 2 +- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/wp-includes/rest-api.php b/wp-includes/rest-api.php index e9a5799b60..c18c63ec2a 100644 --- a/wp-includes/rest-api.php +++ b/wp-includes/rest-api.php @@ -2052,15 +2052,28 @@ function rest_filter_response_by_context( $data, $schema, $context ) { return $data; } + $is_array_type = 'array' === $type || ( is_array( $type ) && in_array( 'array', $type, true ) ); + $is_object_type = 'object' === $type || ( is_array( $type ) && in_array( 'object', $type, true ) ); + + if ( $is_array_type && $is_object_type ) { + if ( rest_is_array( $data ) ) { + $is_object_type = false; + } else { + $is_array_type = false; + } + } + + $has_additional_properties = $is_object_type && isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ); + foreach ( $data as $key => $value ) { $check = array(); - if ( 'array' === $type || ( is_array( $type ) && in_array( 'array', $type, true ) ) ) { + if ( $is_array_type ) { $check = isset( $schema['items'] ) ? $schema['items'] : array(); - } elseif ( 'object' === $type || ( is_array( $type ) && in_array( 'object', $type, true ) ) ) { + } elseif ( $is_object_type ) { if ( isset( $schema['properties'][ $key ] ) ) { $check = $schema['properties'][ $key ]; - } elseif ( isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ) ) { + } elseif ( $has_additional_properties ) { $check = $schema['additionalProperties']; } } @@ -2070,6 +2083,12 @@ function rest_filter_response_by_context( $data, $schema, $context ) { } if ( ! in_array( $context, $check['context'], true ) ) { + if ( $is_array_type ) { + // All array items share schema, so there's no need to check each one. + $data = array(); + break; + } + if ( is_object( $data ) ) { unset( $data->$key ); } else { diff --git a/wp-includes/version.php b/wp-includes/version.php index a3dd724224..36f062872b 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.5-beta3-48554'; +$wp_version = '5.5-beta3-48555'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.