From 082cb0ab205fb6787cba9b6ab8096c8070ef20b8 Mon Sep 17 00:00:00 2001 From: Jake Spurlock Date: Thu, 16 Jul 2020 21:42:48 +0000 Subject: [PATCH] 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 --- src/wp-includes/post.php | 7 +++++++ tests/phpunit/tests/post.php | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 097f8186e9..20c4d4da42 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -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 ); } diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index ca18c41b0e..14cfb31d16 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -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 */