Skip to:
Content

BuddyPress.org

Ticket #8448: 8448.lowercase-emails.patch

File 8448.lowercase-emails.patch, 4.4 KB (added by dcavins, 4 years ago)

When adding, updating or fetching opt-outs, be sure that the email address string has been converted to lowercase, or else the comparisons of the hashed values will fail.

  • src/bp-core/classes/class-bp-optout.php

    diff --git src/bp-core/classes/class-bp-optout.php src/bp-core/classes/class-bp-optout.php
    index 90dff5f83..1f0e3d4f8 100644
    class BP_Optout { 
    212212         */
    213213        protected static function _insert( $data = array(), $data_format = array() ) {
    214214                global $wpdb;
    215                 // We must hash the email address at insert.
     215                // We must lowercase and hash the email address at insert.
     216                $data['email_address_hash'] = strtolower( $data['email_address_hash'] );
    216217                $data['email_address_hash'] = wp_hash( $data['email_address_hash'] );
    217218                return $wpdb->insert( BP_Optout::get_table_name(), $data, $data_format );
    218219        }
    class BP_Optout { 
    237238        protected static function _update( $data = array(), $where = array(), $data_format = array(), $where_format = array() ) {
    238239                global $wpdb;
    239240
    240                 // Ensure that a passed email address is hashed.
     241                // Ensure that a passed email address is lowercased and hashed.
    241242                if ( ! empty( $data['email_address_hash'] ) && is_email( $data['email_address_hash'] ) ) {
     243                        $data['email_address_hash'] = strtolower( $data['email_address_hash'] );
    242244                        $data['email_address_hash'] = wp_hash( $data['email_address_hash'] );
    243245                }
    244246
    class BP_Optout { 
    296298
    297299                        $email_clean = array();
    298300                        foreach ( $emails as $email ) {
     301                                $email         = strtolower( $email );
    299302                                $email_hash    = wp_hash( $email );
    300303                                $email_clean[] = $wpdb->prepare( '%s', $email_hash );
    301304                        }
    class BP_Optout { 
    330333                // search_terms.
    331334                if ( ! empty( $args['search_terms'] ) ) {
    332335                        // Matching email_address is an exact match because of the hashing.
     336                        $args['search_terms']             = strtolower( $args['search_terms'] );
    333337                        $search_terms_like                = wp_hash( $args['search_terms'] );
    334338                        $where_conditions['search_terms'] = $wpdb->prepare( '( email_address_hash LIKE %s )', $search_terms_like );
    335339                }
  • tests/phpunit/testcases/core/optouts.php

    diff --git tests/phpunit/testcases/core/optouts.php tests/phpunit/testcases/core/optouts.php
    index 7c0d1ecc5..0369f714e 100644
     
    100100                $this->set_current_user( $old_current_user );
    101101        }
    102102
     103        public function test_bp_optouts_get_by_email_address_mismatched_case() {
     104                $old_current_user = get_current_user_id();
     105
     106                $u1 = $this->factory->user->create();
     107                $this->set_current_user( $u1 );
     108
     109                // Create a couple of optouts.
     110                $args = array(
     111                        'email_address'     => 'ONE@wpfrost.org',
     112                        'user_id'           => $u1,
     113                        'email_type'        => 'annoyance'
     114                );
     115                $i1 = bp_add_optout( $args );
     116                $args['email_address'] = 'two@WP.org';
     117                $i2 = bp_add_optout( $args );
     118
     119                $get_args = array(
     120                        'email_address'  => 'one@WPfrost.org',
     121                        'fields'         => 'ids',
     122                );
     123                $optouts = bp_get_optouts( $get_args );
     124                $this->assertEqualSets( array( $i1 ), $optouts );
     125
     126                $this->set_current_user( $old_current_user );
     127        }
     128
     129        public function test_bp_optouts_get_by_search_terms_mismatched_case() {
     130                $old_current_user = get_current_user_id();
     131
     132                $u1 = $this->factory->user->create();
     133                $this->set_current_user( $u1 );
     134
     135                // Create a couple of optouts.
     136                $args = array(
     137                        'email_address'     => 'ONE@wpfrost.org',
     138                        'user_id'           => $u1,
     139                        'email_type'        => 'annoyance'
     140                );
     141                $i1 = bp_add_optout( $args );
     142                $args['email_address'] = 'two@WP.org';
     143                $i2 = bp_add_optout( $args );
     144
     145                $get_args = array(
     146                        'search_terms'   => 'oNe@wPfrost.org',
     147                        'fields'         => 'ids',
     148                );
     149                $optouts = bp_get_optouts( $get_args );
     150                $this->assertEqualSets( array( $i1 ), $optouts );
     151
     152                $this->set_current_user( $old_current_user );
     153        }
     154
     155
     156        public function test_bp_optouts_get_by_email_address_mismatched_case_after_update() {
     157                $old_current_user = get_current_user_id();
     158
     159                $u1 = $this->factory->user->create();
     160                $this->set_current_user( $u1 );
     161
     162                // Create an opt-out.
     163                $args = array(
     164                        'email_address'     => 'ONE@wpfrost.org',
     165                        'user_id'           => $u1,
     166                        'email_type'        => 'annoyance'
     167                );
     168                $i1 = bp_add_optout( $args );
     169                // Update it.
     170                $oo_class                = new BP_Optout( $i1 );
     171                $oo_class->email_address = 'One@wpFrost.org';
     172                $oo_class->save();
     173
     174                $get_args = array(
     175                        'email_address'  => 'one@WPfrost.org',
     176                        'fields'         => 'ids',
     177                );
     178                $optouts = bp_get_optouts( $get_args );
     179                $this->assertEqualSets( array( $i1 ), $optouts );
     180
     181                $this->set_current_user( $old_current_user );
     182        }
     183
    103184}