From 040304def7fc6b5eb73878a8e5d37d872163b8aa Mon Sep 17 00:00:00 2001 From: "K. Adam White" Date: Tue, 8 Oct 2019 04:43:10 +0000 Subject: [PATCH] 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 --- .../rest-api/class-wp-rest-server.php | 5 ---- .../tests/rest-api/rest-posts-controller.php | 1 + .../tests/rest-api/rest-search-controller.php | 14 +++++++++ tests/phpunit/tests/rest-api/rest-server.php | 29 +++++++++++++++++-- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/rest-api/class-wp-rest-server.php b/src/wp-includes/rest-api/class-wp-rest-server.php index 05cc240c02..8b09076da0 100644 --- a/src/wp-includes/rest-api/class-wp-rest-server.php +++ b/src/wp-includes/rest-api/class-wp-rest-server.php @@ -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 ) { diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index fa7396afcc..d1a30c69a4 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -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'] ); diff --git a/tests/phpunit/tests/rest-api/rest-search-controller.php b/tests/phpunit/tests/rest-api/rest-search-controller.php index 60d1ef0bfb..9a3982a701 100644 --- a/tests/phpunit/tests/rest-api/rest-search-controller.php +++ b/tests/phpunit/tests/rest-api/rest-search-controller.php @@ -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. */ diff --git a/tests/phpunit/tests/rest-api/rest-server.php b/tests/phpunit/tests/rest-api/rest-server.php index efb656ed4e..74d957cbe5 100644 --- a/tests/phpunit/tests/rest-api/rest-server.php +++ b/tests/phpunit/tests/rest-api/rest-server.php @@ -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 );