From 5bc27525241d425fed20ebf67ca7b84205b8cb43 Mon Sep 17 00:00:00 2001 From: Jake Spurlock Date: Thu, 23 Sep 2021 20:26:28 +0000 Subject: [PATCH] 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 --- src/wp-admin/includes/ms.php | 3 + .../tests/multisite/msPermalinkCollision.php | 82 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 tests/phpunit/tests/multisite/msPermalinkCollision.php diff --git a/src/wp-admin/includes/ms.php b/src/wp-admin/includes/ms.php index 7796745a2e..76af47f8af 100644 --- a/src/wp-admin/includes/ms.php +++ b/src/wp-admin/includes/ms.php @@ -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; diff --git a/tests/phpunit/tests/multisite/msPermalinkCollision.php b/tests/phpunit/tests/multisite/msPermalinkCollision.php new file mode 100644 index 0000000000..1a8c25a75c --- /dev/null +++ b/tests/phpunit/tests/multisite/msPermalinkCollision.php @@ -0,0 +1,82 @@ +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;