From b394c9c144f17903771af3963200c8585cb0611f Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 24 Feb 2023 01:21:54 +0000 Subject: [PATCH] Posts, Post Types: Pass the post object to `_update_posts_count_on_delete()`. The function checks the status of the post being deleted, and then only calls `update_posts_count()` if the deleted post was previously published, as the update query would be unnecessary otherwise. However, by the time the function runs, the post is already deleted from the database, and the post status check fails. This commit uses the previously retrieved post object for the status check, so that the function proceeds as expected. Includes updating the unit test to call `wp_delete_post()` with the `$force_delete` argument, so that the post is actually deleted, not trashed, and the `after_delete_post` action is run. Follow-up to [28835], [52207], [54760], [54762]. Fixes #57023. git-svn-id: https://develop.svn.wordpress.org/trunk@55419 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/ms-blogs.php | 8 ++++---- src/wp-includes/ms-default-filters.php | 2 +- tests/phpunit/tests/multisite/updatePostsCount.php | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/ms-blogs.php b/src/wp-includes/ms-blogs.php index 19594eef6b..c5ef1c157c 100644 --- a/src/wp-includes/ms-blogs.php +++ b/src/wp-includes/ms-blogs.php @@ -877,12 +877,12 @@ function _update_blog_date_on_post_delete( $post_id ) { * Handler for updating the current site's posts count when a post is deleted. * * @since 4.0.0 + * @since 6.2.0 Added the `$post` parameter. * - * @param int $post_id Post ID. + * @param int $post_id Post ID. + * @param WP_Post $post Post object. */ -function _update_posts_count_on_delete( $post_id ) { - $post = get_post( $post_id ); - +function _update_posts_count_on_delete( $post_id, $post ) { if ( ! $post || 'publish' !== $post->post_status || 'post' !== $post->post_type ) { return; } diff --git a/src/wp-includes/ms-default-filters.php b/src/wp-includes/ms-default-filters.php index c0af7215ff..8682d48e18 100644 --- a/src/wp-includes/ms-default-filters.php +++ b/src/wp-includes/ms-default-filters.php @@ -75,7 +75,7 @@ add_action( 'template_redirect', 'maybe_redirect_404' ); add_filter( 'allowed_redirect_hosts', 'redirect_this_site' ); // Administration. -add_action( 'after_delete_post', '_update_posts_count_on_delete' ); +add_action( 'after_delete_post', '_update_posts_count_on_delete', 10, 2 ); 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 index 270ac60c49..bf3fcb879f 100644 --- a/tests/phpunit/tests/multisite/updatePostsCount.php +++ b/tests/phpunit/tests/multisite/updatePostsCount.php @@ -30,7 +30,7 @@ if ( is_multisite() ) : $post_count_after_creating = get_site()->post_count; - wp_delete_post( $post_id ); + wp_delete_post( $post_id, true ); $post_count_after_deleting = get_site()->post_count; @@ -47,7 +47,7 @@ if ( is_multisite() ) : /* * Check that posts count is updated when a post is deleted: - * add_action( 'deleted_post', '_update_posts_count_on_delete' ); + * add_action( 'after_delete_post', '_update_posts_count_on_delete', 10, 2 ); * * Check that _update_posts_count_on_delete() is called on that filter, * which then calls update_posts_count() to update the count.