REST API: Permit embedding of the 'self' link relation in the /search endpoint.

Removes a special-case prohibition against embedding 'self' which prevented ?_embed from being used with the /wp/v2/search endpoint.

Props TimothyBlynJacobs, chrisvanpatten, kadamwhite.
Fixes #47684.



git-svn-id: https://develop.svn.wordpress.org/trunk@46434 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
K. Adam White 2019-10-08 04:43:10 +00:00
parent e17e5aab68
commit 040304def7
4 changed files with 42 additions and 7 deletions

View File

@ -564,11 +564,6 @@ class WP_REST_Server {
$embedded = array();
foreach ( $data['_links'] as $rel => $links ) {
// Ignore links to self, for obvious reasons.
if ( 'self' === $rel ) {
continue;
}
$embeds = array();
foreach ( $links as $item ) {

View File

@ -1363,6 +1363,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$this->assertEquals( rest_url( '/wp/v2/posts/' . self::$post_id ), $links['self'][0]['href'] );
$this->assertEquals( rest_url( '/wp/v2/posts' ), $links['collection'][0]['href'] );
$this->assertArrayNotHasKey( 'embeddable', $links['self'][0]['attributes'] );
$this->assertEquals( rest_url( '/wp/v2/types/' . get_post_type( self::$post_id ) ), $links['about'][0]['href'] );

View File

@ -500,6 +500,20 @@ class WP_Test_REST_Search_Controller extends WP_Test_REST_Controller_Testcase {
$this->assertEqualSets( array( 'test_first_type', 'test_second_type', WP_REST_Search_Controller::TYPE_ANY ), $params[ WP_REST_Search_Controller::PROP_SUBTYPE ]['items']['enum'] );
}
/**
* @ticket 47684
*/
public function test_search_result_links_are_embedded() {
$response = $this->do_request_with_params( array( 'per_page' => 1 ) );
$data = rest_get_server()->response_to_data( $response, true )[0];
$this->assertArrayHasKey( '_embedded', $data );
$this->assertArrayHasKey( 'self', $data['_embedded'] );
$this->assertCount( 1, $data['_embedded']['self'] );
$this->assertArrayHasKey( WP_REST_Search_Controller::PROP_ID, $data['_embedded']['self'][0] );
$this->assertEquals( $data[ WP_REST_Search_Controller::PROP_ID ], $data['_embedded']['self'][0][ WP_REST_Search_Controller::PROP_ID ] );
}
/**
* Perform a REST request to our search endpoint with given parameters.
*/

View File

@ -569,6 +569,7 @@ class Tests_REST_Server extends WP_Test_REST_TestCase {
/**
* @depends test_link_embedding
* @ticket 47684
*/
public function test_link_embedding_self() {
// Register our testing route.
@ -582,8 +583,32 @@ class Tests_REST_Server extends WP_Test_REST_TestCase {
);
$response = new WP_REST_Response();
// 'self' should be ignored.
$response->add_link( 'self', rest_url( '/test/notembeddable' ), array( 'embeddable' => true ) );
// 'self' should not be special-cased, and may be marked embeddable.
$response->add_link( 'self', rest_url( '/test/embeddable' ), array( 'embeddable' => true ) );
$data = rest_get_server()->response_to_data( $response, true );
$this->assertArrayHasKey( '_embedded', $data );
}
/**
* @depends test_link_embedding
* @ticket 47684
*/
public function test_link_embedding_self_non_embeddable() {
// Register our testing route.
rest_get_server()->register_route(
'test',
'/test/embeddable',
array(
'methods' => 'GET',
'callback' => array( $this, 'embedded_response_callback' ),
)
);
$response = new WP_REST_Response();
// 'self' should not be special-cased, and should be ignored if not marked embeddable.
$response->add_link( 'self', rest_url( '/test/notembeddable' ) );
$data = rest_get_server()->response_to_data( $response, true );