Make WordPress Core

Changeset 34073


Ignore:
Timestamp:
09/12/2015 03:36:12 AM (11 years ago)
Author:
boonebgorges
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/query.php

    r33963 r34073  
    39273927
    39283928                $this->queried_object = null;
    3929                 $this->queried_object_id = 0;
     3929                $this->queried_object_id = null;
    39303930
    39313931                if ( $this->is_category || $this->is_tag || $this->is_tax ) {
  • trunk/tests/phpunit/tests/query.php

    r33850 r34073  
    418418                $this->assertEqualSets( array( $p1, $p2, $p3 ), wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' ) );
    419419        }
     420
     421        /**
     422         * @ticket 31355
     423         */
     424        public function test_pages_dont_404_when_queried_post_id_is_modified() {
     425                $post_id = $this->factory->post->create( array( 'post_title' => 'A Test Page', 'post_type' => 'page' ) );
     426
     427                add_action( 'parse_query', array( $this, 'filter_parse_query_to_modify_queried_post_id' ) );
     428
     429                $url = get_permalink( $post_id );
     430                $this->go_to( $url );
     431
     432                remove_action( 'parse_query', array( $this, 'filter_parse_query_to_modify_queried_post_id' ) );
     433
     434                $this->assertFalse( $GLOBALS['wp_query']->is_404() );
     435                $this->assertEquals( $post_id, $GLOBALS['wp_query']->post->ID );
     436        }
     437
     438        /**
     439         * @ticket 31355
     440         */
     441        public function test_custom_hierarchical_post_types_404_when_queried_post_id_is_modified() {
     442                global $wp_rewrite;
     443
     444                register_post_type( 'guide', array( 'name' => 'Guide', 'public' => true, 'hierarchical' => true ) );
     445                $wp_rewrite->flush_rules();
     446                $post_id = $this->factory->post->create( array( 'post_title' => 'A Test Guide', 'post_type' => 'guide' ) );
     447
     448                add_action( 'parse_query', array( $this, 'filter_parse_query_to_modify_queried_post_id' ) );
     449
     450                $url = get_permalink( $post_id );
     451                $this->go_to( $url );
     452
     453                remove_action( 'parse_query', array( $this, 'filter_parse_query_to_modify_queried_post_id' ) );
     454
     455                $this->assertFalse( $GLOBALS['wp_query']->is_404() );
     456                $this->assertEquals( $post_id, $GLOBALS['wp_query']->post->ID );
     457        }
     458
     459        public function filter_parse_query_to_modify_queried_post_id( $query ) {
     460                $post = get_queried_object();
     461        }
    420462}
Note: See TracChangeset for help on using the changeset viewer.