Skip to content

Commit 640a533

Browse files
committed
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
1 parent 11e91d5 commit 640a533

3 files changed

Lines changed: 72 additions & 6 deletions

File tree

src/wp-includes/rest-api/class-wp-rest-server.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,10 +1273,23 @@ public function get_index( $request ) {
12731273
);
12741274

12751275
$response = new WP_REST_Response( $available );
1276-
$response->add_link( 'help', 'https://developer.wordpress.org/rest-api/' );
1277-
$this->add_active_theme_link_to_index( $response );
1278-
$this->add_site_logo_to_index( $response );
1279-
$this->add_site_icon_to_index( $response );
1276+
1277+
$fields = isset( $request['_fields'] ) ? $request['_fields'] : '';
1278+
$fields = wp_parse_list( $fields );
1279+
if ( empty( $fields ) ) {
1280+
$fields[] = '_links';
1281+
}
1282+
1283+
if ( $request->has_param( '_embed' ) ) {
1284+
$fields[] = '_embedded';
1285+
}
1286+
1287+
if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
1288+
$response->add_link( 'help', 'https://developer.wordpress.org/rest-api/' );
1289+
$this->add_active_theme_link_to_index( $response );
1290+
$this->add_site_logo_to_index( $response );
1291+
$this->add_site_icon_to_index( $response );
1292+
}
12801293

12811294
/**
12821295
* Filters the REST API root index data.

tests/phpunit/tests/rest-api.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2512,10 +2512,10 @@ public function test_rest_preload_api_request_fields() {
25122512
$this->assertArrayHasKey( 'description', $preload_data['/']['body'] );
25132513
$this->assertArrayHasKey( 'routes', $preload_data['/']['body'] );
25142514

2515-
// Filtered request only has the desired fields + links
2515+
// Filtered request only has the desired fields.
25162516
$this->assertSame(
25172517
array_keys( $preload_data['/?_fields=description']['body'] ),
2518-
array( 'description', '_links' )
2518+
array( 'description' )
25192519
);
25202520
}
25212521

tests/phpunit/tests/rest-api/rest-server.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,59 @@ public function test_get_index() {
11191119
$this->assertArrayHasKey( 'site_icon_url', $data );
11201120
}
11211121

1122+
/**
1123+
* @ticket 57902
1124+
*
1125+
* @covers WP_REST_Server::get_index
1126+
*/
1127+
public function test_get_index_fields_name() {
1128+
$server = new WP_REST_Server();
1129+
1130+
$request = new WP_REST_Request( 'GET', '/' );
1131+
$request->set_param( '_fields', 'name' );
1132+
$index = $server->dispatch( $request );
1133+
$index = rest_filter_response_fields( $index, $server, $request );
1134+
$data = $index->get_data();
1135+
$links = $index->get_links();
1136+
1137+
$this->assertArrayHasKey( 'name', $data );
1138+
$this->assertArrayNotHasKey( 'help', $links );
1139+
}
1140+
1141+
/**
1142+
* @ticket 57902
1143+
*
1144+
* @covers WP_REST_Server::get_index
1145+
*
1146+
* @dataProvider data_get_index_should_return_help_and_not_name
1147+
*
1148+
* @param string $field The field to add to the request.
1149+
*/
1150+
public function test_get_index_should_return_help_and_not_name( $field ) {
1151+
$server = new WP_REST_Server();
1152+
1153+
$request = new WP_REST_Request( 'GET', '/' );
1154+
$request->set_param( '_fields', $field );
1155+
$index = $server->dispatch( $request );
1156+
$index = rest_filter_response_fields( $index, $server, $request );
1157+
$data = $index->get_data();
1158+
$links = $index->get_links();
1159+
1160+
$this->assertArrayNotHasKey( 'name', $data );
1161+
$this->assertArrayHasKey( 'help', $links );
1162+
}
1163+
1164+
/**
1165+
* Data provider.
1166+
*
1167+
* @throws Exception
1168+
*
1169+
* @return array
1170+
*/
1171+
public function data_get_index_should_return_help_and_not_name() {
1172+
return self::text_array_to_dataprovider( array( '_links', '_embedded' ) );
1173+
}
1174+
11221175
/**
11231176
* @ticket 50152
11241177
*/

0 commit comments

Comments
 (0)