Code Modernization: Check the return type of parse_url() in WP::parse_request().

As per the PHP manual:
> If the `component` parameter is omitted, an associative array is returned.
> If the `component` parameter is specified, `parse_url()` returns a string (or an int, in the case of `PHP_URL_PORT`) instead of an array. If the requested component doesn't exist within the given URL, `null` will be returned.

Reference: [https://www.php.net/manual/en/function.parse-url.php#refsect1-function.parse-url-returnvalues PHP Manual: parse_url(): Return Values]

In this case, `parse_url()` is called with the `PHP_URL_PATH` as `$component`. This will return `null` in the majority of cases, as – exсept for subdirectory-based sites – `home_url()` returns a URL without the trailing slash, like `http://example.org`.

The return value of `parse_url()` was subsequently passed to `trim()`, leading to a `trim(): Passing null to parameter #1 ($string) of type string is deprecated` notice on PHP 8.1.

Fixed by adjusting the logic flow to:
* Only pass the return value of `parse_url()` to follow-on functions if it makes sense, i.e. if it isn't `null`, nor an empty string.
* Preventing calls to `preg_replace()` and `trim()` further down in the function logic flow, when `preg_replace()`/`trim()` would have nothing to do anyhow.

Follow-up to [25617].

Props jrf, hellofromTonya, SergeyBiryukov.
See #53635.

git-svn-id: https://develop.svn.wordpress.org/trunk@51622 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2021-08-16 20:16:13 +00:00
parent c9df4f75a2
commit 18bda14e1e
2 changed files with 58 additions and 8 deletions

View File

@ -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.

View File

@ -0,0 +1,42 @@
<?php
/**
* @group wp
* @covers WP::parse_request
*/
class Tests_WP_ParseRequest extends WP_UnitTestCase {
/**
* @var WP
*/
protected $wp;
public function set_up() {
parent::set_up();
$this->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 );
}
}