Customize: Allow post/page stubs to be edited in WP Admin as "customization drafts" when changeset is saved as draft or scheduled.

* Update stubs to have draft status when changeset is saved as draft, instead of preventing auto-draft garbage collection by giving them a far-future `post_date`.
* Show notice in publish metabox when editing a customization draft indicating that it will be published automatically with its changeset; a link to Customizer is included.
* Include a new "Customization Draft" display post state in the post list table.
* Disconnect stubs from their changesets when they are updated with a status other than "Draft".
* Trash customization drafts when their related changeset is trashed or deleted.
* Add a `_customize_changeset_uuid` postmeta to stubs to link them with their associated changeset.
* Include `customize_changeset_uuid` as context when requesting to insert a new auto-draft.

Props westonruter, melchoyce.
See #39896, #39752, #34923.
Fixes #42220.


git-svn-id: https://develop.svn.wordpress.org/trunk@41887 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Weston Ruter
2017-10-17 20:14:56 +00:00
parent ee678ee1f5
commit ce7a93c235
11 changed files with 225 additions and 41 deletions

View File

@@ -603,6 +603,7 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
$this->assertEquals( 'post', $post->post_type );
$this->assertEquals( '', $post->post_name );
$this->assertEquals( 'hello-world', get_post_meta( $post->ID, '_customize_draft_post_name', true ) );
$this->assertEquals( $this->wp_customize->changeset_uuid(), get_post_meta( $post->ID, '_customize_changeset_uuid', true ) );
}
/**

View File

@@ -541,6 +541,7 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
// Non-existent post types allowed as of #39610.
$r = $menus->insert_auto_draft_post( array( 'post_title' => 'Non-existent', 'post_type' => 'nonexistent' ) );
$this->assertInstanceOf( 'WP_Post', $r );
$this->assertEquals( $this->wp_customize->changeset_uuid(), get_post_meta( $r->ID, '_customize_changeset_uuid', true ) );
$r = $menus->insert_auto_draft_post( array( 'post_type' => 'post' ) );
$this->assertInstanceOf( 'WP_Error', $r );
@@ -555,6 +556,7 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
$this->assertEquals( 'Hello World', $r->post_title );
$this->assertEquals( '', $r->post_name );
$this->assertEquals( 'hello-world', get_post_meta( $r->ID, '_customize_draft_post_name', true ) );
$this->assertEquals( $this->wp_customize->changeset_uuid(), get_post_meta( $r->ID, '_customize_changeset_uuid', true ) );
$this->assertEquals( 'post', $r->post_type );
$r = $menus->insert_auto_draft_post( array( 'post_title' => 'Hello World', 'post_type' => 'post', 'post_name' => 'greetings-world', 'post_content' => 'Hi World' ) );
@@ -563,6 +565,7 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
$this->assertEquals( 'post', $r->post_type );
$this->assertEquals( '', $r->post_name );
$this->assertEquals( 'greetings-world', get_post_meta( $r->ID, '_customize_draft_post_name', true ) );
$this->assertEquals( $this->wp_customize->changeset_uuid(), get_post_meta( $r->ID, '_customize_changeset_uuid', true ) );
$this->assertEquals( 'Hi World', $r->post_content );
}
@@ -716,11 +719,25 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
'post_author' => $administrator_user_id,
) );
$draft_post_id = $this->factory()->post->create( array(
'post_status' => 'draft',
'post_title' => 'Draft',
'post_author' => $administrator_user_id,
) );
$private_post_id = $this->factory()->post->create( array(
'post_status' => 'private',
'post_title' => 'Private',
'post_author' => $administrator_user_id,
) );
$value = array(
'bad',
$contributor_post_id,
$author_post_id,
$administrator_post_id,
$draft_post_id,
$private_post_id,
);
wp_set_current_user( $contributor_user_id );
@@ -733,7 +750,7 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
wp_set_current_user( $administrator_user_id );
$sanitized = $menus->sanitize_nav_menus_created_posts( $value );
$this->assertEquals( array( $contributor_post_id, $author_post_id, $administrator_post_id ), $sanitized );
$this->assertEquals( array( $contributor_post_id, $author_post_id, $administrator_post_id, $draft_post_id ), $sanitized );
}
/**
@@ -746,15 +763,55 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
do_action( 'customize_register', $this->wp_customize );
$post_ids = array();
for ( $i = 0; $i < 3; $i += 1 ) {
$r = $menus->insert_auto_draft_post( array(
'post_title' => 'Auto Draft ' . $i,
'post_type' => 'post',
'post_name' => 'auto-draft-' . $i,
) );
$this->assertInstanceOf( 'WP_Post', $r );
$post_ids[] = $r->ID;
}
// Auto-draft.
$r = $menus->insert_auto_draft_post( array(
'post_title' => 'Auto Draft',
'post_type' => 'post',
'post_name' => 'auto-draft-1',
) );
$this->assertInstanceOf( 'WP_Post', $r );
$post_ids[] = $r->ID;
// Draft.
$r = $menus->insert_auto_draft_post( array(
'post_title' => 'Draft',
'post_type' => 'post',
'post_name' => 'auto-draft-2',
) );
$this->assertInstanceOf( 'WP_Post', $r );
wp_update_post( array(
'ID' => $r->ID,
'post_status' => 'draft',
) );
$post_ids[] = $r->ID;
$drafted_post_ids = $post_ids;
// Private (this will exclude it from being considered a stub).
$r = $menus->insert_auto_draft_post( array(
'post_title' => 'Private',
'post_type' => 'post',
'post_name' => 'auto-draft-3',
) );
$this->assertInstanceOf( 'WP_Post', $r );
wp_update_post( array(
'ID' => $r->ID,
'post_status' => 'private',
) );
$post_ids[] = $r->ID;
$private_post_id = $r->ID;
// Trashed (this will exclude it from being considered a stub).
$r = $menus->insert_auto_draft_post( array(
'post_title' => 'Trash',
'post_type' => 'post',
'post_name' => 'auto-draft-4',
) );
$this->assertInstanceOf( 'WP_Post', $r );
wp_trash_post( $r->ID );
$post_ids[] = $r->ID;
$trashed_post_id = $r->ID;
$pre_published_post_id = $this->factory()->post->create( array( 'post_status' => 'publish' ) );
@@ -763,9 +820,13 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
$setting = $this->wp_customize->get_setting( $setting_id );
$this->assertInstanceOf( 'WP_Customize_Filter_Setting', $setting );
$this->assertEquals( array( $menus, 'sanitize_nav_menus_created_posts' ), $setting->sanitize_callback );
$this->assertEquals( $post_ids, $setting->post_value() );
foreach ( $post_ids as $post_id ) {
$this->assertEquals( 'auto-draft', get_post_status( $post_id ) );
$this->assertEquals( $drafted_post_ids, $setting->post_value() );
$this->assertArrayNotHasKey( $private_post_id, $post_ids );
$this->assertArrayNotHasKey( $trashed_post_id, $post_ids );
$this->assertEquals( 'auto-draft', get_post_status( $drafted_post_ids[0] ) );
$this->assertEquals( 'draft', get_post_status( $drafted_post_ids[1] ) );
foreach ( $drafted_post_ids as $post_id ) {
$this->assertEmpty( get_post( $post_id )->post_name );
$this->assertNotEmpty( get_post_meta( $post_id, '_customize_draft_post_name', true ) );
}
@@ -773,14 +834,17 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
$save_action_count = did_action( 'customize_save_nav_menus_created_posts' );
$setting->save();
$this->assertEquals( $save_action_count + 1, did_action( 'customize_save_nav_menus_created_posts' ) );
foreach ( $post_ids as $post_id ) {
foreach ( $drafted_post_ids as $post_id ) {
$this->assertEquals( 'publish', get_post_status( $post_id ) );
$this->assertRegExp( '/^auto-draft-\d+$/', get_post( $post_id )->post_name );
$this->assertEmpty( get_post_meta( $post_id, '_customize_draft_post_name', true ) );
}
$this->assertEquals( 'private', get_post_status( $private_post_id ) );
$this->assertEquals( 'trash', get_post_status( $trashed_post_id ) );
// Ensure that unique slugs were assigned.
$posts = array_map( 'get_post', $post_ids );
$posts = array_map( 'get_post', $drafted_post_ids );
$post_names = wp_list_pluck( $posts, 'post_name' );
$this->assertEqualSets( $post_names, array_unique( $post_names ) );
}

View File

@@ -549,9 +549,21 @@ class Test_Nav_Menus extends WP_UnitTestCase {
* @covers _wp_delete_customize_changeset_dependent_auto_drafts()
*/
function test_wp_delete_customize_changeset_dependent_auto_drafts() {
$nav_created_post_ids = $this->factory()->post->create_many(2, array(
$auto_draft_post_id = $this->factory()->post->create( array(
'post_status' => 'auto-draft',
) );
$draft_post_id = $this->factory()->post->create( array(
'post_status' => 'draft',
) );
$private_post_id = $this->factory()->post->create( array(
'post_status' => 'private',
) );
$nav_created_post_ids = array(
$auto_draft_post_id,
$draft_post_id,
$private_post_id,
);
$data = array(
'nav_menus_created_posts' => array(
'value' => $nav_created_post_ids,
@@ -566,11 +578,13 @@ class Test_Nav_Menus extends WP_UnitTestCase {
$wp_customize->save_changeset_post( array(
'data' => $data,
) );
$this->assertInstanceOf( 'WP_Post', get_post( $nav_created_post_ids[0] ) );
$this->assertInstanceOf( 'WP_Post', get_post( $nav_created_post_ids[1] ) );
$this->assertEquals( 'auto-draft', get_post_status( $auto_draft_post_id ) );
$this->assertEquals( 'draft', get_post_status( $draft_post_id ) );
$this->assertEquals( 'private', get_post_status( $private_post_id ) );
wp_delete_post( $wp_customize->changeset_post_id(), true );
$this->assertNotInstanceOf( 'WP_Post', get_post( $nav_created_post_ids[0] ) );
$this->assertNotInstanceOf( 'WP_Post', get_post( $nav_created_post_ids[1] ) );
$this->assertFalse( get_post_status( $auto_draft_post_id ) );
$this->assertEquals( 'trash', get_post_status( $draft_post_id ) );
$this->assertEquals( 'private', get_post_status( $private_post_id ) );
}
/**

View File

@@ -318,6 +318,7 @@ class Tests_Theme extends WP_UnitTestCase {
function test_wp_keep_alive_customize_changeset_dependent_auto_drafts() {
$nav_created_post_ids = $this->factory()->post->create_many(2, array(
'post_status' => 'auto-draft',
'post_date' => gmdate( 'Y-m-d H:i:s', strtotime( '-2 days' ) ),
) );
$data = array(
'nav_menus_created_posts' => array(
@@ -328,17 +329,41 @@ class Tests_Theme extends WP_UnitTestCase {
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
$wp_customize = new WP_Customize_Manager();
do_action( 'customize_register', $wp_customize );
// The post_date for auto-drafts is bumped to match the changeset post_date whenever it is modified to keep them from from being garbage collected by wp_delete_auto_drafts().
$wp_customize->save_changeset_post( array(
'data' => $data,
) );
$this->assertEquals( get_post( $nav_created_post_ids[0] )->post_date, get_post( $wp_customize->changeset_post_id() )->post_date );
$this->assertEquals( get_post( $nav_created_post_ids[1] )->post_date, get_post( $wp_customize->changeset_post_id() )->post_date );
$this->assertEquals( 'auto-draft', get_post_status( $nav_created_post_ids[0] ) );
$this->assertEquals( 'auto-draft', get_post_status( $nav_created_post_ids[1] ) );
// Stubs transition to drafts when changeset is saved as a draft.
$wp_customize->save_changeset_post( array(
'status' => 'draft',
'data' => $data,
) );
$expected_year = date( 'Y' ) + 100;
$this->assertEquals( $expected_year, date( 'Y', strtotime( get_post( $nav_created_post_ids[0] )->post_date ) ) );
$this->assertEquals( $expected_year, date( 'Y', strtotime( get_post( $nav_created_post_ids[1] )->post_date ) ) );
$this->assertEquals( get_post( $nav_created_post_ids[0] )->post_date, get_post( $wp_customize->changeset_post_id() )->post_date );
$this->assertEquals( get_post( $nav_created_post_ids[1] )->post_date, get_post( $wp_customize->changeset_post_id() )->post_date );
$this->assertEquals( 'draft', get_post_status( $nav_created_post_ids[0] ) );
$this->assertEquals( 'draft', get_post_status( $nav_created_post_ids[1] ) );
// Status remains unchanged for stub that the user broke out of the changeset.
wp_update_post( array(
'ID' => $nav_created_post_ids[1],
'post_status' => 'private',
) );
$wp_customize->save_changeset_post( array(
'status' => 'draft',
'data' => $data,
) );
$this->assertEquals( 'draft', get_post_status( $nav_created_post_ids[0] ) );
$this->assertEquals( 'private', get_post_status( $nav_created_post_ids[1] ) );
// Draft stub is trashed when the changeset is trashed.
$wp_customize->trash_changeset_post( $wp_customize->changeset_post_id() );
$this->assertEquals( 'trash', get_post_status( $nav_created_post_ids[0] ) );
$this->assertEquals( 'private', get_post_status( $nav_created_post_ids[1] ) );
}
}