Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -4316,8 +4316,8 @@ function wp_trash_post( $post_id = 0 ) {
*/
do_action( 'wp_trash_post', $post_id, $previous_status );

add_post_meta( $post_id, '_wp_trash_meta_status', $previous_status );
add_post_meta( $post_id, '_wp_trash_meta_time', time() );
update_post_meta( $post_id, '_wp_trash_meta_status', $previous_status );
update_post_meta( $post_id, '_wp_trash_meta_time', time() );

$post_updated = wp_update_post(
array(
Expand Down Expand Up @@ -5246,6 +5246,22 @@ function wp_insert_post( $postarr, $wp_error = false, $fire_after_hooks = true )
return 0;
}
}

/*
* If the post leaves the Trash without wp_untrash_post(), clear its trash meta
* and restore its comments the way wp_untrash_post() does.
* wp_untrash_post() deletes the trash status meta before it updates the post,
* so this does not run for it.
*/
if (
'trash' === $previous_status
&& 'trash' !== $data['post_status']
&& metadata_exists( 'post', $post_id, '_wp_trash_meta_status' )
) {
delete_post_meta( $post_id, '_wp_trash_meta_status' );
delete_post_meta( $post_id, '_wp_trash_meta_time' );
wp_untrash_post_comments( $post_id );
}
} else {
// If there is a suggested ID, use it if not already present.
if ( ! empty( $import_id ) ) {
Expand Down
77 changes: 77 additions & 0 deletions tests/phpunit/tests/post/wpInsertPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,83 @@ public function test_wp_untrash_post_status_filter_restores_post_to_correct_stat
$this->assertSame( $post_status, get_post( $page_id )->post_status );
}

/**
* Tests that a post that leaves the Trash through an update loses its trash meta,
* and that the comments trashed with it are restored.
*
* @ticket 66809
* @since 7.3.0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @since 7.3.0

*
* @dataProvider data_various_post_statuses
*
* @param string $post_status The status the trashed post is updated to.
*/
public function test_updating_a_trashed_post_to_another_status_should_clear_trash_meta_and_restore_comments( $post_status ) {
$post_id = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$approved_comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_approved' => '1',
)
);
$pending_comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_approved' => '0',
)
);

wp_trash_post( $post_id );

$result = wp_update_post(
array(
'ID' => $post_id,
'post_status' => $post_status,
),
true
);

$this->assertSame( $post_id, $result, 'The trashed post should be updated.' );
$this->assertSame( $post_status, get_post_status( $post_id ), 'The post should have the new status.' );
$this->assertFalse( metadata_exists( 'post', $post_id, '_wp_trash_meta_status' ), 'The _wp_trash_meta_status meta should be deleted.' );
$this->assertFalse( metadata_exists( 'post', $post_id, '_wp_trash_meta_time' ), 'The _wp_trash_meta_time meta should be deleted.' );
$this->assertFalse( metadata_exists( 'post', $post_id, '_wp_trash_meta_comments_status' ), 'The _wp_trash_meta_comments_status meta should be deleted.' );
$this->assertSame( '1', get_comment( $approved_comment_id )->comment_approved, 'The approved comment should be approved again.' );
$this->assertSame( '0', get_comment( $pending_comment_id )->comment_approved, 'The pending comment should be pending again.' );
}

/**
* Tests that a trashed post that is updated without leaving the Trash keeps its trash meta,
* and that its comments stay trashed.
*
* @ticket 66809
* @since 7.3.0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @since 7.3.0

*/
public function test_updating_a_trashed_post_without_changing_its_status_should_keep_trash_meta() {
$post_id = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_approved' => '1',
)
);

wp_trash_post( $post_id );

wp_update_post(
array(
'ID' => $post_id,
'post_title' => 'Updated title',
)
);

