Skip to content

Commit 18bda14

Browse files
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
1 parent c9df4f7 commit 18bda14

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-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.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/**
4+
* @group wp
5+
* @covers WP::parse_request
6+
*/
7+
class Tests_WP_ParseRequest extends WP_UnitTestCase {
8+
/**
9+
* @var WP
10+
*/
11+
protected $wp;
12+
13+
public function set_up() {
14+
parent::set_up();
15+
$this->wp = new WP();
16+
}
17+
18+
/**
19+
* Test that PHP 8.1 "passing null to non-nullable" deprecation notice
20+
* is not thrown when the home URL has no path/trailing slash (default setup).
21+
*
22+
* Note: This does not test the actual functioning of the parse_request() method.
23+
* It just and only tests for/against the deprecation notice.
24+
*
25+
* @ticket 53635
26+
*/
27+
public function test_no_deprecation_notice_when_home_url_has_no_path() {
28+
// Make sure rewrite rules are not empty.
29+
$this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
30+
31+
// Make sure the test will function independently of whatever the test user set in wp-tests-config.php.
32+
add_filter(
33+
'home_url',
34+
static function ( $url ) {
35+
return 'http://example.org';
36+
}
37+
);
38+
39+
$this->wp->parse_request();
40+
$this->assertSame( '', $this->wp->request );
41+
}
42+
}

0 commit comments

Comments
 (0)