diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php
index 3bc6b59091..04bd1580f7 100644
|
|
|
abstract class WP_REST_Controller {
|
| 385 | 385 | |
| 386 | 386 | $additional_fields = $this->get_additional_fields(); |
| 387 | 387 | |
| | 388 | $requested_fields = $this->get_fields_for_response( $request ); |
| | 389 | |
| 388 | 390 | foreach ( $additional_fields as $field_name => $field_options ) { |
| 389 | 391 | |
| 390 | 392 | if ( ! $field_options['get_callback'] ) { |
| 391 | 393 | continue; |
| 392 | 394 | } |
| 393 | 395 | |
| | 396 | if ( ! in_array( $field_name, $requested_fields, true ) ) { |
| | 397 | continue; |
| | 398 | } |
| | 399 | |
| 394 | 400 | $object[ $field_name ] = call_user_func( $field_options['get_callback'], $object, $field_name, $request, $this->get_object_type() ); |
| 395 | 401 | } |
| 396 | 402 | |
diff --git tests/phpunit/tests/rest-api/rest-controller.php tests/phpunit/tests/rest-api/rest-controller.php
index 88941ec1ee..b325fb19e2 100644
|
|
|
class WP_Test_REST_Controller extends WP_Test_REST_TestCase {
|
| 213 | 213 | 'someinteger', |
| 214 | 214 | ), $fields ); |
| 215 | 215 | } |
| | 216 | |
| | 217 | public function test_add_additional_fields_to_object_respects_fields_param() { |
| | 218 | $controller = new WP_REST_Test_Controller(); |
| | 219 | $request = new WP_REST_Request( 'GET', '/wp/v2/testroute' ); |
| | 220 | $schema = $controller->get_item_schema(); |
| | 221 | $field = 'somefield'; |
| | 222 | |
| | 223 | $listener = new MockAction(); |
| | 224 | $method = 'action'; |
| | 225 | |
| | 226 | register_rest_field( |
| | 227 | $schema['title'], |
| | 228 | $field, |
| | 229 | array( |
| | 230 | 'get_callback' => array( $listener, $method ), |
| | 231 | 'schema' => array( |
| | 232 | 'type' => 'string', |
| | 233 | ), |
| | 234 | ) |
| | 235 | ); |
| | 236 | |
| | 237 | $item = []; |
| | 238 | |
| | 239 | $controller->prepare_item_for_response( $item, $request ); |
| | 240 | |
| | 241 | $first_call_count = $listener->get_call_count( $method ); |
| | 242 | |
| | 243 | $this->assertTrue( $first_call_count > 0 ); |
| | 244 | |
| | 245 | $request->set_param( '_fields', 'somestring' ); |
| | 246 | |
| | 247 | $controller->prepare_item_for_response( $item, $request ); |
| | 248 | |
| | 249 | $this->assertSame( $first_call_count, $listener->get_call_count( $method ) ); |
| | 250 | |
| | 251 | $request->set_param( '_fields', $field ); |
| | 252 | |
| | 253 | $controller->prepare_item_for_response( $item, $request ); |
| | 254 | |
| | 255 | $this->assertTrue( $listener->get_call_count( $method ) > $first_call_count ); |
| | 256 | } |
| 216 | 257 | } |
diff --git tests/phpunit/tests/rest-api/rest-test-controller.php tests/phpunit/tests/rest-api/rest-test-controller.php
index 5b90f76e44..a8c3fefe0a 100644
|
|
|
|
| 10 | 10 | * @group restapi |
| 11 | 11 | */ |
| 12 | 12 | 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 | } |
| 13 | 27 | |
| 14 | 28 | /** |
| 15 | | * Get the Post type's schema, conforming to JSON Schema |
| | 29 | * Get the item's schema, conforming to JSON Schema. |
| 16 | 30 | * |
| 17 | 31 | * @return array |
| 18 | 32 | */ |
| … |
… |
class WP_REST_Test_Controller extends WP_REST_Controller {
|
| 72 | 86 | ), |
| 73 | 87 | ); |
| 74 | 88 | |
| 75 | | return $schema; |
| | 89 | return $this->add_additional_fields_schema( $schema ); |
| 76 | 90 | } |
| 77 | 91 | |
| 78 | 92 | } |