From 65390cd9cf1d82e15ff80c03966f1ed1022c6dbc Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 15 Oct 2015 06:48:22 +0000 Subject: [PATCH] In `WP::parse_request()` and `url_to_postid()`, if a post slug clashes with a trashed page, return the post instead of the page. Props kovshenin, SergeyBiryukov, igmoweb. Fixes #21970. git-svn-id: https://develop.svn.wordpress.org/trunk@35195 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp.php | 9 +++++++- src/wp-includes/rewrite-functions.php | 9 +++++++- tests/phpunit/tests/rewrite.php | 32 +++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp.php b/src/wp-includes/class-wp.php index bba47ffb71..4acadadca1 100644 --- a/src/wp-includes/class-wp.php +++ b/src/wp-includes/class-wp.php @@ -209,8 +209,15 @@ class WP { if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) { // This is a verbose page match, let's check to be sure about it. - if ( ! get_page_by_path( $matches[ $varmatch[1] ] ) ) + $page = get_page_by_path( $matches[ $varmatch[1] ] ); + if ( ! $page ) { continue; + } + + $post_status_obj = get_post_status_object( $page->post_status ); + if ( ! $post_status_obj->public && ! $post_status_obj->protected && ! $post_status_obj->private ) { + continue; + } } // Got a match. diff --git a/src/wp-includes/rewrite-functions.php b/src/wp-includes/rewrite-functions.php index a42535b4a4..2f9be5ad99 100644 --- a/src/wp-includes/rewrite-functions.php +++ b/src/wp-includes/rewrite-functions.php @@ -397,8 +397,15 @@ function url_to_postid( $url ) { if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) { // This is a verbose page match, let's check to be sure about it. - if ( ! get_page_by_path( $matches[ $varmatch[1] ] ) ) + $page = get_page_by_path( $matches[ $varmatch[1] ] ); + if ( ! $page ) { continue; + } + + $post_status_obj = get_post_status_object( $page->post_status ); + if ( ! $post_status_obj->public && ! $post_status_obj->protected && ! $post_status_obj->private ) { + continue; + } } // Got a match. diff --git a/tests/phpunit/tests/rewrite.php b/tests/phpunit/tests/rewrite.php index 601b4cd528..02b3441a2b 100644 --- a/tests/phpunit/tests/rewrite.php +++ b/tests/phpunit/tests/rewrite.php @@ -306,4 +306,36 @@ class Tests_Rewrite extends WP_UnitTestCase { $this->assertQueryTrue( 'is_front_page', 'is_page', 'is_singular' ); $this->assertFalse( is_home() ); } + + /** + * @ticket 21970 + */ + function test_url_to_postid_with_post_slug_that_clashes_with_a_trashed_page() { + $this->set_permalink_structure( '/%postname%/' ); + + $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_status' => 'trash' ) ); + $post_id = $this->factory->post->create( array( 'post_title' => get_post( $page_id )->post_title ) ); + + $this->assertEquals( $post_id, url_to_postid( get_permalink( $post_id ) ) ); + + $this->set_permalink_structure(); + } + + /** + * @ticket 21970 + */ + function test_parse_request_with_post_slug_that_clashes_with_a_trashed_page() { + $this->set_permalink_structure( '/%postname%/' ); + + $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_status' => 'trash' ) ); + $post_id = $this->factory->post->create( array( 'post_title' => get_post( $page_id )->post_title ) ); + + $this->go_to( get_permalink( $post_id ) ); + + $this->assertTrue( is_single() ); + $this->assertFalse( is_404() ); + + $this->set_permalink_structure(); + } + }