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/tests/phpunit/tests/rest-api/rest-post-types-controller.php

    r43007 r43087  
    122122        $response = $endpoint->prepare_item_for_response( $obj, $request );
    123123        $this->check_post_type_obj( 'edit', $obj, $response->get_data(), $response->get_links() );
     124    }
     125
     126    public function test_prepare_item_limit_fields() {
     127        $obj      = get_post_type_object( 'post' );
     128        $request  = new WP_REST_Request;
     129        $endpoint = new WP_REST_Post_Types_Controller;
     130        $request->set_param( 'context', 'edit' );
     131        $request->set_param( '_fields', 'id,name' );
     132        $response = $endpoint->prepare_item_for_response( $obj, $request );
     133        $this->assertEquals( array(
     134            // 'id' doesn't exist in this context.
     135            'name',
     136        ), array_keys( $response->get_data() ) );
    124137    }
    125138
Note: See TracChangeset for help on using the changeset viewer.