diff --git a/src/wp-includes/class-wp.php b/src/wp-includes/class-wp.php index 8c7c48b3b2..770df083e7 100644 --- a/src/wp-includes/class-wp.php +++ b/src/wp-includes/class-wp.php @@ -170,8 +170,13 @@ class WP { list( $req_uri ) = explode( '?', $_SERVER['REQUEST_URI'] ); $self = $_SERVER['PHP_SELF']; - $home_path = trim( parse_url( home_url(), PHP_URL_PATH ), '/' ); - $home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) ); + + $home_path = parse_url( home_url(), PHP_URL_PATH ); + $home_path_regex = ''; + if ( is_string( $home_path ) && '' !== $home_path ) { + $home_path = trim( $home_path, '/' ); + $home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) ); + } /* * Trim path info from the end and the leading home path from the front. @@ -180,14 +185,17 @@ class WP { */ $req_uri = str_replace( $pathinfo, '', $req_uri ); $req_uri = trim( $req_uri, '/' ); - $req_uri = preg_replace( $home_path_regex, '', $req_uri ); - $req_uri = trim( $req_uri, '/' ); - $pathinfo = trim( $pathinfo, '/' ); - $pathinfo = preg_replace( $home_path_regex, '', $pathinfo ); $pathinfo = trim( $pathinfo, '/' ); $self = trim( $self, '/' ); - $self = preg_replace( $home_path_regex, '', $self ); - $self = trim( $self, '/' ); + + if ( ! empty( $home_path_regex ) ) { + $req_uri = preg_replace( $home_path_regex, '', $req_uri ); + $req_uri = trim( $req_uri, '/' ); + $pathinfo = preg_replace( $home_path_regex, '', $pathinfo ); + $pathinfo = trim( $pathinfo, '/' ); + $self = preg_replace( $home_path_regex, '', $self ); + $self = trim( $self, '/' ); + } // The requested permalink is in $pathinfo for path info requests and // $req_uri for other requests. diff --git a/tests/phpunit/tests/wp/parseRequest.php b/tests/phpunit/tests/wp/parseRequest.php new file mode 100644 index 0000000000..2f022d7d2d --- /dev/null +++ b/tests/phpunit/tests/wp/parseRequest.php @@ -0,0 +1,42 @@ +wp = new WP(); + } + + /** + * Test that PHP 8.1 "passing null to non-nullable" deprecation notice + * is not thrown when the home URL has no path/trailing slash (default setup). + * + * Note: This does not test the actual functioning of the parse_request() method. + * It just and only tests for/against the deprecation notice. + * + * @ticket 53635 + */ + public function test_no_deprecation_notice_when_home_url_has_no_path() { + // Make sure rewrite rules are not empty. + $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); + + // Make sure the test will function independently of whatever the test user set in wp-tests-config.php. + add_filter( + 'home_url', + static function ( $url ) { + return 'http://example.org'; + } + ); + + $this->wp->parse_request(); + $this->assertSame( '', $this->wp->request ); + } +}