Check attachments as well as the current post type when checking for duplicate post slugs. This avoids creating a page with a slug which conflicts with an existing attachment (the inverse is already handled).

Updates the existing test for pages which should take priority over attachments in cases where an existing clash exists.

Fixes #18962
Props boonebgorges


git-svn-id: https://develop.svn.wordpress.org/trunk@30629 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn 2014-11-30 04:48:54 +00:00
parent c1d0555b32
commit 76a9abd7a7
2 changed files with 46 additions and 1 deletions

View File

@ -3703,7 +3703,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_name = %s AND post_type = %s 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 ) );
/** /**

View File

@ -504,10 +504,17 @@ class Tests_Post extends WP_UnitTestCase {
* @ticket 15665 * @ticket 15665
*/ */
function test_get_page_by_path_priority() { function test_get_page_by_path_priority() {
global $wpdb;
$attachment = $this->factory->post->create_and_get( array( 'post_title' => 'some-page', 'post_type' => 'attachment' ) ); $attachment = $this->factory->post->create_and_get( array( 'post_title' => 'some-page', 'post_type' => 'attachment' ) );
$page = $this->factory->post->create_and_get( array( 'post_title' => 'some-page', 'post_type' => 'page' ) ); $page = $this->factory->post->create_and_get( array( 'post_title' => 'some-page', 'post_type' => 'page' ) );
$other_att = $this->factory->post->create_and_get( array( 'post_title' => 'some-other-page', 'post_type' => 'attachment' ) ); $other_att = $this->factory->post->create_and_get( array( 'post_title' => 'some-other-page', 'post_type' => 'attachment' ) );
$wpdb->update( $wpdb->posts, array( 'post_name' => 'some-page' ), array( 'ID' => $page->ID ) );
clean_post_cache( $page->ID );
$page = get_post( $page->ID );
$this->assertEquals( 'some-page', $attachment->post_name ); $this->assertEquals( 'some-page', $attachment->post_name );
$this->assertEquals( 'some-page', $page->post_name ); $this->assertEquals( 'some-page', $page->post_name );
@ -987,6 +994,44 @@ class Tests_Post extends WP_UnitTestCase {
_unregister_post_type( 'post-type-1' ); _unregister_post_type( 'post-type-1' );
} }
/**
* @ticket 18962
*/
function test_wp_unique_post_slug_with_hierarchy_and_attachments() {
register_post_type( 'post-type-1', array( 'hierarchical' => true ) );
$args = array(
'post_type' => 'post-type-1',
'post_name' => 'some-slug',
'post_status' => 'publish',
);
$one = $this->factory->post->create( $args );
$args = array(
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
'post_name' => 'image'
);
$attachment = $this->factory->attachment->create_object( 'image.jpg', $one, $args );
$args = array(
'post_type' => 'post-type-1',
'post_name' => 'image',
'post_status' => 'publish',
'post_parent' => $one
);
$two = $this->factory->post->create( $args );
$this->assertEquals( 'some-slug', get_post( $one )->post_name );
$this->assertEquals( 'image', get_post( $attachment )->post_name );
$this->assertEquals( 'image-2', get_post( $two )->post_name );
// 'image' can be a child of image-2
$this->assertEquals( 'image', wp_unique_post_slug( 'image', 0, 'publish', 'post-type-1', $two ) );
_unregister_post_type( 'post-type-1' );
}
/** /**
* @ticket 21212 * @ticket 21212
*/ */