Make WordPress Core

Changeset 53760


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.

Location:
trunk
Files:
23 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                /**
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-directory-controller.php

    r53315 r53760  
    119119                // Restores the more descriptive, specific name for use within this method.
    120120                $plugin = $item;
     121
     122                $fields = $this->get_fields_for_response( $request );
    121123
    122124                // There might be multiple blocks in a plugin. Only the first block is mapped.
    … …  
    147149
    148150                $response = new WP_REST_Response( $block );
    149                 $response->add_links( $this->prepare_links( $plugin ) );
     151
     152                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
     153                        $response->add_links( $this->prepare_links( $plugin ) );
     154                }
    150155
    151156                return $response;
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php

    r53724 r53760  
    307307                $response = rest_ensure_response( $data );
    308308
    309                 $response->add_links( $this->prepare_links( $block_type ) );
     309                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
     310                        $response->add_links( $this->prepare_links( $block_type ) );
     311                }
    310312
    311313                /**
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    r53729 r53760  
    11201120                $response = rest_ensure_response( $data );
    11211121
    1122                 $response->add_links( $this->prepare_links( $comment ) );
     1122                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
     1123                        $response->add_links( $this->prepare_links( $comment ) );
     1124                }
    11231125
    11241126                /**
  • 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;
  • 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;
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php

    r53679 r53760  
    611611                $response = rest_ensure_response( $data );
    612612
    613                 $links = $this->prepare_links( $item );
    614                 $response->add_links( $links );
    615 
    616                 if ( ! empty( $links['self']['href'] ) ) {
    617                         $actions = $this->get_available_actions( $item, $request );
    618 
    619                         $self = $links['self']['href'];
    620 
    621                         foreach ( $actions as $rel ) {
    622                                 $response->add_link( $rel, $self );
     613                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
     614                        $links = $this->prepare_links( $item );
     615                        $response->add_links( $links );
     616
     617                        if ( ! empty( $links['self']['href'] ) ) {
     618                                $actions = $this->get_available_actions( $item, $request );
     619
     620                                $self = $links['self']['href'];
     621
     622                                foreach ( $actions as $rel ) {
     623                                        $response->add_link( $rel, $self );
     624                                }
    623625                        }
    624626                }
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menu-locations-controller.php

    r53724 r53760  
    194194                $response = rest_ensure_response( $data );
    195195
    196                 $response->add_links( $this->prepare_links( $location ) );
     196                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
     197                        $response->add_links( $this->prepare_links( $location ) );
     198                }
    197199
    198200                /**
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menus-controller.php

    r52079 r53760  
    135135
    136136                $response = rest_ensure_response( $data );
    137                 $response->add_links( $this->prepare_links( $term ) );
     137
     138                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
     139                        $response->add_links( $this->prepare_links( $term ) );
     140                }
    138141
    139142                /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-plugins-controller.php

    r53724 r53760  
    577577         */
    578578        public function prepare_item_for_response( $item, $request ) {
     579                $fields = $this->get_fields_for_response( $request );
     580
    579581                $item   = _get_plugin_data_markup_translate( $item['_file'], $item, false );
    580582                $marked = _get_plugin_data_markup_translate( $item['_file'], $item, true );
    … …  
    601603
    602604                $response = new WP_REST_Response( $data );
    603                 $response->add_links( $this->prepare_links( $item ) );
     605
     606                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
     607                        $response->add_links( $this->prepare_links( $item ) );
     608                }
    604609
    605610                /**
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php

    r53724 r53760  
    245245                $response = rest_ensure_response( $data );
    246246
    247                 $response->add_links( $this->prepare_links( $post_type ) );
     247                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
     248                        $response->add_links( $this->prepare_links( $post_type ) );
     249                }
    248250
    249251                /**
  • 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                }
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php

    r51786 r53760  
    211211                $response = rest_ensure_response( $data );
    212212
    213                 $links               = $handler->prepare_item_links( $item_id );
    214                 $links['collection'] = array(
    215                         'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
    216                 );
    217                 $response->add_links( $links );
     213                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
     214                        $links               = $handler->prepare_item_links( $item_id );
     215                        $links['collection'] = array(
     216                                'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
     217                        );
     218                        $response->add_links( $links );
     219                }
    218220
    219221                return $response;
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php

    r52362 r53760  
    369369                $response = rest_ensure_response( $data );
    370370
    371                 $response->add_links( $this->prepare_links( $sidebar ) );
     371                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
     372                        $response->add_links( $this->prepare_links( $sidebar ) );
     373                }
    372374
    373375                /**
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php

    r53724 r53760  
    273273                $response = rest_ensure_response( $data );
    274274
    275                 $response->add_links( $this->prepare_links( $taxonomy ) );
     275                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
     276                        $response->add_links( $this->prepare_links( $taxonomy ) );
     277                }
    276278
    277279                /**
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php

    r52434 r53760  
    663663                $response = rest_ensure_response( $data );
    664664
    665                 $links = $this->prepare_links( $template->id );
    666                 $response->add_links( $links );
    667                 if ( ! empty( $links['self']['href'] ) ) {
    668                         $actions = $this->get_available_actions();
    669                         $self    = $links['self']['href'];
    670                         foreach ( $actions as $rel ) {
    671                                 $response->add_link( $rel, $self );
     665                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
     666                        $links = $this->prepare_links( $template->id );
     667                        $response->add_links( $links );
     668                        if ( ! empty( $links['self']['href'] ) ) {
     669                                $actions = $this->get_available_actions();
     670                                $self    = $links['self']['href'];
     671                                foreach ( $actions as $rel ) {
     672                                        $response->add_link( $rel, $self );
     673                                }
    672674                        }
    673675                }
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

    r52068 r53760  
    860860                $response = rest_ensure_response( $data );
    861861
    862                 $response->add_links( $this->prepare_links( $item ) );
     862                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
     863                        $response->add_links( $this->prepare_links( $item ) );
     864                }
    863865
    864866                /**
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php

    r53544 r53760  
    332332                $response = rest_ensure_response( $data );
    333333
    334                 $response->add_links( $this->prepare_links( $theme ) );
     334                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
     335                        $response->add_links( $this->prepare_links( $theme ) );
     336                }
    335337
    336338                /**
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    r52978 r53760  
    10731073                $response = rest_ensure_response( $data );
    10741074
    1075                 $response->add_links( $this->prepare_links( $user ) );
     1075                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
     1076                        $response->add_links( $this->prepare_links( $user ) );
     1077                }
    10761078
    10771079                /**
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php

    r52278 r53760  
    336336                $response = rest_ensure_response( $data );
    337337
    338                 $response->add_links( $this->prepare_links( $widget_type ) );
     338                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
     339                        $response->add_links( $this->prepare_links( $widget_type ) );
     340                }
    339341
    340342                /**
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php

    r52362 r53760  
    727727                $response = rest_ensure_response( $prepared );
    728728
    729                 $response->add_links( $this->prepare_links( $prepared ) );
     729                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
     730                        $response->add_links( $this->prepare_links( $prepared ) );
     731                }
    730732
    731733                /**
  • trunk/tests/phpunit/tests/rest-api/rest-controller.php

    r51908 r53760  
    387387                                'somearray',
    388388                                'someobject',
     389                                '_links',
    389390                        ),
    390391                        $fields
    … …  
    422423                                        'somearray',
    423424                                        'someobject',
    424                                 ),
    425                         ),
    426                 );
     425                                        '_links',
     426                                ),
     427                        ),
     428                );
     429        }
     430
     431        public function test_get_fields_for_response_respects_embed() {
     432                $controller = new WP_REST_Test_Controller();
     433                $request    = new WP_REST_Request( 'GET', '/wp/v2/testroute' );
     434
     435                $this->assertNotContains( '_embedded', $controller->get_fields_for_response( $request ) );
     436
     437                $request->set_param( '_embed', 1 );
     438
     439                $this->assertContains( '_embedded', $controller->get_fields_for_response( $request ) );
    427440        }
    428441
  • trunk/tests/phpunit/tests/rest-api/rest-search-controller.php

    r53485 r53760  
    444444                                'id',
    445445                                'title',
    446                                 '_links',
    447446                        ),
    448447                        array_keys( $data[0] )
Note: See TracChangeset for help on using the changeset viewer.