Repository navigation
Fix: Clear trash meta when a post leaves the Trash through a save. #16214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -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 | ||||
| * | ||||
| * @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 | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
| */ | ||||
| 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. | ||||
| * | ||||
|
|
||||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -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 | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
| */ | ||||
| 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 | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
| */ | ||||
| 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.' ); | ||||
| } | ||||
| } | ||||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -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 | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
| */ | ||||
| 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.' ); | ||||
| } | ||||
| } | ||||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -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 | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
| * | ||||
| * @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 | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
| * | ||||
| * @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. | ||||
|
|
||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.