From c2de42b9adec84397b4c3e5895809f01880a241a Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Tue, 14 Jun 2022 13:08:39 +0000 Subject: [PATCH] REST API: Prime caches for featured images in post controller. Ensure that featured image caches are primed for post collections in the post REST API controller, by calling the `update_post_thumbnail_cache` function. Props Spacedmonkey, TimothyBlynJacobs, mitogh. Fixes #55592. git-svn-id: https://develop.svn.wordpress.org/trunk@53499 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-rest-posts-controller.php | 3 ++ .../tests/rest-api/rest-posts-controller.php | 36 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index d3f5fc18dd..ae203b41aa 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -370,6 +370,9 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { $posts = array(); update_post_author_caches( $query_result ); + if ( post_type_supports( $this->post_type, 'thumbnail' ) ) { + update_post_thumbnail_cache( $posts_query ); + } foreach ( $query_result as $post ) { if ( ! $this->check_read_permission( $post ) ) { diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index 64d8ddbc90..e4d7b5de4b 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -1514,6 +1514,42 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertPostsWhere( " AND {posts}.ID NOT IN ($id3) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" ); } + /** + * @ticket 55592 + * @covers WP_REST_Posts_Controller::get_items() + */ + public function test_get_items_with_featured_media() { + $file = DIR_TESTDATA . '/images/canola.jpg'; + $attachment_ids = array(); + $post_ids = array(); + for ( $i = 0; $i < 3; $i++ ) { + $post_ids[ $i ] = self::factory()->post->create( array( 'post_status' => 'publish' ) ); + $attachment_ids[ $i ] = self::factory()->attachment->create_object( + $file, + $post_ids[ $i ], + array( + 'post_mime_type' => 'image/jpeg', + ) + ); + set_post_thumbnail( $post_ids[ $i ], $attachment_ids[ $i ] ); + } + + // Attachment creation warms thumbnail ids. Needs clean up for test. + wp_cache_delete_multiple( $attachment_ids, 'posts' ); + + $filter = new MockAction(); + add_filter( 'update_post_metadata_cache', array( $filter, 'filter' ), 10, 2 ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $request->set_param( 'include', $post_ids ); + rest_get_server()->dispatch( $request ); + + $args = $filter->get_args(); + $last = end( $args ); + $this->assertIsArray( $last, 'The last value is not an array' ); + $this->assertEqualSets( $attachment_ids, $last[1] ); + } + public function test_get_items_pagination_headers() { $total_posts = self::$total_posts; $total_pages = (int) ceil( $total_posts / 10 );