From ad163d354735aa9f7a7ba59110ac562245f2fe96 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 10 Sep 2015 16:53:52 +0000 Subject: [PATCH] In `get_page_uri()`, do not add parent slugs to orphaned pages. Adds unit test. Props filosofo, MikeHansenMe, MikeHansenMe, chriscct7. Fixes #15963. git-svn-id: https://develop.svn.wordpress.org/trunk@34001 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post-functions.php | 5 ++++- tests/phpunit/tests/post.php | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/post-functions.php b/src/wp-includes/post-functions.php index 7f9a5460f3..bc084c46fa 100644 --- a/src/wp-includes/post-functions.php +++ b/src/wp-includes/post-functions.php @@ -4196,7 +4196,10 @@ function get_page_uri( $page ) { $uri = $page->post_name; foreach ( $page->ancestors as $parent ) { - $uri = get_post( $parent )->post_name . '/' . $uri; + $parent = get_post( $parent ); + if ( 'publish' === $parent->post_status ) { + $uri = $parent->post_name . '/' . $uri; + } } return $uri; diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index ac2ca71ce5..d6ce0309e6 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -650,6 +650,24 @@ class Tests_Post extends WP_UnitTestCase { $this->assertFalse( get_page_uri( $post_id ) ); } + /** + * @ticket 15963 + */ + function test_get_post_uri_check_orphan() { + $parent_id = $this->factory->post->create( array( 'post_name' => 'parent' ) ); + $child_id = $this->factory->post->create( array( 'post_name' => 'child', 'post_parent' => $parent_id ) ); + + // check the parent for good measure + $this->assertEquals( 'parent', get_page_uri( $parent_id ) ); + + // try the child normally + $this->assertEquals( 'parent/child', get_page_uri( $child_id ) ); + + // now delete the parent and check + wp_delete_post( $parent_id ); + $this->assertEquals( 'child', get_page_uri( $child_id ) ); + } + /** * @ticket 23708 */