From c25c175b470b963694ead8c6b94aa9bdd1085edd Mon Sep 17 00:00:00 2001 From: Jb Audras Date: Thu, 18 Nov 2021 15:10:18 +0000 Subject: [PATCH] Posts, Post Types: Multisite: Decrement `post_count` option value when a post is deleted. Previously, the `post_count` option value was not decremented when a post was deleted. This change moves the `_update_posts_count_on_delete` action from `delete_post` hook to `after_delete_post` to ensure the deletion is taken into account. Props henry.wright, pbearne, audrasjb. Fixes #53443. git-svn-id: https://develop.svn.wordpress.org/trunk@52207 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/ms-default-filters.php | 2 +- .../tests/multisite/updatePostsCount.php | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/tests/multisite/updatePostsCount.php diff --git a/src/wp-includes/ms-default-filters.php b/src/wp-includes/ms-default-filters.php index 042751e491..1b32f7a32b 100644 --- a/src/wp-includes/ms-default-filters.php +++ b/src/wp-includes/ms-default-filters.php @@ -76,7 +76,7 @@ add_filter( 'allowed_redirect_hosts', 'redirect_this_site' ); // Administration. add_filter( 'term_id_filter', 'global_terms', 10, 2 ); -add_action( 'delete_post', '_update_posts_count_on_delete' ); +add_action( 'after_delete_post', '_update_posts_count_on_delete' ); add_action( 'delete_post', '_update_blog_date_on_post_delete' ); add_action( 'transition_post_status', '_update_blog_date_on_post_publish', 10, 3 ); add_action( 'transition_post_status', '_update_posts_count_on_transition_post_status', 10, 3 ); diff --git a/tests/phpunit/tests/multisite/updatePostsCount.php b/tests/phpunit/tests/multisite/updatePostsCount.php new file mode 100644 index 0000000000..2806aaedd5 --- /dev/null +++ b/tests/phpunit/tests/multisite/updatePostsCount.php @@ -0,0 +1,56 @@ +blog->create(); + switch_to_blog( $blog_id ); + + $current_post_count = (int) get_option( 'post_count' ); + + $post_id = self::factory()->post->create( + array( + 'post_type' => 'post', + 'post_author' => '1', + 'post_date' => '2012-10-23 19:34:42', + 'post_status' => 'publish', + ) + ); + + /** + * Check that add_action( 'deleted_post', '_update_posts_count_on_delete' ) is called when a post is created. + * Check that _update_posts_count_on_transition_post_status() is called on that filter which then calls + * update_posts_count to update the count. + */ + $this->assertEquals( $current_post_count + 1, (int) get_option( 'post_count' ), 'post added' ); + + wp_delete_post( $post_id ); + + /** + * Check that add_action( 'transition_post_status', '_update_posts_count_on_transition_post_status', 10, 3 ) + * is called when a post is deleted. + * Check that _update_posts_count_on_delete() is called on that filter which then calls update_posts_count + * to update the count. + */ + $this->assertEquals( $current_post_count, (int) get_option( 'post_count' ), 'post deleted' ); + + restore_current_blog(); + + } + } + +endif;