Skip to:
Content

BuddyPress.org

Changeset 10792


Ignore:
Timestamp:
05/19/2016 08:50:46 PM (3 years ago)
Author:
r-a-y
Message:

Emails: Ensure that passing a known WP user email address to bp_send_email() will render the {{recipient.name}} token.

Commit also adds several unit tests for the BP_Email_Recipient class.

Fixes #7044.

Location:
trunk
Files:
2 edited

Legend:

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

    r10599 r10792  
    5656        $name = sanitize_text_field( $name );
    5757
    58         // User ID, WP_User object.
    59         if ( is_int( $email_or_user ) || is_object( $email_or_user ) ) {
    60             $this->user_object = is_object( $email_or_user ) ? $email_or_user : get_user_by( 'id', $email_or_user );
     58        // User ID, email address or WP_User object.
     59        if ( is_int( $email_or_user ) || ( is_string( $email_or_user ) && is_email( $email_or_user ) ) || is_object( $email_or_user ) ) {
     60            // We already have a WP user.
     61            if ( is_object( $email_or_user ) ) {
     62                $this->user_object = $email_or_user;
    6163
    62             if ( $this->user_object ) {
    63                 // This is escaped with esc_html in bp_core_get_user_displayname()
    64                 $name = wp_specialchars_decode( bp_core_get_user_displayname( $this->user_object->ID ), ENT_QUOTES );
    65 
    66                 $this->address = $this->user_object->user_email;
    67                 $this->name    = sanitize_text_field( $name );
     64            // Query for WP user by user ID.
     65            } elseif ( is_int( $email_or_user ) ) {
     66                $this->user_object = get_user_by( 'id', $email_or_user );
    6867            }
    6968
    70         // Array, address, and name.
     69            // Set email address.
     70            if ( empty( $this->user_object ) && is_email( $email_or_user ) ) {
     71                $address = $email_or_user;
     72            }
     73
     74        // Array or miscellaneous string.
    7175        } else {
    7276            if ( ! is_array( $email_or_user ) ) {
     
    8185                $name    = current( $email_or_user );
    8286            }
     87        }
    8388
    84             if ( is_email( $address ) ) {
    85                 $this->address = sanitize_email( $address );
    86             }
     89        // Set address if we have one.
     90        if ( ! empty( $address ) ) {
     91            $this->address = sanitize_email( $address );
     92        }
    8793
     94        // Still no user object; try to query user by email address.
     95        if ( empty( $this->user_object ) ) {
     96            $this->get_user( 'search-email' );
     97        }
     98
     99        // We have a user object; so set address and name from DB.
     100        if ( $this->user_object ) {
     101            // This is escaped with esc_html in bp_core_get_user_displayname()
     102            $wp_name = wp_specialchars_decode( bp_core_get_user_displayname( $this->user_object->ID ), ENT_QUOTES );
     103
     104            $this->address = $this->user_object->user_email;
     105            $this->name    = sanitize_text_field( $wp_name );
     106
     107        }
     108
     109        // Custom name override.
     110        if ( $name ) {
    88111            $this->name = $name;
    89112        }
  • trunk/tests/phpunit/testcases/core/class-bp-email-recipient.php

    r10470 r10792  
    4848    }
    4949
    50     public function test_return_with_address_and_optional_name() {
     50    public function test_return_with_known_address_and_optional_name() {
    5151        $email     = 'test@example.com';
     52        $name      = 'Custom';
     53        $recipient = new BP_Email_Recipient( $email, $name );
     54
     55        $this->assertSame( 'test@example.com', $recipient->get_address() );
     56        $this->assertSame( 'Custom', $recipient->get_name() );
     57    }
     58
     59    public function test_return_with_known_address_and_empty_name() {
     60        $email     = 'test@example.com';
     61        $recipient = new BP_Email_Recipient( $email );
     62
     63        $this->assertSame( 'test@example.com', $recipient->get_address() );
     64
     65        // Should fallback to WP user name.
     66        $this->assertSame( 'Unit Test', $recipient->get_name() );
     67    }
     68
     69    public function test_return_with_unknown_address_and_optional_name() {
     70        $email     = 'unknown@example.com';
     71        $name      = 'Custom';
     72        $recipient = new BP_Email_Recipient( $email, $name );
     73
     74        $this->assertSame( $email, $recipient->get_address() );
     75        $this->assertSame( $name, $recipient->get_name() );
     76    }
     77
     78    public function test_return_with_unknown_address_and_empty_name() {
     79        $email     = 'unknown@example.com';
    5280        $recipient = new BP_Email_Recipient( $email );
    5381
     
    5684    }
    5785
    58     public function test_return_with_array_and_optional_name() {
     86    public function test_return_with_unknown_array_and_optional_name() {
     87        $email     = 'unknown@example.com';
     88        $name      = 'Custom';
     89        $recipient = new BP_Email_Recipient( array( $email => $name ) );
     90
     91        $this->assertSame( $email, $recipient->get_address() );
     92        $this->assertSame( $name, $recipient->get_name() );
     93    }
     94
     95    public function test_return_with_unknown_array_and_empty_name() {
     96        $email     = 'unknown@example.com';
     97        $recipient = new BP_Email_Recipient( array( $email ) );
     98
     99        $this->assertSame( $email, $recipient->get_address() );
     100        $this->assertEmpty( $recipient->get_name() );
     101    }
     102
     103    public function test_return_with_known_array_and_optional_name() {
     104        $email     = 'test@example.com';
     105        $name      = 'Custom';
     106        $recipient = new BP_Email_Recipient( array( $email => $name ) );
     107
     108        $this->assertSame( $email, $recipient->get_address() );
     109        $this->assertSame( $name, $recipient->get_name() );
     110    }
     111
     112    public function test_return_with_known_array_and_empty_name() {
    59113        $email     = 'test@example.com';
    60114        $recipient = new BP_Email_Recipient( array( $email ) );
    61115
    62116        $this->assertSame( $email, $recipient->get_address() );
    63         $this->assertEmpty( $recipient->get_name() );
     117
     118        // Should fallback to WP user name.
     119        $this->assertSame( 'Unit Test', $recipient->get_name() );
    64120    }
    65121
Note: See TracChangeset for help on using the changeset viewer.