$this->assertSame( 'trash', get_post_status( $post_id ), 'The post should stay in the Trash.' );
$this->assertSame( array( 'publish' ), get_post_meta( $post_id, '_wp_trash_meta_status' ), 'The _wp_trash_meta_status meta should be kept.' );
$this->assertTrue( metadata_exists( 'post', $post_id, '_wp_trash_meta_time' ), 'The _wp_trash_meta_time meta should be kept.' );
$this->assertTrue( metadata_exists( 'post', $post_id, '_wp_trash_meta_comments_status' ), 'The _wp_trash_meta_comments_status meta should be kept.' );
$this->assertSame( 'post-trashed', get_comment( $comment_id )->comment_approved, 'The comment should stay trashed.' );
}

/**
* Data for testing the ability for users to set the post slug.
*
Expand Down
40 changes: 40 additions & 0 deletions tests/phpunit/tests/post/wpTrashPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,44 @@ function ( $post_id, $previous_status ) {

$this->assertGreaterThan( 0, did_action( 'trashed_post' ), 'trashed_post action was not called.' );
}

/**
* Tests that trashing a post again after it left the Trash through an update
* stores only its latest status.
*
* @ticket 66809
* @since 7.3.0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @since 7.3.0

*/
public function test_trash_post_after_restoring_it_by_update_should_store_one_trash_status() {
wp_trash_post( $this->post->ID );
wp_update_post(
array(
'ID' => $this->post->ID,
'post_status' => 'publish',
)
);
wp_trash_post( $this->post->ID );

$this->assertSame( array( 'publish' ), get_post_meta( $this->post->ID, '_wp_trash_meta_status' ), 'Trashed post should have one _wp_trash_meta_status meta set to its latest status.' );
$this->assertCount( 1, get_post_meta( $this->post->ID, '_wp_trash_meta_time' ), 'Trashed post should have only one _wp_trash_meta_time meta set.' );
}

/**
* Tests that wp_trash_post() replaces trash meta left on a post that is not in the Trash.
*
* @ticket 66809
* @since 7.3.0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @since 7.3.0

*/
public function test_trash_post_should_replace_leftover_trash_meta() {
add_post_meta( $this->post->ID, '_wp_trash_meta_status', 'pending' );
add_post_meta( $this->post->ID, '_wp_trash_meta_time', 1 );

wp_trash_post( $this->post->ID );

$trash_time = get_post_meta( $this->post->ID, '_wp_trash_meta_time' );

$this->assertSame( array( $this->post->post_status ), get_post_meta( $this->post->ID, '_wp_trash_meta_status' ), 'Trashed post should have one _wp_trash_meta_status meta set to previous post status.' );
$this->assertCount( 1, $trash_time, 'Trashed post should have only one _wp_trash_meta_time meta set.' );
$this->assertNotSame( '1', $trash_time[0], 'Trashed post should have _wp_trash_meta_time meta set to the time it was trashed.' );
}
}
37 changes: 37 additions & 0 deletions tests/phpunit/tests/post/wpUntrashPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,41 @@ function ( $post_id, $previous_status ) {

$this->assertGreaterThan( 0, did_action( 'untrashed_post' ), 'untrashed_post action was not called.' );
}

/**
* Tests that wp_untrash_post() restores the comments trashed with the post once,
* after the post leaves the Trash.
*
* @ticket 66809
* @since 7.3.0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @since 7.3.0

*/
public function test_untrash_post_restores_comments_once() {
$post_id = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_approved' => '1',
)
);

wp_trash_post( $post_id );

$statuses_when_restoring_comments = array();
add_action(
'untrash_post_comments',
static function ( $post_id ) use ( &$statuses_when_restoring_comments ) {
$statuses_when_restoring_comments[] = get_post_status( $post_id );
}
);

$result = wp_untrash_post( $post_id );

