diff --git a/src/wp-includes/class-wp-customize-manager.php b/src/wp-includes/class-wp-customize-manager.php index 440a53712c..127d572be7 100644 --- a/src/wp-includes/class-wp-customize-manager.php +++ b/src/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/src/wp-includes/class-wp-customize-nav-menus.php b/src/wp-includes/class-wp-customize-nav-menus.php index 1305a147e0..ca1b72f7eb 100644 --- a/src/wp-includes/class-wp-customize-nav-menus.php +++ b/src/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/src/wp-includes/post.php b/src/wp-includes/post.php index 1496d6d1af..c81f5c589e 100644 --- a/src/wp-includes/post.php +++ b/src/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/tests/phpunit/tests/ajax/CustomizeMenus.php b/tests/phpunit/tests/ajax/CustomizeMenus.php index fef7831dfc..7dc2bf1f7a 100644 --- a/tests/phpunit/tests/ajax/CustomizeMenus.php +++ b/tests/phpunit/tests/ajax/CustomizeMenus.php @@ -601,7 +601,8 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase { $post = get_post( $response['data']['post_id'] ); $this->assertEquals( 'Hello World', $post->post_title ); $this->assertEquals( 'post', $post->post_type ); - $this->assertEquals( 'hello-world', $post->post_name ); + $this->assertEquals( '', $post->post_name ); + $this->assertEquals( 'hello-world', get_post_meta( $post->ID, '_customize_draft_post_name', true ) ); } /** diff --git a/tests/phpunit/tests/customize/manager.php b/tests/phpunit/tests/customize/manager.php index 345e666b36..39f304f04e 100644 --- a/tests/phpunit/tests/customize/manager.php +++ b/tests/phpunit/tests/customize/manager.php @@ -453,8 +453,13 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase { $this->assertEquals( 'inherit', $post->post_status ); } else { $this->assertEquals( 'auto-draft', $post->post_status ); + $this->assertEmpty( $post->post_name ); } - $posts_by_name[ $post->post_name ] = $post->ID; + $post_name = $post->post_name; + if ( empty( $post_name ) ) { + $post_name = get_post_meta( $post->ID, '_customize_draft_post_name', true ); + } + $posts_by_name[ $post_name ] = $post->ID; } $this->assertEquals( array( 'waffles', 'canola', 'home', 'about', 'blog', 'custom' ), array_keys( $posts_by_name ) ); $this->assertEquals( 'Custom', get_post( $posts_by_name['custom'] )->post_title ); @@ -464,6 +469,7 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase { $this->assertEquals( '', get_post_thumbnail_id( $posts_by_name['blog'] ) ); $attachment_metadata = wp_get_attachment_metadata( $posts_by_name['waffles'] ); $this->assertEquals( 'Waffles', get_post( $posts_by_name['waffles'] )->post_title ); + $this->assertEquals( 'waffles', get_post_meta( $posts_by_name['waffles'], '_customize_draft_post_name', true ) ); $this->assertArrayHasKey( 'file', $attachment_metadata ); $this->assertContains( 'waffles', $attachment_metadata['file'] ); @@ -488,7 +494,7 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase { // Ensure that re-importing doesn't cause auto-drafts to balloon. $wp_customize->import_theme_starter_content(); $changeset_data = $wp_customize->changeset_data(); - $this->assertEqualSets( array_values( $posts_by_name ), $changeset_data['nav_menus_created_posts']['value'], 'Auto-drafts should not get re-created and amended with each import.' ); + $this->assertEqualSets( array_values( $posts_by_name ), $changeset_data['nav_menus_created_posts']['value'] ); // Auto-drafts should not get re-created and amended with each import. // Test that saving non-starter content on top of the changeset clears the starter_content flag. $wp_customize->save_changeset_post( array( @@ -539,6 +545,8 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase { $this->assertContains( 'canola', get_custom_logo() ); $this->assertContains( 'waffles', get_header_image() ); $this->assertContains( 'waffles', get_background_image() ); + $this->assertEquals( 'waffles', get_post( $posts_by_name['waffles'] )->post_name ); + $this->assertEmpty( get_post_meta( $posts_by_name['waffles'], '_customize_draft_post_name', true ) ); } /** diff --git a/tests/phpunit/tests/customize/nav-menus.php b/tests/phpunit/tests/customize/nav-menus.php index f8b38a181f..e68ab0539a 100644 --- a/tests/phpunit/tests/customize/nav-menus.php +++ b/tests/phpunit/tests/customize/nav-menus.php @@ -549,14 +549,16 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { $r = $menus->insert_auto_draft_post( array( 'post_title' => 'Hello World', 'post_type' => 'post' ) ); $this->assertInstanceOf( 'WP_Post', $r ); $this->assertEquals( 'Hello World', $r->post_title ); - $this->assertEquals( 'hello-world', $r->post_name ); + $this->assertEquals( '', $r->post_name ); + $this->assertEquals( 'hello-world', get_post_meta( $r->ID, '_customize_draft_post_name', 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' ) ); $this->assertInstanceOf( 'WP_Post', $r ); $this->assertEquals( 'Hello World', $r->post_title ); $this->assertEquals( 'post', $r->post_type ); - $this->assertEquals( 'greetings-world', $r->post_name ); + $this->assertEquals( '', $r->post_name ); + $this->assertEquals( 'greetings-world', get_post_meta( $r->ID, '_customize_draft_post_name', true ) ); $this->assertEquals( 'Hi World', $r->post_content ); } @@ -739,11 +741,17 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { $menus = new WP_Customize_Nav_Menus( $this->wp_customize ); do_action( 'customize_register', $this->wp_customize ); - $post_ids = $this->factory()->post->create_many( 3, array( - 'post_status' => 'auto-draft', - 'post_type' => 'post', - 'post_name' => 'auto-draft', - ) ); + $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; + } + $pre_published_post_id = $this->factory()->post->create( array( 'post_status' => 'publish' ) ); $setting_id = 'nav_menus_created_posts'; @@ -754,6 +762,8 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { $this->assertEquals( $post_ids, $setting->post_value() ); foreach ( $post_ids as $post_id ) { $this->assertEquals( 'auto-draft', get_post_status( $post_id ) ); + $this->assertEmpty( get_post( $post_id )->post_name ); + $this->assertNotEmpty( get_post_meta( $post_id, '_customize_draft_post_name', true ) ); } $save_action_count = did_action( 'customize_save_nav_menus_created_posts' ); @@ -761,6 +771,8 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { $this->assertEquals( $save_action_count + 1, did_action( 'customize_save_nav_menus_created_posts' ) ); foreach ( $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 ) ); } // Ensure that unique slugs were assigned. diff --git a/tests/phpunit/tests/post/wpUniquePostSlug.php b/tests/phpunit/tests/post/wpUniquePostSlug.php index a728454da6..536017db31 100644 --- a/tests/phpunit/tests/post/wpUniquePostSlug.php +++ b/tests/phpunit/tests/post/wpUniquePostSlug.php @@ -347,46 +347,4 @@ class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { $found = wp_unique_post_slug( 'embed', $p, 'publish', 'attachment', 0 ); $this->assertSame( 'embed-2', $found ); } - - /** - * @ticket 38928 - */ - public function test_non_unique_slugs_for_existing_auto_draft_posts() { - $auto_draft_post_id = self::factory()->post->create( array( - 'post_type' => 'post', - 'post_name' => 'existing-post', - 'post_status' => 'auto-draft', - ) ); - $auto_draft_page_id = self::factory()->post->create( array( - 'post_type' => 'page', - 'post_name' => 'existing-page', - 'post_status' => 'auto-draft', - ) ); - $auto_draft_attachment_id = self::factory()->attachment->create_object( 'image.jpg', $auto_draft_page_id, array( - 'post_mime_type' => 'image/jpeg', - 'post_type' => 'attachment', - 'post_name' => 'existing-attachment', - 'post_status' => 'auto-draft', - ) ); - - $post_id = self::factory()->post->create( array( 'post_type' => 'post' ) ); - $page_id = self::factory()->post->create( array( 'post_type' => 'page' ) ); - $attachment_id = self::factory()->attachment->create_object( 'image2.jpg', $page_id, array( - 'post_mime_type' => 'image/jpeg', - 'post_type' => 'attachment', - 'post_name' => 'existing-image', - ) ); - - $this->assertEquals( 'existing-post', wp_unique_post_slug( 'existing-post', $post_id, 'publish', get_post_type( $post_id ), 0 ) ); - wp_publish_post( $auto_draft_post_id ); - $this->assertEquals( 'existing-post-2', wp_unique_post_slug( 'existing-post', $post_id, 'publish', get_post_type( $post_id ), 0 ) ); - - $this->assertEquals( 'existing-page', wp_unique_post_slug( 'existing-page', $page_id, 'publish', get_post_type( $page_id ), 0 ) ); - wp_publish_post( $auto_draft_page_id ); - $this->assertEquals( 'existing-page-2', wp_unique_post_slug( 'existing-page', $page_id, 'publish', get_post_type( $page_id ), 0 ) ); - - $this->assertEquals( 'existing-attachment', wp_unique_post_slug( 'existing-attachment', $attachment_id, 'publish', get_post_type( $attachment_id ), 0 ) ); - wp_publish_post( $auto_draft_attachment_id ); - $this->assertEquals( 'existing-attachment-2', wp_unique_post_slug( 'existing-attachment', $attachment_id, 'publish', get_post_type( $attachment_id ), 0 ) ); - } }