Make WordPress Core


Ignore:
Timestamp:
10/14/2024 10:20:09 PM (16 months ago)
Author:
peterwilsoncc
Message:

Media: Account for post ID queries in update_post_thumbnail_cache().

Updates update_post_thumbnail_cache() to account for WP_Query objects that only contain the post ID field rather than the entire post object.

This changes passes the $post value to get_post_thumbnail_id() rather than assuming the presence of the ID property. Additionally, the posts to which the thumbnail is attached are now primed prior to calling the function to avoid numerous unnecessary database queries.

The test WP_Test_REST_Posts_Controller::test_get_items_primes_parent_post_caches() is modified to account for an order of operations change for the priming of post meta caches. The cache is no longer primed in the final call to update_meta_cache() so the tests need to account for the post meta to be primed in any call to the function.

Props antpb, jorbin, khokansardar, linsoftware, mukesh27, oglekler, rajinsharwar, sumitsingh, xendo.
Fixes #59521.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/thumbnails.php

    r57105 r59235  
    6969    }
    7070
    71     public function test_update_post_thumbnail_cache() {
    72         set_post_thumbnail( self::$post, self::$attachment_id );
    73 
     71    /**
     72     * Ensure `update_post_thumbnail_cache()` works when querying post objects.
     73     *
     74     * @ticket 59521
     75     * @ticket 30017
     76     * @ticket 33968
     77     *
     78     * @covers ::update_post_thumbnail_cache
     79     */
     80    public function test_update_post_thumbnail_cache_when_querying_full_post_objects() {
     81        set_post_thumbnail( self::$post, self::$attachment_id );
     82
     83        // Test case where `$query->posts` should return Array of post objects.
    7484        $query = new WP_Query(
    7585            array(
     
    8090        );
    8191
    82         $this->assertFalse( $query->thumbnails_cached );
     92        $this->assertFalse( $query->thumbnails_cached, 'Thumbnails should not be cached prior to calling update_post_thumbnail_cache().' );
    8393
    8494        update_post_thumbnail_cache( $query );
    8595
    86         $this->assertTrue( $query->thumbnails_cached );
     96        $this->assertTrue( $query->thumbnails_cached, 'Thumbnails should be cached after calling update_post_thumbnail_cache().' );
     97    }
     98
     99    /**
     100     * Ensure `update_post_thumbnail_cache()` works when querying post IDs.
     101     *
     102     * @ticket 59521
     103     *
     104     * @covers ::update_post_thumbnail_cache
     105     */
     106    public function test_update_post_thumbnail_cache_when_querying_post_id_field() {
     107        set_post_thumbnail( self::$post, self::$attachment_id );
     108
     109        // Test case where `$query2->posts` should return Array of post IDs.
     110        $query = new WP_Query(
     111            array(
     112                'post_type' => 'any',
     113                'post__in'  => array( self::$post->ID ),
     114                'orderby'   => 'post__in',
     115                'fields'    => 'ids',
     116            )
     117        );
     118
     119        $this->assertFalse( $query->thumbnails_cached, 'Thumbnails should not be cached prior to calling update_post_thumbnail_cache().' );
     120
     121        update_post_thumbnail_cache( $query );
     122
     123        $this->assertTrue( $query->thumbnails_cached, 'Thumbnails should be cached after calling update_post_thumbnail_cache().' );
    87124    }
    88125
Note: See TracChangeset for help on using the changeset viewer.