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-post-statuses-controller.php

    r41731 r43445  
    196196    public function prepare_item_for_response( $status, $request ) {
    197197
    198         $data = array(
    199             'name'         => $status->label,
    200             'private'      => (bool) $status->private,
    201             'protected'    => (bool) $status->protected,
    202             'public'       => (bool) $status->public,
    203             'queryable'    => (bool) $status->publicly_queryable,
    204             'show_in_list' => (bool) $status->show_in_admin_all_list,
    205             'slug'         => $status->name,
    206         );
     198        $fields = $this->get_fields_for_response( $request );
     199        $data   = array();
     200
     201        if ( in_array( 'name', $fields, true ) ) {
     202            $data['name'] = $status->label;
     203        }
     204
     205        if ( in_array( 'private', $fields, true ) ) {
     206            $data['private'] = (bool) $status->private;
     207        }
     208
     209        if ( in_array( 'protected', $fields, true ) ) {
     210            $data['protected'] = (bool) $status->protected;
     211        }
     212
     213        if ( in_array( 'public', $fields, true ) ) {
     214            $data['public'] = (bool) $status->public;
     215        }
     216
     217        if ( in_array( 'queryable', $fields, true ) ) {
     218            $data['queryable'] = (bool) $status->publicly_queryable;
     219        }
     220
     221        if ( in_array( 'show_in_list', $fields, true ) ) {
     222            $data['show_in_list'] = (bool) $status->show_in_admin_all_list;
     223        }
     224
     225        if ( in_array( 'slug', $fields, true ) ) {
     226            $data['slug'] = $status->name;
     227        }
    207228
    208229        $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
Note: See TracChangeset for help on using the changeset viewer.