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
This commit is contained in:
Jonny Harris 2023-09-13 14:32:33 +00:00
parent 11e91d50d5
commit 640a53354c
3 changed files with 72 additions and 6 deletions

View File

@ -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.

View File

@ -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' )
);
}

View File

@ -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
*/