From 640a53354cecf1f65098a311edc7c974037ff28b Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Wed, 13 Sep 2023 14:32:33 +0000 Subject: [PATCH] REST API: Avoid unnecessarily preparing item links REST API index. Building upon the changes introduced in [53760], this commit refines the behavior of the REST API index. Specifically, it addresses performance concerns related to the unnecessary preparation of item links, such as site icon and logo links. Prior to this update, the index controller was invoking the prepare_links method regardless of whether the _links or _embedded fields were requested in the response. This led to unnecessary database lookups and decreased overall performance. In this commit, we implement a more efficient approach. Now, the prepare_links method will only be called when the _links or _embedded fields are explicitly requested in the response. This optimization ensures that we prepare links only when they are intended for inclusion in the API response, reducing unnecessary overhead. By implementing this improvement, we enhance the overall efficiency and performance of the WordPress core REST API index controller. Props spacedmonkey, niravsherasiya7707, dlh, mukesh27, costdev, swissspidy. Fixes #57902. git-svn-id: https://develop.svn.wordpress.org/trunk@56566 602fd350-edb4-49c9-b593-d223f7449a82 --- .../rest-api/class-wp-rest-server.php | 21 ++++++-- tests/phpunit/tests/rest-api.php | 4 +- tests/phpunit/tests/rest-api/rest-server.php | 53 +++++++++++++++++++ 3 files changed, 72 insertions(+), 6 deletions(-) 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 */