Make WordPress Core


Ignore:
Timestamp:
07/21/2020 09:20:22 PM (4 years ago)
Author:
TimothyBlynJacobs
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api.php

    r48526 r48555  
    20532053    }
    20542054
     2055    $is_array_type  = 'array' === $type || ( is_array( $type ) && in_array( 'array', $type, true ) );
     2056    $is_object_type = 'object' === $type || ( is_array( $type ) && in_array( 'object', $type, true ) );
     2057
     2058    if ( $is_array_type && $is_object_type ) {
     2059        if ( rest_is_array( $data ) ) {
     2060            $is_object_type = false;
     2061        } else {
     2062            $is_array_type = false;
     2063        }
     2064    }
     2065
     2066    $has_additional_properties = $is_object_type && isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] );
     2067
    20552068    foreach ( $data as $key => $value ) {
    20562069        $check = array();
    20572070
    2058         if ( 'array' === $type || ( is_array( $type ) && in_array( 'array', $type, true ) ) ) {
     2071        if ( $is_array_type ) {
    20592072            $check = isset( $schema['items'] ) ? $schema['items'] : array();
    2060         } elseif ( 'object' === $type || ( is_array( $type ) && in_array( 'object', $type, true ) ) ) {
     2073        } elseif ( $is_object_type ) {
    20612074            if ( isset( $schema['properties'][ $key ] ) ) {
    20622075                $check = $schema['properties'][ $key ];
    2063             } elseif ( isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ) ) {
     2076            } elseif ( $has_additional_properties ) {
    20642077                $check = $schema['additionalProperties'];
    20652078            }
     
    20712084
    20722085        if ( ! in_array( $context, $check['context'], true ) ) {
     2086            if ( $is_array_type ) {
     2087                // All array items share schema, so there's no need to check each one.
     2088                $data = array();
     2089                break;
     2090            }
     2091
    20732092            if ( is_object( $data ) ) {
    20742093                unset( $data->$key );
Note: See TracChangeset for help on using the changeset viewer.