Make WordPress Core


Ignore:
Timestamp:
07/31/2019 08:20:02 PM (6 years ago)
Author:
kadamwhite
Message:

REST API: Skip processing fields which are not present in the selected context.

In WP_REST_Controller::get_fields_for_response(), exclude fields which are not registered to appear in the request's context.

In conjunction with r45705 this prevents the unnecessary computation of the sample permalink when making a request that is not context=edit.

Props dlh.
Fixes #45605.

File:
1 edited

Legend:

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

    r45566 r45706  
    518518     */
    519519    public function get_fields_for_response( $request ) {
    520         $schema = $this->get_item_schema();
    521         $fields = isset( $schema['properties'] ) ? array_keys( $schema['properties'] ) : array();
     520        $schema     = $this->get_item_schema();
     521        $properties = isset( $schema['properties'] ) ? $schema['properties'] : array();
    522522
    523523        $additional_fields = $this->get_additional_fields();
     
    526526            // because it won't be present in $this->get_item_schema().
    527527            if ( is_null( $field_options['schema'] ) ) {
    528                 $fields[] = $field_name;
    529             }
    530         }
     528                $properties[ $field_name ] = $field_options;
     529            }
     530        }
     531
     532        // Exclude fields that specify a different context than the request context.
     533        $context = $request['context'];
     534        if ( $context ) {
     535            foreach ( $properties as $name => $options ) {
     536                if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) {
     537                    unset( $properties[ $name ] );
     538                }
     539            }
     540        }
     541
     542        $fields = array_keys( $properties );
    531543
    532544        if ( ! isset( $request['_fields'] ) ) {
Note: See TracChangeset for help on using the changeset viewer.