Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#47773 closed defect (bug) (fixed)

wp_trash_post() ignores errors from wp_update_post()

Reported by: siliconforks's profile siliconforks Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 5.3 Priority: normal
Severity: normal Version: 5.2.2
Component: Posts, Post Types Keywords: has-patch
Focuses: Cc:

Description

The documentation for wp_trash_post() states that it returns "false or null on failure". It does indeed do that for some failure conditions, but it calls wp_update_post() without checking its return value, so it just ignores any errors which occur there.

https://core.trac.wordpress.org/browser/tags/5.2.2/src/wp-includes/post.php?rev=45549#L2985

One of the consequences of this is that, in the admin section, if a user trashes a post and the wp_update_post() call fails for some reason, the success message "1 post moved to the Trash" will be displayed even though the post did not actually get moved to the trash.

Attachments (2)

47773.diff (683 bytes) - added by abhijitrakas 5 years ago.
Add check for post status update failure
47773.1.diff (597 bytes) - added by abhijitrakas 5 years ago.
Modify code to detect false value on post update

Download all attachments as: .zip

Change History (8)

#1 @SergeyBiryukov
5 years ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release

@abhijitrakas
5 years ago

Add check for post status update failure

#2 @abhijitrakas
5 years ago

  • Keywords has-patch added; needs-patch removed

#3 @manzoorwani.jk
5 years ago

@abhijitrakas You could simply pass the second argument as false to wp_update_post to make it not return WP_Error and then check only for the false value, like this

<?php
$post_updated = wp_update_post(
        array(
                'ID'          => $post_id,
                'post_status' => 'trash',
        ),
        false
);
// Check if post was not updated.
if ( ! $post_updated ) {
        return false;
}

Also, there is wp_trash_post_comments which could also fail.

@abhijitrakas
5 years ago

Modify code to detect false value on post update

#4 @abhijitrakas
5 years ago

@manzoorwanijk No need to pass the second parameter as it's the default param in wp_update_post function.

For wp_trash_post_comments we can't stop the process. If we return false on fail of wp_trash_post_comments function, then the user might get confused as we return an error but the post is not there in the list as post status already changed.

#5 @SergeyBiryukov
5 years ago

  • Milestone changed from Future Release to 5.3
  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

#6 @SergeyBiryukov
5 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 45724:

Posts, Post Types: Check the result of wp_update_post() in wp_trash_post() and wp_untrash_post().

Props abhijitrakas, manzoorwani.jk, siliconforks.
Fixes #47773.

Note: See TracTickets for help on using tickets.