Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST API: Introduce selective link embedding. #149

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/wp-includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1571,3 +1571,25 @@ function rest_preload_api_request( $memo, $path ) {

return $memo;
}

/**
* Parses the "_embed" parameter into the list of resources to embed.
*
* @since 5.4.0
*
* @param string|array $embed Raw "_embed" parameter value.
* @return true|string[] Either true to embed all embeds, or a list of relations to embed.
*/
function rest_parse_embed_param( $embed ) {
if ( ! $embed || 'true' === $embed || '1' === $embed ) {
return true;
}

$rels = wp_parse_list( $embed );

if ( ! $rels ) {
return true;
}

return $rels;
}
23 changes: 17 additions & 6 deletions src/wp-includes/rest-api/class-wp-rest-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@ public function serve_request( $path = null ) {
}

// Embed links inside the request.
$result = $this->response_to_data( $result, isset( $_GET['_embed'] ) );
$embed = isset( $_GET['_embed'] ) ? rest_parse_embed_param( $_GET['_embed'] ) : false;
$result = $this->response_to_data( $result, $embed );

/**
* Filters the API response.
Expand Down Expand Up @@ -450,9 +451,10 @@ public function serve_request( $path = null ) {
* Converts a response to data to send.
*
* @since 4.4.0
* @since 5.4.0 The $embed parameter can now contain a list of link relations to include.
*
* @param WP_REST_Response $response Response object.
* @param bool $embed Whether links should be embedded.
* @param bool|string[] $embed Whether to embed all links, a filtered list of link relations, or no links.
* @return array {
* Data with sub-requests embedded.
*
Expand All @@ -473,9 +475,11 @@ public function response_to_data( $response, $embed ) {
$this->embed_cache = array();
// Determine if this is a numeric array.
if ( wp_is_numeric_array( $data ) ) {
$data = array_map( array( $this, 'embed_links' ), $data );
foreach ( $data as $key => $item ) {
$data[ $key ] = $this->embed_links( $item, $embed );
}
} else {
$data = $this->embed_links( $data );
$data = $this->embed_links( $data, $embed );
}
$this->embed_cache = array();
}
Expand Down Expand Up @@ -571,23 +575,30 @@ public static function get_compact_response_links( $response ) {
* Embeds the links from the data into the request.
*
* @since 4.4.0
* @since 5.4.0 The $embed parameter can now contain a list of link relations to include.
*
* @param array $data Data from the request.
* @param array $data Data from the request.
* @param bool|string[] $embed Whether to embed all links or a filtered list of link relations.
* @return array {
* Data with sub-requests embedded.
*
* @type array [$_links] Links.
* @type array [$_embedded] Embeddeds.
* }
*/
protected function embed_links( $data ) {
protected function embed_links( $data, $embed = true ) {
if ( empty( $data['_links'] ) ) {
return $data;
}

$embedded = array();

foreach ( $data['_links'] as $rel => $links ) {
// If a list of relations was specified, and the link relation is not in the whitelist, don't process the link.
if ( is_array( $embed ) && ! in_array( $rel, $embed, true ) ) {
continue;
}

$embeds = array();

foreach ( $links as $item ) {
Expand Down
27 changes: 27 additions & 0 deletions tests/phpunit/tests/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -906,4 +906,31 @@ function test_rest_ensure_response_accepts_path_string() {
$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' ) ),
);
}
}
60 changes: 60 additions & 0 deletions tests/phpunit/tests/rest-api/rest-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,66 @@ public function test_removing_links_for_href() {
$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(
Expand Down