Skip to content

Commit ce7a93c

Browse files
committed
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
1 parent ee678ee commit ce7a93c

11 files changed

Lines changed: 225 additions & 41 deletions

File tree

src/wp-admin/css/common.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,11 @@ th.action-links {
964964
text-align: right;
965965
}
966966

967+
#misc-publishing-actions .notice {
968+
margin-left: 10px;
969+
margin-right: 10px;
970+
}
971+
967972
/* Filter bar */
968973
.wp-filter {
969974
display: inline-block;

src/wp-admin/includes/meta-boxes.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,26 @@ function post_submit_meta_box( $post, $args = array() ) {
223223
</div><?php // /misc-pub-section ?>
224224
<?php endif; ?>
225225

226+
<?php if ( 'draft' === $post->post_status && get_post_meta( $post->ID, '_customize_changeset_uuid', true ) ) : ?>
227+
<div class="notice notice-info notice-alt inline">
228+
<p>
229+
<?php
230+
echo sprintf(
231+
/* translators: %s is the URL to the Customizer */
232+
__( 'This draft comes from your <a href="%s">unpublished customization changes</a>. You can edit, but there&#8217;s no need to publish now. It will be published automatically with those changes.' ),
233+
esc_url(
234+
add_query_arg(
235+
'changeset_uuid',
236+
rawurlencode( get_post_meta( $post->ID, '_customize_changeset_uuid', true ) ),
237+
admin_url( 'customize.php' )
238+
)
239+
)
240+
);
241+
?>
242+
</p>
243+
</div>
244+
<?php endif; ?>
245+
226246
<?php
227247
/**
228248
* Fires after the post time/date setting in the Publish meta box.

src/wp-admin/includes/template.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,8 +1736,15 @@ function _post_states($post) {
17361736
$post_states['protected'] = __('Password protected');
17371737
if ( 'private' == $post->post_status && 'private' != $post_status )
17381738
$post_states['private'] = __('Private');
1739-
if ( 'draft' == $post->post_status && 'draft' != $post_status )
1740-
$post_states['draft'] = __('Draft');
1739+
if ( 'draft' === $post->post_status ) {
1740+
if ( get_post_meta( $post->ID, '_customize_changeset_uuid', true ) ) {
1741+
$post_states[] = __( 'Customization Draft' );
1742+
} elseif ( 'draft' !== $post_status ) {
1743+
$post_states['draft'] = __( 'Draft' );
1744+
}
1745+
} elseif ( 'trash' === $post->post_status && get_post_meta( $post->ID, '_customize_changeset_uuid', true ) ) {
1746+
$post_states[] = __( 'Customization Draft' );
1747+
}
17411748
if ( 'pending' == $post->post_status && 'pending' != $post_status )
17421749
$post_states['pending'] = _x('Pending', 'post status');
17431750
if ( is_sticky($post->ID) )

src/wp-admin/js/customize-nav-menus.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
request = wp.ajax.post( 'customize-nav-menus-insert-auto-draft', {
9898
'customize-menus-nonce': api.settings.nonce['customize-menus'],
9999
'wp_customize': 'on',
100+
'customize_changeset_uuid': api.settings.changeset.uuid,
100101
'params': params
101102
} );
102103

src/wp-includes/class-wp-customize-nav-menus.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,10 @@ public function insert_auto_draft_post( $postarr ) {
794794
return new WP_Error( 'status_forbidden', __( 'Status is forbidden' ) );
795795
}
796796

797+
/*
798+
* If the changeset is a draft, this will change to draft the next time the changeset
799+
* is updated; otherwise, auto-draft will persist in autosave revisions, until save.
800+
*/
797801
$postarr['post_status'] = 'auto-draft';
798802

799803
// Auto-drafts are allowed to have empty post_names, so it has to be explicitly set.
@@ -804,6 +808,7 @@ public function insert_auto_draft_post( $postarr ) {
804808
$postarr['meta_input'] = array();
805809
}
806810
$postarr['meta_input']['_customize_draft_post_name'] = $postarr['post_name'];
811+
$postarr['meta_input']['_customize_changeset_uuid'] = $this->manager->changeset_uuid();
807812
unset( $postarr['post_name'] );
808813

809814
add_filter( 'wp_insert_post_empty_content', '__return_false', 1000 );
@@ -1172,7 +1177,7 @@ public function make_auto_draft_status_previewable() {
11721177
}
11731178

11741179
/**
1175-
* Sanitize post IDs for auto-draft posts created for nav menu items to be published.
1180+
* Sanitize post IDs for posts created for nav menu items to be published.
11761181
*
11771182
* @since 4.7.0
11781183
*
@@ -1186,7 +1191,7 @@ public function sanitize_nav_menus_created_posts( $value ) {
11861191
continue;
11871192
}
11881193
$post = get_post( $post_id );
1189-
if ( 'auto-draft' !== $post->post_status ) {
1194+
if ( 'auto-draft' !== $post->post_status && 'draft' !== $post->post_status ) {
11901195
continue;
11911196
}
11921197
$post_type_obj = get_post_type_object( $post->post_type );
@@ -1217,6 +1222,13 @@ public function save_nav_menus_created_posts( $setting ) {
12171222
$post_ids = $setting->post_value();
12181223
if ( ! empty( $post_ids ) ) {
12191224
foreach ( $post_ids as $post_id ) {
1225+
1226+
// Prevent overriding the status that a user may have prematurely updated the post to.
1227+
$current_status = get_post_status( $post_id );
1228+
if ( 'auto-draft' !== $current_status && 'draft' !== $current_status ) {
1229+
continue;
1230+
}
1231+
12201232
$target_status = 'attachment' === get_post_type( $post_id ) ? 'inherit' : 'publish';
12211233
$args = array(
12221234
'ID' => $post_id,

src/wp-includes/nav-menu.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,9 +1051,15 @@ function _wp_delete_customize_changeset_dependent_auto_drafts( $post_id ) {
10511051
return;
10521052
}
10531053
remove_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' );
1054-
foreach ( $data['nav_menus_created_posts']['value'] as $post_id ) {
1055-
if ( ! empty( $post_id ) && 'auto-draft' === get_post_status( $post_id ) ) {
1056-
wp_delete_post( $post_id, true );
1054+
foreach ( $data['nav_menus_created_posts']['value'] as $stub_post_id ) {
1055+
if ( empty( $stub_post_id ) ) {
1056+
continue;
1057+
}
1058+
if ( 'auto-draft' === get_post_status( $stub_post_id ) ) {
1059+
wp_delete_post( $stub_post_id, true );
1060+
} elseif ( 'draft' === get_post_status( $stub_post_id ) ) {
1061+
wp_trash_post( $stub_post_id );
1062+
delete_post_meta( $stub_post_id, '_customize_changeset_uuid' );
10571063
}
10581064
}
10591065
add_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' );

src/wp-includes/theme.php

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3051,7 +3051,7 @@ function is_customize_preview() {
30513051
}
30523052

30533053
/**
3054-
* Make sure that auto-draft posts get their post_date bumped to prevent premature garbage-collection.
3054+
* Make sure that auto-draft posts get their post_date bumped or status changed to draft to prevent premature garbage-collection.
30553055
*
30563056
* When a changeset is updated but remains an auto-draft, ensure the post_date
30573057
* for the auto-draft posts remains the same so that it will be
@@ -3060,6 +3060,14 @@ function is_customize_preview() {
30603060
* to have a far-future post_date so that they will never be garbage collected
30613061
* unless the changeset post itself is deleted.
30623062
*
3063+
* When a changeset is updated to be a persistent draft or to be scheduled for
3064+
* publishing, then transition any dependent auto-drafts to a draft status so
3065+
* that they likewise will not be garbage-collected but also so that they can
3066+
* be edited in the admin before publishing since there is not yet a post/page
3067+
* editing flow in the Customizer. See #39752.
3068+
*
3069+
* @link https://core.trac.wordpress.org/ticket/39752
3070+
*
30633071
* @since 4.8.0
30643072
* @access private
30653073
* @see wp_delete_auto_drafts()
@@ -3078,34 +3086,55 @@ function _wp_keep_alive_customize_changeset_dependent_auto_drafts( $new_status,
30783086
return;
30793087
}
30803088

3089+
$data = json_decode( $post->post_content, true );
3090+
if ( empty( $data['nav_menus_created_posts']['value'] ) ) {
3091+
return;
3092+
}
3093+
3094+
/*
3095+
* Actually, in lieu of keeping alive, trash any customization drafts here if the changeset itself is
3096+
* getting trashed. This is needed because when a changeset transitions to a draft, then any of the
3097+
* dependent auto-draft post/page stubs will also get transitioned to customization drafts which
3098+
* are then visible in the WP Admin. We cannot wait for the deletion of the changeset in which
3099+
* _wp_delete_customize_changeset_dependent_auto_drafts() will be called, since they need to be
3100+
* trashed to remove from visibility immediately.
3101+
*/
3102+
if ( 'trash' === $new_status ) {
3103+
foreach ( $data['nav_menus_created_posts']['value'] as $post_id ) {
3104+
if ( ! empty( $post_id ) && 'draft' === get_post_status( $post_id ) ) {
3105+
wp_trash_post( $post_id );
3106+
}
3107+
}
3108+
return;
3109+
}
3110+
3111+
$post_args = array();
30813112
if ( 'auto-draft' === $new_status ) {
30823113
/*
30833114
* Keep the post date for the post matching the changeset
30843115
* so that it will not be garbage-collected before the changeset.
30853116
*/
3086-
$new_post_date = $post->post_date;
3117+
$post_args['post_date'] = $post->post_date; // Note wp_delete_auto_drafts() only looks at this date.
30873118
} else {
30883119
/*
30893120
* Since the changeset no longer has an auto-draft (and it is not published)
30903121
* it is now a persistent changeset, a long-lived draft, and so any
3091-
* associated auto-draft posts should have their dates
3092-
* pushed out very far into the future to prevent them from ever
3093-
* being garbage-collected.
3122+
* associated auto-draft posts should likewise transition into having a draft
3123+
* status. These drafts will be treated differently than regular drafts in
3124+
* that they will be tied to the given changeset. The publish metabox is
3125+
* replaced with a notice about how the post is part of a set of customized changes
3126+
* which will be published when the changeset is published.
30943127
*/
3095-
$new_post_date = gmdate( 'Y-m-d H:i:d', strtotime( '+100 years' ) );
3128+
$post_args['post_status'] = 'draft';
30963129
}
30973130

3098-
$data = json_decode( $post->post_content, true );
3099-
if ( empty( $data['nav_menus_created_posts']['value'] ) ) {
3100-
return;
3101-
}
31023131
foreach ( $data['nav_menus_created_posts']['value'] as $post_id ) {
31033132
if ( empty( $post_id ) || 'auto-draft' !== get_post_status( $post_id ) ) {
31043133
continue;
31053134
}
31063135
$wpdb->update(
31073136
$wpdb->posts,
3108-
array( 'post_date' => $new_post_date ), // Note wp_delete_auto_drafts() only looks at this date.
3137+
$post_args,
31093138
array( 'ID' => $post_id )
31103139
);
31113140
clean_post_cache( $post_id );

tests/phpunit/tests/ajax/CustomizeMenus.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ function test_ajax_insert_auto_draft_post_success() {
603603
$this->assertEquals( 'post', $post->post_type );
604604
$this->assertEquals( '', $post->post_name );
605605
$this->assertEquals( 'hello-world', get_post_meta( $post->ID, '_customize_draft_post_name', true ) );
606+
$this->assertEquals( $this->wp_customize->changeset_uuid(), get_post_meta( $post->ID, '_customize_changeset_uuid', true ) );
606607
}
607608

608609
/**

tests/phpunit/tests/customize/nav-menus.php

Lines changed: 79 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@ public function test_insert_auto_draft_post() {
541541
// Non-existent post types allowed as of #39610.
542542
$r = $menus->insert_auto_draft_post( array( 'post_title' => 'Non-existent', 'post_type' => 'nonexistent' ) );
543543
$this->assertInstanceOf( 'WP_Post', $r );
544+
$this->assertEquals( $this->wp_customize->changeset_uuid(), get_post_meta( $r->ID, '_customize_changeset_uuid', true ) );
544545

545546
$r = $menus->insert_auto_draft_post( array( 'post_type' => 'post' ) );
546547
$this->assertInstanceOf( 'WP_Error', $r );
@@ -555,6 +556,7 @@ public function test_insert_auto_draft_post() {
555556
$this->assertEquals( 'Hello World', $r->post_title );
556557
$this->assertEquals( '', $r->post_name );
557558
$this->assertEquals( 'hello-world', get_post_meta( $r->ID, '_customize_draft_post_name', true ) );
559+
$this->assertEquals( $this->wp_customize->changeset_uuid(), get_post_meta( $r->ID, '_customize_changeset_uuid', true ) );
558560
$this->assertEquals( 'post', $r->post_type );
559561

560562
$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 @@ public function test_insert_auto_draft_post() {
563565
$this->assertEquals( 'post', $r->post_type );
564566
$this->assertEquals( '', $r->post_name );
565567
$this->assertEquals( 'greetings-world', get_post_meta( $r->ID, '_customize_draft_post_name', true ) );
568+
$this->assertEquals( $this->wp_customize->changeset_uuid(), get_post_meta( $r->ID, '_customize_changeset_uuid', true ) );
566569
$this->assertEquals( 'Hi World', $r->post_content );
567570
}
568571

@@ -716,11 +719,25 @@ function test_sanitize_nav_menus_created_posts() {
716719
'post_author' => $administrator_user_id,
717720
) );
718721

722+
$draft_post_id = $this->factory()->post->create( array(
723+
'post_status' => 'draft',
724+
'post_title' => 'Draft',
725+
'post_author' => $administrator_user_id,
726+
) );
727+
728+
$private_post_id = $this->factory()->post->create( array(
729+
'post_status' => 'private',
730+
'post_title' => 'Private',
731+
'post_author' => $administrator_user_id,
732+
) );
733+
719734
$value = array(
720735
'bad',
721736
$contributor_post_id,
722737
$author_post_id,
723738
$administrator_post_id,
739+
$draft_post_id,
740+
$private_post_id,
724741
);
725742

726743
wp_set_current_user( $contributor_user_id );
@@ -733,7 +750,7 @@ function test_sanitize_nav_menus_created_posts() {
733750

734751
wp_set_current_user( $administrator_user_id );
735752
$sanitized = $menus->sanitize_nav_menus_created_posts( $value );
736-
$this->assertEquals( array( $contributor_post_id, $author_post_id, $administrator_post_id ), $sanitized );
753+
$this->assertEquals( array( $contributor_post_id, $author_post_id, $administrator_post_id, $draft_post_id ), $sanitized );
737754
}
738755

739756
/**
@@ -746,15 +763,55 @@ function test_save_nav_menus_created_posts() {
746763
do_action( 'customize_register', $this->wp_customize );
747764

748765
$post_ids = array();
749-
for ( $i = 0; $i < 3; $i += 1 ) {
750-
$r = $menus->insert_auto_draft_post( array(
751-
'post_title' => 'Auto Draft ' . $i,
752-
'post_type' => 'post',
753-
'post_name' => 'auto-draft-' . $i,
754-
) );
755-
$this->assertInstanceOf( 'WP_Post', $r );
756-
$post_ids[] = $r->ID;
757-
}
766+
767+
// Auto-draft.
768+
$r = $menus->insert_auto_draft_post( array(
769+
'post_title' => 'Auto Draft',
770+
'post_type' => 'post',
771+
'post_name' => 'auto-draft-1',
772+
) );
773+
$this->assertInstanceOf( 'WP_Post', $r );
774+
$post_ids[] = $r->ID;
775+
776+
// Draft.
777+
$r = $menus->insert_auto_draft_post( array(
778+
'post_title' => 'Draft',
779+
'post_type' => 'post',
780+
'post_name' => 'auto-draft-2',
781+
) );
782+
$this->assertInstanceOf( 'WP_Post', $r );
783+
wp_update_post( array(
784+
'ID' => $r->ID,
785+
'post_status' => 'draft',
786+
) );
787+
$post_ids[] = $r->ID;
788+
789+
$drafted_post_ids = $post_ids;
790+
791+
// Private (this will exclude it from being considered a stub).
792+
$r = $menus->insert_auto_draft_post( array(
793+
'post_title' => 'Private',
794+
'post_type' => 'post',
795+
'post_name' => 'auto-draft-3',
796+
) );
797+
$this->assertInstanceOf( 'WP_Post', $r );
798+
wp_update_post( array(
799+
'ID' => $r->ID,
800+
'post_status' => 'private',
801+
) );
802+
$post_ids[] = $r->ID;
803+
$private_post_id = $r->ID;
804+
805+
// Trashed (this will exclude it from being considered a stub).
806+
$r = $menus->insert_auto_draft_post( array(
807+
'post_title' => 'Trash',
808+
'post_type' => 'post',
809+
'post_name' => 'auto-draft-4',
810+
) );
811+
$this->assertInstanceOf( 'WP_Post', $r );
812+
wp_trash_post( $r->ID );
813+
$post_ids[] = $r->ID;
814+
$trashed_post_id = $r->ID;
758815

759816
$pre_published_post_id = $this->factory()->post->create( array( 'post_status' => 'publish' ) );
760817

@@ -763,24 +820,31 @@ function test_save_nav_menus_created_posts() {
763820
$setting = $this->wp_customize->get_setting( $setting_id );
764821
$this->assertInstanceOf( 'WP_Customize_Filter_Setting', $setting );
765822
$this->assertEquals( array( $menus, 'sanitize_nav_menus_created_posts' ), $setting->sanitize_callback );
766-
$this->assertEquals( $post_ids, $setting->post_value() );
767-
foreach ( $post_ids as $post_id ) {
768-
$this->assertEquals( 'auto-draft', get_post_status( $post_id ) );
823+
$this->assertEquals( $drafted_post_ids, $setting->post_value() );
824+
$this->assertArrayNotHasKey( $private_post_id, $post_ids );
825+
$this->assertArrayNotHasKey( $trashed_post_id, $post_ids );
826+
827+
$this->assertEquals( 'auto-draft', get_post_status( $drafted_post_ids[0] ) );
828+
$this->assertEquals( 'draft', get_post_status( $drafted_post_ids[1] ) );
829+
foreach ( $drafted_post_ids as $post_id ) {
769830
$this->assertEmpty( get_post( $post_id )->post_name );
770831
$this->assertNotEmpty( get_post_meta( $post_id, '_customize_draft_post_name', true ) );
771832
}
772833

773834
$save_action_count = did_action( 'customize_save_nav_menus_created_posts' );
774835
$setting->save();
775836
$this->assertEquals( $save_action_count + 1, did_action( 'customize_save_nav_menus_created_posts' ) );
776-
foreach ( $post_ids as $post_id ) {
837+
foreach ( $drafted_post_ids as $post_id ) {
777838
$this->assertEquals( 'publish', get_post_status( $post_id ) );
778839
$this->assertRegExp( '/^auto-draft-\d+$/', get_post( $post_id )->post_name );
779840
$this->assertEmpty( get_post_meta( $post_id, '_customize_draft_post_name', true ) );
780841
}
781842

843+
$this->assertEquals( 'private', get_post_status( $private_post_id ) );
844+
$this->assertEquals( 'trash', get_post_status( $trashed_post_id ) );
845+
782846
// Ensure that unique slugs were assigned.
783-
$posts = array_map( 'get_post', $post_ids );
847+
$posts = array_map( 'get_post', $drafted_post_ids );
784848
$post_names = wp_list_pluck( $posts, 'post_name' );
785849
$this->assertEqualSets( $post_names, array_unique( $post_names ) );
786850
}

0 commit comments

Comments
 (0)