Make WordPress Core


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

    r53724 r53760  
    393393                $response = rest_ensure_response( $data );
    394394
    395                 $links = $this->prepare_links( $post->ID );
    396                 $response->add_links( $links );
    397                 if ( ! empty( $links['self']['href'] ) ) {
    398                         $actions = $this->get_available_actions();
    399                         $self    = $links['self']['href'];
    400                         foreach ( $actions as $rel ) {
    401                                 $response->add_link( $rel, $self );
     395                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
     396                        $links = $this->prepare_links( $post->ID );
     397                        $response->add_links( $links );
     398                        if ( ! empty( $links['self']['href'] ) ) {
     399                                $actions = $this->get_available_actions();
     400                                $self    = $links['self']['href'];
     401                                foreach ( $actions as $rel ) {
     402                                        $response->add_link( $rel, $self );
     403                                }
    402404                        }
    403405                }
     
    591593                $response = rest_ensure_response( $data );
    592594
    593                 $links = array(
    594                         'self' => array(
    595                                 'href' => rest_url( sprintf( '%s/%s/themes/%s', $this->namespace, $this->rest_base, $request['stylesheet'] ) ),
    596                         ),
    597                 );
    598 
    599                 $response->add_links( $links );
     595                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
     596                        $links = array(
     597                                'self' => array(
     598                                        'href' => rest_url( sprintf( '%s/%s/themes/%s', $this->namespace, $this->rest_base, $request['stylesheet'] ) ),
     599                                ),
     600                        );
     601                        $response->add_links( $links );
     602                }
    600603
    601604                return $response;
Note: See TracChangeset for help on using the changeset viewer.