WordPress.org

Make WordPress Core


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

    r42724 r43087  
    130130        $data = $endpoint->prepare_item_for_response( $obj, $request );
    131131        $this->check_post_status_obj( $obj, $data->get_data(), $data->get_links() );
     132    }
     133
     134    public function test_prepare_item_limit_fields() {
     135        $obj      = get_post_status_object( 'publish' );
     136        $request  = new WP_REST_Request;
     137        $endpoint = new WP_REST_Post_Statuses_Controller;
     138        $request->set_param( 'context', 'edit' );
     139        $request->set_param( '_fields', 'id,name' );
     140        $response = $endpoint->prepare_item_for_response( $obj, $request );
     141        $this->assertEquals( array(
     142            // 'id' doesn't exist in this context.
     143            'name',
     144        ), array_keys( $response->get_data() ) );
    132145    }
    133146
Note: See TracChangeset for help on using the changeset viewer.