Make WordPress Core


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

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-types-controller.php

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