Make WordPress Core

Changeset 49844


Ignore:
Timestamp:
12/20/2020 03:07:23 PM (4 years ago)
Author:
johnbillion
Message:

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

Location:
trunk
Files:
2 edited

Legend:

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

    r49692 r49844  
    162162     * @global PHPMailer\PHPMailer\PHPMailer $phpmailer
    163163     *
    164      * @param string|array $to          Array or comma-separated list of email addresses to send message.
    165      * @param string       $subject     Email subject
    166      * @param string       $message     Message contents
    167      * @param string|array $headers     Optional. Additional headers.
    168      * @param string|array $attachments Optional. Paths to files to attach.
    169      * @return bool Whether the email contents were sent successfully.
     164     * @param string|string[] $to          Array or comma-separated list of email addresses to send message.
     165     * @param string          $subject     Email subject.
     166     * @param string          $message     Message contents.
     167     * @param string|string[] $headers     Optional. Additional headers.
     168     * @param string|string[] $attachments Optional. Paths to files to attach.
     169     * @return bool Whether the email was sent successfully.
    170170     */
    171171    function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
     
    188188         */
    189189        $atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) );
     190
     191        /**
     192         * Filters whether to preempt sending an email.
     193         *
     194         * Returning a non-null value will short-circuit {@see wp_mail()}, returning
     195         * that value instead. A boolean return value should be used to indicate whether
     196         * the email was successfully sent.
     197         *
     198         * @since 5.7.0
     199         *
     200         * @param null|bool $return Short-circuit return value.
     201         * @param array     $atts {
     202         *     Array of the `wp_mail()` arguments.
     203         *
     204         *     @type string|string[] $to          Array or comma-separated list of email addresses to send message.
     205         *     @type string          $subject     Email subject.
     206         *     @type string          $message     Message contents.
     207         *     @type string|string[] $headers     Additional headers.
     208         *     @type string|string[] $attachments Paths to files to attach.
     209         * }
     210         */
     211        $pre_wp_mail = apply_filters( 'pre_wp_mail', null, $atts );
     212
     213        if ( null !== $pre_wp_mail ) {
     214            return $pre_wp_mail;
     215        }
    190216
    191217        if ( isset( $atts['to'] ) ) {
  • trunk/tests/phpunit/tests/mail.php

    r48937 r49844  
    416416        $this->assertTrue( $phpmailer->validateAddress( 'foo@192.168.1.1' ), 'Assert PHPMailer accepts IP address email addresses' );
    417417    }
     418
     419    /**
     420     * Test for short-circuiting wp_mail().
     421     *
     422     * @ticket 35069
     423     */
     424    public function test_wp_mail_can_be_shortcircuited() {
     425        $result1 = wp_mail( WP_TESTS_EMAIL, 'Foo', 'Bar' );
     426
     427        add_filter( 'pre_wp_mail', '__return_false' );
     428        $result2 = wp_mail( WP_TESTS_EMAIL, 'Foo', 'Bar' );
     429        remove_filter( 'pre_wp_mail', '__return_false' );
     430
     431        $this->assertTrue( $result1 );
     432        $this->assertFalse( $result2 );
     433    }
    418434}
Note: See TracChangeset for help on using the changeset viewer.