From d1ffc367e691a64a0d4bf0c8f5bd3645ad1fd308 Mon Sep 17 00:00:00 2001 From: Lukas Pawlik Date: Thu, 29 Oct 2020 15:08:19 +0100 Subject: [PATCH 1/4] Ensure backward compatibility --- src/wp-includes/rest-api/class-wp-rest-server.php | 6 ++++++ 1 file changed, 6 insertions(+) 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 3eeefda167e12..955c708507a94 100644 --- a/src/wp-includes/rest-api/class-wp-rest-server.php +++ b/src/wp-includes/rest-api/class-wp-rest-server.php @@ -716,6 +716,12 @@ protected function embed_links( $data, $embed = true ) { continue; } + $matched = $this->match_request_to_handler( $request ); + + if ( ! empty( $matched ) && ! is_wp_error( $matched ) && isset( $matched[1]['args']['per_page']['maximum'] ) ) { + $request['per_page'] = (int) $matched[1]['args']['per_page']['maximum']; + } + // Embedded resources get passed context=embed. if ( empty( $request['context'] ) ) { $request['context'] = 'embed'; From 69c55e3d2b805eb86281c3e66c286bc1aa9c5e3f Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Mon, 11 Dec 2023 23:11:02 +0000 Subject: [PATCH 2/4] Add unit tests. --- .../rest-api/class-wp-rest-server.php | 9 +++--- .../tests/rest-api/rest-posts-controller.php | 18 +++++++++++ tests/phpunit/tests/rest-api/rest-server.php | 31 +++++++++++++++++++ 3 files changed, 54 insertions(+), 4 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 0c0a89401a92b..004eee596cff7 100644 --- a/src/wp-includes/rest-api/class-wp-rest-server.php +++ b/src/wp-includes/rest-api/class-wp-rest-server.php @@ -735,10 +735,11 @@ protected function embed_links( $data, $embed = true ) { continue; } - $matched = $this->match_request_to_handler( $request ); - - if ( ! empty( $matched ) && ! is_wp_error( $matched ) && isset( $matched[1]['args']['per_page']['maximum'] ) ) { - $request['per_page'] = (int) $matched[1]['args']['per_page']['maximum']; + if ( empty( $request['per_page'] ) ) { + $matched = $this->match_request_to_handler( $request ); + if ( ! empty( $matched ) && ! is_wp_error( $matched ) && isset( $matched[1]['args']['per_page']['maximum'] ) ) { + $request['per_page'] = (int) $matched[1]['args']['per_page']['maximum']; + } } // Embedded resources get passed context=embed. diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index f1af2f3959937..2d1d9130ca236 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -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; @@ -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( @@ -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() { + $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 */ diff --git a/tests/phpunit/tests/rest-api/rest-server.php b/tests/phpunit/tests/rest-api/rest-server.php index a49a6859a8ebb..d72292f593f13 100644 --- a/tests/phpunit/tests/rest-api/rest-server.php +++ b/tests/phpunit/tests/rest-api/rest-server.php @@ -931,6 +931,37 @@ public function test_link_embedding_without_links() { $this->assertSame( 'data', $result['untouched'] ); } + /** + * Ensure embedding is with links in the data. + */ + 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 * From b798c910087a168aac7b57d4006d42a061023926 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Thu, 25 Jan 2024 10:41:47 +0000 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: Mukesh Panchal --- tests/phpunit/tests/rest-api/rest-server.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/tests/rest-api/rest-server.php b/tests/phpunit/tests/rest-api/rest-server.php index d72292f593f13..8d05192bc1fd9 100644 --- a/tests/phpunit/tests/rest-api/rest-server.php +++ b/tests/phpunit/tests/rest-api/rest-server.php @@ -933,6 +933,8 @@ public function test_link_embedding_without_links() { /** * Ensure embedding is with links in the data. + * + * @ticket 43439 */ public function test_link_embedding_with_links() { $data = array( From 5da631691f5b2a35d004e3dc634c2a32ced7dc80 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Tue, 13 Feb 2024 12:20:39 +0000 Subject: [PATCH 4/4] Update src/wp-includes/rest-api/class-wp-rest-server.php Co-authored-by: Timothy Jacobs --- src/wp-includes/rest-api/class-wp-rest-server.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 3b03da1acc694..380eea2ca326f 100644 --- a/src/wp-includes/rest-api/class-wp-rest-server.php +++ b/src/wp-includes/rest-api/class-wp-rest-server.php @@ -745,7 +745,7 @@ protected function embed_links( $data, $embed = true ) { if ( empty( $request['per_page'] ) ) { $matched = $this->match_request_to_handler( $request ); - if ( ! empty( $matched ) && ! is_wp_error( $matched ) && isset( $matched[1]['args']['per_page']['maximum'] ) ) { + if ( ! is_wp_error( $matched ) && isset( $matched[1]['args']['per_page']['maximum'] ) ) { $request['per_page'] = (int) $matched[1]['args']['per_page']['maximum']; } }