From c718849baac4ac6efd4fd625f8330e11df8e769e Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Tue, 14 Jul 2015 12:27:03 +0000 Subject: [PATCH] When creating a new post with an empty `post_name` and `post_title`, don't generate a `post_name` that conflicts with a date archive permalink. See #5305. git-svn-id: https://develop.svn.wordpress.org/trunk@33261 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post.php | 2 +- tests/phpunit/tests/post.php | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index cd54a34324..04d624af2f 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -3451,7 +3451,7 @@ function wp_insert_post( $postarr, $wp_error = false ) { } if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) { - $data['post_name'] = sanitize_title( $data['post_title'], $post_ID ); + $data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'], $post_ID ), $post_ID, $data['post_status'], $post_type, $post_parent ); $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where ); } diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index 2122d03599..22310c0aad 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -411,6 +411,29 @@ class Tests_Post extends WP_UnitTestCase { $this->assertEquals(false, $this->_next_schedule_for_post('publish_future_post', $id)); } + /** + * @ticket 5305 + */ + public function test_wp_insert_post_should_not_allow_a_bare_numeric_slug_that_might_conflict_with_a_date_archive_when_generating_from_an_empty_post_title() { + global $wp_rewrite; + $wp_rewrite->init(); + $wp_rewrite->set_permalink_structure( '/%postname%/' ); + $wp_rewrite->flush_rules(); + + $p = wp_insert_post( array( + 'post_title' => '', + 'post_content' => 'test', + 'post_status' => 'publish', + 'post_type' => 'post', + ) ); + + $post = get_post( $p ); + + $wp_rewrite->set_permalink_structure( '' ); + + $this->assertEquals( "$p-2", $post->post_name ); + } + /** * @ticket 5364 */