Skip to content

Commit

Permalink
Fix: post endpoint return 404 on trash not found (#36768)
Browse files Browse the repository at this point in the history
* Fix: post endpoint to return a 404 if a post has been deleted but we are trying to trash it

* changelog
  • Loading branch information
enejb committed Apr 8, 2024
1 parent d11d5af commit e3d0065
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Post endpoint: return a 404 if a post is being trashed but does not exist
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ public function write_post( $path, $blog_id, $post_id ) {
return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
}

$post = get_post( $post_id );
if ( ! $post || is_wp_error( $post ) ) {
return new WP_Error( 'unknown_post', 'Unknown post', 404 );
}

if ( isset( $input['status'] ) && 'trash' === $input['status'] && ! current_user_can( 'delete_post', $post_id ) ) {
return new WP_Error( 'unauthorized', 'User cannot delete post', 403 );
}
Expand All @@ -341,12 +346,8 @@ public function write_post( $path, $blog_id, $post_id ) {
$input['status'] = 'publish';
}

$post = get_post( $post_id );
$_post_type = ( ! empty( $input['type'] ) ) ? $input['type'] : $post->post_type;
$post_type = get_post_type_object( $_post_type );
if ( ! $post || is_wp_error( $post ) ) {
return new WP_Error( 'unknown_post', 'Unknown post', 404 );
}

if ( ! current_user_can( 'edit_post', $post->ID ) ) {
return new WP_Error( 'unauthorized', 'User cannot edit post', 403 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ public function write_post( $path, $blog_id, $post_id ) {
return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
}

$post = get_post( $post_id );
if ( ! $post || is_wp_error( $post ) ) {
return new WP_Error( 'unknown_post', 'Unknown post', 404 );
}

if ( isset( $input['status'] ) && 'trash' === $input['status'] && ! current_user_can( 'delete_post', $post_id ) ) {
return new WP_Error( 'unauthorized', 'User cannot delete post', 403 );
}
Expand All @@ -267,11 +272,6 @@ public function write_post( $path, $blog_id, $post_id ) {
$input['status'] = 'publish';
}

$post = get_post( $post_id );
if ( ! $post || is_wp_error( $post ) ) {
return new WP_Error( 'unknown_post', 'Unknown post', 404 );
}

$_post_type = ( ! empty( $input['type'] ) ) ? $input['type'] : $post->post_type;
$post_type = get_post_type_object( $_post_type );

Expand Down

0 comments on commit e3d0065

Please sign in to comment.