diff --git src/wp-includes/post.php src/wp-includes/post.php
index 6c58c85..d2d4cb0 100644
|
|
|
function wp_insert_post( $postarr, $wp_error = false ) { |
| 3450 | 3450 | } |
| 3451 | 3451 | |
| 3452 | 3452 | if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) { |
| 3453 | | $data['post_name'] = sanitize_title( $data['post_title'], $post_ID ); |
| | 3453 | $data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'], $post_ID ), $post_ID, $data['post_status'], $post_type, $post_parent ); |
| 3454 | 3454 | $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where ); |
| 3455 | 3455 | } |
| 3456 | 3456 | |
| … |
… |
function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p |
| 3815 | 3815 | $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1"; |
| 3816 | 3816 | $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) ); |
| 3817 | 3817 | |
| 3818 | | // Prevent post slugs that could result in URLs that conflict with date archives. |
| | 3818 | // Prevent new post slugs that could result in URLs that conflict with date archives. |
| | 3819 | $post = get_post( $post_ID ); |
| 3819 | 3820 | $conflicts_with_date_archive = false; |
| 3820 | | if ( 'post' === $post_type && preg_match( '/^[0-9]+$/', $slug ) && $slug_num = intval( $slug ) ) { |
| | 3821 | if ( 'post' === $post_type && ( ! $post || $post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) && $slug_num = intval( $slug ) ) { |
| 3821 | 3822 | $permastructs = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) ); |
| 3822 | 3823 | $postname_index = array_search( '%postname%', $permastructs ); |
| 3823 | 3824 | |
diff --git tests/phpunit/tests/post.php tests/phpunit/tests/post.php
index 2122d03..22310c0 100644
|
|
|
class Tests_Post extends WP_UnitTestCase { |
| 412 | 412 | } |
| 413 | 413 | |
| 414 | 414 | /** |
| | 415 | * @ticket 5305 |
| | 416 | */ |
| | 417 | 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() { |
| | 418 | global $wp_rewrite; |
| | 419 | $wp_rewrite->init(); |
| | 420 | $wp_rewrite->set_permalink_structure( '/%postname%/' ); |
| | 421 | $wp_rewrite->flush_rules(); |
| | 422 | |
| | 423 | $p = wp_insert_post( array( |
| | 424 | 'post_title' => '', |
| | 425 | 'post_content' => 'test', |
| | 426 | 'post_status' => 'publish', |
| | 427 | 'post_type' => 'post', |
| | 428 | ) ); |
| | 429 | |
| | 430 | $post = get_post( $p ); |
| | 431 | |
| | 432 | $wp_rewrite->set_permalink_structure( '' ); |
| | 433 | |
| | 434 | $this->assertEquals( "$p-2", $post->post_name ); |
| | 435 | } |
| | 436 | |
| | 437 | /** |
| 415 | 438 | * @ticket 5364 |
| 416 | 439 | */ |
| 417 | 440 | function test_delete_future_post_cron() { |
diff --git tests/phpunit/tests/post/wpUniquePostSlug.php tests/phpunit/tests/post/wpUniquePostSlug.php
index 3256172..a7c3060 100644
|
|
|
class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { |
| 189 | 189 | /** |
| 190 | 190 | * @ticket 5305 |
| 191 | 191 | */ |
| | 192 | public function test_slugs_resulting_in_permalinks_that_resemble_year_archives_should_not_be_suffixed_for_already_published_posts() { |
| | 193 | global $wp_rewrite; |
| | 194 | $wp_rewrite->init(); |
| | 195 | $wp_rewrite->set_permalink_structure( '/%postname%/' ); |
| | 196 | $wp_rewrite->flush_rules(); |
| | 197 | |
| | 198 | $p = $this->factory->post->create( array( |
| | 199 | 'post_type' => 'post', |
| | 200 | 'post_name' => 'foo', |
| | 201 | 'post_status' => 'publish', |
| | 202 | ) ); |
| | 203 | |
| | 204 | $found = wp_unique_post_slug( '2015', $p, 'publish', 'post', 0 ); |
| | 205 | $this->assertEquals( '2015-2', $found ); |
| | 206 | |
| | 207 | $wp_rewrite->set_permalink_structure( '' ); |
| | 208 | flush_rewrite_rules(); |
| | 209 | } |
| | 210 | |
| | 211 | /** |
| | 212 | * @ticket 5305 |
| | 213 | */ |
| 192 | 214 | public function test_yearlike_slugs_should_not_be_suffixed_if_permalink_structure_does_not_result_in_a_clash_with_year_archives() { |
| 193 | 215 | global $wp_rewrite; |
| 194 | 216 | $wp_rewrite->init(); |