Skip to content

Commit 8c85f2d

Browse files
committed
REST API: Don't add fields to object when not included in ?_fields=.
In [43087], we improved REST API performance by only rendering the fields specified in the request. Similarly, any fields registered with `register_rest_field()` should only be rendered when included in `?_fields=`. Props dlh, danielbachhuber. Merges [43736] to trunk. Fixes #45099. git-svn-id: https://develop.svn.wordpress.org/trunk@43986 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 140a95c commit 8c85f2d

3 files changed

Lines changed: 63 additions & 2 deletions

File tree

src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,18 @@ protected function add_additional_fields_to_object( $object, $request ) {
385385

386386
$additional_fields = $this->get_additional_fields();
387387

388+
$requested_fields = $this->get_fields_for_response( $request );
389+
388390
foreach ( $additional_fields as $field_name => $field_options ) {
389391

390392
if ( ! $field_options['get_callback'] ) {
391393
continue;
392394
}
393395

396+
if ( ! in_array( $field_name, $requested_fields, true ) ) {
397+
continue;
398+
}
399+
394400
$object[ $field_name ] = call_user_func( $field_options['get_callback'], $object, $field_name, $request, $this->get_object_type() );
395401
}
396402

tests/phpunit/tests/rest-api/rest-controller.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,45 @@ public function test_get_fields_for_response() {
231231
$fields
232232
);
233233
}
234+
235+
public function test_add_additional_fields_to_object_respects_fields_param() {
236+
$controller = new WP_REST_Test_Controller();
237+
$request = new WP_REST_Request( 'GET', '/wp/v2/testroute' );
238+
$schema = $controller->get_item_schema();
239+
$field = 'somefield';
240+
241+
$listener = new MockAction();
242+
$method = 'action';
243+
244+
register_rest_field(
245+
$schema['title'],
246+
$field,
247+
array(
248+
'get_callback' => array( $listener, $method ),
249+
'schema' => array(
250+
'type' => 'string',
251+
),
252+
)
253+
);
254+
255+
$item = array();
256+
257+
$controller->prepare_item_for_response( $item, $request );
258+
259+
$first_call_count = $listener->get_call_count( $method );
260+
261+
$this->assertTrue( $first_call_count > 0 );
262+
263+
$request->set_param( '_fields', 'somestring' );
264+
265+
$controller->prepare_item_for_response( $item, $request );
266+
267+
$this->assertSame( $first_call_count, $listener->get_call_count( $method ) );
268+
269+
$request->set_param( '_fields', $field );
270+
271+
$controller->prepare_item_for_response( $item, $request );
272+
273+
$this->assertTrue( $listener->get_call_count( $method ) > $first_call_count );
274+
}
234275
}

tests/phpunit/tests/rest-api/rest-test-controller.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,23 @@
1010
* @group restapi
1111
*/
1212
class WP_REST_Test_Controller extends WP_REST_Controller {
13+
/**
14+
* Prepares the item for the REST response.
15+
*
16+
* @param mixed $item WordPress representation of the item.
17+
* @param WP_REST_Request $request Request object.
18+
* @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
19+
*/
20+
public function prepare_item_for_response( $item, $request ) {
21+
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
22+
$item = $this->add_additional_fields_to_object( $item, $request );
23+
$item = $this->filter_response_by_context( $item, $context );
24+
$response = rest_ensure_response( $item );
25+
return $response;
26+
}
1327

1428
/**
15-
* Get the Post type's schema, conforming to JSON Schema
29+
* Get the item's schema, conforming to JSON Schema.
1630
*
1731
* @return array
1832
*/
@@ -72,7 +86,7 @@ public function get_item_schema() {
7286
),
7387
);
7488

75-
return $schema;
89+
return $this->add_additional_fields_schema( $schema );
7690
}
7791

7892
}

0 commit comments

Comments
 (0)