Skip to content

Commit bc669c4

Browse files
committed
PHP 8.1: WP::parse_request(): prevent "passing null to non-nullable" notice
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. Ref: https://www.php.net/manual/en/function.parse-url.php#refsect1-function.parse-url-returnvalues In this case, `parse_url()` is called with the `PHP_URL_PATH` as `$component`. This will return `null` in the majority of cases, as - expect for subdirectory-based sites - `home_url()` will return a URL without 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` deprecation 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.
1 parent 37e5e52 commit bc669c4

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/wp-includes/class-wp.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,13 @@ public function parse_request( $extra_query_vars = '' ) {
170170

171171
list( $req_uri ) = explode( '?', $_SERVER['REQUEST_URI'] );
172172
$self = $_SERVER['PHP_SELF'];
173-
$home_path = trim( parse_url( home_url(), PHP_URL_PATH ), '/' );
174-
$home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) );
173+
174+
$home_path = parse_url( home_url(), PHP_URL_PATH );
175+
$home_path_regex = '';
176+
if ( is_string( $home_path ) && '' !== $home_path ) {
177+
$home_path = trim( $home_path, '/' );
178+
$home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) );
179+
}
175180

176181
/*
177182
* Trim path info from the end and the leading home path from the front.
@@ -180,14 +185,17 @@ public function parse_request( $extra_query_vars = '' ) {
180185
*/
181186
$req_uri = str_replace( $pathinfo, '', $req_uri );
182187
$req_uri = trim( $req_uri, '/' );
183-
$req_uri = preg_replace( $home_path_regex, '', $req_uri );
184-
$req_uri = trim( $req_uri, '/' );
185-
$pathinfo = trim( $pathinfo, '/' );
186-
$pathinfo = preg_replace( $home_path_regex, '', $pathinfo );
187188
$pathinfo = trim( $pathinfo, '/' );
188189
$self = trim( $self, '/' );
189-
$self = preg_replace( $home_path_regex, '', $self );
190-
$self = trim( $self, '/' );
190+
191+
if ( ! empty( $home_path_regex ) ) {
192+
$req_uri = preg_replace( $home_path_regex, '', $req_uri );
193+
$req_uri = trim( $req_uri, '/' );
194+
$pathinfo = preg_replace( $home_path_regex, '', $pathinfo );
195+
$pathinfo = trim( $pathinfo, '/' );
196+
$self = preg_replace( $home_path_regex, '', $self );
197+
$self = trim( $self, '/' );
198+
}
191199

192200
// The requested permalink is in $pathinfo for path info requests and
193201
// $req_uri for other requests.

0 commit comments

Comments
 (0)