Skip to content

Commit eb1dff0

Browse files
committed
Query: Prevent get_queried_object() from returning false.
The documented return type is `WP_Term|WP_Post_Type|WP_Post|WP_User|null`, but `get_queried_object()` could return `false` from author queries because `get_userdata()` yields `false` when the resolved author ID is `null` or matches no user. Guard the `get_userdata()` call and its result so `$this->queried_object` stays `null` in those cases. The adjacent posts page branch is hardened the same way, since `get_post()` can return `null` for a missing or deleted page, which previously caused a property access on `null`. Developed in #12069. Follow-up to r52822, r3290. Props tusharbharti, tommusrhodus, yusufmudagal, westonruter. Fixes #65400. git-svn-id: https://develop.svn.wordpress.org/trunk@62476 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 55e3325 commit eb1dff0

2 files changed

Lines changed: 55 additions & 7 deletions

File tree

‎src/wp-includes/class-wp-query.php‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4019,17 +4019,17 @@ public function get_queried_object() {
40194019
}
40204020
} elseif ( $this->is_post_type_archive ) {
40214021
$post_type = $this->get( 'post_type' );
4022-
40234022
if ( is_array( $post_type ) ) {
40244023
$post_type = reset( $post_type );
40254024
}
40264025

40274026
$this->queried_object = get_post_type_object( $post_type );
40284027
} elseif ( $this->is_posts_page ) {
4029-
$page_for_posts = get_option( 'page_for_posts' );
4030-
4031-
$this->queried_object = get_post( $page_for_posts );
4032-
$this->queried_object_id = (int) $this->queried_object->ID;
4028+
$posts_page = get_post( get_option( 'page_for_posts' ) );
4029+
if ( $posts_page ) {
4030+
$this->queried_object = $posts_page;
4031+
$this->queried_object_id = (int) $posts_page->ID;
4032+
}
40334033
} elseif ( $this->is_singular && ! empty( $this->post ) ) {
40344034
$this->queried_object = $this->post;
40354035
$this->queried_object_id = (int) $this->post->ID;
@@ -4041,13 +4041,17 @@ public function get_queried_object() {
40414041
$this->queried_object_id = $author;
40424042
} elseif ( $author_name ) {
40434043
$user = get_user_by( 'slug', $author_name );
4044-
40454044
if ( $user ) {
40464045
$this->queried_object_id = $user->ID;
40474046
}
40484047
}
40494048

4050-
$this->queried_object = get_userdata( $this->queried_object_id );
4049+
if ( $this->queried_object_id ) {
4050+
$user = get_userdata( $this->queried_object_id );
4051+
if ( $user ) {
4052+
$this->queried_object = $user;
4053+
}
4054+
}
40514055
}
40524056

40534057
return $this->queried_object;

‎tests/phpunit/tests/query.php‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,50 @@ public function test_get_queried_object_should_work_for_author_name_before_get_p
723723
$this->assertSame( get_queried_object_id(), $user_id );
724724
}
725725

726+
/**
727+
* @ticket 65400
728+
*
729+
* @covers ::get_queried_object
730+
* @covers WP_Query::get_queried_object
731+
*/
732+
public function test_get_queried_object_should_return_null_when_author_id_is_non_existent(): void {
733+
add_action(
734+
'wp',
735+
static function () {
736+
/** @var WP_Query $wp_query */
737+
global $wp_query;
738+
$wp_query->is_author = true;
739+
$wp_query->set( 'author', 999999 );
740+
}
741+
);
742+
743+
$this->go_to( home_url( '/' ) );
744+
745+
$this->assertNull( get_queried_object() );
746+
}
747+
748+
/**
749+
* @ticket 65400
750+
*
751+
* @covers ::get_queried_object
752+
* @covers WP_Query::get_queried_object
753+
*/
754+
public function test_get_queried_object_should_return_null_when_author_is_unset(): void {
755+
// Trigger is_author without a valid author query var.
756+
add_action(
757+
'wp',
758+
static function () {
759+
/** @var WP_Query $wp_query */
760+
global $wp_query;
761+
$wp_query->is_author = true;
762+
}
763+
);
764+
765+
$this->go_to( home_url( '/' ) );
766+
767+
$this->assertNull( get_queried_object() );
768+
}
769+
726770
/**
727771
* Tests that the `posts_clauses` filter receives an array of clauses
728772
* with the other `posts_*` filters applied, e.g. `posts_join_paged`.

0 commit comments

Comments
 (0)