From 8fe059d426e8f33168ab44e6e63f99eb1f2f80d7 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Thu, 20 Nov 2014 16:45:48 +0000 Subject: [PATCH] Correct an SQL syntax error introduced in r30158. Adds tests. Fixes #30339 See #18962 Props julien731 git-svn-id: https://develop.svn.wordpress.org/trunk@30480 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post.php | 2 +- tests/phpunit/tests/post.php | 61 ++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 81cd12a12e..39eb6a35e9 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -3720,7 +3720,7 @@ function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p $suffix = 2; do { $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; - $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID, $post_parent ) ); + $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID, $post_parent ) ); $suffix++; } while ( $post_name_check ); $slug = $alt_post_name; diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index 94f6817463..0962f92bbd 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -1017,6 +1017,67 @@ class Tests_Post extends WP_UnitTestCase { _unregister_post_type( 'post-type-2' ); } + /** + * @ticket 30339 + */ + function test_wp_unique_post_slug_with_hierarchy() { + 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['post_name'] = 'some-slug-2'; + $two = $this->factory->post->create( $args ); + + $this->assertEquals( 'some-slug', get_post( $one )->post_name ); + $this->assertEquals( 'some-slug-2', get_post( $two )->post_name ); + + $this->assertEquals( 'some-slug-3', wp_unique_post_slug( 'some-slug', 0, 'publish', 'post-type-1', 0 ) ); + + _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 */