From 7d1e95ec7f52b2ea29bdd6778f7e89686eb9e23b Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Wed, 13 Sep 2023 14:34:20 +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. Built from https://develop.svn.wordpress.org/trunk@56566 git-svn-id: http://core.svn.wordpress.org/trunk@56078 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/rest-api/class-wp-rest-server.php | 21 +++++++++++++++---- wp-includes/version.php | 2 +- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/wp-includes/rest-api/class-wp-rest-server.php b/wp-includes/rest-api/class-wp-rest-server.php index 9df4556ada..50e7bd9796 100644 --- a/wp-includes/rest-api/class-wp-rest-server.php +++ b/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/wp-includes/version.php b/wp-includes/version.php index 4fba75fd1c..9ba4c26aa1 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.4-alpha-56565'; +$wp_version = '6.4-alpha-56566'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.