From 5ef83659d0d27fd2caf4d92446fd63b7a169371e Mon Sep 17 00:00:00 2001 From: Jb Audras Date: Mon, 10 Oct 2022 23:01:08 +0000 Subject: [PATCH] Query: Prevent PHP notice when `get_post_type_object()` returns `null` in `is_post_type_archive()`. This changeset avoids potential PHP notices caused by `get_post_type_object()` returning `null` when called inside `is_post_type_archive()`. Props sean212, costdev. Fixes #56287. git-svn-id: https://develop.svn.wordpress.org/trunk@54464 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-query.php | 4 ++++ tests/phpunit/tests/query.php | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index 8525987454..e48d942f0c 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -3935,6 +3935,10 @@ class WP_Query { } $post_type_object = get_post_type_object( $post_type ); + if ( ! $post_type_object ) { + return false; + } + return in_array( $post_type_object->name, (array) $post_types, true ); } diff --git a/tests/phpunit/tests/query.php b/tests/phpunit/tests/query.php index fef3e19748..a6162b8a0a 100644 --- a/tests/phpunit/tests/query.php +++ b/tests/phpunit/tests/query.php @@ -769,4 +769,23 @@ class Tests_Query extends WP_UnitTestCase { $this->assertArrayHasKey( 'join', $posts_clauses_request ); $this->assertSame( '/* posts_join_request */', $posts_clauses_request['join'] ); } + + /** + * Tests that is_post_type_archive() returns false for an undefined post type. + * + * @ticket 56287 + * + * @covers ::is_post_type_archive + */ + public function test_is_post_type_archive_should_return_false_for_an_undefined_post_type() { + global $wp_query; + + $post_type = '56287-post-type'; + + // Force the request to be a post type archive. + $wp_query->is_post_type_archive = true; + $wp_query->set( 'post_type', $post_type ); + + $this->assertFalse( is_post_type_archive( $post_type ) ); + } }