PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 4.0.0
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v4.0.0
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
profile-builder / admin / advanced-settings / includes / forms / confirm-user-email-change.php

confirm-user-email-change.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 4.0.0, at admin/advanced-settings/includes/forms/confirm-user-email-change.php

330 lines 12.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) ) exit;
4
5 if ( !current_user_can( 'manage_options' ) )
6 add_filter( 'wppb_email_confirmation_on_user_email_change', '__return_true' );
7
8
9 /**
10 * Get transient check key
11 *
12 * @return string
13 */
14 function wppb_toolbox_pending_email_change_request_transient_key() {
15 $current_user = wp_get_current_user();
16 $pending_change_request_data = get_user_meta($current_user->ID, '_wppb_pending_email_change_request', true);
17
18 if (isset( $pending_change_request_data['new_email'] ) )
19 $pending_new_email_address = $pending_change_request_data['new_email'];
20 else $pending_new_email_address = '';
21
22 $transient_check_key = Wordpress_Creation_Kit_PB::wck_generate_slug(sanitize_email($pending_new_email_address));
23
24 return $transient_check_key;
25 }
26 add_filter('wppb_pending_email_change_transient_key','wppb_toolbox_pending_email_change_request_transient_key');
27
28
29 /**
30 * Get new email address
31 *
32 * @return mixed|string
33 */
34 function wppb_toolbox_pending_new_email_address() {
35 $current_user = wp_get_current_user();
36 $pending_change_request_data = get_user_meta($current_user->ID, '_wppb_pending_email_change_request', true);
37
38 if ( isset( $pending_change_request_data['new_email'] ))
39 $pending_new_email_address = $pending_change_request_data['new_email'];
40 else $pending_new_email_address = '';
41
42 return $pending_new_email_address;
43 }
44 add_filter('wppb_new_email_address','wppb_toolbox_pending_new_email_address');
45
46
47 /**
48 * Set email input status
49 *
50 * @return string
51 */
52 function wppb_toolbox_check_pending_email() {
53 $current_user = wp_get_current_user();
54 $unapproved_email_address = wppb_user_meta_exists( $current_user->ID, '_wppb_epaa_email' );
55
56 if ( empty( $unapproved_email_address ) && !isset( $_GET['wppb_epaa_review_users'] )) {
57 $transient_check_key = apply_filters('wppb_pending_email_change_transient_key', '');
58
59 if ( !empty( $transient_check_key ) )
60 $transient_check = get_transient( 'wppb_pending_email_change_request_exists_' . $transient_check_key );
61 else $transient_check = false;
62
63 if ( $transient_check !== false )
64 $input_status = 'disabled';
65 else $input_status = 'enabled';
66
67 } else $input_status = 'needs_approval';
68
69 return $input_status;
70 }
71 add_filter('wppb_set_input_status','wppb_toolbox_check_pending_email');
72
73
74 /**
75 * Check if approval is needed
76 *
77 * @return string
78 */
79 function wppb_toolbox_check_approval() {
80 $approval_needed = 'no';
81 $all_fields = get_option( 'wppb_manage_fields' );
82
83 foreach ($all_fields as $field) {
84 if ($field['field'] === 'Default - E-mail') {
85 if ( isset( $field['edit-profile-approved-by-admin'] ) && $field['edit-profile-approved-by-admin'] === 'yes') {
86 $approval_needed = 'yes';
87 }
88 }
89 }
90
91 return $approval_needed;
92
93 }
94 add_filter('wppb_check_approval_activation','wppb_toolbox_check_approval');
95
96
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 */
104 function wppb_toolbox_handle_email_change_request( $input_email ) {
105 if( apply_filters( 'wppb_email_confirmation_on_user_email_change', false ) && is_user_logged_in() && !isset( $_GET['wppb_epaa_review_users'] )) {
106 $current_user = wp_get_current_user();
107 $user_current_email = $current_user->user_email;
108
109 // check if the email address entered into the input is different from the current email address
110 if ( $input_email != $user_current_email ) {
111
112 $transient_check_key = Wordpress_Creation_Kit_PB::wck_generate_slug( sanitize_email( $input_email ));
113 $transient_check = get_transient( 'wppb_pending_email_change_request_exists_'.$transient_check_key );
114 $needs_approval = apply_filters( 'wppb_check_approval_activation','' );
115
116 if ( !wppb_user_meta_exists( $current_user->ID, '_wppb_email_change_request_nonce' )) {
117 $pending_request_nonce = wp_create_nonce('wppb_email_change_action_nonce');
118 update_user_meta($current_user->ID, '_wppb_email_change_request_nonce', $pending_request_nonce);
119 }
120
121 // check if a pending change request exists
122 if ( $transient_check === false && $needs_approval === 'no') {
123
124 // send confirmation email to new email address
125 do_action('wppb_send_mail_address_change_request', $input_email);
126
127 $input_email = $user_current_email;
128
129 } elseif ( $needs_approval === 'yes' ) {
130 update_user_meta( $current_user->ID, '_wppb_pending_email_change_request_page_url', wppb_curpageurl() );
131 }
132 }
133 }
134 return $input_email;
135 }
136 add_filter( 'wppb_before_processing_email_from_forms', 'wppb_toolbox_handle_email_change_request' );
137
138
139 /**
140 * Edit content and send email change request notification
141 *
142 * @param $new_email_address
143 * @return void
144 */
145 function wppb_toolbox_send_change_request_mail( $new_email_address ) {
146
147 $needs_approval = apply_filters( 'wppb_check_approval_activation','' );
148
149 if ( $needs_approval === 'no') {
150 $new_email = $new_email_address;
151 $current_user = wp_get_current_user();
152 $current_url = wppb_curpageurl();
153 }
154 elseif ( $needs_approval === 'yes' ) {
155 $new_email = $new_email_address['email'];
156 $current_user = get_user_by( 'id',$new_email_address['user_id'] );
157 $current_url = get_user_meta( $new_email_address['user_id'], '_wppb_pending_email_change_request_page_url', true );
158 }
159
160
161 $hash = md5( $new_email );
162 $new_user_email = array(
163 'hash' => $hash,
164 'new_email' => $new_email,
165 );
166
167 update_user_meta( $current_user->ID, '_wppb_pending_email_change_request', $new_user_email );
168
169 $pending_request_nonce = get_user_meta($current_user->ID,'_wppb_email_change_request_nonce',true);
170
171 $arr_params = array(
172 'wppb_email_change_action' => 'update_email_address',
173 'wppb_new_user_email' => $hash,
174 '_wpnonce' => $pending_request_nonce
175 );
176
177 $confirmation_url = add_query_arg($arr_params, $current_url);
178
179 $change_request_subject = sprintf( __('Email address change request for %s', 'profile-builder'), $current_user->user_email );
180 $change_request_subject = apply_filters( 'wppb_user_email_change_request_notification_subject', $change_request_subject, $current_user );
181
182 $change_request_message = sprintf( __('Someone requested to change the email address for your account.<br/>If this was a mistake, just ignore this email and nothing will happen.<br/>To update your account email address to the one requested (%1$s), visit the following link: %2$s', 'profile-builder'), $new_user_email['new_email'],'<a href="'.$confirmation_url.'">Change email address!</a>' );
183 $change_request_message = apply_filters( 'wppb_user_email_change_request_notification_content', $change_request_message, $current_user, $confirmation_url );
184
185 wppb_mail($new_email,$change_request_subject,$change_request_message);
186
187 // set transient
188 $transient_key = Wordpress_Creation_Kit_PB::wck_generate_slug( sanitize_email( $new_email ));
189 set_transient('wppb_pending_email_change_request_exists_' . $transient_key, true, WEEK_IN_SECONDS);
190 }
191 add_action('wppb_send_mail_address_change_request','wppb_toolbox_send_change_request_mail');
192
193
194 /**
195 * Update user email | delete transient | delete change request user meta
196 *
197 * @return void
198 */
199 function wppb_toolbox_change_user_email_address() {
200
201 if( !isset( $_GET['wppb_new_user_email'] ) )
202 return;
203
204 $current_user = wp_get_current_user();
205 $new_email = get_user_meta( $current_user->ID, '_wppb_pending_email_change_request', true );
206
207 if ( $new_email && hash_equals( $new_email['hash'], $_GET['wppb_new_user_email'] ) ) {
208 $transient_check_key = apply_filters('wppb_pending_email_change_transient_key', '');
209 $update_user_args = array(
210 'ID' => $current_user->ID,
211 'user_email' => esc_html( trim( $new_email['new_email'] ) )
212 );
213
214 wp_update_user( $update_user_args );
215
216 $remove_change_request_metas = array('_wppb_pending_email_change_request','_wppb_pending_email_change_request_page_url','_wppb_email_change_request_nonce');
217 foreach ( $remove_change_request_metas as $meta ) {
218 delete_user_meta( $current_user->ID, $meta );
219 }
220
221 delete_transient('wppb_pending_email_change_request_exists_' . $transient_check_key );
222
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 );
236 exit;
237 }
238
239 }
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 ) {
251
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 */
273 function wppb_toolbox_cancel_pending_user_email_change_request() {
274 $current_user = wp_get_current_user();
275 $transient_check_key = apply_filters('wppb_pending_email_change_transient_key', '');
276
277 $unapproved_email_address = wppb_user_meta_exists($current_user->ID, '_wppb_epaa_email' );
278 if (!empty($unapproved_email_address)) {
279 delete_user_meta( $current_user->ID, '_wppb_epaa_email' );
280 }
281
282 $remove_change_request_metas = array( '_wppb_pending_email_change_request' , '_wppb_pending_email_change_request_page_url' , '_wppb_email_change_request_nonce' );
283 foreach ( $remove_change_request_metas as $meta ) {
284 delete_user_meta( $current_user->ID, $meta );
285 }
286
287 delete_transient('wppb_pending_email_change_request_exists_' . $transient_check_key );
288
289 wp_redirect( remove_query_arg( array_keys( $_GET ), wppb_curpageurl() ) );
290 exit;
291 }
292
293
294 /**
295 * Handle email change request
296 *
297 * @return void
298 */
299 function wppb_toolbox_handle_email_change() {
300 if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce(sanitize_text_field( $_GET['_wpnonce'] ), 'wppb_email_change_action_nonce' ) && isset( $_GET['wppb_email_change_action'] ) ) {
301 if ( $_GET['wppb_email_change_action'] == 'update_email_address' && isset( $_GET['wppb_new_user_email'] ) )
302 wppb_toolbox_change_user_email_address();
303 else if ( $_GET['wppb_email_change_action'] == 'cancel_pending_email_address_change' )
304 wppb_toolbox_cancel_pending_user_email_change_request();
305 }
306 }
307 add_action('init', 'wppb_toolbox_handle_email_change');
308
309
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 */
317 function wppb_toolbox_remove_email_from_userdata_update( $userdata ) {
318 $transient_check_key = apply_filters( 'wppb_pending_email_change_transient_key', '' );
319
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' ) )
326 unset( $userdata['user_email'] );
327
328 return $userdata;
329 }
330 add_filter( 'wppb_build_userdata', 'wppb_toolbox_remove_email_from_userdata_update', 20, 3 );