Make WordPress Core

Changeset 55419


Ignore:
Timestamp:
02/24/2023 01:21:54 AM (16 months ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/ms-blogs.php

    r55398 r55419  
    878878 *
    879879 * @since 4.0.0
    880  *
    881  * @param int $post_id Post ID.
    882  */
    883 function _update_posts_count_on_delete( $post_id ) {
    884     $post = get_post( $post_id );
    885 
     880 * @since 6.2.0 Added the `$post` parameter.
     881 *
     882 * @param int     $post_id Post ID.
     883 * @param WP_Post $post    Post object.
     884 */
     885function _update_posts_count_on_delete( $post_id, $post ) {
    886886    if ( ! $post || 'publish' !== $post->post_status || 'post' !== $post->post_type ) {
    887887        return;
  • trunk/src/wp-includes/ms-default-filters.php

    r54240 r55419  
    7676
    7777// Administration.
    78 add_action( 'after_delete_post', '_update_posts_count_on_delete' );
     78add_action( 'after_delete_post', '_update_posts_count_on_delete', 10, 2 );
    7979add_action( 'delete_post', '_update_blog_date_on_post_delete' );
    8080add_action( 'transition_post_status', '_update_blog_date_on_post_publish', 10, 3 );
  • trunk/tests/phpunit/tests/multisite/updatePostsCount.php

    r54762 r55419  
    3131            $post_count_after_creating = get_site()->post_count;
    3232
    33             wp_delete_post( $post_id );
     33            wp_delete_post( $post_id, true );
    3434
    3535            $post_count_after_deleting = get_site()->post_count;
     
    4848            /*
    4949             * Check that posts count is updated when a post is deleted:
    50              * add_action( 'deleted_post', '_update_posts_count_on_delete' );
     50             * add_action( 'after_delete_post', '_update_posts_count_on_delete', 10, 2 );
    5151             *
    5252             * Check that _update_posts_count_on_delete() is called on that filter,
Note: See TracChangeset for help on using the changeset viewer.