Make WordPress Core


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.