From b7c8dbf4a200d3b101a02ecab26bbeb87f4fe5ca Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sat, 12 Sep 2015 03:36:12 +0000 Subject: [PATCH] Better default values in `WP_Query::get_queried_object()`. Setting the default value of the `queried_object_id` property to `0` meant that, when called early enough in the WP bootstrap, `get_queried_object()` could short-circuit the normal query by fooling it into thinking that the request was for a page with id 0. Setting the default value to `null` instead avoids this problem. Props gradyetc, jazbek. Fixes #31355. git-svn-id: https://develop.svn.wordpress.org/trunk@34073 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/query.php | 2 +- tests/phpunit/tests/query.php | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 270d0eb5dc..ba9ac64d44 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -3926,7 +3926,7 @@ class WP_Query { return $this->queried_object; $this->queried_object = null; - $this->queried_object_id = 0; + $this->queried_object_id = null; if ( $this->is_category || $this->is_tag || $this->is_tax ) { if ( $this->is_category ) { diff --git a/tests/phpunit/tests/query.php b/tests/phpunit/tests/query.php index 64734e3f64..4bbf882d65 100644 --- a/tests/phpunit/tests/query.php +++ b/tests/phpunit/tests/query.php @@ -417,4 +417,46 @@ class Tests_Query extends WP_UnitTestCase { $this->assertEqualSets( array( $p1, $p2, $p3 ), wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' ) ); } + + /** + * @ticket 31355 + */ + public function test_pages_dont_404_when_queried_post_id_is_modified() { + $post_id = $this->factory->post->create( array( 'post_title' => 'A Test Page', 'post_type' => 'page' ) ); + + add_action( 'parse_query', array( $this, 'filter_parse_query_to_modify_queried_post_id' ) ); + + $url = get_permalink( $post_id ); + $this->go_to( $url ); + + remove_action( 'parse_query', array( $this, 'filter_parse_query_to_modify_queried_post_id' ) ); + + $this->assertFalse( $GLOBALS['wp_query']->is_404() ); + $this->assertEquals( $post_id, $GLOBALS['wp_query']->post->ID ); + } + + /** + * @ticket 31355 + */ + public function test_custom_hierarchical_post_types_404_when_queried_post_id_is_modified() { + global $wp_rewrite; + + register_post_type( 'guide', array( 'name' => 'Guide', 'public' => true, 'hierarchical' => true ) ); + $wp_rewrite->flush_rules(); + $post_id = $this->factory->post->create( array( 'post_title' => 'A Test Guide', 'post_type' => 'guide' ) ); + + add_action( 'parse_query', array( $this, 'filter_parse_query_to_modify_queried_post_id' ) ); + + $url = get_permalink( $post_id ); + $this->go_to( $url ); + + remove_action( 'parse_query', array( $this, 'filter_parse_query_to_modify_queried_post_id' ) ); + + $this->assertFalse( $GLOBALS['wp_query']->is_404() ); + $this->assertEquals( $post_id, $GLOBALS['wp_query']->post->ID ); + } + + public function filter_parse_query_to_modify_queried_post_id( $query ) { + $post = get_queried_object(); + } }