Make WordPress Core

Changeset 56566


Ignore:
Timestamp:
09/13/2023 02:32:33 PM (9 months ago)
Author:
spacedmonkey
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/class-wp-rest-server.php

    r56193 r56566  
    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        /**
  • trunk/tests/phpunit/tests/rest-api.php

    r55562 r56566  
    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    }
  • trunk/tests/phpunit/tests/rest-api/rest-server.php

    r56559 r56566  
    11181118        $this->assertArrayHasKey( 'site_icon', $data );
    11191119        $this->assertArrayHasKey( 'site_icon_url', $data );
     1120    }
     1121
     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' ) );
    11201173    }
    11211174
Note: See TracChangeset for help on using the changeset viewer.