Posts, Post Types: Don't add a trailing number when there is a unique post parent.

WordPress tries to avoid an issue where slugs might match an existing slug of a page/post. 
If we are in a hierarchical post type, there will be a level, and we can leave it the same.

Props stormrockwell, SergeyBiryukov, terriann, tubys, jeremyfelt, Daschmi, MaximeCulea, knutsp, whyisjake.
Fixes #51147.
See also #44112 and #45260.


git-svn-id: https://develop.svn.wordpress.org/trunk@51855 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jake Spurlock 2021-09-23 20:26:28 +00:00
parent 36ed6d5107
commit 5bc2752524
2 changed files with 85 additions and 0 deletions

View File

@ -736,6 +736,9 @@ function avoid_blog_page_permalink_collision( $data, $postarr ) {
if ( ! is_main_site() ) {
return $data;
}
if ( isset( $data['post_parent'] ) && $data['post_parent'] ) {
return $data;
}
$post_name = $data['post_name'];
$c = 0;

View File

@ -0,0 +1,82 @@
<?php
if ( is_multisite() ) :
/**
* Tests specific to `avoid_blog_page_permalink_collision()` in multisite.
*
* @group multisite
* @group only
*/
class Tests_Multisite_MS_Permalink_Collision extends WP_UnitTestCase {
protected $suppress = false;
protected static $site_id;
protected static $root_page;
protected static $child_page;
protected static $post_and_blog_path = 'permalink-collison';
/**
* Create a blog and the pages we need to test the collision.
*/
public static function wpSetUpBeforeClass( $factory ) {
self::$site_id = self::factory()->blog->create(
array(
'path' => '/' . self::$post_and_blog_path,
)
);
self::$root_page = self::factory()->post->create_and_get(
array(
'post_type' => 'page',
'post_title' => 'Bar',
'post_name' => self::$post_and_blog_path,
)
);
self::$child_page = self::factory()->post->create_and_get(
array(
'post_parent' => self::$root_page->ID,
'post_type' => 'page',
'post_title' => 'Bar',
'post_name' => self::$post_and_blog_path,
)
);
}
public function setUp() {
global $wpdb;
parent::setUp();
$this->suppress = $wpdb->suppress_errors();
}
public function tearDown() {
global $wpdb;
$wpdb->suppress_errors( $this->suppress );
parent::tearDown();
}
/**
* Delete blog and pages we created.
*/
public static function wpTearDownAfterClass() {
wp_delete_site( self::$site_id );
wp_delete_post( self::$root_page->ID );
wp_delete_post( self::$child_page->ID );
}
public function test_avoid_blog_page_permalink_collision_renames_post_name() {
$this->assertNotEquals( self::$post_and_blog_path, self::$root_page->post_name );
}
/**
* Ensure `avoid_blog_page_permalink_collision()` doesn't rename child pages post_name.
*
* @ticket 51147
*/
public function test_avoid_blog_page_permalink_collision_doesnt_rename_child_pages() {
$this->assertEquals( self::$post_and_blog_path, self::$child_page->post_name );
}
}
endif;