Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}
}

Expand All @@ -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;
Expand Down
101 changes: 101 additions & 0 deletions tests/phpunit/tests/post/wpUniquePostSlug.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading