From 3b379031066b00e5186ca10cf850ccf606ba398f Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 5 Dec 2016 19:33:42 +0000 Subject: [PATCH] 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 --- wp-includes/class-wp-customize-manager.php | 9 +++++++-- wp-includes/class-wp-customize-nav-menus.php | 17 ++++++++++++++++- wp-includes/post.php | 6 +++--- wp-includes/version.php | 2 +- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/wp-includes/class-wp-customize-manager.php b/wp-includes/class-wp-customize-manager.php index 440a53712c..127d572be7 100644 --- a/wp-includes/class-wp-customize-manager.php +++ b/wp-includes/class-wp-customize-manager.php @@ -1006,7 +1006,11 @@ final class WP_Customize_Manager { 'posts_per_page' => -1, ) ); 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( - 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( '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; } 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; diff --git a/wp-includes/class-wp-customize-nav-menus.php b/wp-includes/class-wp-customize-nav-menus.php index 1305a147e0..ca1b72f7eb 100644 --- a/wp-includes/class-wp-customize-nav-menus.php +++ b/wp-includes/class-wp-customize-nav-menus.php @@ -803,6 +803,11 @@ final class WP_Customize_Nav_Menus { if ( empty( $postarr['post_name'] ) ) { $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 ); $r = wp_insert_post( wp_slash( $postarr ), true ); @@ -1192,9 +1197,19 @@ final class WP_Customize_Nav_Menus { if ( ! empty( $post_ids ) ) { foreach ( $post_ids as $post_id ) { $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. - 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' ); } } } diff --git a/wp-includes/post.php b/wp-includes/post.php index 1496d6d1af..c81f5c589e 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -3678,7 +3678,7 @@ function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p if ( 'attachment' == $post_type ) { // 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 ) ); /** @@ -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 * 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 ) ); /** @@ -3730,7 +3730,7 @@ function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p } } else { // 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 ) ); // Prevent new post slugs that could result in URLs that conflict with date archives. diff --git a/wp-includes/version.php b/wp-includes/version.php index 63c6c5842b..feddecdf01 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @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.