| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
/* handle field output */ |
| 5 |
function wppb_email_handler( $output, $form_location, $field, $user_id, $field_check_errors, $request_data ){ |
| 6 |
$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'], true ) ); |
| 7 |
$item_description = wppb_icl_t( 'plugin profile-builder-pro', 'default_field_'.$field['id'].'_description_translation', $field['description'], true ); |
| 8 |
$input_value = ''; |
| 9 |
if( $form_location == 'edit_profile' ) |
| 10 |
$input_value = get_the_author_meta( 'user_email', $user_id ); |
| 11 |
|
| 12 |
if ( trim( $input_value ) == '' ) |
| 13 |
$input_value = $field['default-value']; |
| 14 |
|
| 15 |
$input_value = ( isset( $request_data['email'] ) ? trim( $request_data['email'] ) : $input_value ); |
| 16 |
$input_value = apply_filters( 'wppb_form_email_field_value', $input_value, $field, $form_location ); |
| 17 |
|
| 18 |
$input_value = apply_filters( 'wppb_before_processing_email_from_forms' , stripslashes( $input_value ) ); |
| 19 |
|
| 20 |
if ( $form_location != 'back_end' ){ |
| 21 |
$error_mark = ( ( $field['required'] == 'Yes' ) ? '<span class="wppb-required" title="'.wppb_required_field_error($field["field-title"]).'">*</span>' : '' ); |
| 22 |
|
| 23 |
if ( array_key_exists( $field['id'], $field_check_errors ) ) |
| 24 |
$error_mark = '<img src="'.WPPB_PLUGIN_URL.'assets/images/pencil_delete.png" title="'.wppb_required_field_error($field["field-title"]).'"/>'; |
| 25 |
|
| 26 |
$extra_attr = apply_filters( 'wppb_extra_attribute', '', $field, $form_location ); |
| 27 |
|
| 28 |
$email_input_status = !current_user_can( 'manage_options' ) ? apply_filters( 'wppb_set_input_status', '' ) : ''; |
| 29 |
|
| 30 |
$output = ' |
| 31 |
<label for="email">'.$item_title.$error_mark.'</label> |
| 32 |
<input class="text-input default_field_email '. apply_filters( 'wppb_fields_extra_css_class', '', $field ) .'" name="email" maxlength="'. apply_filters( 'wppb_maximum_character_length', 60, $field ) .'" type="email" id="email" value="'. esc_attr( $input_value ) .'" '. $extra_attr .' '. (( $form_location != 'register' ) ? $email_input_status : '') .' />'; |
| 33 |
if( !empty( $item_description ) ) |
| 34 |
$output .= '<span class="wppb-description-delimiter">'. $item_description .'</span>'; |
| 35 |
|
| 36 |
if( $form_location == 'edit_profile' && is_user_logged_in() ) { |
| 37 |
if ( $email_input_status == 'enabled' ) { |
| 38 |
$output .= '<span class="wppb-description-delimiter">' . __('If you change this, we will send you an email at your new address to confirm it.', 'profile-builder') . '<br><strong>' . __('The new address will not become active until confirmed.', 'profile-builder') . '</strong></span>'; |
| 39 |
} |
| 40 |
else if ( $email_input_status == 'disabled' ) { |
| 41 |
$current_url = wppb_curpageurl(); |
| 42 |
$pending_request_nonce = wp_create_nonce( 'wppb_email_change_action_nonce' ); |
| 43 |
$arr_params = array( 'wppb_email_change_action' => 'cancel_pending_email_address_change','_wpnonce' => $pending_request_nonce ); |
| 44 |
$cancel_request_url = add_query_arg($arr_params, $current_url); |
| 45 |
$pending_new_email_address = apply_filters('wppb_new_email_address',''); |
| 46 |
|
| 47 |
$output .= '<span class="wppb-description-delimiter">' . sprintf(__('There is a pending change request of your email to: %s', 'profile-builder'), '<strong>' . $pending_new_email_address . '</strong>'); |
| 48 |
$output .= '<a class="wppb-cancel-request" style="float: right;" href="' . esc_url($cancel_request_url) . '">' . __('Cancel request', 'profile-builder') . '</a></span>'; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
return apply_filters( 'wppb_'.$form_location.'_email', $output, $form_location, $field, $user_id, $field_check_errors, $request_data ); |
| 56 |
} |
| 57 |
add_filter( 'wppb_output_form_field_default-e-mail', 'wppb_email_handler', 10, 6 ); |
| 58 |
|
| 59 |
|
| 60 |
/* handle field validation */ |
| 61 |
function wppb_check_email_value( $message, $field, $request_data, $form_location ){ |
| 62 |
global $wpdb; |
| 63 |
|
| 64 |
if ( isset( $request_data['email'] ) ) { |
| 65 |
|
| 66 |
$request_data['email'] = apply_filters('wppb_before_processing_email_from_forms', stripslashes( $request_data['email'] ) ); |
| 67 |
|
| 68 |
if ((isset($request_data['email']) && (trim($request_data['email']) == '')) && ($field['required'] == 'Yes')) |
| 69 |
return wppb_required_field_error($field["field-title"]); |
| 70 |
|
| 71 |
if (isset($request_data['email']) && !is_email(trim($request_data['email']))) { |
| 72 |
return __('The email you entered is not a valid email address.', 'profile-builder'); |
| 73 |
} |
| 74 |
|
| 75 |
if (empty($request_data['email'])) { |
| 76 |
return __('You must enter a valid email address.', 'profile-builder'); |
| 77 |
} |
| 78 |
|
| 79 |
$wppb_generalSettings = get_option('wppb_general_settings'); |
| 80 |
if (isset($wppb_generalSettings['emailConfirmation']) && ($wppb_generalSettings['emailConfirmation'] == 'yes')) { |
| 81 |
$user_signup = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $wpdb->base_prefix . "signups WHERE user_email = %s AND active=0", trim( $request_data['email'] ) ) ); |
| 82 |
|
| 83 |
if (!empty($user_signup)) { |
| 84 |
if ($form_location == 'register') { |
| 85 |
return __('This email is already reserved to be used soon.', 'profile-builder') . '<br/>' . __('Please try a different one!', 'profile-builder'); |
| 86 |
} else if ($form_location == 'edit_profile') { |
| 87 |
$current_user = wp_get_current_user(); |
| 88 |
|
| 89 |
if ( !current_user_can('edit_users') ) { |
| 90 |
if ( $current_user->user_email != trim( $request_data['email'] ) ) |
| 91 |
return __('This email is already reserved to be used soon.', 'profile-builder') . '<br/>' . __('Please try a different one!', 'profile-builder'); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
$users = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->users} WHERE user_email = %s", trim( $request_data['email'] ) ) ); |
| 98 |
|
| 99 |
if (!empty($users)) { |
| 100 |
if ($form_location == 'register') |
| 101 |
return __('This email is already in use.', 'profile-builder') . '<br/>' . __('Please try a different one!', 'profile-builder'); |
| 102 |
|
| 103 |
if ($form_location == 'edit_profile') { |
| 104 |
if( isset( $_SERVER['HTTP_REFERER'] ) ) |
| 105 |
$url_parts = parse_url( esc_url_raw( $_SERVER['HTTP_REFERER'] ) ); |
| 106 |
|
| 107 |
if (isset($url_parts['query'])) { |
| 108 |
parse_str($url_parts['query'], $query); |
| 109 |
} |
| 110 |
|
| 111 |
if (isset($_GET['edit_user']) && !empty($_GET['edit_user'])) { |
| 112 |
$current_user_id = absint($_GET['edit_user']); |
| 113 |
} elseif (defined('DOING_AJAX') && DOING_AJAX && isset($query['edit_user']) && !empty($query['edit_user'])) { |
| 114 |
$current_user_id = $query['edit_user']; |
| 115 |
} else { |
| 116 |
$current_user = wp_get_current_user(); |
| 117 |
$current_user_id = $current_user->ID; |
| 118 |
} |
| 119 |
foreach ($users as $user) |
| 120 |
if ($user->ID != $current_user_id) |
| 121 |
return __('This email is already in use.', 'profile-builder') . '<br/>' . __('Please try a different one!', 'profile-builder'); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return $message; |
| 127 |
} |
| 128 |
add_filter( 'wppb_check_form_field_default-e-mail', 'wppb_check_email_value', 10, 4 ); |
| 129 |
|
| 130 |
/* handle field save */ |
| 131 |
function wppb_userdata_add_email( $userdata, $global_request, $form_args ){ |
| 132 |
if( wppb_field_exists_in_form( 'Default - E-mail', $form_args ) ) { |
| 133 |
if (isset($global_request['email'])) { |
| 134 |
$global_request['email'] = apply_filters('wppb_before_processing_email_from_forms', stripslashes( $global_request['email'] ) ); |
| 135 |
$userdata['user_email'] = sanitize_text_field(trim($global_request['email'])); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
return $userdata; |
| 140 |
} |
| 141 |
add_filter( 'wppb_build_userdata', 'wppb_userdata_add_email', 10, 3 ); |