Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#42296 closed defect (bug) (invalid)

REST API: logical error while checking for already deleted post

Reported by: schlessera Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.7.2
Component: REST API Keywords:
Focuses: rest-api Cc:


In the delete_item() method of the REST API posts controller, there's a piece of code that checks whether the post to be deleted has already been deleted before:

if ( 'trash' === $post->post_status ) {
    return new WP_Error( 'rest_already_trashed', __( 'The post has already been deleted.' ), array( 'status' => 410 ) );

The problem with this code is that the post was fetched with the get_post() method that was introduced with Changeset 39954. That method already returns a rest_post_invalid_id if the post_type does not match the one that was used for constructing the controller. So the condition that is being checked for can never be true. It seems this was an unintentional breaking change in v4.7.2.

I can see two valid options for making this work correctly (and return the correct error type):

  1. Avoid the use of the get_post() method in delete_item(), and fetch the post directly. This makes it possible to keep posts of type trash as valid results still.
  1. Change the get_post() method to also let posts of type trash through. This might mean that some of the controller method might need additional filtering.

Change History (2)

#1 @joehoyle
4 years ago

@schlessera I might be missing something here, but the post_type doesn't change when a post is trashed, so why would this->get_post() ever return rest_post_invalid_id? It sounds as if you are getting post type and status crossed over?

#2 @schlessera
4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

@joehoyle Just looked through this again, and you are right. I must have "crossed the beams" here...

Closing this, as it seems to be invalid.

Note: See TracTickets for help on using tickets.