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. git-svn-id: https://develop.svn.wordpress.org/trunk@39506 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
2253e42f06
commit
c4bffeb619
@ -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;
|
||||||
|
@ -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' );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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.
|
||||||
|
@ -601,7 +601,8 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
|
|||||||
$post = get_post( $response['data']['post_id'] );
|
$post = get_post( $response['data']['post_id'] );
|
||||||
$this->assertEquals( 'Hello World', $post->post_title );
|
$this->assertEquals( 'Hello World', $post->post_title );
|
||||||
$this->assertEquals( 'post', $post->post_type );
|
$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 ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -453,8 +453,13 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
|
|||||||
$this->assertEquals( 'inherit', $post->post_status );
|
$this->assertEquals( 'inherit', $post->post_status );
|
||||||
} else {
|
} else {
|
||||||
$this->assertEquals( 'auto-draft', $post->post_status );
|
$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( array( 'waffles', 'canola', 'home', 'about', 'blog', 'custom' ), array_keys( $posts_by_name ) );
|
||||||
$this->assertEquals( 'Custom', get_post( $posts_by_name['custom'] )->post_title );
|
$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'] ) );
|
$this->assertEquals( '', get_post_thumbnail_id( $posts_by_name['blog'] ) );
|
||||||
$attachment_metadata = wp_get_attachment_metadata( $posts_by_name['waffles'] );
|
$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( $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->assertArrayHasKey( 'file', $attachment_metadata );
|
||||||
$this->assertContains( 'waffles', $attachment_metadata['file'] );
|
$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.
|
// Ensure that re-importing doesn't cause auto-drafts to balloon.
|
||||||
$wp_customize->import_theme_starter_content();
|
$wp_customize->import_theme_starter_content();
|
||||||
$changeset_data = $wp_customize->changeset_data();
|
$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.
|
// Test that saving non-starter content on top of the changeset clears the starter_content flag.
|
||||||
$wp_customize->save_changeset_post( array(
|
$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( 'canola', get_custom_logo() );
|
||||||
$this->assertContains( 'waffles', get_header_image() );
|
$this->assertContains( 'waffles', get_header_image() );
|
||||||
$this->assertContains( 'waffles', get_background_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 ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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' ) );
|
$r = $menus->insert_auto_draft_post( array( 'post_title' => 'Hello World', 'post_type' => 'post' ) );
|
||||||
$this->assertInstanceOf( 'WP_Post', $r );
|
$this->assertInstanceOf( 'WP_Post', $r );
|
||||||
$this->assertEquals( 'Hello World', $r->post_title );
|
$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 );
|
$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' ) );
|
$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->assertInstanceOf( 'WP_Post', $r );
|
||||||
$this->assertEquals( 'Hello World', $r->post_title );
|
$this->assertEquals( 'Hello World', $r->post_title );
|
||||||
$this->assertEquals( 'post', $r->post_type );
|
$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 );
|
$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 );
|
$menus = new WP_Customize_Nav_Menus( $this->wp_customize );
|
||||||
do_action( 'customize_register', $this->wp_customize );
|
do_action( 'customize_register', $this->wp_customize );
|
||||||
|
|
||||||
$post_ids = $this->factory()->post->create_many( 3, array(
|
$post_ids = array();
|
||||||
'post_status' => 'auto-draft',
|
for ( $i = 0; $i < 3; $i += 1 ) {
|
||||||
'post_type' => 'post',
|
$r = $menus->insert_auto_draft_post( array(
|
||||||
'post_name' => 'auto-draft',
|
'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' ) );
|
$pre_published_post_id = $this->factory()->post->create( array( 'post_status' => 'publish' ) );
|
||||||
|
|
||||||
$setting_id = 'nav_menus_created_posts';
|
$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() );
|
$this->assertEquals( $post_ids, $setting->post_value() );
|
||||||
foreach ( $post_ids as $post_id ) {
|
foreach ( $post_ids as $post_id ) {
|
||||||
$this->assertEquals( 'auto-draft', get_post_status( $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' );
|
$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' ) );
|
$this->assertEquals( $save_action_count + 1, did_action( 'customize_save_nav_menus_created_posts' ) );
|
||||||
foreach ( $post_ids as $post_id ) {
|
foreach ( $post_ids as $post_id ) {
|
||||||
$this->assertEquals( 'publish', get_post_status( $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.
|
// Ensure that unique slugs were assigned.
|
||||||
|
@ -347,46 +347,4 @@ class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase {
|
|||||||
$found = wp_unique_post_slug( 'embed', $p, 'publish', 'attachment', 0 );
|
$found = wp_unique_post_slug( 'embed', $p, 'publish', 'attachment', 0 );
|
||||||
$this->assertSame( 'embed-2', $found );
|
$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 ) );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user