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.



git-svn-id: https://develop.svn.wordpress.org/trunk@48497 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jake Spurlock 2020-07-16 21:42:48 +00:00
parent ceccb7b3b1
commit 082cb0ab20
2 changed files with 23 additions and 0 deletions

View File

@ -2647,6 +2647,13 @@ function wp_count_posts( $type = 'post', $perm = '' ) {
$counts = wp_cache_get( $cache_key, 'counts' );
if ( false !== $counts ) {
// We may have cached this before every status was registered.
foreach ( get_post_stati() as $status ) {
if ( ! isset( $counts->{$status} ) ) {
$counts->{$status} = 0;
}
}
/** This filter is documented in wp-includes/post.php */
return apply_filters( 'wp_count_posts', $counts, $type, $perm );
}

View File

@ -879,6 +879,22 @@ class Tests_Post extends WP_UnitTestCase {
$this->assertNotEquals( $initial_counts->publish, $after_trash_counts->publish );
}
/**
* @ticket 49685
*/
function test_wp_count_posts_status_changes_visible() {
self::factory()->post->create_many( 3 );
// Trigger a cache.
wp_count_posts();
register_post_status( 'test' );
$counts = wp_count_posts();
$this->assertTrue( isset( $counts->test ) );
$this->assertEquals( 0, $counts->test );
}
/**
* @ticket 13771
*/