Make WordPress Core

Changeset 59235


Ignore:
Timestamp:
10/14/2024 10:20:09 PM (7 weeks 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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post-thumbnail-template.php

    r55821 r59235  
    113113    $thumb_ids = array();
    114114
     115    /*
     116     * $wp_query may contain an array of post objects or post IDs.
     117     *
     118     * This ensures the cache is primed for all post objects to avoid
     119     * `get_post()` calls in `get_the_post_thumbnail()` triggering an
     120     * additional database call for each post.
     121     */
     122    $parent_post_ids = array();
    115123    foreach ( $wp_query->posts as $post ) {
    116         $id = get_post_thumbnail_id( $post->ID );
     124        if ( $post instanceof WP_Post ) {
     125            $parent_post_ids[] = $post->ID;
     126        } elseif ( is_int( $post ) ) {
     127            $parent_post_ids[] = $post;
     128        }
     129    }
     130    _prime_post_caches( $parent_post_ids, false, true );
     131
     132    foreach ( $wp_query->posts as $post ) {
     133        $id = get_post_thumbnail_id( $post );
    117134        if ( $id ) {
    118135            $thumb_ids[] = $id;
  • 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
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r59120 r59235  
    17131713        rest_get_server()->dispatch( $request );
    17141714
    1715         $args = $filter->get_args();
    1716         $last = end( $args );
    1717         $this->assertIsArray( $last, 'The last value is not an array' );
    1718         $this->assertSameSets( $parent_ids, $last[1] );
     1715        $events = $filter->get_events();
     1716        $args   = wp_list_pluck( $events, 'args' );
     1717        $primed = false;
     1718        sort( $parent_ids );
     1719        foreach ( $args as $arg ) {
     1720            sort( $arg[1] );
     1721            if ( $parent_ids === $arg[1] ) {
     1722                $primed = $arg;
     1723                break;
     1724            }
     1725        }
     1726
     1727        $this->assertIsArray( $primed, 'The last value is not an array' );
     1728        $this->assertSameSets( $parent_ids, $primed[1] );
    17191729    }
    17201730
Note: See TracChangeset for help on using the changeset viewer.