diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 5806d879f1c5e..14a284c940507 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -5904,8 +5904,9 @@ function wp_unique_post_slug( $slug, $post_id, $post_status, $post_type, $post_p $post = get_post( $post_id ); - // Prevent new post slugs that could result in URLs that conflict with date archives. - $conflicts_with_date_archive = false; + // Prevent new post slugs that could result in URLs that conflict with date archives or category pagination. + $conflicts_with_date_archive = false; + $conflicts_with_category_pagination = false; if ( 'post' === $post_type && ( ! $post || $post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) ) { $slug_num = (int) $slug; @@ -5926,6 +5927,11 @@ function wp_unique_post_slug( $slug, $post_id, $post_status, $post_type, $post_p ) { $conflicts_with_date_archive = true; } + + // A numeric slug after a hierarchical category is matched as the page number of the category. + if ( $postname_index && '%category%' === $permastructs[ $postname_index - 1 ] ) { + $conflicts_with_category_pagination = true; + } } } @@ -5943,6 +5949,7 @@ function wp_unique_post_slug( $slug, $post_id, $post_status, $post_type, $post_p if ( $post_name_check || in_array( $slug, $feeds, true ) || 'embed' === $slug || $conflicts_with_date_archive + || $conflicts_with_category_pagination || $is_bad_flat_slug ) { $suffix = 2; diff --git a/tests/phpunit/tests/post/wpUniquePostSlug.php b/tests/phpunit/tests/post/wpUniquePostSlug.php index 4f290d2d7a74b..24ed89987f97a 100644 --- a/tests/phpunit/tests/post/wpUniquePostSlug.php +++ b/tests/phpunit/tests/post/wpUniquePostSlug.php @@ -328,6 +328,107 @@ public function test_daylike_slugs_should_not_be_suffixed_for_invalid_day_number $this->assertSame( '32', $found ); } + /** + * @ticket 37812 + */ + public function test_numeric_slugs_should_be_suffixed_when_postname_follows_category() { + $this->set_permalink_structure( '/%category%/%postname%/' ); + + $p = self::factory()->post->create( + array( + 'post_type' => 'post', + 'post_name' => 'foo', + ) + ); + + $found = wp_unique_post_slug( '12345', $p, 'publish', 'post', 0 ); + $this->assertSame( '12345-2', $found ); + } + + /** + * @ticket 37812 + */ + public function test_numeric_slugs_should_not_be_suffixed_when_postname_does_not_follow_category() { + $this->set_permalink_structure( '/%category%/%post_id%/%postname%/' ); + + $p = self::factory()->post->create( + array( + 'post_type' => 'post', + 'post_name' => 'foo', + ) + ); + + $found = wp_unique_post_slug( '12345', $p, 'publish', 'post', 0 ); + $this->assertSame( '12345', $found ); + } + + /** + * @ticket 37812 + */ + public function test_non_numeric_slugs_should_not_be_suffixed_when_postname_follows_category() { + $this->set_permalink_structure( '/%category%/%postname%/' ); + + $p = self::factory()->post->create( + array( + 'post_type' => 'post', + 'post_name' => 'foo', + ) + ); + + $found = wp_unique_post_slug( '12345abc', $p, 'publish', 'post', 0 ); + $this->assertSame( '12345abc', $found ); + } + + /** + * @ticket 37812 + */ + public function test_numeric_slugs_should_not_be_suffixed_for_existing_posts_when_postname_follows_category() { + global $wpdb; + + $this->set_permalink_structure( '/%category%/%postname%/' ); + + $p = self::factory()->post->create( + array( + 'post_type' => 'post', + 'post_name' => 'foo', + ) + ); + $wpdb->update( $wpdb->posts, array( 'post_name' => '12345' ), array( 'ID' => $p ) ); + clean_post_cache( $p ); + + $found = wp_unique_post_slug( '12345', $p, 'publish', 'post', 0 ); + $this->assertSame( '12345', $found ); + } + + /** + * @ticket 37812 + */ + public function test_post_with_numeric_title_in_child_category_should_be_reachable() { + $this->set_permalink_structure( '/%category%/%postname%/' ); + + $parent_id = self::factory()->category->create( array( 'slug' => 'parent' ) ); + $child_id = self::factory()->category->create( + array( + 'slug' => 'child', + 'parent' => $parent_id, + ) + ); + + $post_id = self::factory()->post->create( + array( + 'post_type' => 'post', + 'post_status' => 'publish', + 'post_title' => '12345', + 'post_category' => array( $child_id ), + ) + ); + + $this->go_to( get_permalink( $post_id ) ); + + $this->assertQueryTrue( 'is_single', 'is_singular' ); + $this->assertSame( $post_id, get_queried_object_id() ); + } + /** * @ticket 34971 */