| 1 |
<?php |
| 2 |
/* handle field output */ |
| 3 |
function wppb_email_handler( $output, $form_location, $field, $user_id, $field_check_errors, $request_data ){ |
| 4 |
$item_title = apply_filters( 'wppb_'.$form_location.'_email_item_title', wppb_icl_t( 'plugin profile-builder-pro', 'default_field_'.$field['id'].'_title_translation', $field['field-title'] ) ); |
| 5 |
$item_description = wppb_icl_t( 'plugin profile-builder-pro', 'default_field_'.$field['id'].'_description_translation', $field['description'] ); |
| 6 |
$input_value = ''; |
| 7 |
if( $form_location == 'edit_profile' ) |
| 8 |
$input_value = get_the_author_meta( 'user_email', $user_id ); |
| 9 |
|
| 10 |
if ( trim( $input_value ) == '' ) |
| 11 |
$input_value = $field['default-value']; |
| 12 |
|
| 13 |
$input_value = ( isset( $request_data['email'] ) ? trim( $request_data['email'] ) : $input_value ); |
| 14 |
|
| 15 |
if ( $form_location != 'back_end' ){ |
| 16 |
$error_mark = ( ( $field['required'] == 'Yes' ) ? '<span class="wppb-required" title="'.wppb_required_field_error($field["field-title"]).'">*</span>' : '' ); |
| 17 |
|
| 18 |
if ( array_key_exists( $field['id'], $field_check_errors ) ) |
| 19 |
$error_mark = '<img src="'.WPPB_PLUGIN_URL.'assets/images/pencil_delete.png" title="'.wppb_required_field_error($field["field-title"]).'"/>'; |
| 20 |
|
| 21 |
$output = ' |
| 22 |
<label for="email">'.$item_title.$error_mark.'</label> |
| 23 |
<input class="text-input default_field_email" name="email" maxlength="'. apply_filters( 'wppb_maximum_character_length', 70 ) .'" type="text" id="email" value="'. esc_attr( $input_value ) .'" />'; |
| 24 |
if( !empty( $item_description ) ) |
| 25 |
$output .= '<span class="wppb-description-delimiter">'. $item_description .'</span>'; |
| 26 |
|
| 27 |
} |
| 28 |
|
| 29 |
return apply_filters( 'wppb_'.$form_location.'_email', $output, $form_location, $field, $user_id, $field_check_errors, $request_data ); |
| 30 |
} |
| 31 |
add_filter( 'wppb_output_form_field_default-e-mail', 'wppb_email_handler', 10, 6 ); |
| 32 |
|
| 33 |
|
| 34 |
/* handle field validation */ |
| 35 |
function wppb_check_email_value( $message, $field, $request_data, $form_location ){ |
| 36 |
global $wpdb; |
| 37 |
|
| 38 |
if ( ( isset( $request_data['email'] ) && ( trim( $request_data['email'] ) == '' ) ) && ( $field['required'] == 'Yes' ) ) |
| 39 |
return wppb_required_field_error($field["field-title"]); |
| 40 |
|
| 41 |
if ( isset( $request_data['email'] ) && !is_email( trim( $request_data['email'] ) ) ){ |
| 42 |
return __( 'The email you entered is not a valid email address.', 'profilebuilder' ); |
| 43 |
} |
| 44 |
|
| 45 |
if ( is_multisite() || ( !is_multisite() && ( isset( $wppb_generalSettings['emailConfirmation'] ) && ( $wppb_generalSettings['emailConfirmation'] == 'yes' ) ) ) ){ |
| 46 |
$user_signup = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM ".$wpdb->prefix."signups WHERE user_email = %s", $request_data['email'] ) ); |
| 47 |
|
| 48 |
if ( !empty( $user_signup ) ) |
| 49 |
return __( 'This email is already reserved to be used soon.', 'profilebuilder' ) .'<br/>'. __( 'Please try a different one!', 'profilebuilder' ); |
| 50 |
} |
| 51 |
|
| 52 |
$users = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->users} WHERE user_email = %s", $request_data['email'] ) ); |
| 53 |
if ( !empty( $users ) ){ |
| 54 |
if ( $form_location == 'register' ) |
| 55 |
return __( 'This email is already in use.', 'profilebuilder' ) .'<br/>'. __( 'Please try a different one!', 'profilebuilder' ); |
| 56 |
|
| 57 |
if ( $form_location == 'edit_profile' ){ |
| 58 |
$current_user = wp_get_current_user(); |
| 59 |
|
| 60 |
foreach ( $users as $user ) |
| 61 |
if ( $user->ID != $current_user->ID ) |
| 62 |
return __( 'This email is already in use.', 'profilebuilder' ) .'<br/>'. __( 'Please try a different one!', 'profilebuilder' ); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
return $message; |
| 67 |
} |
| 68 |
add_filter( 'wppb_check_form_field_default-e-mail', 'wppb_check_email_value', 10, 4 ); |
| 69 |
|
| 70 |
/* handle field save */ |
| 71 |
function wppb_userdata_add_email( $userdata, $global_request ){ |
| 72 |
if ( isset( $global_request['email'] ) ) |
| 73 |
$userdata['user_email'] = sanitize_text_field( trim( $global_request['email'] ) ); |
| 74 |
|
| 75 |
return $userdata; |
| 76 |
} |
| 77 |
add_filter( 'wppb_build_userdata', 'wppb_userdata_add_email', 10, 2 ); |