Make WordPress Core


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

    r53759 r53760  
    19341934        $response = rest_ensure_response( $data );
    19351935
    1936         $links = $this->prepare_links( $post );
    1937         $response->add_links( $links );
    1938 
    1939         if ( ! empty( $links['self']['href'] ) ) {
    1940             $actions = $this->get_available_actions( $post, $request );
    1941 
    1942             $self = $links['self']['href'];
    1943 
    1944             foreach ( $actions as $rel ) {
    1945                 $response->add_link( $rel, $self );
     1936        if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
     1937            $links = $this->prepare_links( $post );
     1938            $response->add_links( $links );
     1939
     1940            if ( ! empty( $links['self']['href'] ) ) {
     1941                $actions = $this->get_available_actions( $post, $request );
     1942
     1943                $self = $links['self']['href'];
     1944
     1945                foreach ( $actions as $rel ) {
     1946                    $response->add_link( $rel, $self );
     1947                }
    19461948            }
    19471949        }
Note: See TracChangeset for help on using the changeset viewer.