REST API: Introduce selective link embedding.

Previously the _embed flag would embed all embeddable links in a response even if only a subset of the links were necessary. Now, a list of link relations can be passed in the _embed parameter to restrict the list of embedded objects.

Props rheinardkorf, adamsilverstein, jnylen0, cklosows, chrisvanpatten, TimothyBlynJacobs.
Fixes #39696.


git-svn-id: https://develop.svn.wordpress.org/trunk@47224 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Timothy Jacobs
2020-02-09 20:52:06 +00:00
parent c647007a82
commit 98e5dd52de
4 changed files with 126 additions and 6 deletions
+27
View File
@@ -906,4 +906,31 @@ class Tests_REST_API extends WP_UnitTestCase {
$this->assertEquals( '/wp/v2/posts', $request->get_route() );
$this->assertEquals( 'GET', $request->get_method() );
}
/**
* @dataProvider _dp_rest_parse_embed_param
*/
public function test_rest_parse_embed_param( $expected, $embed ) {
$this->assertEquals( $expected, rest_parse_embed_param( $embed ) );
}
public function _dp_rest_parse_embed_param() {
return array(
array( true, '' ),
array( true, null ),
array( true, '1' ),
array( true, 'true' ),
array( true, array() ),
array( array( 'author' ), 'author' ),
array( array( 'author', 'replies' ), 'author,replies' ),
array( array( 'author', 'replies' ), 'author,replies ' ),
array( array( 'wp:term' ), 'wp:term' ),
array( array( 'wp:term', 'wp:attachment' ), 'wp:term,wp:attachment' ),
array( array( 'author' ), array( 'author' ) ),
array( array( 'author', 'replies' ), array( 'author', 'replies' ) ),
array( array( 'https://api.w.org/term' ), 'https://api.w.org/term' ),
array( array( 'https://api.w.org/term', 'https://api.w.org/attachment' ), 'https://api.w.org/term,https://api.w.org/attachment' ),
array( array( 'https://api.w.org/term', 'https://api.w.org/attachment' ), array( 'https://api.w.org/term', 'https://api.w.org/attachment' ) ),
);
}
}
@@ -845,6 +845,66 @@ class Tests_REST_Server extends WP_Test_REST_TestCase {
$this->assertEquals( $self_not_filtered, $data['_links']['self'][0] );
}
/**
* @dataProvider _dp_response_to_data_embedding
*/
public function test_response_to_data_embedding( $expected, $embed ) {
$response = new WP_REST_Response();
$response->add_link( 'author', rest_url( '404' ), array( 'embeddable' => true ) );
$response->add_link( 'https://api.w.org/term', rest_url( '404' ), array( 'embeddable' => true ) );
$response->add_link( 'https://wordpress.org', rest_url( '404' ), array( 'embeddable' => true ) );
$response->add_link( 'no-embed', rest_url( '404' ) );
$data = rest_get_server()->response_to_data( $response, $embed );
if ( false === $expected ) {
$this->assertArrayNotHasKey( '_embedded', $data );
} else {
$this->assertEqualSets( $expected, array_keys( $data['_embedded'] ) );
}
}
public function _dp_response_to_data_embedding() {
return array(
array(
array( 'author', 'wp:term', 'https://wordpress.org' ),
true,
),
array(
array( 'author', 'wp:term', 'https://wordpress.org' ),
array( 'author', 'wp:term', 'https://wordpress.org' ),
),
array(
array( 'author' ),
array( 'author' ),
),
array(
array( 'wp:term' ),
array( 'wp:term' ),
),
array(
array( 'https://wordpress.org' ),
array( 'https://wordpress.org' ),
),
array(
array( 'author', 'wp:term' ),
array( 'author', 'wp:term' ),
),
array(
false,
false,
),
array(
false,
array( 'no-embed' ),
),
array(
array( 'author' ),
array( 'author', 'no-embed' ),
),
);
}
public function test_get_index() {
$server = new WP_REST_Server();
$server->register_route(