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/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
Note: See TracChangeset for help on using the changeset viewer.