Query: Avoid PHP notices when get_queried_object() returns null.

`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
This commit is contained in:
Sergey Biryukov 2022-10-11 18:13:34 +00:00
parent bb0ef7cb0e
commit aa11f4cef1
2 changed files with 130 additions and 0 deletions

View File

@ -3963,6 +3963,9 @@ class WP_Query {
$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;
@ -3996,6 +3999,9 @@ class WP_Query {
}
$author_obj = $this->get_queried_object();
if ( ! $author_obj ) {
return false;
}
$author = array_map( 'strval', (array) $author );
@ -4032,6 +4038,9 @@ class WP_Query {
}
$cat_obj = $this->get_queried_object();
if ( ! $cat_obj ) {
return false;
}
$category = array_map( 'strval', (array) $category );
@ -4068,6 +4077,9 @@ class WP_Query {
}
$tag_obj = $this->get_queried_object();
if ( ! $tag_obj ) {
return false;
}
$tag = array_map( 'strval', (array) $tag );
@ -4315,6 +4327,9 @@ class WP_Query {
}
$page_obj = $this->get_queried_object();
if ( ! $page_obj ) {
return false;
}
$page = array_map( 'strval', (array) $page );
@ -4422,6 +4437,9 @@ class WP_Query {
}
$post_obj = $this->get_queried_object();
if ( ! $post_obj ) {
return false;
}
$post = array_map( 'strval', (array) $post );
@ -4469,6 +4487,9 @@ class WP_Query {
}
$post_obj = $this->get_queried_object();
if ( ! $post_obj ) {
return false;
}
return in_array( $post_obj->post_type, (array) $post_types, true );
}

View File

@ -788,4 +788,113 @@ class Tests_Query extends WP_UnitTestCase {
$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' ) );
}
}