From 446d463a195f2b36eac8169cf4a6f7f3bf5742ac Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sat, 1 Oct 2022 03:25:10 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@54364 git-svn-id: http://core.svn.wordpress.org/trunk@53923 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/rewrite.php | 17 +++++++++++++++-- wp-includes/version.php | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/wp-includes/rewrite.php b/wp-includes/rewrite.php index af66d02b94..477a85fbef 100644 --- a/wp-includes/rewrite.php +++ b/wp-includes/rewrite.php @@ -500,8 +500,21 @@ function url_to_postid( $url ) { */ $url = apply_filters( 'url_to_postid', $url ); - $url_host = str_replace( 'www.', '', parse_url( $url, PHP_URL_HOST ) ); - $home_url_host = str_replace( 'www.', '', parse_url( home_url(), PHP_URL_HOST ) ); + $url_host = parse_url( $url, PHP_URL_HOST ); + + if ( is_string( $url_host ) ) { + $url_host = str_replace( 'www.', '', $url_host ); + } else { + $url_host = ''; + } + + $home_url_host = parse_url( home_url(), PHP_URL_HOST ); + + if ( is_string( $home_url_host ) ) { + $home_url_host = str_replace( 'www.', '', $home_url_host ); + } else { + $home_url_host = ''; + } // Bail early if the URL does not belong to this site. if ( $url_host && $url_host !== $home_url_host ) { diff --git a/wp-includes/version.php b/wp-includes/version.php index c1aee4edbd..92d8816795 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.1-beta2-54363'; +$wp_version = '6.1-beta2-54364'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.