Skip to content

Commit

Permalink
Query: Avoid PHP notices when get_queried_object() returns null.
Browse files Browse the repository at this point in the history
`WP_Query` methods assume that `get_queried_object()` would return a non-null value, which is not always the case.

This commit resolves various warnings in `WP_Query` along the lines of:
{{{
Attempt to read property "post_type" on null in wp-includes/class-wp-query.php on line 4338
}}}

Follow-up to [1728], [3639], [8807], [49119].

Props dd32, yellyc, boonebgorges, darkskipper, Howdy_McGee, swissspidy, nacin, mikeschroder, mikejolley, sterlo, datainterlock, utsavmadaan823, kanlukasz, woji29911, hellofromTonya, zikubd, deksar, bwbama, noplanman, nouarah, SergeyBiryukov.
Fixes #29660.

git-svn-id: https://develop.svn.wordpress.org/trunk@54496 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov authored and = committed Nov 4, 2022
1 parent 4f4b4b5 commit 9d7ce91
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/wp-includes/class-wp-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -3963,6 +3963,9 @@ public function is_attachment( $attachment = '' ) {
$attachment = array_map( 'strval', (array) $attachment );

$post_obj = $this->get_queried_object();
if ( ! $post_obj ) {
return false;
}

if ( in_array( (string) $post_obj->ID, $attachment, true ) ) {
return true;
Expand Down Expand Up @@ -3996,6 +3999,9 @@ public function is_author( $author = '' ) {
}

$author_obj = $this->get_queried_object();
if ( ! $author_obj ) {
return false;
}

$author = array_map( 'strval', (array) $author );

Expand Down Expand Up @@ -4032,6 +4038,9 @@ public function is_category( $category = '' ) {
}

$cat_obj = $this->get_queried_object();
if ( ! $cat_obj ) {
return false;
}

$category = array_map( 'strval', (array) $category );

Expand Down Expand Up @@ -4068,6 +4077,9 @@ public function is_tag( $tag = '' ) {
}

$tag_obj = $this->get_queried_object();
if ( ! $tag_obj ) {
return false;
}

$tag = array_map( 'strval', (array) $tag );

Expand Down Expand Up @@ -4315,6 +4327,9 @@ public function is_page( $page = '' ) {
}

$page_obj = $this->get_queried_object();
if ( ! $page_obj ) {
return false;
}

$page = array_map( 'strval', (array) $page );

Expand Down Expand Up @@ -4422,6 +4437,9 @@ public function is_single( $post = '' ) {
}

$post_obj = $this->get_queried_object();
if ( ! $post_obj ) {
return false;
}

$post = array_map( 'strval', (array) $post );

Expand Down Expand Up @@ -4469,6 +4487,9 @@ public function is_singular( $post_types = '' ) {
}

$post_obj = $this->get_queried_object();
if ( ! $post_obj ) {
return false;
}

return in_array( $post_obj->post_type, (array) $post_types, true );
}
Expand Down
109 changes: 109 additions & 0 deletions tests/phpunit/tests/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -788,4 +788,113 @@ public function test_is_post_type_archive_should_return_false_for_an_undefined_p

$this->assertFalse( is_post_type_archive( $post_type ) );
}

/**
* @ticket 29660
*/
public function test_query_singular_404_does_not_throw_warning() {
$q = new WP_Query(
array(
'pagename' => 'non-existent-page',
)
);

$this->assertSame( 0, $q->post_count );
$this->assertFalse( $q->is_single() );

$this->assertTrue( $q->is_singular() );
$this->assertFalse( $q->is_singular( 'page' ) );

$this->assertTrue( $q->is_page() );
$this->assertFalse( $q->is_page( 'non-existent-page' ) );
}

/**
* @ticket 29660
*/
public function test_query_single_404_does_not_throw_warning() {
$q = new WP_Query(
array(
'name' => 'non-existent-post',
)
);

$this->assertSame( 0, $q->post_count );
$this->assertFalse( $q->is_page() );

$this->assertTrue( $q->is_singular() );
$this->assertFalse( $q->is_singular( 'post' ) );

$this->assertTrue( $q->is_single() );
$this->assertFalse( $q->is_single( 'non-existent-post' ) );
}

/**
* @ticket 29660
*/
public function test_query_attachment_404_does_not_throw_warning() {
$q = new WP_Query(
array(
'attachment' => 'non-existent-attachment',
)
);

$this->assertSame( 0, $q->post_count );

$this->assertTrue( $q->is_singular() );
$this->assertFalse( $q->is_singular( 'attachment' ) );

$this->assertTrue( $q->is_attachment() );
$this->assertFalse( $q->is_attachment( 'non-existent-attachment' ) );
}

/**
* @ticket 29660
*/
public function test_query_author_404_does_not_throw_warning() {
$q = new WP_Query(
array(
'author_name' => 'non-existent-author',
)
);

$this->assertSame( 0, $q->post_count );

$this->assertTrue( $q->is_author() );
$this->assertFalse( $q->is_author( 'non-existent-author' ) );
}

/**
* @ticket 29660
*/
public function test_query_category_404_does_not_throw_warning() {
$q = new WP_Query(
array(
'category_name' => 'non-existent-category',
)
);

$this->assertSame( 0, $q->post_count );

$this->assertTrue( $q->is_category() );
$this->assertFalse( $q->is_tax() );
$this->assertFalse( $q->is_category( 'non-existent-category' ) );
}

/**
* @ticket 29660
*/
public function test_query_tag_404_does_not_throw_warning() {
$q = new WP_Query(
array(
'tag' => 'non-existent-tag',
)
);

$this->assertSame( 0, $q->post_count );

$this->assertTrue( $q->is_tag() );
$this->assertFalse( $q->is_tax() );
$this->assertFalse( $q->is_tag( 'non-existent-tag' ) );
}
}

0 comments on commit 9d7ce91

Please sign in to comment.