Skip to:
Content

BuddyPress.org

Changeset 10481


Ignore:
Timestamp:
01/28/2016 07:26:34 PM (9 years ago)
Author:
djpaul
Message:

Emails: update set_to, set_bcc, set_cc to correctly handle multiple recipients.

Also tweaks the PHPDoc to make the intended use of the array argument
clearer.

See #6592

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-functions.php

    r10478 r10481  
    28392839 * @param string $email_type Type of email being sent.
    28402840 * @param string|array|int|WP_User $to Either a email address, user ID, WP_User object,
    2841  *                                     or an array containg the address and name.
     2841 *                                     or an array containing any combination of the above.
    28422842 * @param array $args {
    28432843 *     Optional. Array of extra. parameters.
  • trunk/src/bp-core/classes/class-bp-email-recipient.php

    r10473 r10481  
    5050     *
    5151     * @param string|array|int|WP_User $email_or_user Either a email address, user ID, WP_User object,
    52      *                                                or an array containg the address and name.
     52     *                                                or an array containing any combination of the above.
    5353     * @param string $name Optional. If $email_or_user is a string, this is the recipient's name.
    5454     */
     
    9595         *
    9696         * @param string|array|int|WP_User $email_or_user Either a email address, user ID, WP_User object,
    97          *                                                or an array containg the address and name.
     97         *                                                or an array containing any combination of the above.
    9898         * @param string $name If $email_or_user is a string, this is the recipient's name.
    9999         * @param BP_Email_Recipient $this Current instance of the email type class.
  • trunk/src/bp-core/classes/class-bp-email.php

    r10470 r10481  
    520520     *
    521521     * @param string|array|int|WP_User $bcc_address Either a email address, user ID, WP_User object,
    522      *                                              or an array containg the address and name.
     522     *                                              or an array containing any combination of the above.
    523523     * @param string $name Optional. If $bcc_address is a string, this is the recipient's name.
    524524     * @return BP_Email
    525525     */
    526526    public function set_bcc( $bcc_address, $name = '' ) {
    527         $bcc = array( new BP_Email_Recipient( $bcc_address, $name ) );
     527        $bcc = array();
     528
     529        if ( is_array( $bcc_address ) ) {
     530            foreach ( $bcc_address as $address ) {
     531                $bcc[] = new BP_Email_Recipient( $address );
     532            }
     533
     534        } else {
     535            $bcc = array( new BP_Email_Recipient( $bcc_address, $name ) );
     536        }
    528537
    529538        /**
     
    534543         * @param BP_Email_Recipient[] $bcc BCC recipients.
    535544         * @param string|array|int|WP_User $bcc_address Either a email address, user ID, WP_User object,
    536          *                                              or an array containg the address and name.
     545         *                                              or an array containing any combination of the above.
    537546         * @param string $name Optional. If $bcc_address is a string, this is the recipient's name.
    538547         * @param BP_Email $this Current instance of the email type class.
     
    555564     *
    556565     * @param string|array|int|WP_User $cc_address Either a email address, user ID, WP_User object,
    557      *                                             or an array containg the address and name.
     566     *                                             or an array containing any combination of the above.
    558567     * @param string $name Optional. If $cc_address is a string, this is the recipient's name.
    559568     * @return BP_Email
    560569     */
    561570    public function set_cc( $cc_address, $name = '' ) {
    562         $cc = array( new BP_Email_Recipient( $cc_address, $name ) );
     571        $cc = array();
     572
     573        if ( is_array( $cc_address ) ) {
     574            foreach ( $cc_address as $address ) {
     575                $cc[] = new BP_Email_Recipient( $address );
     576            }
     577
     578        } else {
     579            $cc = array( new BP_Email_Recipient( $cc_address, $name ) );
     580        }
    563581
    564582        /**
     
    569587         * @param BP_Email_Recipient[] $cc CC recipients.
    570588         * @param string|array|int|WP_User $cc_address Either a email address, user ID, WP_User object,
    571          *                                             or an array containg the address and name.
     589         *                                             or an array containing any combination of the above.
    572590         * @param string $name Optional. If $cc_address is a string, this is the recipient's name.
    573591         * @param BP_Email $this Current instance of the email type class.
     
    658676     * @since 2.5.0
    659677     *
    660      * @param string|array|int|WP_User $email_address Either a email address, user ID, WP_User object,
    661      *                                                or an array containg the address and name.
     678     * @param string|array|int|WP_User $email_address Either a email address, user ID, or WP_User object.
    662679     * @param string $name Optional. If $email_address is a string, this is the recipient's name.
    663680     * @return BP_Email
     
    672689         *
    673690         * @param BP_Email_Recipient $from Sender details.
    674          * @param string|array|int|WP_User $email_address Either a email address, user ID, WP_User object,
    675          *                                                or an array containg the address and name.
     691         * @param string|array|int|WP_User $email_address Either a email address, user ID, or WP_User object.
    676692         * @param string $name Optional. If $email_address is a string, this is the recipient's name.
    677693         * @param BP_Email $this Current instance of the email type class.
     
    727743     *
    728744     * @param string|array|int|WP_User $email_address Either a email address, user ID, WP_User object,
    729      *                                                or an array containg the address and name.
     745     *                                                or an array containing any combination of the above.
    730746     * @param string $name Optional. If $email_address is a string, this is the recipient's name.
    731747     * @return BP_Email
     
    741757         * @param BP_Email_Recipient $reply_to "Reply to" recipient.
    742758         * @param string|array|int|WP_User $email_address Either a email address, user ID, WP_User object,
    743          *                                                or an array containg the address and name.
     759         *                                                or an array containing any combination of the above.
    744760         * @param string $name Optional. If $email_address is a string, this is the recipient's name.
    745761         * @param BP_Email $this Current instance of the email type class.
     
    816832     *
    817833     * @param string|array|int|WP_User $to_address Either a email address, user ID, WP_User object,
    818      *                                             or an array containg the address and name.
     834     *                                             or an array containing any combination of the above.
    819835     * @param string $name Optional. If $to_address is a string, this is the recipient's name.
    820836     * @return BP_Email
    821837     */
    822838    public function set_to( $to_address, $name = '' ) {
    823         $to = array( new BP_Email_Recipient( $to_address, $name ) );
     839        $to = array();
     840
     841        if ( is_array( $to_address ) ) {
     842            foreach ( $to_address as $address ) {
     843                $to[] = new BP_Email_Recipient( $address );
     844            }
     845
     846        } else {
     847            $to = array( new BP_Email_Recipient( $to_address, $name ) );
     848        }
    824849
    825850        /**
  • trunk/tests/phpunit/testcases/core/class-bp-email.php

    r10470 r10481  
    55 */
    66class BP_Tests_Email extends BP_UnitTestCase {
     7    protected $u1;
     8    protected $u2;
     9
    710    public function setUp() {
    811        parent::setUp();
     12
     13        $this->u1 = $this->factory->user->create( array(
     14            'display_name' => 'Unit Test',
     15            'user_email'   => 'test1@example.com',
     16        ) );
     17
     18        $this->u2 = $this->factory->user->create( array(
     19            'display_name' => 'Unit Test2',
     20            'user_email'   => 'test2@example.com',
     21        ) );
     22
    923        remove_filter( 'bp_email_get_headers', 'bp_email_set_default_headers', 6, 4 );
    1024        remove_filter( 'bp_email_get_tokens', 'bp_email_set_default_tokens', 6, 4 );
     
    156170        $this->assertSame( '<b>hello world</b><b>hello world</b>', $email->get_content() );
    157171    }
     172
     173    public function test_multiple_recipients_are_supported_by_address() {
     174        $email1 = 'test1@example.com';
     175        $email2 = 'test2@example.com';
     176        $email  = new BP_Email( 'fake_type' );
     177
     178        $email->set_to( array( $email1, $email2 ) );
     179        $addresses = $email->get_to();
     180
     181        $this->assertCount( 2, $addresses );
     182        $this->assertSame( $email1, $addresses[0]->get_address() );
     183        $this->assertSame( $email2, $addresses[1]->get_address() );
     184    }
     185
     186    public function test_multiple_recipients_are_supported_by_wp_user_object() {
     187        $user1 = get_user_by( 'id', $this->u1 );
     188        $user2 = get_user_by( 'id', $this->u2 );
     189        $email = new BP_Email( 'fake_type' );
     190
     191        $email->set_to( array( $user1, $user2 ) );
     192        $addresses = $email->get_to();
     193
     194        $this->assertCount( 2, $addresses );
     195        $this->assertSame( $user1->user_email, $addresses[0]->get_address() );
     196        $this->assertSame( $user2->user_email, $addresses[1]->get_address() );
     197    }
     198
     199    public function test_multiple_recipients_are_supported_by_wp_user_id() {
     200        $user1 = get_user_by( 'id', $this->u1 );
     201        $user2 = get_user_by( 'id', $this->u2 );
     202        $email = new BP_Email( 'fake_type' );
     203        $email->set_to( array( $this->u1, $this->u2 ) );
     204        $addresses = $email->get_to();
     205
     206        $this->assertCount( 2, $addresses );
     207        $this->assertSame( $user1->user_email, $addresses[0]->get_address() );
     208        $this->assertSame( $user2->user_email, $addresses[1]->get_address() );
     209    }
     210
     211    public function test_multiple_recipients_are_supported() {
     212        $user1 = get_user_by( 'id', $this->u1 );
     213        $user2 = get_user_by( 'id', $this->u2 );
     214        $user3 = 'test3@example.com';
     215        $email = new BP_Email( 'fake_type' );
     216
     217        $email->set_to( array( $user1, $this->u2, $user3 ) );
     218        $addresses = $email->get_to();
     219
     220        $this->assertCount( 3, $addresses );
     221        $this->assertSame( $user1->user_email, $addresses[0]->get_address() );
     222        $this->assertSame( $user2->user_email, $addresses[1]->get_address() );
     223        $this->assertSame( $user3,             $addresses[2]->get_address() );
     224    }
    158225}
Note: See TracChangeset for help on using the changeset viewer.