← All changes
|
admin/advanced-settings/includes/forms/confirm-user-email-change.php
+108
-14
3.9.6
→
4.0.3
View file →
| @@ -1,11 +1,17 @@ | ||
| 1 | 1 | <?php |
| 2 | +// Exit if accessed directly | |
| 3 | +if ( ! defined( 'ABSPATH' ) ) exit; | |
| 2 | 4 | |
| 3 | 5 | if ( !current_user_can( 'manage_options' ) ) |
| 4 | 6 | add_filter( 'wppb_email_confirmation_on_user_email_change', '__return_true' ); |
| 5 | 7 | |
| 6 | 8 | |
| 7 | -// get transient check key | |
| 9 | +/** | |
| 10 | + * Get transient check key | |
| 11 | + * | |
| 12 | + * @return string | |
| 13 | + */ | |
| 8 | 14 | function wppb_toolbox_pending_email_change_request_transient_key() { |
| 9 | 15 | $current_user = wp_get_current_user(); |
| 10 | 16 | $pending_change_request_data = get_user_meta($current_user->ID, '_wppb_pending_email_change_request', true); |
| 11 | 17 | |
| @@ -19,9 +25,13 @@ | ||
| 19 | 25 | } |
| 20 | 26 | add_filter('wppb_pending_email_change_transient_key','wppb_toolbox_pending_email_change_request_transient_key'); |
| 21 | 27 | |
| 22 | 28 | |
| 23 | -// get new email address | |
| 29 | +/** | |
| 30 | + * Get new email address | |
| 31 | + * | |
| 32 | + * @return mixed|string | |
| 33 | + */ | |
| 24 | 34 | function wppb_toolbox_pending_new_email_address() { |
| 25 | 35 | $current_user = wp_get_current_user(); |
| 26 | 36 | $pending_change_request_data = get_user_meta($current_user->ID, '_wppb_pending_email_change_request', true); |
| 27 | 37 | |
| @@ -33,9 +43,13 @@ | ||
| 33 | 43 | } |
| 34 | 44 | add_filter('wppb_new_email_address','wppb_toolbox_pending_new_email_address'); |
| 35 | 45 | |
| 36 | 46 | |
| 37 | -// set email input status | |
| 47 | +/** | |
| 48 | + * Set email input status | |
| 49 | + * | |
| 50 | + * @return string | |
| 51 | + */ | |
| 38 | 52 | function wppb_toolbox_check_pending_email() { |
| 39 | 53 | $current_user = wp_get_current_user(); |
| 40 | 54 | $unapproved_email_address = wppb_user_meta_exists( $current_user->ID, '_wppb_epaa_email' ); |
| 41 | 55 | |
| @@ -56,9 +70,13 @@ | ||
| 56 | 70 | } |
| 57 | 71 | add_filter('wppb_set_input_status','wppb_toolbox_check_pending_email'); |
| 58 | 72 | |
| 59 | 73 | |
| 60 | -// check if approval is needed | |
| 74 | +/** | |
| 75 | + * Check if approval is needed | |
| 76 | + * | |
| 77 | + * @return string | |
| 78 | + */ | |
| 61 | 79 | function wppb_toolbox_check_approval() { |
| 62 | 80 | $approval_needed = 'no'; |
| 63 | 81 | $all_fields = get_option( 'wppb_manage_fields' ); |
| 64 | 82 | |
| @@ -75,9 +93,15 @@ | ||
| 75 | 93 | } |
| 76 | 94 | add_filter('wppb_check_approval_activation','wppb_toolbox_check_approval'); |
| 77 | 95 | |
| 78 | 96 | |
| 79 | -// handle email change request notification (send if admin approval is not active) | |
| 97 | +/** | |
| 98 | + * Handle email change request notification | |
| 99 | + * - send if admin approval is not active | |
| 100 | + * | |
| 101 | + * @param $input_email | |
| 102 | + * @return mixed|string | |
| 103 | + */ | |
| 80 | 104 | function wppb_toolbox_handle_email_change_request( $input_email ) { |
| 81 | 105 | if( apply_filters( 'wppb_email_confirmation_on_user_email_change', false ) && is_user_logged_in() && !isset( $_GET['wppb_epaa_review_users'] )) { |
| 82 | 106 | $current_user = wp_get_current_user(); |
| 83 | 107 | $user_current_email = $current_user->user_email; |
| @@ -111,9 +135,14 @@ | ||
| 111 | 135 | } |
| 112 | 136 | add_filter( 'wppb_before_processing_email_from_forms', 'wppb_toolbox_handle_email_change_request' ); |
| 113 | 137 | |
| 114 | 138 | |
| 115 | -// edit content and send email change request notification | |
| 139 | +/** | |
| 140 | + * Edit content and send email change request notification | |
| 141 | + * | |
| 142 | + * @param $new_email_address | |
| 143 | + * @return void | |
| 144 | + */ | |
| 116 | 145 | function wppb_toolbox_send_change_request_mail( $new_email_address ) { |
| 117 | 146 | |
| 118 | 147 | $needs_approval = apply_filters( 'wppb_check_approval_activation','' ); |
| 119 | 148 | |
| @@ -138,9 +167,13 @@ | ||
| 138 | 167 | update_user_meta( $current_user->ID, '_wppb_pending_email_change_request', $new_user_email ); |
| 139 | 168 | |
| 140 | 169 | $pending_request_nonce = get_user_meta($current_user->ID,'_wppb_email_change_request_nonce',true); |
| 141 | 170 | |
| 142 | - $arr_params = array( 'wppb_email_change_action' => 'update_email_address' ,'wppb_new_user_email' => $hash, '_wpnonce' => $pending_request_nonce ); | |
| 171 | + $arr_params = array( | |
| 172 | + 'wppb_email_change_action' => 'update_email_address', | |
| 173 | + 'wppb_new_user_email' => $hash, | |
| 174 | + '_wpnonce' => $pending_request_nonce | |
| 175 | + ); | |
| 143 | 176 | |
| 144 | 177 | $confirmation_url = add_query_arg($arr_params, $current_url); |
| 145 | 178 | |
| 146 | 179 | $change_request_subject = sprintf( __('Email address change request for %s', 'profile-builder'), $current_user->user_email ); |
| @@ -157,9 +190,13 @@ | ||
| 157 | 190 | } |
| 158 | 191 | add_action('wppb_send_mail_address_change_request','wppb_toolbox_send_change_request_mail'); |
| 159 | 192 | |
| 160 | 193 | |
| 161 | -// update user email | delete transient | delete change request user meta | |
| 194 | +/** | |
| 195 | + * Update user email | delete transient | delete change request user meta | |
| 196 | + * | |
| 197 | + * @return void | |
| 198 | + */ | |
| 162 | 199 | function wppb_toolbox_change_user_email_address() { |
| 163 | 200 | |
| 164 | 201 | if( !isset( $_GET['wppb_new_user_email'] ) ) |
| 165 | 202 | return; |
| @@ -182,16 +219,58 @@ | ||
| 182 | 219 | } |
| 183 | 220 | |
| 184 | 221 | delete_transient('wppb_pending_email_change_request_exists_' . $transient_check_key ); |
| 185 | 222 | |
| 186 | - wp_redirect( remove_query_arg( array_keys( $_GET ), wppb_curpageurl() ) ); | |
| 223 | + // remove unnecessary params | |
| 224 | + $current_url = remove_query_arg( array_keys( $_GET ), wppb_curpageurl()); | |
| 225 | + $email_changed_success_nonce = wp_create_nonce( '_wppb_email_changed_success_nonce' ); | |
| 226 | + | |
| 227 | + $arr_params = array( | |
| 228 | + 'wppb_user_email_changed' => 'success', | |
| 229 | + '_wpnonce' => $email_changed_success_nonce | |
| 230 | + ); | |
| 231 | + | |
| 232 | + // add email changed success params | |
| 233 | + $redirect_url = add_query_arg( $arr_params, $current_url ); | |
| 234 | + | |
| 235 | + wp_redirect( $redirect_url ); | |
| 187 | 236 | exit; |
| 188 | 237 | } |
| 189 | 238 | |
| 190 | 239 | } |
| 191 | 240 | |
| 241 | +/** | |
| 242 | + * Display user email updated success notification | |
| 243 | + * | |
| 244 | + * @param $form_name | |
| 245 | + * @param $form_id | |
| 246 | + * @param $form_type | |
| 247 | + * @param $is_elementor_edit_mode_or_divi_ajax | |
| 248 | + * @return void | |
| 249 | + */ | |
| 250 | +function wppb_toolbox_change_user_email_address_success_notice( $form_name, $form_id, $form_type, $is_elementor_edit_mode_or_divi_ajax ) { | |
| 192 | 251 | |
| 193 | -// cancel pending request (delete transient | delete change request user meta) | |
| 252 | + if ( !isset( $_GET['wppb_user_email_changed'] ) || $_GET['wppb_user_email_changed'] != 'success' || !isset( $_GET['_wpnonce'] ) || !wp_verify_nonce( sanitize_text_field( $_GET['_wpnonce'] ), '_wppb_email_changed_success_nonce' ) ) | |
| 253 | + return; | |
| 254 | + | |
| 255 | + // don't display this success message if the form is submitted after the confirmation link was accessed | |
| 256 | + if ( isset( $_POST['action'] ) && $_POST['action'] == 'edit_profile' ) | |
| 257 | + return; | |
| 258 | + | |
| 259 | + $notice_start = apply_filters( 'wppb_form_message_tpl_start', '<p class="alert wppb-success" id="wppb_form_general_message">' ); | |
| 260 | + $notice_message = apply_filters( 'wppb_user_email_changed_success_message', esc_html(__( 'Your email address has been successfully updated!', 'profile-builder' )) ); | |
| 261 | + $notice_end = apply_filters( 'wppb_form_message_tpl_end', '</p>' ); | |
| 262 | + | |
| 263 | + echo wp_kses_post( $notice_start . $notice_message . $notice_end ); | |
| 264 | +} | |
| 265 | +add_action( 'wppb_before_edit_profile_fields', 'wppb_toolbox_change_user_email_address_success_notice', 10, 4 ); | |
| 266 | + | |
| 267 | + | |
| 268 | +/** | |
| 269 | + * Cancel pending request (delete transient | delete change request user meta) | |
| 270 | + * | |
| 271 | + * @return void | |
| 272 | + */ | |
| 194 | 273 | function wppb_toolbox_cancel_pending_user_email_change_request() { |
| 195 | 274 | $current_user = wp_get_current_user(); |
| 196 | 275 | $transient_check_key = apply_filters('wppb_pending_email_change_transient_key', ''); |
| 197 | 276 | |
| @@ -211,8 +290,13 @@ | ||
| 211 | 290 | exit; |
| 212 | 291 | } |
| 213 | 292 | |
| 214 | 293 | |
| 294 | +/** | |
| 295 | + * Handle email change request | |
| 296 | + * | |
| 297 | + * @return void | |
| 298 | + */ | |
| 215 | 299 | function wppb_toolbox_handle_email_change() { |
| 216 | 300 | if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce(sanitize_text_field( $_GET['_wpnonce'] ), 'wppb_email_change_action_nonce' ) && isset( $_GET['wppb_email_change_action'] ) ) { |
| 217 | 301 | if ( $_GET['wppb_email_change_action'] == 'update_email_address' && isset( $_GET['wppb_new_user_email'] ) ) |
| 218 | 302 | wppb_toolbox_change_user_email_address(); |
| @@ -222,15 +306,25 @@ | ||
| 222 | 306 | } |
| 223 | 307 | add_action('init', 'wppb_toolbox_handle_email_change'); |
| 224 | 308 | |
| 225 | 309 | |
| 226 | -// if there is an email change request we don't update the email address until the confirmation link, sent to user by email, is clicked | |
| 310 | +/** | |
| 311 | + * Handle user email address update | |
| 312 | + * - if there is an email change request we don't update the email address until the confirmation link, sent to user by email, is clicked | |
| 313 | + * | |
| 314 | + * @param $userdata | |
| 315 | + * @return mixed | |
| 316 | + */ | |
| 227 | 317 | function wppb_toolbox_remove_email_from_userdata_update( $userdata ) { |
| 228 | - $transient_check_key = apply_filters('wppb_pending_email_change_transient_key', ''); | |
| 229 | - $transient_check = get_transient('wppb_pending_email_change_request_exists_' . $transient_check_key); | |
| 318 | + $transient_check_key = apply_filters( 'wppb_pending_email_change_transient_key', '' ); | |
| 230 | 319 | |
| 231 | - if ($transient_check !== false && ( !isset( $_POST['action'] ) || $_POST['action'] != 'register' ) ) | |
| 320 | + if( empty( $transient_check_key ) ) | |
| 321 | + return $userdata; | |
| 322 | + | |
| 323 | + $transient_check = get_transient( 'wppb_pending_email_change_request_exists_' . $transient_check_key ); | |
| 324 | + | |
| 325 | + if ( $transient_check !== false && ( !isset( $_POST['action'] ) || $_POST['action'] != 'register' ) ) | |
| 232 | 326 | unset( $userdata['user_email'] ); |
| 233 | 327 | |
| 234 | 328 | return $userdata; |
| 235 | 329 | } |
| 236 | 330 | add_filter( 'wppb_build_userdata', 'wppb_toolbox_remove_email_from_userdata_update', 20, 3 ); |