mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 22:30:04 +00:00
REST API: Skip processing fields which are not present in the selected context.
In `WP_REST_Controller::get_fields_for_response()`, exclude fields which are not registered to appear in the request's context. In conjunction with r45705 this prevents the unnecessary computation of the sample permalink when making a request that is not context=edit. Props dlh. Fixes #45605. git-svn-id: https://develop.svn.wordpress.org/trunk@45706 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -255,6 +255,94 @@ class WP_Test_REST_Controller extends WP_Test_REST_TestCase {
|
||||
);
|
||||
}
|
||||
|
||||
public function test_get_fields_for_response_filters_by_context() {
|
||||
$controller = new WP_REST_Test_Controller();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/testroute' );
|
||||
$request->set_param( 'context', 'view' );
|
||||
|
||||
$schema = $controller->get_item_schema();
|
||||
$field = 'somefield';
|
||||
|
||||
$listener = new MockAction();
|
||||
$method = 'action';
|
||||
|
||||
register_rest_field(
|
||||
$schema['title'],
|
||||
$field,
|
||||
array(
|
||||
'schema' => array(
|
||||
'type' => 'string',
|
||||
'context' => array( 'embed' ),
|
||||
),
|
||||
'get_callback' => array( $listener, $method ),
|
||||
)
|
||||
);
|
||||
|
||||
$controller->prepare_item_for_response( array(), $request );
|
||||
|
||||
$this->assertSame( 0, $listener->get_call_count( $method ) );
|
||||
|
||||
$request->set_param( 'context', 'embed' );
|
||||
|
||||
$controller->prepare_item_for_response( array(), $request );
|
||||
|
||||
$this->assertGreaterThan( 0, $listener->get_call_count( $method ) );
|
||||
}
|
||||
|
||||
public function test_filtering_fields_for_response_by_context_returns_fields_with_no_context() {
|
||||
$controller = new WP_REST_Test_Controller();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/testroute' );
|
||||
$request->set_param( 'context', 'view' );
|
||||
|
||||
$schema = $controller->get_item_schema();
|
||||
$field = 'somefield';
|
||||
|
||||
$listener = new MockAction();
|
||||
$method = 'action';
|
||||
|
||||
register_rest_field(
|
||||
$schema['title'],
|
||||
$field,
|
||||
array(
|
||||
'schema' => array(
|
||||
'type' => 'string',
|
||||
),
|
||||
'get_callback' => array( $listener, $method ),
|
||||
)
|
||||
);
|
||||
|
||||
$controller->prepare_item_for_response( array(), $request );
|
||||
|
||||
$this->assertGreaterThan( 0, $listener->get_call_count( $method ) );
|
||||
}
|
||||
|
||||
public function test_filtering_fields_for_response_by_context_returns_fields_with_no_schema() {
|
||||
$controller = new WP_REST_Test_Controller();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/testroute' );
|
||||
$request->set_param( 'context', 'view' );
|
||||
|
||||
$schema = $controller->get_item_schema();
|
||||
$field = 'somefield';
|
||||
|
||||
$listener = new MockAction();
|
||||
$method = 'action';
|
||||
|
||||
register_rest_field(
|
||||
$schema['title'],
|
||||
$field,
|
||||
array(
|
||||
'get_callback' => array( $listener, $method ),
|
||||
)
|
||||
);
|
||||
|
||||
$controller->prepare_item_for_response( array(), $request );
|
||||
|
||||
$this->assertGreaterThan( 0, $listener->get_call_count( $method ) );
|
||||
}
|
||||
|
||||
public function test_add_additional_fields_to_object_respects_fields_param() {
|
||||
$controller = new WP_REST_Test_Controller();
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/testroute' );
|
||||
|
||||
Reference in New Issue
Block a user