Make WordPress Core


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

    r42724 r43087  
    236236        $this->assertEquals( 200, $response->get_status() );
    237237        $this->check_get_revision_response( $response, $this->revision_1 );
     238    }
     239
     240    public function test_prepare_item_limit_fields() {
     241        wp_set_current_user( self::$editor_id );
     242        $request  = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
     243        $endpoint = new WP_REST_Revisions_Controller( 'post' );
     244        $request->set_param( 'context', 'edit' );
     245        $request->set_param( '_fields', 'id,slug' );
     246        $revision = get_post( $this->revision_id1 );
     247        $response = $endpoint->prepare_item_for_response( $revision, $request );
     248        $this->assertEquals( array(
     249            'id',
     250            'slug',
     251        ), array_keys( $response->get_data() ) );
    238252    }
    239253
Note: See TracChangeset for help on using the changeset viewer.