Posts, Post Types: Switch to restoring posts to draft status by default when they are untrashed.

This allows for edits to be made to a restored post before it goes live again. This also prevents scheduled posts being published unexpectedly if they are untrashed after their originally scheduled date.

The old behaviour of restoring untrashed posts to their original status can be reinstated using the `wp_untrash_post_set_previous_status()` helper function.

Also fixes an issue where the incorrect post ID gets passed to hooks if no post ID is passed to the function.

Props harrym, bananastalktome, jaredcobb, chriscct7, melchoyce, johnbillion, pankajmohale

Fixes #23022


git-svn-id: https://develop.svn.wordpress.org/trunk@49125 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn
2020-10-11 13:37:04 +00:00
parent 06b267ee3d
commit 402820ebba
5 changed files with 152 additions and 11 deletions
+68
View File
@@ -159,11 +159,57 @@ class Tests_WPInsertPost extends WP_UnitTestCase {
);
wp_untrash_post( $about_page_id );
wp_update_post(
array(
'ID' => $about_page_id,
'post_status' => 'publish',
)
);
$this->assertSame( 'about', get_post( $another_about_page_id )->post_name );
$this->assertSame( 'about-2', get_post( $about_page_id )->post_name );
}
/**
* @ticket 23022
* @dataProvider data_various_post_statuses
*/
function test_untrashing_a_post_should_always_restore_it_to_draft_status( $post_status ) {
$page_id = self::factory()->post->create(
array(
'post_type' => 'page',
'post_status' => $post_status,
)
);
wp_trash_post( $page_id );
wp_untrash_post( $page_id );
$this->assertSame( 'draft', get_post( $page_id )->post_status );
}
/**
* @ticket 23022
* @dataProvider data_various_post_statuses
*/
function test_wp_untrash_post_status_filter_restores_post_to_correct_status( $post_status ) {
add_filter( 'wp_untrash_post_status', 'wp_untrash_post_set_previous_status', 10, 3 );
$page_id = self::factory()->post->create(
array(
'post_type' => 'page',
'post_status' => $post_status,
)
);
wp_trash_post( $page_id );
wp_untrash_post( $page_id );
remove_filter( 'wp_untrash_post_status', 'wp_untrash_post_set_previous_status', 10, 3 );
$this->assertSame( $post_status, get_post( $page_id )->post_status );
}
/**
* Data for testing the ability for users to set the post slug.
*
@@ -183,6 +229,28 @@ class Tests_WPInsertPost extends WP_UnitTestCase {
);
}
/**
* Data for testing post statuses.
*
* @return array Array of test arguments.
*/
function data_various_post_statuses() {
return array(
array(
'draft',
),
array(
'pending',
),
array(
'private',
),
array(
'publish',
),
);
}
/**
* Test contributor making changes to the pending post slug.
*