Customize: Defer populating `post_name` for `auto-draft` posts in customized state until posts are published.

The ultimate `post_name` is stored in postmeta until the post is published. The `get_page_by_path()` function does not exclude `auto-draft` posts. Revert changes to `wp_unique_post_slug()` from [39411] which excluded `auto-draft` posts.

Props westonruter, dlh for testing, helen for testing.
See #38114, #38928.
Fixes #39078.

Built from https://develop.svn.wordpress.org/trunk@39506


git-svn-id: http://core.svn.wordpress.org/trunk@39446 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Weston Ruter 2016-12-05 19:33:42 +00:00
parent 6a3de4d857
commit 3b37903106
4 changed files with 27 additions and 7 deletions

View File

@ -1006,7 +1006,11 @@ final class WP_Customize_Manager {
'posts_per_page' => -1, 'posts_per_page' => -1,
) ); ) );
foreach ( $existing_posts_query->posts as $existing_post ) { foreach ( $existing_posts_query->posts as $existing_post ) {
$existing_starter_content_posts[ $existing_post->post_type . ':' . $existing_post->post_name ] = $existing_post; $post_name = $existing_post->post_name;
if ( empty( $post_name ) ) {
$post_name = get_post_meta( $existing_post->ID, '_customize_draft_post_name', true );
}
$existing_starter_content_posts[ $existing_post->post_type . ':' . $post_name ] = $existing_post;
} }
} }
@ -1067,7 +1071,7 @@ final class WP_Customize_Manager {
} }
$attachment_post_data = array_merge( $attachment_post_data = array_merge(
wp_array_slice_assoc( $attachment, array( 'post_title', 'post_content', 'post_excerpt', 'post_name' ) ), wp_array_slice_assoc( $attachment, array( 'post_title', 'post_content', 'post_excerpt' ) ),
array( array(
'post_status' => 'auto-draft', // So attachment will be garbage collected in a week if changeset is never published. 'post_status' => 'auto-draft', // So attachment will be garbage collected in a week if changeset is never published.
) )
@ -1085,6 +1089,7 @@ final class WP_Customize_Manager {
continue; continue;
} }
update_post_meta( $attachment_id, '_starter_content_theme', $this->get_stylesheet() ); update_post_meta( $attachment_id, '_starter_content_theme', $this->get_stylesheet() );
update_post_meta( $attachment_id, '_customize_draft_post_name', $attachment['post_name'] );
} }
$attachment_ids[ $symbol ] = $attachment_id; $attachment_ids[ $symbol ] = $attachment_id;

View File

@ -803,6 +803,11 @@ final class WP_Customize_Nav_Menus {
if ( empty( $postarr['post_name'] ) ) { if ( empty( $postarr['post_name'] ) ) {
$postarr['post_name'] = sanitize_title( $postarr['post_title'] ); $postarr['post_name'] = sanitize_title( $postarr['post_title'] );
} }
if ( ! isset( $postarr['meta_input'] ) ) {
$postarr['meta_input'] = array();
}
$postarr['meta_input']['_customize_draft_post_name'] = $postarr['post_name'];
unset( $postarr['post_name'] );
add_filter( 'wp_insert_post_empty_content', '__return_false', 1000 ); add_filter( 'wp_insert_post_empty_content', '__return_false', 1000 );
$r = wp_insert_post( wp_slash( $postarr ), true ); $r = wp_insert_post( wp_slash( $postarr ), true );
@ -1192,9 +1197,19 @@ final class WP_Customize_Nav_Menus {
if ( ! empty( $post_ids ) ) { if ( ! empty( $post_ids ) ) {
foreach ( $post_ids as $post_id ) { foreach ( $post_ids as $post_id ) {
$target_status = 'attachment' === get_post_type( $post_id ) ? 'inherit' : 'publish'; $target_status = 'attachment' === get_post_type( $post_id ) ? 'inherit' : 'publish';
$args = array(
'ID' => $post_id,
'post_status' => $target_status,
);
$post_name = get_post_meta( $post_id, '_customize_draft_post_name', true );
if ( $post_name ) {
$args['post_name'] = $post_name;
}
// Note that wp_publish_post() cannot be used because unique slugs need to be assigned. // Note that wp_publish_post() cannot be used because unique slugs need to be assigned.
wp_update_post( array( 'ID' => $post_id, 'post_status' => $target_status ) ); wp_update_post( wp_slash( $args ) );
delete_post_meta( $post_id, '_customize_draft_post_name' );
} }
} }
} }

View File

@ -3678,7 +3678,7 @@ function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p
if ( 'attachment' == $post_type ) { if ( 'attachment' == $post_type ) {
// Attachment slugs must be unique across all types. // Attachment slugs must be unique across all types.
$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_status != 'auto-draft' AND post_name = %s AND ID != %d LIMIT 1"; $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1";
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) ); $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) );
/** /**
@ -3706,7 +3706,7 @@ function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p
* Page slugs must be unique within their own trees. Pages are in a separate * Page slugs must be unique within their own trees. Pages are in a separate
* namespace than posts so page slugs are allowed to overlap post slugs. * namespace than posts so page slugs are allowed to overlap post slugs.
*/ */
$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_status != 'auto-draft' AND post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1"; $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1";
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) ); $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) );
/** /**
@ -3730,7 +3730,7 @@ function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p
} }
} else { } else {
// Post slugs must be unique across all posts. // Post slugs must be unique across all posts.
$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_status != 'auto-draft' AND post_name = %s AND post_type = %s AND ID != %d LIMIT 1"; $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) ); $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) );
// Prevent new post slugs that could result in URLs that conflict with date archives. // Prevent new post slugs that could result in URLs that conflict with date archives.

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.8-alpha-39504'; $wp_version = '4.8-alpha-39506';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.