Mail: Introduce a pre_wp_mail filter to allow short-circuiting the wp_mail() function without having to override the pluggable function.

Props DvanKooten, swissspidy, SergeyBiryukov, jtsternberg, ericlewis, Mte90, birgire, ayeshrajans

Fixes #35069


git-svn-id: https://develop.svn.wordpress.org/trunk@49844 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn
2020-12-20 15:07:23 +00:00
parent b04671e52f
commit 99cfb00d13
2 changed files with 48 additions and 6 deletions

View File

@@ -161,12 +161,12 @@ if ( ! function_exists( 'wp_mail' ) ) :
*
* @global PHPMailer\PHPMailer\PHPMailer $phpmailer
*
* @param string|array $to Array or comma-separated list of email addresses to send message.
* @param string $subject Email subject
* @param string $message Message contents
* @param string|array $headers Optional. Additional headers.
* @param string|array $attachments Optional. Paths to files to attach.
* @return bool Whether the email contents were sent successfully.
* @param string|string[] $to Array or comma-separated list of email addresses to send message.
* @param string $subject Email subject.
* @param string $message Message contents.
* @param string|string[] $headers Optional. Additional headers.
* @param string|string[] $attachments Optional. Paths to files to attach.
* @return bool Whether the email was sent successfully.
*/
function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
// Compact the input, apply the filters, and extract them back out.
@@ -188,6 +188,32 @@ if ( ! function_exists( 'wp_mail' ) ) :
*/
$atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) );
/**
* Filters whether to preempt sending an email.
*
* Returning a non-null value will short-circuit {@see wp_mail()}, returning
* that value instead. A boolean return value should be used to indicate whether
* the email was successfully sent.
*
* @since 5.7.0
*
* @param null|bool $return Short-circuit return value.
* @param array $atts {
* Array of the `wp_mail()` arguments.
*
* @type string|string[] $to Array or comma-separated list of email addresses to send message.
* @type string $subject Email subject.
* @type string $message Message contents.
* @type string|string[] $headers Additional headers.
* @type string|string[] $attachments Paths to files to attach.
* }
*/
$pre_wp_mail = apply_filters( 'pre_wp_mail', null, $atts );
if ( null !== $pre_wp_mail ) {
return $pre_wp_mail;
}
if ( isset( $atts['to'] ) ) {
$to = $atts['to'];
}

View File

@@ -415,4 +415,20 @@ class Tests_Mail extends WP_UnitTestCase {
$phpmailer = $GLOBALS['phpmailer'];
$this->assertTrue( $phpmailer->validateAddress( 'foo@192.168.1.1' ), 'Assert PHPMailer accepts IP address email addresses' );
}
/**
* Test for short-circuiting wp_mail().
*
* @ticket 35069
*/
public function test_wp_mail_can_be_shortcircuited() {
$result1 = wp_mail( WP_TESTS_EMAIL, 'Foo', 'Bar' );
add_filter( 'pre_wp_mail', '__return_false' );
$result2 = wp_mail( WP_TESTS_EMAIL, 'Foo', 'Bar' );
remove_filter( 'pre_wp_mail', '__return_false' );
$this->assertTrue( $result1 );
$this->assertFalse( $result2 );
}
}