Make WordPress Core

Changeset 54496


Ignore:
Timestamp:
10/11/2022 06:13:34 PM (4 years ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

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

    r54464 r54496  
    39643964
    39653965                $post_obj = $this->get_queried_object();
     3966                if ( ! $post_obj ) {
     3967                        return false;
     3968                }
    39663969
    39673970                if ( in_array( (string) $post_obj->ID, $attachment, true ) ) {
     
    39974000
    39984001                $author_obj = $this->get_queried_object();
     4002                if ( ! $author_obj ) {
     4003                        return false;
     4004                }
    39994005
    40004006                $author = array_map( 'strval', (array) $author );
     
    40334039
    40344040                $cat_obj = $this->get_queried_object();
     4041                if ( ! $cat_obj ) {
     4042                        return false;
     4043                }
    40354044
    40364045                $category = array_map( 'strval', (array) $category );
     
    40694078
    40704079                $tag_obj = $this->get_queried_object();
     4080                if ( ! $tag_obj ) {
     4081                        return false;
     4082                }
    40714083
    40724084                $tag = array_map( 'strval', (array) $tag );
     
    43164328
    43174329                $page_obj = $this->get_queried_object();
     4330                if ( ! $page_obj ) {
     4331                        return false;
     4332                }
    43184333
    43194334                $page = array_map( 'strval', (array) $page );
     
    44234438
    44244439                $post_obj = $this->get_queried_object();
     4440                if ( ! $post_obj ) {
     4441                        return false;
     4442                }
    44254443
    44264444                $post = array_map( 'strval', (array) $post );
     
    44704488
    44714489                $post_obj = $this->get_queried_object();
     4490                if ( ! $post_obj ) {
     4491                        return false;
     4492                }
    44724493
    44734494                return in_array( $post_obj->post_type, (array) $post_types, true );
  • trunk/tests/phpunit/tests/query.php

    r54464 r54496  
    789789                $this->assertFalse( is_post_type_archive( $post_type ) );
    790790        }
     791
     792        /**
     793         * @ticket 29660
     794         */
     795        public function test_query_singular_404_does_not_throw_warning() {
     796                $q = new WP_Query(
     797                        array(
     798                                'pagename' => 'non-existent-page',
     799                        )
     800                );
     801
     802                $this->assertSame( 0, $q->post_count );
     803                $this->assertFalse( $q->is_single() );
     804
     805                $this->assertTrue( $q->is_singular() );
     806                $this->assertFalse( $q->is_singular( 'page' ) );
     807
     808                $this->assertTrue( $q->is_page() );
     809                $this->assertFalse( $q->is_page( 'non-existent-page' ) );
     810        }
     811
     812        /**
     813         * @ticket 29660
     814         */
     815        public function test_query_single_404_does_not_throw_warning() {
     816                $q = new WP_Query(
     817                        array(
     818                                'name' => 'non-existent-post',
     819                        )
     820                );
     821
     822                $this->assertSame( 0, $q->post_count );
     823                $this->assertFalse( $q->is_page() );
     824
     825                $this->assertTrue( $q->is_singular() );
     826                $this->assertFalse( $q->is_singular( 'post' ) );
     827
     828                $this->assertTrue( $q->is_single() );
     829                $this->assertFalse( $q->is_single( 'non-existent-post' ) );
     830        }
     831
     832        /**
     833         * @ticket 29660
     834         */
     835        public function test_query_attachment_404_does_not_throw_warning() {
     836                $q = new WP_Query(
     837                        array(
     838                                'attachment' => 'non-existent-attachment',
     839                        )
     840                );
     841
     842                $this->assertSame( 0, $q->post_count );
     843
     844                $this->assertTrue( $q->is_singular() );
     845                $this->assertFalse( $q->is_singular( 'attachment' ) );
     846
     847                $this->assertTrue( $q->is_attachment() );
     848                $this->assertFalse( $q->is_attachment( 'non-existent-attachment' ) );
     849        }
     850
     851        /**
     852         * @ticket 29660
     853         */
     854        public function test_query_author_404_does_not_throw_warning() {
     855                $q = new WP_Query(
     856                        array(
     857                                'author_name' => 'non-existent-author',
     858                        )
     859                );
     860
     861                $this->assertSame( 0, $q->post_count );
     862
     863                $this->assertTrue( $q->is_author() );
     864                $this->assertFalse( $q->is_author( 'non-existent-author' ) );
     865        }
     866
     867        /**
     868         * @ticket 29660
     869         */
     870        public function test_query_category_404_does_not_throw_warning() {
     871                $q = new WP_Query(
     872                        array(
     873                                'category_name' => 'non-existent-category',
     874                        )
     875                );
     876
     877                $this->assertSame( 0, $q->post_count );
     878
     879                $this->assertTrue( $q->is_category() );
     880                $this->assertFalse( $q->is_tax() );
     881                $this->assertFalse( $q->is_category( 'non-existent-category' ) );
     882        }
     883
     884        /**
     885         * @ticket 29660
     886         */
     887        public function test_query_tag_404_does_not_throw_warning() {
     888                $q = new WP_Query(
     889                        array(
     890                                'tag' => 'non-existent-tag',
     891                        )
     892                );
     893
     894                $this->assertSame( 0, $q->post_count );
     895
     896                $this->assertTrue( $q->is_tag() );
     897                $this->assertFalse( $q->is_tax() );
     898                $this->assertFalse( $q->is_tag( 'non-existent-tag' ) );
     899        }
    791900}
Note: See TracChangeset for help on using the changeset viewer.