Make WordPress Core


Ignore:
Timestamp:
07/13/2018 06:50:51 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.

Merges [43087] to the 4.9 branch.

Props danielbachhuber.
See #43874.

Location:
branches/4.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

  • branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php

    r43443 r43445  
    178178    public function prepare_item_for_response( $taxonomy, $request ) {
    179179        $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
    180         $data = array(
    181             'name'         => $taxonomy->label,
    182             'slug'         => $taxonomy->name,
    183             'capabilities' => $taxonomy->cap,
    184             'description'  => $taxonomy->description,
    185             'labels'       => $taxonomy->labels,
    186             'types'        => $taxonomy->object_type,
    187             'show_cloud'   => $taxonomy->show_tagcloud,
    188             'hierarchical' => $taxonomy->hierarchical,
    189             'rest_base'    => $base,
    190         );
     180
     181        $fields = $this->get_fields_for_response( $request );
     182        $data   = array();
     183
     184        if ( in_array( 'name', $fields, true ) ) {
     185            $data['name'] = $taxonomy->label;
     186        }
     187
     188        if ( in_array( 'slug', $fields, true ) ) {
     189            $data['slug'] = $taxonomy->name;
     190        }
     191
     192        if ( in_array( 'capabilities', $fields, true ) ) {
     193            $data['capabilities'] = $taxonomy->cap;
     194        }
     195
     196        if ( in_array( 'description', $fields, true ) ) {
     197            $data['description'] = $taxonomy->description;
     198        }
     199
     200        if ( in_array( 'labels', $fields, true ) ) {
     201            $data['labels'] = $taxonomy->labels;
     202        }
     203
     204        if ( in_array( 'types', $fields, true ) ) {
     205            $data['types'] = $taxonomy->object_type;
     206        }
     207
     208        if ( in_array( 'show_cloud', $fields, true ) ) {
     209            $data['show_cloud'] = $taxonomy->show_tagcloud;
     210        }
     211
     212        if ( in_array( 'hierarchical', $fields, true ) ) {
     213            $data['hierarchical'] = $taxonomy->hierarchical;
     214        }
     215
     216        if ( in_array( 'rest_base', $fields, true ) ) {
     217            $data['rest_base'] = $base;
     218        }
     219
     220        if ( in_array( 'visibility', $fields, true ) ) {
     221            $data['visibility'] = array(
     222                'public'             => (bool) $taxonomy->public,
     223                'publicly_queryable' => (bool) $taxonomy->publicly_queryable,
     224                'show_admin_column'  => (bool) $taxonomy->show_admin_column,
     225                'show_in_nav_menus'  => (bool) $taxonomy->show_in_nav_menus,
     226                'show_in_quick_edit' => (bool) $taxonomy->show_in_quick_edit,
     227                'show_ui'            => (bool) $taxonomy->show_ui,
     228            );
     229        }
    191230
    192231        $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
Note: See TracChangeset for help on using the changeset viewer.