Skip to:
Content

BuddyPress.org

Changeset 10482


Ignore:
Timestamp:
01/30/2016 08:39:59 PM (10 years ago)
Author:
djpaul
Message:

Emails: new param for set_bcc|cc|to() to add to current values

The default behaviour remains to replace the current value.

This will allow plugin developers to easily append a new email
recipient without having to re-add the existing recipients.

See #6592

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/classes/class-bp-email.php

    r10481 r10482  
    522522         *                                              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.
    524          * @return BP_Email
    525          */
    526         public function set_bcc( $bcc_address, $name = '' ) {
    527                 $bcc = array();
     524         * @param string $operation Optional. If "replace", $to_address replaces current setting (default).
     525         *                          If "add", $to_address is added to the current setting.
     526         * @return BP_Email
     527         */
     528        public function set_bcc( $bcc_address, $name = '', $operation = 'replace' ) {
     529                $bcc = ( $operation !== 'replace' ) ? $this->bcc : array();
    528530
    529531                if ( is_array( $bcc_address ) ) {
     
    533535
    534536                } else {
    535                         $bcc = array( new BP_Email_Recipient( $bcc_address, $name ) );
     537                        $bcc[] = new BP_Email_Recipient( $bcc_address, $name );
    536538                }
    537539
     
    545547                 *                                              or an array containing any combination of the above.
    546548                 * @param string $name Optional. If $bcc_address is a string, this is the recipient's name.
    547                  * @param BP_Email $this Current instance of the email type class.
    548                  */
    549                 $this->bcc = apply_filters( 'bp_email_set_bcc', $bcc, $bcc_address, $name, $this );
     549                 * @param string $operation If "replace", $to_address replaced previous recipients. If "add",
     550                 *                          $to_address was added to the array of recipients.
     551                 * @param BP_Email $this Current instance of the email type class.
     552                 */
     553                $this->bcc = apply_filters( 'bp_email_set_bcc', $bcc, $bcc_address, $name, $operation, $this );
    550554
    551555                return $this;
     
    566570         *                                             or an array containing any combination of the above.
    567571         * @param string $name Optional. If $cc_address is a string, this is the recipient's name.
    568          * @return BP_Email
    569          */
    570         public function set_cc( $cc_address, $name = '' ) {
    571                 $cc = array();
     572         * @param string $operation Optional. If "replace", $to_address replaces current setting (default).
     573         *                          If "add", $to_address is added to the current setting.
     574         * @return BP_Email
     575         */
     576        public function set_cc( $cc_address, $name = '', $operation = 'replace' ) {
     577                $cc = ( $operation !== 'replace' ) ? $this->cc : array();
    572578
    573579                if ( is_array( $cc_address ) ) {
     
    577583
    578584                } else {
    579                         $cc = array( new BP_Email_Recipient( $cc_address, $name ) );
     585                        $cc[] = new BP_Email_Recipient( $cc_address, $name );
    580586                }
    581587
     
    589595                 *                                             or an array containing any combination of the above.
    590596                 * @param string $name Optional. If $cc_address is a string, this is the recipient's name.
    591                  * @param BP_Email $this Current instance of the email type class.
    592                  */
    593                 $this->cc = apply_filters( 'bp_email_set_cc', $cc, $cc_address, $name, $this );
     597                 * @param string $operation If "replace", $to_address replaced previous recipients. If "add",
     598                 *                          $to_address was added to the array of recipients.
     599                 * @param BP_Email $this Current instance of the email type class.
     600                 */
     601                $this->cc = apply_filters( 'bp_email_set_cc', $cc, $cc_address, $name, $operation, $this );
    594602
    595603                return $this;
     
    834842         *                                             or an array containing any combination of the above.
    835843         * @param string $name Optional. If $to_address is a string, this is the recipient's name.
    836          * @return BP_Email
    837          */
    838         public function set_to( $to_address, $name = '' ) {
    839                 $to = array();
     844         * @param string $operation Optional. If "replace", $to_address replaces current setting (default).
     845         *                          If "add", $to_address is added to the current setting.
     846         * @return BP_Email
     847         */
     848        public function set_to( $to_address, $name = '', $operation = 'replace' ) {
     849                $to = ( $operation !== 'replace' ) ? $this->to : array();
    840850
    841851                if ( is_array( $to_address ) ) {
     
    845855
    846856                } else {
    847                         $to = array( new BP_Email_Recipient( $to_address, $name ) );
     857                        $to[] = new BP_Email_Recipient( $to_address, $name );
    848858                }
    849859
     
    856866                 * @param string $to_address "To" address.
    857867                 * @param string $name "To" name.
    858                  * @param BP_Email $this Current instance of the email type class.
    859                  */
    860                 $this->to = apply_filters( 'bp_email_set_to', $to, $to_address, $name, $this );
     868                 * @param string $operation If "replace", $to_address replaced previous recipients. If "add",
     869                 *                          $to_address was added to the array of recipients.
     870                 * @param BP_Email $this Current instance of the email type class.
     871                 */
     872                $this->to = apply_filters( 'bp_email_set_to', $to, $to_address, $name, $operation, $this );
    861873
    862874                return $this;
  • trunk/tests/phpunit/testcases/core/class-bp-email.php

    r10481 r10482  
    223223                $this->assertSame( $user3,             $addresses[2]->get_address() );
    224224        }
     225
     226        public function test_replacing_existing_recipients_with_new_recipients() {
     227                $email              = new BP_Email( 'fake_type' );
     228                $original_recipient = 'test1@example.com';
     229                $new_recipient      = 'test2@example.com';
     230
     231                $email->set_to( $original_recipient );
     232                $addresses = $email->get_to();
     233                $this->assertSame( $original_recipient, $addresses[0]->get_address() );
     234
     235                $email->set_to( $new_recipient );
     236                $addresses = $email->get_to();
     237                $this->assertSame( $new_recipient, $addresses[0]->get_address() );
     238        }
     239
     240        public function test_appending_new_recipients_to_existing_recipients() {
     241                $email              = new BP_Email( 'fake_type' );
     242                $original_recipient = 'test1@example.com';
     243                $new_recipient      = 'test2@example.com';
     244
     245                $email->set_to( $original_recipient );
     246                $addresses = $email->get_to();
     247                $this->assertSame( $original_recipient, $addresses[0]->get_address() );
     248
     249                $email->set_to( $new_recipient, '', 'add' );
     250                $addresses = $email->get_to();
     251                $this->assertSame( $original_recipient, $addresses[0]->get_address() );
     252                $this->assertSame( $new_recipient, $addresses[1]->get_address() );
     253        }
    225254}
Note: See TracChangeset for help on using the changeset viewer.