Make WordPress Core

Changeset 51622


Ignore:
Timestamp:
08/16/2021 08:16:13 PM (3 years ago)
Author:
SergeyBiryukov
Message:

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

Location:
trunk
Files:
2 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp.php

    r51518 r51622  
    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            /*
     
    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
Note: See TracChangeset for help on using the changeset viewer.