Make WordPress Core

Opened 2 weeks ago

Last modified 2 weeks ago

#63522 new defect (bug)

Warnings related to get_queried_object returning null

Reported by: dd32's profile dd32 Owned by:
Milestone: Awaiting Review Priority: low
Severity: minor Version:
Component: General Keywords: has-patch needs-unit-tests
Focuses: Cc:

Description (last modified by dd32)

Similar to #29660 and resolved for a lot of things in [54496], get_body_class() and wp_list_pages() throw many PHP warnings due to missing post objects when the request is singular, but no post is located.

These include in get_body_class():

E_WARNING: Attempt to read property "ID" on null in wp-includes/post-template.php:676
E_WARNING: Attempt to read property "post_type" on null in wp-includes/post-template.php:677

and in wp_list_pages():

E_WARNING: Attempt to read property "post_type" on null in wp-includes/post-template.php:1356

and again in feed_links_extra():

E_WARNING: Attempt to read property "comment_count" on null in wp-includes/general-template.php:3351
E_WARNING: Attempt to read property "ID" on null in wp-includes/general-template.php:3359

(although in this case, it's not get_queried_object(), just an expectation of get_post(0) working, which is effectively the same thing)

and finally, in redirect_canonical(), when the permalink to a post can't be found:

Warning: Undefined array key "scheme" in wp-includes/canonical.php on line 752
Warning: Undefined array key "host" in wp-includes/canonical.php on line xxx

Change History (4)

This ticket was mentioned in PR #8892 on WordPress/wordpress-develop by @dd32.


2 weeks ago
#1

  • Keywords has-patch added

#2 @dd32
2 weeks ago

  • Keywords needs-unit-tests added

Some tests would be good here, similar to those in [54496].

#3 @dd32
2 weeks ago

  • Description modified (diff)

#4 @dd32
2 weeks ago

  • Description modified (diff)

Added an extra bonus in redirect_canonical()
https://github.com/dd32/wordpress-develop/commit/648ad2a505e1817c9424800feacc06b0801fe630

redirect_canonical(): Don't attempt a ?page to /page/ redirect if the base url is unknown.
When redirect_canonical() attempts to redirect from ?page=2 to /page/2/ if the base-url is unknown the result is an attempt to redirect to /2/ which through a series of unfortunate assumptions in the code (Only full URLs are expected) results in an attempt to redirect to ://hostname.example/2/ and causes a series of php warnings such as:

Warning: Undefined array key "scheme" in wp-includes/canonical.php on line 752
Warning: Undefined array key "host" in wp-includes/canonical.php on line 717
Warning: Undefined array key "host" in wp-includes/canonical.php on line 728
Warning: Undefined array key "host" in wp-includes/canonical.php on line 731

By only attempting to perform the singular pagination redirects when we actually have a singular permalink to redirect to, it prevents a chain of awkward warnings that appear unrelated.

Unfortunately I couldn't immediately figure out how to trigger it on a vanilla install, but here's the type of request from GlotPress:

GET https://translate.wordpress.test/projects/meta/wordpress-org/en-au/default/?name=blahblahblah&page=2

which eventually attempts:

redirect_canonical( $requested_url = '://translate.wordpress.test/2/?name=blahblahblah', $do_redirect = FALSE )

As such, there's an alternate way that could be avoided, and may work better in some situations, but the above change resolves the main warning.

  • wp-includes/canonical.php

     
    615618                unset( $redirect['port'] );
    616619        }
    617620
     621        // If the redirect scheme is unknown, default to the current scheme.
     622        if ( empty( $redirect['scheme'] ) ) {
     623                $redirect['scheme'] = is_ssl() ? 'https' : 'http';
     624        }
     625
    618626        // Trailing /index.php.
    619627        $redirect['path'] = preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path'] );
Note: See TracTickets for help on using tickets.