From a752dae3acf8d080e186382183883b5ae6bf432b Mon Sep 17 00:00:00 2001 From: champsupertramp Date: Fri, 1 Apr 2016 23:14:34 +0800 Subject: [PATCH] Fix secondary email unique validation --- core/um-actions-form.php | 13 +++++++++++-- core/um-short-functions.php | 25 ++++++++++++++++++------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/core/um-actions-form.php b/core/um-actions-form.php index 18d74b30..7812b55d 100644 --- a/core/um-actions-form.php +++ b/core/um-actions-form.php @@ -372,8 +372,17 @@ $ultimatemember->form->add_error($key, __('This is not a valid email','ultimatemember') ); } else if ( $args[$key] != '' && email_exists( $args[$key] ) ) { $ultimatemember->form->add_error($key, __('This email is already linked to an existing account','ultimatemember') ); - } else if ( $args[$key] != '' && um_is_meta_value_exists( $key, $args[ $key ] ) ) { - $ultimatemember->form->add_error($key, __('This email is already linked to an existing account','ultimatemember') ); + } else if ( $args[$key] != '' ) { + + $users = get_users('meta_value='.$args[ $key ]); + + foreach ( $users as $user ) { + if( $user->ID != $args['user_id'] ){ + $ultimatemember->form->add_error($key, __('This email is already linked to an existing account','ultimatemember') ); + } + } + + } } diff --git a/core/um-short-functions.php b/core/um-short-functions.php index cf4848c3..a402fbd0 100644 --- a/core/um-short-functions.php +++ b/core/um-short-functions.php @@ -1481,16 +1481,27 @@ function um_fetch_user( $user_id ) { * @param mixed $value * @return integer */ - function um_is_meta_value_exists( $key, $value ){ + function um_is_meta_value_exists( $key, $value, $return_user_id = false ){ global $wpdb; - $count = $wpdb->get_var( $wpdb->prepare( - "SELECT COUNT(*) as count FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s ", - $key, - $value - ) ); + if( ! $return_user_id ){ + $count = $wpdb->get_var( $wpdb->prepare( + "SELECT COUNT(*) as count FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s ", + $key, + $value + ) ); + + return $count; + } + + $user_id = $wpdb->get_var( $wpdb->prepare( + "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s ", + $key, + $value + ) ); + + return $user_id; - return $count; } /**