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

    r39161 r43445  
    191191        $this->assertEquals( 'a', $args['somedefault']['default'] );
    192192    }
     193
     194    public function test_get_fields_for_response() {
     195        $controller = new WP_REST_Test_Controller();
     196        $request    = new WP_REST_Request( 'GET', '/wp/v2/testroute' );
     197        $fields     = $controller->get_fields_for_response( $request );
     198        $this->assertEquals( array(
     199            'somestring',
     200            'someinteger',
     201            'someboolean',
     202            'someurl',
     203            'somedate',
     204            'someemail',
     205            'someenum',
     206            'someargoptions',
     207            'somedefault',
     208        ), $fields );
     209        $request->set_param( '_fields', 'somestring,someinteger' );
     210        $fields = $controller->get_fields_for_response( $request );
     211        $this->assertEquals( array(
     212            'somestring',
     213            'someinteger',
     214        ), $fields );
     215    }
    193216}
Note: See TracChangeset for help on using the changeset viewer.