Make WordPress Core


Ignore:
Timestamp:
07/22/2022 01:58:46 PM (22 months ago)
Author:
spacedmonkey
Message:

REST API: Avoid unnecessarily preparing item links.

Do not call the prepare_links methods in core REST API controllers, unless the _links or _embedded fields are requested. There is no need to prepare links if they are never returned in the response. This saves resources, as many calls to prepare_links methods perform database queries.

Props Spacedmonkey, timothyblynjacobs, rachelbaker, desrosj, dlh, hellofromTonya.
Fixes #52992.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-application-passwords-controller.php

    r53724 r53760  
    611611        }
    612612
     613        $fields = $this->get_fields_for_response( $request );
     614
    613615        $prepared = array(
    614616            'uuid'      => $item['uuid'],
     
    628630
    629631        $response = new WP_REST_Response( $prepared );
    630         $response->add_links( $this->prepare_links( $user, $item ) );
     632
     633        if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
     634            $response->add_links( $this->prepare_links( $user, $item ) );
     635        }
    631636
    632637        /**
Note: See TracChangeset for help on using the changeset viewer.