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

Allow _embed to include all embedded objects #5758

Closed
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions src/wp-includes/rest-api/class-wp-rest-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,13 @@ protected function embed_links( $data, $embed = true ) {
continue;
}

if ( empty( $request['per_page'] ) ) {
$matched = $this->match_request_to_handler( $request );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@spacedmonkey, this mutates the $request and sets the default context=view, which prevents the code below from setting it to an embed.

This is the cause of navigation fallback regression - https://core.trac.wordpress.org/ticket/43439#comment:17.

if ( ! empty( $matched ) && ! is_wp_error( $matched ) && isset( $matched[1]['args']['per_page']['maximum'] ) ) {
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
$request['per_page'] = (int) $matched[1]['args']['per_page']['maximum'];
}
}

// Embedded resources get passed context=embed.
if ( empty( $request['context'] ) ) {
$request['context'] = 'embed';
Expand Down
18 changes: 18 additions & 0 deletions tests/phpunit/tests/rest-api/rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te

protected static $supported_formats;
protected static $post_ids = array();
protected static $terms = array();
protected static $total_posts = 30;
protected static $per_page = 50;

Expand All @@ -28,6 +29,8 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te

public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$post_id = $factory->post->create();
self::$terms = $factory->term->create_many( 15, array( 'taxonomy' => 'category' ) );
wp_set_object_terms( self::$post_id, self::$terms, 'category' );

self::$superadmin_id = $factory->user->create(
array(
Expand Down Expand Up @@ -223,6 +226,21 @@ public function test_registered_get_item_params() {
$this->assertSame( array( 'context', 'id', 'password' ), $keys );
}

public function test_registered_get_items_embed() {
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_param( 'include', array( self::$post_id ) );
$response = rest_get_server()->dispatch( $request );
$response = rest_get_server()->response_to_data( $response, true );
$this->assertArrayHasKey( '_embedded', $response[0], 'The _embedded key must exist' );
$this->assertArrayHasKey( 'wp:term', $response[0]['_embedded'], 'The wp:term key must exist' );
$this->assertCount( 15, $response[0]['_embedded']['wp:term'][0], 'Should should be 15 terms and not the default 10' );
$i = 0;
foreach ( $response[0]['_embedded']['wp:term'][0] as $term ) {
$this->assertSame( self::$terms[ $i ], $term['id'], 'Check term id existing in response' );
++$i;
}
}

/**
* @ticket 43701
*/
Expand Down
31 changes: 31 additions & 0 deletions tests/phpunit/tests/rest-api/rest-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,37 @@ public function test_link_embedding_without_links() {
$this->assertSame( 'data', $result['untouched'] );
}

/**
* Ensure embedding is with links in the data.
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
*/
public function test_link_embedding_with_links() {
$data = array(
'_links' => array(
'wp:term' => array(
array(
'taxonomy' => 'category',
'embeddable' => true,
'href' => get_rest_url( 0, '/wp/v2/categories' ),
),
array(
'taxonomy' => 'post_tag',
'embeddable' => true,
'href' => get_rest_url( 0, '/wp/v2/tags' ),
),
),
),
);

$mock = new MockAction();
add_filter( 'rest_post_dispatch', array( $mock, 'filter' ), 10, 3 );

rest_get_server()->embed_links( $data, true );
$args = $mock->get_args();
foreach ( $args as $arg ) {
$this->assertSame( 100, $arg[2]['per_page'], 'Posts per page should be 100' );
}
}

/**
* Ensure embed_links handles WP_Error objects returned by dispatch
*
Expand Down
Loading