$this->assertInstanceOf( 'WP_Post', $result, 'wp_untrash_post returned value should be an instance of WP_Post.' );
$this->assertSame( array( 'draft' ), $statuses_when_restoring_comments, 'Comments should be restored once, after the post left the Trash.' );
$this->assertSame( 'draft', get_post_status( $post_id ), 'Untrashed post should be a draft.' );
$this->assertSame( '1', get_comment( $comment_id )->comment_approved, 'The comment should be approved again.' );
$this->assertSame( array(), get_post_meta( $post_id, '_wp_trash_meta_status' ), 'Untrashed post should not have _wp_trash_meta_status meta anymore.' );
$this->assertSame( array(), get_post_meta( $post_id, '_wp_trash_meta_time' ), 'Untrashed post should not have _wp_trash_meta_time meta anymore.' );
$this->assertSame( array(), get_post_meta( $post_id, '_wp_trash_meta_comments_status' ), 'Untrashed post should not have _wp_trash_meta_comments_status meta anymore.' );
}
}
65 changes: 65 additions & 0 deletions tests/phpunit/tests/rest-api/rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -6365,6 +6365,71 @@ public function test_get_posts_ignore_sticky_honors_include() {
$this->assertSame( $rest_ids, $post_ids, 'Response is same as WP_Query with ignore_sticky_posts=truehas no sticky post at the top.' );
}

/**
* Tests that a post that leaves the Trash through an update loses its trash meta,
* and that the comments trashed with it are restored.
*
* @ticket 66809
* @since 7.3.0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @since 7.3.0

*
* @covers WP_REST_Posts_Controller::update_item
*/
public function test_update_item_restoring_a_trashed_post_should_clear_trash_meta_and_restore_comments() {
wp_set_current_user( self::$editor_id );

$post_id = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_approved' => '1',
)
);

wp_trash_post( $post_id );

$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) );
$request->set_param( 'status', 'draft' );
$response = rest_get_server()->dispatch( $request );

$this->assertSame( 200, $response->get_status(), 'The trashed post should be updated.' );
$this->assertSame( 'draft', $response->get_data()['status'], 'The post should be a draft.' );
$this->assertFalse( metadata_exists( 'post', $post_id, '_wp_trash_meta_status' ), 'The _wp_trash_meta_status meta should be deleted.' );
$this->assertFalse( metadata_exists( 'post', $post_id, '_wp_trash_meta_time' ), 'The _wp_trash_meta_time meta should be deleted.' );
$this->assertFalse( metadata_exists( 'post', $post_id, '_wp_trash_meta_comments_status' ), 'The _wp_trash_meta_comments_status meta should be deleted.' );
$this->assertSame( '1', get_comment( $comment_id )->comment_approved, 'The comment should be approved again.' );
}

/**
* Tests that trashing a post again after it left the Trash through an update
* stores only its latest status.
*
* @ticket 66809
* @since 7.3.0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @since 7.3.0

*
* @covers WP_REST_Posts_Controller::update_item
* @covers WP_REST_Posts_Controller::delete_item
*/
public function test_delete_item_after_restoring_a_trashed_post_by_update_should_store_one_trash_status() {
wp_set_current_user( self::$editor_id );

$post_id = self::factory()->post->create( array( 'post_status' => 'draft' ) );
$path = sprintf( '/wp/v2/posts/%d', $post_id );

$response = rest_get_server()->dispatch( new WP_REST_Request( 'DELETE', $path ) );
$this->assertSame( 200, $response->get_status(), 'The post should be trashed.' );

$request = new WP_REST_Request( 'PUT', $path );
$request->set_param( 'status', 'publish' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status(), 'The trashed post should be updated.' );

$response = rest_get_server()->dispatch( new WP_REST_Request( 'DELETE', $path ) );
$this->assertSame( 200, $response->get_status(), 'The post should be trashed again.' );

$this->assertSame( array( 'publish' ), get_post_meta( $post_id, '_wp_trash_meta_status' ), 'The post should have one _wp_trash_meta_status meta set to its latest status.' );
$this->assertCount( 1, get_post_meta( $post_id, '_wp_trash_meta_time' ), 'The post should have only one _wp_trash_meta_time meta set.' );
}

/**
* Internal function used to disable an insert query which
* will trigger a wpdb error for testing purposes.
Expand Down
Loading