Ticket #45220: 45220.2.diff
File 45220.2.diff, 2.8 KB (added by , 6 years ago) |
---|
-
src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php
diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php index f98d1ad57e..caee7204ea 100644
a b abstract class WP_REST_Controller { 519 519 public function get_fields_for_response( $request ) { 520 520 $schema = $this->get_item_schema(); 521 521 $fields = isset( $schema['properties'] ) ? array_keys( $schema['properties'] ) : array(); 522 523 $additional_fields = $this->get_additional_fields(); 524 foreach ( $additional_fields as $field_name => $field_options ) { 525 // For back-compat, include any field with an empty schema 526 // because it won't be present in $this->get_item_schema(). 527 if ( is_null( $field_options['schema'] ) ) { 528 $fields[] = $field_name; 529 } 530 } 531 522 532 if ( ! isset( $request['_fields'] ) ) { 523 533 return $fields; 524 534 } -
tests/phpunit/tests/rest-api/rest-posts-controller.php
diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index 10f6a6eebc..ff97d2648d 100644
a b class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 3365 3365 $wp_rest_additional_fields = array(); 3366 3366 } 3367 3367 3368 /** 3369 * @ticket 45220 3370 */ 3371 public function test_get_additional_field_registration_null_schema() { 3372 register_rest_field( 'post', 'my_custom_int', array( 3373 'schema' => null, 3374 'get_callback' => array( $this, 'additional_field_get_callback' ), 3375 'update_callback' => null, 3376 ) ); 3377 $post_id = $this->factory->post->create(); 3378 3379 // 'my_custom_int' should appear because ?_fields= isn't set. 3380 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id ); 3381 $response = $this->server->dispatch( $request ); 3382 $this->assertArrayHasKey( 'my_custom_int', $response->data ); 3383 3384 // 'my_custom_int' should appear because it's present in ?_fields=. 3385 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id ); 3386 $request->set_param( '_fields', 'title,my_custom_int' ); 3387 $response = $this->server->dispatch( $request ); 3388 $this->assertArrayHasKey( 'my_custom_int', $response->data ); 3389 3390 // 'my_custom_int' should not appear because it's not present in ?_fields=. 3391 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id ); 3392 $request->set_param( '_fields', 'title' ); 3393 $response = $this->server->dispatch( $request ); 3394 $this->assertArrayNotHasKey( 'my_custom_int', $response->data ); 3395 3396 global $wp_rest_additional_fields; 3397 $wp_rest_additional_fields = array(); 3398 } 3399 3368 3400 public function test_additional_field_update_errors() { 3369 3401 $schema = array( 3370 3402 'type' => 'integer',