diff --git a/src/wp-includes/rest-api/class-wp-rest-server.php b/src/wp-includes/rest-api/class-wp-rest-server.php index 9df4556ada..50e7bd9796 100644 --- a/src/wp-includes/rest-api/class-wp-rest-server.php +++ b/src/wp-includes/rest-api/class-wp-rest-server.php @@ -1273,10 +1273,23 @@ class WP_REST_Server { ); $response = new WP_REST_Response( $available ); - $response->add_link( 'help', 'https://developer.wordpress.org/rest-api/' ); - $this->add_active_theme_link_to_index( $response ); - $this->add_site_logo_to_index( $response ); - $this->add_site_icon_to_index( $response ); + + $fields = isset( $request['_fields'] ) ? $request['_fields'] : ''; + $fields = wp_parse_list( $fields ); + if ( empty( $fields ) ) { + $fields[] = '_links'; + } + + if ( $request->has_param( '_embed' ) ) { + $fields[] = '_embedded'; + } + + if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { + $response->add_link( 'help', 'https://developer.wordpress.org/rest-api/' ); + $this->add_active_theme_link_to_index( $response ); + $this->add_site_logo_to_index( $response ); + $this->add_site_icon_to_index( $response ); + } /** * Filters the REST API root index data. diff --git a/tests/phpunit/tests/rest-api.php b/tests/phpunit/tests/rest-api.php index 20b23fc0fb..4a575b8803 100644 --- a/tests/phpunit/tests/rest-api.php +++ b/tests/phpunit/tests/rest-api.php @@ -2512,10 +2512,10 @@ class Tests_REST_API extends WP_UnitTestCase { $this->assertArrayHasKey( 'description', $preload_data['/']['body'] ); $this->assertArrayHasKey( 'routes', $preload_data['/']['body'] ); - // Filtered request only has the desired fields + links + // Filtered request only has the desired fields. $this->assertSame( array_keys( $preload_data['/?_fields=description']['body'] ), - array( 'description', '_links' ) + array( 'description' ) ); } diff --git a/tests/phpunit/tests/rest-api/rest-server.php b/tests/phpunit/tests/rest-api/rest-server.php index a642d37c56..6432026697 100644 --- a/tests/phpunit/tests/rest-api/rest-server.php +++ b/tests/phpunit/tests/rest-api/rest-server.php @@ -1119,6 +1119,59 @@ class Tests_REST_Server extends WP_Test_REST_TestCase { $this->assertArrayHasKey( 'site_icon_url', $data ); } + /** + * @ticket 57902 + * + * @covers WP_REST_Server::get_index + */ + public function test_get_index_fields_name() { + $server = new WP_REST_Server(); + + $request = new WP_REST_Request( 'GET', '/' ); + $request->set_param( '_fields', 'name' ); + $index = $server->dispatch( $request ); + $index = rest_filter_response_fields( $index, $server, $request ); + $data = $index->get_data(); + $links = $index->get_links(); + + $this->assertArrayHasKey( 'name', $data ); + $this->assertArrayNotHasKey( 'help', $links ); + } + + /** + * @ticket 57902 + * + * @covers WP_REST_Server::get_index + * + * @dataProvider data_get_index_should_return_help_and_not_name + * + * @param string $field The field to add to the request. + */ + public function test_get_index_should_return_help_and_not_name( $field ) { + $server = new WP_REST_Server(); + + $request = new WP_REST_Request( 'GET', '/' ); + $request->set_param( '_fields', $field ); + $index = $server->dispatch( $request ); + $index = rest_filter_response_fields( $index, $server, $request ); + $data = $index->get_data(); + $links = $index->get_links(); + + $this->assertArrayNotHasKey( 'name', $data ); + $this->assertArrayHasKey( 'help', $links ); + } + + /** + * Data provider. + * + * @throws Exception + * + * @return array + */ + public function data_get_index_should_return_help_and_not_name() { + return self::text_array_to_dataprovider( array( '_links', '_embedded' ) ); + } + /** * @ticket 50152 */