Skip to content

Commit

Permalink
Code Modernization: Check the return type of wp_parse_url() in `wp_…
Browse files Browse the repository at this point in the history
…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: [https://www.php.net/manual/en/function.parse-url.php#refsect1-function.parse-url-returnvalues 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.

git-svn-id: https://develop.svn.wordpress.org/trunk@52799 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Feb 25, 2022
1 parent 06b740f commit 391922e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/wp-includes/pluggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,16 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
*/
if ( ! isset( $from_email ) ) {
// Get the site domain and get rid of www.
$sitename = wp_parse_url( network_home_url(), PHP_URL_HOST );
if ( 'www.' === substr( $sitename, 0, 4 ) ) {
$sitename = substr( $sitename, 4 );
}
$sitename = wp_parse_url( network_home_url(), PHP_URL_HOST );
$from_email = 'wordpress@';

if ( null !== $sitename ) {
if ( 'www.' === substr( $sitename, 0, 4 ) ) {
$sitename = substr( $sitename, 4 );
}

$from_email = 'wordpress@' . $sitename;
$from_email .= $sitename;
}
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/phpunit/tests/mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,28 @@ public function test_wp_mail_with_empty_from_name_for_the_from_header() {
$this->assertStringContainsString( $expected, $mailer->get_sent()->header );
}

/**
* Tests that wp_mail() returns false with an empty home URL and does not error out on PHP 8.1.
*
* @ticket 54730
*/
public function test_wp_mail_with_empty_home_url() {
$to = '[email protected]';
$subject = 'Testing';
$message = 'Test Message';

// Multisite test runs.
add_filter( 'network_home_url', '__return_empty_string' );

// Single site test runs.
add_filter( 'home_url', '__return_empty_string' );

$success = wp_mail( $to, $subject, $message );

$this->assertFalse( $success, 'wp_mail() should have returned false' );
$this->assertGreaterThan( 0, did_action( 'wp_mail_failed' ), 'wp_mail_failed action was not called' );
}

/**
* @ticket 30266
*/
Expand Down

0 comments on commit 391922e

Please sign in to comment.