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.



git-svn-id: https://develop.svn.wordpress.org/trunk@43087 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast
2018-05-02 01:24:30 +00:00
parent 1a4e28818f
commit 4ac3f4c13a
22 changed files with 526 additions and 167 deletions

View File

@@ -200,4 +200,27 @@ class WP_Test_REST_Controller extends WP_Test_REST_TestCase {
$this->assertEquals( 'a', $args['somedefault']['default'] );
}
public function test_get_fields_for_response() {
$controller = new WP_REST_Test_Controller();
$request = new WP_REST_Request( 'GET', '/wp/v2/testroute' );
$fields = $controller->get_fields_for_response( $request );
$this->assertEquals( array(
'somestring',
'someinteger',
'someboolean',
'someurl',
'somedate',
'someemail',
'someenum',
'someargoptions',
'somedefault',
), $fields );
$request->set_param( '_fields', 'somestring,someinteger' );
$fields = $controller->get_fields_for_response( $request );
$this->assertEquals( array(
'somestring',
'someinteger',
), $fields );
}
}