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.
Built from https://develop.svn.wordpress.org/trunk@55419


git-svn-id: http://core.svn.wordpress.org/trunk@54952 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2023-02-24 01:23:20 +00:00
parent f51b63c1fe
commit 896f5ef18f
3 changed files with 6 additions and 6 deletions

View File

@ -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;
}

View File

@ -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 );

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.2-beta3-55418';
$wp_version = '6.2-beta3-55419';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.