Make WordPress Core


Ignore:
Timestamp:
05/02/2018 01:24:30 AM (8 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-taxonomies-controller.php

    r42973 r43087  
    182182        public function prepare_item_for_response( $taxonomy, $request ) {
    183183                $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
    184                 $data = array(
    185                         'name'         => $taxonomy->label,
    186                         'slug'         => $taxonomy->name,
    187                         'capabilities' => $taxonomy->cap,
    188                         'description'  => $taxonomy->description,
    189                         'labels'       => $taxonomy->labels,
    190                         'types'        => $taxonomy->object_type,
    191                         'show_cloud'   => $taxonomy->show_tagcloud,
    192                         'hierarchical' => $taxonomy->hierarchical,
    193                         'rest_base'    => $base,
    194                         'visibility'   => array(
     184
     185                $fields = $this->get_fields_for_response( $request );
     186                $data   = array();
     187
     188                if ( in_array( 'name', $fields, true ) ) {
     189                        $data['name'] = $taxonomy->label;
     190                }
     191
     192                if ( in_array( 'slug', $fields, true ) ) {
     193                        $data['slug'] = $taxonomy->name;
     194                }
     195
     196                if ( in_array( 'capabilities', $fields, true ) ) {
     197                        $data['capabilities'] = $taxonomy->cap;
     198                }
     199
     200                if ( in_array( 'description', $fields, true ) ) {
     201                        $data['description'] = $taxonomy->description;
     202                }
     203
     204                if ( in_array( 'labels', $fields, true ) ) {
     205                        $data['labels'] = $taxonomy->labels;
     206                }
     207
     208                if ( in_array( 'types', $fields, true ) ) {
     209                        $data['types'] = $taxonomy->object_type;
     210                }
     211
     212                if ( in_array( 'show_cloud', $fields, true ) ) {
     213                        $data['show_cloud'] = $taxonomy->show_tagcloud;
     214                }
     215
     216                if ( in_array( 'hierarchical', $fields, true ) ) {
     217                        $data['hierarchical'] = $taxonomy->hierarchical;
     218                }
     219
     220                if ( in_array( 'rest_base', $fields, true ) ) {
     221                        $data['rest_base'] = $base;
     222                }
     223
     224                if ( in_array( 'visibility', $fields, true ) ) {
     225                        $data['visibility'] = array(
    195226                                'public'             => (bool) $taxonomy->public,
    196227                                'publicly_queryable' => (bool) $taxonomy->publicly_queryable,
     
    199230                                'show_in_quick_edit' => (bool) $taxonomy->show_in_quick_edit,
    200231                                'show_ui'            => (bool) $taxonomy->show_ui,
    201                         ),
    202                 );
     232                        );
     233                }
    203234
    204235                $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
Note: See TracChangeset for help on using the changeset viewer.