Skip to content

Commit 16d0911

Browse files
Code Modernization: Check the return type of parse_url() in url_to_postid().
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 `PHP_URL_HOST` as `$component`, which returns `null` if the URL only has a path. The return value of `parse_url()` was then passed to `str_replace()`, leading to a notice on PHP 8.1: {{{ str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated }}} Adding validation for the return type value of `parse_url()` prevents that. This commit addresses a few errors in the test suite along the lines of: {{{ 5) Tests_Rewrite::test_url_to_postid_home_has_path str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated /var/www/src/wp-includes/rewrite.php:503 /var/www/tests/phpunit/tests/rewrite.php:271 /var/www/vendor/bin/phpunit:123 }}} Includes adding a dedicated unit test for a URL that only has a path. Follow-up to [41786], [51606], [51622], [51626], [51629], [51630], [52799]. Props jrf, aristath, poena, justinahinon, SergeyBiryukov. See #55656. git-svn-id: https://develop.svn.wordpress.org/trunk@54364 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 62360f5 commit 16d0911

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/wp-includes/rewrite.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,21 @@ function url_to_postid( $url ) {
500500
*/
501501
$url = apply_filters( 'url_to_postid', $url );
502502

503-
$url_host = str_replace( 'www.', '', parse_url( $url, PHP_URL_HOST ) );
504-
$home_url_host = str_replace( 'www.', '', parse_url( home_url(), PHP_URL_HOST ) );
503+
$url_host = parse_url( $url, PHP_URL_HOST );
504+
505+
if ( is_string( $url_host ) ) {
506+
$url_host = str_replace( 'www.', '', $url_host );
507+
} else {
508+
$url_host = '';
509+
}
510+
511+
$home_url_host = parse_url( home_url(), PHP_URL_HOST );
512+
513+
if ( is_string( $home_url_host ) ) {
514+
$home_url_host = str_replace( 'www.', '', $home_url_host );
515+
} else {
516+
$home_url_host = '';
517+
}
505518

506519
// Bail early if the URL does not belong to this site.
507520
if ( $url_host && $url_host !== $home_url_host ) {

tests/phpunit/tests/rewrite.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,17 @@ public function test_url_to_postid_hierarchical_with_matching_leaves() {
255255
$this->assertSame( $grandchild_id_2, url_to_postid( get_permalink( $grandchild_id_2 ) ) );
256256
}
257257

258-
public function test_url_to_postid_home_has_path() {
258+
/**
259+
* @covers ::url_to_postid
260+
*/
261+
public function test_url_to_postid_url_has_only_path() {
262+
$this->assertSame( 0, url_to_postid( '/example/' ) );
263+
}
259264

265+
/**
266+
* @covers ::url_to_postid
267+
*/
268+
public function test_url_to_postid_home_has_only_path() {
260269
update_option( 'home', home_url( '/example/' ) );
261270

262271
$id = self::factory()->post->create(

0 commit comments

Comments
 (0)