Make WordPress Core


Ignore:
Timestamp:
02/25/2022 01:46:23 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Check the return type of wp_parse_url() in wp_mail().

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 PHP 8.1, if the home URL does not have a "host" component, it would lead to a substr(): Passing null to parameter #1 ($string) of type string is deprecated notice.

Changing the logic around and adding validation for the return type value of wp_parse_url() prevents that.

Follow-up to [48601], [51606], [51622], [51626], [51629], [51630].

Props dennisatyoast, jrf.
See #54730.

File:
1 edited

Legend:

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

    r52422 r52799  
    379379        if ( ! isset( $from_email ) ) {
    380380            // Get the site domain and get rid of www.
    381             $sitename = wp_parse_url( network_home_url(), PHP_URL_HOST );
    382             if ( 'www.' === substr( $sitename, 0, 4 ) ) {
    383                 $sitename = substr( $sitename, 4 );
     381            $sitename   = wp_parse_url( network_home_url(), PHP_URL_HOST );
     382            $from_email = 'wordpress@';
     383
     384            if ( null !== $sitename ) {
     385                if ( 'www.' === substr( $sitename, 0, 4 ) ) {
     386                    $sitename = substr( $sitename, 4 );
     387                }
     388
     389                $from_email .= $sitename;
    384390            }
    385 
    386             $from_email = 'wordpress@' . $sitename;
    387391        }
    388392
Note: See TracChangeset for help on using the changeset viewer.