Make WordPress Core


Ignore:
Timestamp:
05/02/2018 01:24:30 AM (7 years ago)
Author:
pento
Message:

REST API: Filter responses based on the _fields parameter, before data is processed.

Historically, the REST API would generate the entire response object, including running expensive filters, then it would apply the _fields parameter, discarding the fields that weren't specificed.

This change causes _fields to be applied earlier, so that only requested fields are processed.

Props danielbachhuber.
See #43874.

File:
1 edited

Legend:

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

    r43007 r43087  
    156156        $supports   = get_all_post_type_supports( $post_type->name );
    157157
    158         $data    = array(
    159             'capabilities' => $post_type->cap,
    160             'description'  => $post_type->description,
    161             'hierarchical' => $post_type->hierarchical,
    162             'viewable'     => is_post_type_viewable( $post_type ),
    163             'labels'       => $post_type->labels,
    164             'name'         => $post_type->label,
    165             'slug'         => $post_type->name,
    166             'supports'     => $supports,
    167             'taxonomies'   => array_values( $taxonomies ),
    168             'rest_base'    => $base,
    169         );
     158        $fields = $this->get_fields_for_response( $request );
     159        $data   = array();
     160
     161        if ( in_array( 'capabilities', $fields, true ) ) {
     162            $data['capabilities'] = $post_type->cap;
     163        }
     164
     165        if ( in_array( 'description', $fields, true ) ) {
     166            $data['description'] = $post_type->description;
     167        }
     168
     169        if ( in_array( 'hierarchical', $fields, true ) ) {
     170            $data['hierarchical'] = $post_type->hierarchical;
     171        }
     172
     173        if ( in_array( 'viewable', $fields, true ) ) {
     174            $data['viewable'] = is_post_type_viewable( $post_type );
     175        }
     176
     177        if ( in_array( 'labels', $fields, true ) ) {
     178            $data['labels'] = $post_type->labels;
     179        }
     180
     181        if ( in_array( 'name', $fields, true ) ) {
     182            $data['name'] = $post_type->label;
     183        }
     184
     185        if ( in_array( 'slug', $fields, true ) ) {
     186            $data['slug'] = $post_type->name;
     187        }
     188
     189        if ( in_array( 'supports', $fields, true ) ) {
     190            $data['supports'] = $supports;
     191        }
     192
     193        if ( in_array( 'taxonomies', $fields, true ) ) {
     194            $data['taxonomies'] = array_values( $taxonomies );
     195        }
     196
     197        if ( in_array( 'rest_base', $fields, true ) ) {
     198            $data['rest_base'] = $base;
     199        }
     200
    170201        $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
    171202        $data    = $this->add_additional_fields_to_object( $data, $request );
Note: See TracChangeset for help on using the changeset viewer.