Make WordPress Core

Changeset 53499


Ignore:
Timestamp:
06/14/2022 01:08:39 PM (4 years ago)
Author:
spacedmonkey
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r53498 r53499  
    371371
    372372                update_post_author_caches( $query_result );
     373                if ( post_type_supports( $this->post_type, 'thumbnail' ) ) {
     374                        update_post_thumbnail_cache( $posts_query );
     375                }
    373376
    374377                foreach ( $query_result as $post ) {
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r53498 r53499  
    15131513
    15141514                $this->assertPostsWhere( " AND {posts}.ID NOT IN ($id3) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" );
     1515        }
     1516
     1517        /**
     1518         * @ticket 55592
     1519         * @covers WP_REST_Posts_Controller::get_items()
     1520         */
     1521        public function test_get_items_with_featured_media() {
     1522                $file           = DIR_TESTDATA . '/images/canola.jpg';
     1523                $attachment_ids = array();
     1524                $post_ids       = array();
     1525                for ( $i = 0; $i < 3; $i++ ) {
     1526                        $post_ids[ $i ]       = self::factory()->post->create( array( 'post_status' => 'publish' ) );
     1527                        $attachment_ids[ $i ] = self::factory()->attachment->create_object(
     1528                                $file,
     1529                                $post_ids[ $i ],
     1530                                array(
     1531                                        'post_mime_type' => 'image/jpeg',
     1532                                )
     1533                        );
     1534                        set_post_thumbnail( $post_ids[ $i ], $attachment_ids[ $i ] );
     1535                }
     1536
     1537                // Attachment creation warms thumbnail ids. Needs clean up for test.
     1538                wp_cache_delete_multiple( $attachment_ids, 'posts' );
     1539
     1540                $filter = new MockAction();
     1541                add_filter( 'update_post_metadata_cache', array( $filter, 'filter' ), 10, 2 );
     1542
     1543                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     1544                $request->set_param( 'include', $post_ids );
     1545                rest_get_server()->dispatch( $request );
     1546
     1547                $args = $filter->get_args();
     1548                $last = end( $args );
     1549                $this->assertIsArray( $last, 'The last value is not an array' );
     1550                $this->assertEqualSets( $attachment_ids, $last[1] );
    15151551        }
    15161552
Note: See TracChangeset for help on using the changeset viewer.