Make WordPress Core

Changeset 56566

Timestamp:
09/13/2023 02:32:33 PM (11 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
     1124
     1125
     1126
     1127
     1128
     1129
     1130
     1131
     1132
     1133
     1134
     1135
     1136
     1137
     1138
     1139
     1140
     1141
     1142
     1143
     1144
     1145
     1146
     1147
     1148
     1149
     1150
     1151
     1152
     1153
     1154
     1155
     1156
     1157
     1158
     1159
     1160
     1161
     1162
     1163
     1164
     1165
     1166
     1167
     1168
     1169
     1170
     1171
     1172
    11201173    }
    11211174
Note: See TracChangeset for help on using the changeset viewer.