Make WordPress Core


Ignore:
Timestamp:
07/22/2022 01:58:46 PM (3 years 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-controller.php

    r51657 r53760  
    582582        $fields = array_keys( $properties );
    583583
     584        /*
     585         * '_links' and '_embedded' are not typically part of the item schema,
     586         * but they can be specified in '_fields', so they are added here as a
     587         * convenience for checking with rest_is_field_included().
     588         */
     589        $fields[] = '_links';
     590        if ( $request->has_param( '_embed' ) ) {
     591            $fields[] = '_embedded';
     592        }
     593
     594        $fields = array_unique( $fields );
     595
    584596        if ( ! isset( $request['_fields'] ) ) {
    585597            return $fields;
Note: See TracChangeset for help on using the changeset viewer.