Make WordPress Core

Changeset 48497


Ignore:
Timestamp:
07/16/2020 09:42:48 PM (4 years ago)
Author:
whyisjake
Message:

Posts, Post Types: Ensure that all post stati are countable in wp_count_posts.

When wp_count_posts() is cached, it does so with all statuses defaulted to 0. The problem is however, if this is called before all plugins have registered their desired statuses, they won't have that default.

Fixes #49685.

Props obliviousharmony, SergeyBiryukov.

Location:
trunk
Files:
2 edited

Legend:

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

    r48480 r48497  
    26482648    $counts = wp_cache_get( $cache_key, 'counts' );
    26492649    if ( false !== $counts ) {
     2650        // We may have cached this before every status was registered.
     2651        foreach ( get_post_stati() as $status ) {
     2652            if ( ! isset( $counts->{$status} ) ) {
     2653                $counts->{$status} = 0;
     2654            }
     2655        }
     2656
    26502657        /** This filter is documented in wp-includes/post.php */
    26512658        return apply_filters( 'wp_count_posts', $counts, $type, $perm );
  • trunk/tests/phpunit/tests/post.php

    r47431 r48497  
    881881
    882882    /**
     883     * @ticket 49685
     884     */
     885    function test_wp_count_posts_status_changes_visible() {
     886        self::factory()->post->create_many( 3 );
     887
     888        // Trigger a cache.
     889        wp_count_posts();
     890
     891        register_post_status( 'test' );
     892
     893        $counts = wp_count_posts();
     894        $this->assertTrue( isset( $counts->test ) );
     895        $this->assertEquals( 0, $counts->test );
     896    }
     897
     898    /**
    883899     * @ticket 13771
    884900     */
Note: See TracChangeset for help on using the changeset viewer.