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 / front-end / recover.php

recover.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 4.0.0, at front-end/recover.php

602 lines 32.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
3
4 /**
5 * Function that checks if a user is approved before reseting the password
6 *
7 * @param string $data either the user login or the users email
8 * @param string $what what field we query for when getting the user
9 */
10 function wppb_check_for_unapproved_user( $data, $what ){
11 $message = '';
12
13 $wppb_generalSettings = get_option( 'wppb_general_settings' );
14 if( wppb_get_admin_approval_option_value() === 'yes' ){
15 $user = ( ( $what == 'user_email' ) ? get_user_by( 'email', $data ) : get_user_by( 'login', $data ) );
16
17 if ( wp_get_object_terms( $user->data->ID, 'user_status' ) ){
18 $message = '<strong>'. __('ERROR: ', 'profile-builder') . '</strong>' . __('Your account has to be confirmed by an administrator before you can use the "Password Reset" feature.', 'profile-builder');
19 $message = apply_filters('wppb_recover_password_unapporved_user', $message);
20 }
21 }
22
23 return $message;
24 }
25
26 /**
27
28 * Function that retrieves the unique user key from the database. If we don't have one we generate one and add it to the database
29 *
30 * @param string $requested_user_login the user login
31 */
32
33 function wppb_retrieve_activation_key( $requested_user_login ){
34
35 $user = get_user_by( 'login', $requested_user_login );
36
37 if( empty( $user ) || !function_exists( 'get_password_reset_key' ) )
38 return false;
39
40 return get_password_reset_key( $user );
41
42 }
43
44 /**
45 * Function that creates a generate new password form
46 *
47 * @param array $post_data $_POST
48 *
49 */
50 function wppb_create_recover_password_form( $user, $post_data, $is_ajax_form = false ){
51 ?>
52 <form enctype="multipart/form-data" method="post" id="wppb-recover-password" class="wppb-user-forms<?php echo ($is_ajax_form ? ' wppb-ajax-form' : ''); ?>" action="<?php echo esc_url( wppb_curpageurl() ); ?>">
53 <ul>
54 <?php
55
56 if( !empty( $post_data['passw1'] ) )
57 $passw_one = $post_data['passw1'];
58 else
59 $passw_one = '';
60
61 if( !empty( $post_data['passw2'] ) )
62 $passw_two = $post_data['passw2'];
63 else
64 $passw_two = '';
65
66 $password_label = __( 'Password', 'profile-builder' );
67 $repeat_password_label = __( 'Repeat Password', 'profile-builder' );
68
69 $recover_inputPassword = '
70 <li class="wppb-form-field passw1'. apply_filters( 'wppb_recover_field_extra_css_class', '', 'passw1') .'">
71 <label for="passw1">'. esc_html( $password_label ) .'</label>
72 <span class="wppb-password-field-container">
73 <input class="password" name="passw1" type="password" id="passw1" value="" autocomplete="off" title="'. esc_attr( wppb_password_length_text() ).'" '. apply_filters( 'wppb_recover_password_extra_attr', '', esc_html( $password_label ), 'password' ) .' />
74 '. wppb_password_visibility_toggle_html() .'
75 </span>
76 <span class="wppb-description-delimiter">'. wppb_password_length_text() .' '. wppb_password_strength_description() .'</span>'.
77 /* if we have active the password strength checker */
78 wppb_password_strength_checker_html().'
79 </li><!-- .passw1 -->
80 <input type="hidden" name="userData" value="'. esc_attr( $user->ID ).'"/>
81 <li class="wppb-form-field passw2'. apply_filters( 'wppb_recover_field_extra_css_class', '', 'passw2') .'">
82 <label for="passw2">'. esc_html( $repeat_password_label ) .'</label>
83 <span class="wppb-password-field-container">
84 <input class="password" name="passw2" type="password" id="passw2" value="" autocomplete="off" '. apply_filters( 'wppb_recover_password_extra_attr', '', esc_html( $repeat_password_label ), 'repeat_password' ) .' />
85 '. wppb_password_visibility_toggle_html() .'
86 </span>
87 </li><!-- .passw2 -->';
88
89 echo apply_filters( 'wppb_recover_password_form_input', $recover_inputPassword, $passw_one, $passw_two, $user->ID ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
90 ?>
91 </ul>
92 <p class="form-submit">
93 <?php $button_name = __('Reset Password', 'profile-builder'); ?>
94 <input name="recover_password2" type="submit" id="wppb-recover-password-button" class="<?php echo esc_attr( apply_filters( 'wppb_recover_submit_class', "submit button" ) ); ?>" value="<?php echo esc_attr( apply_filters('wppb_recover_password_button_name1', $button_name) ); ?>" />
95 <input name="action2" type="hidden" id="action2" value="recover_password2" />
96 <input name="key" type="hidden" id="key" value="<?php echo esc_attr( isset( $_GET['key'] ) ? sanitize_text_field( $_GET['key'] ) : '' ) ?>" />
97 <input name="login" type="hidden" id="login" value="<?php echo esc_attr( isset( $_GET['login'] ) ? sanitize_text_field( $_GET['login'] ) : '' ) ?>" />
98 </p><!-- .form-submit -->
99 <?php wp_nonce_field( 'verify_true_password_recovery2_'.$user->ID, 'password_recovery_nonce_field2' ); ?>
100 </form><!-- #recover_password -->
101 <?php
102 }
103
104 /**
105 * Function that generates the recover password form
106 *
107 * @param WP_User $user the user object
108 * @param array $post_data $_POST
109 *
110 */
111 function wppb_create_generate_password_form( $post_data, $is_ajax_form = false ){
112 ?>
113 <form enctype="multipart/form-data" method="post" id="wppb-recover-password" class="wppb-user-forms<?php echo ($is_ajax_form ? ' wppb-ajax-form' : ''); ?>" action="<?php echo esc_url( wppb_curpageurl() ); ?>">
114 <?php
115 $wppb_generalSettings = get_option( 'wppb_general_settings' );
116
117 if( !empty( $wppb_generalSettings['loginWith'] ) && $wppb_generalSettings['loginWith'] == 'email' ){
118 $recover_notification = '<p>' . __( 'Please enter your email address.', 'profile-builder' );
119 $username_email_label = __( 'Email', 'profile-builder' );
120 }
121 else{
122 $recover_notification = '<p>' . __( 'Please enter your username or email address.', 'profile-builder' );
123 $username_email_label = __( 'Username or Email', 'profile-builder' );
124 }
125
126 $recover_notification .= '<br/>'.__( 'You will receive a link to create a new password via email.', 'profile-builder' ).'</p>';
127 echo wp_kses_post( apply_filters( 'wppb_recover_password_message1', $recover_notification ) );
128
129 $username_email = ( isset( $post_data['username_email'] ) ? $post_data['username_email'] : '' );
130
131 $recover_input = '<ul>
132 <li class="wppb-form-field wppb-username-email'. apply_filters( 'wppb_recover_field_extra_css_class', '', 'username_email') .'">
133 <label for="username_email">'. esc_html( $username_email_label ) .'</label>
134 <input class="text-input" name="username_email" type="text" id="username_email" value="'.esc_attr( trim( $username_email ) ).'" '. apply_filters( 'wppb_recover_password_extra_attr', '', esc_html( $username_email_label ), 'username_email' ) .' />
135 </li><!-- .username_email --></ul>';
136 echo apply_filters( 'wppb_recover_password_generate_password_input', $recover_input, trim( $username_email ) ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
137 ?>
138 <p class="form-submit">
139 <?php $button_name = __('Get Reset Link', 'profile-builder'); ?>
140 <input name="recover_password" type="submit" id="wppb-recover-password-button" class="<?php echo esc_attr( apply_filters( 'wppb_recover_submit_class', "submit button" ) );?>" value="<?php echo esc_attr( apply_filters('wppb_recover_password_button_name3', $button_name) ); ?>" />
141 <input name="action" type="hidden" id="action" value="recover_password" />
142 </p>
143 <?php wp_nonce_field( 'verify_true_password_recovery', 'password_recovery_nonce_field' ); ?>
144 </form>
145 <?php
146 }
147
148 /**
149 * Determine based on the PB login settings what to display in the email sent on password reset: username or email
150 * @param $user
151 * @return mixed
152 */
153 function wppb_get_email_display_username($user){
154 //Get general settings
155 $wppb_generalSettings = get_option( 'wppb_general_settings' );
156
157 if( $wppb_generalSettings['loginWith'] == 'username' || $wppb_generalSettings['loginWith'] == 'usernameemail' )
158 $display_username_email = $user->user_login;
159 else
160 $display_username_email = $user->user_email;
161
162 return $display_username_email;
163 }
164
165 /**
166 * Send the email for the password recovery request
167 * @param $user
168 * @return bool|string|void
169 */
170 function wppb_send_recovery_email( $user, $success ){
171
172 if ( $success == 'wppb_recaptcha_error' || $success == 'wppb_turnstile_error' || $success == 'wppb_captcha_error' )
173 return false;
174
175 $user_object = new WP_User( $user->ID );
176
177 if( empty( $user_object->ID ) )
178 return false;
179
180 $requested_user_id = $user_object->ID;
181 $requested_user_login = $user_object->user_login;
182 $requested_user_email = $user_object->user_email;
183
184 //search if there is already an activation key present, if not create one
185 $key = get_password_reset_key( $user_object );
186
187 $display_username_email = wppb_get_email_display_username($user);
188
189 //send primary email message
190 $recovery_email_message = sprintf( __('Someone requested that the password be reset for the following account: <b>%1$s</b><br/>If this was a mistake, just ignore this email and nothing will happen.<br/>To reset your password, visit the following link:%2$s', 'profile-builder'), $display_username_email, '<a href="'.esc_url( add_query_arg( array( 'key' => $key, 'login' => urlencode( $requested_user_login ) ), wppb_curpageurl() ) ).'">'.esc_url( add_query_arg( array( 'key' => $key, 'login' => urlencode( $requested_user_login ) ), wppb_curpageurl() ) ).'</a>' );
191 $recovery_email_message = apply_filters( 'wppb_recover_password_message_content_sent_to_user1', $recovery_email_message, $requested_user_id, $requested_user_login, $requested_user_email );
192
193 $recovery_email_message_title = sprintf(__('Password Reset from %1$s', 'profile-builder'), $blogname = get_option('blogname') );
194 $recovery_email_message_title = apply_filters('wppb_recover_password_message_title_sent_to_user1', $recovery_email_message_title, $requested_user_login);
195
196 $recovery_email_from = apply_filters ( 'wppb_recover_password_notification_email_from_field', get_bloginfo( 'name' ) );
197 $recovery_email_context = 'email_user_recover';
198
199
200 $sent = false;
201 //send mail to the user notifying him of the reset request
202 if (trim($recovery_email_message_title) != '') {
203 $sent = wppb_mail($requested_user_email, $recovery_email_message_title, $recovery_email_message, $recovery_email_from, $recovery_email_context);
204 }
205
206 return $sent;
207
208 }
209
210 /**
211 * Function that sends the successful password reset email to the user
212 * @param $user
213 * @param $new_pass
214 */
215 function wppb_send_successful_password_reset_email( $user, $new_pass ){
216
217 $display_username_email = wppb_get_email_display_username($user);
218
219 //send secondary mail to the user containing the username and the new password
220 $recovery_email_message = __( 'You have successfully reset your password.', 'profile-builder' );
221 $recovery_email_message = apply_filters( 'wppb_recover_password_message_content_sent_to_user2', $recovery_email_message, $display_username_email, $new_pass, $user->ID );
222 $recovery_email_message_title = sprintf( __('Password Successfully Reset for %1$s on %2$s', 'profile-builder' ), $display_username_email, $blogname = get_option('blogname') );
223 $recovery_email_message_title = apply_filters( 'wppb_recover_password_message_title_sent_to_user2', $recovery_email_message_title, $display_username_email );
224 $recovery_email_from = apply_filters ( 'wppb_recover_password_success_notification_email_from_field', get_bloginfo( 'name' ) );
225 $recovery_email_context = 'email_user_recover_success';
226 //send mail to the user notifying him of the reset request
227 if ( trim( $recovery_email_message_title ) != '' )
228 wppb_mail( $user->user_email, $recovery_email_message_title, $recovery_email_message, $recovery_email_from, $recovery_email_context );
229 }
230
231 /**
232 * Function that sends an email to the admin after the password was reset
233 * we disable the feature to send the admin a notification mail but can be still used using filters
234 * @param $user
235 */
236 function wppb_send_admin_password_reset_email( $user ){
237
238 $display_username_email = wppb_get_email_display_username($user);
239
240 $recovery_admin_email_message = sprintf( __( '%1$s has requested a password change via the password reset feature.<br/>His/her new password is:%2$s', 'profile-builder' ), $display_username_email, '' );
241 $recovery_admin_email_message = apply_filters( 'wppb_recover_password_message_content_sent_to_admin', $recovery_admin_email_message, $display_username_email, '', $user->ID );
242 //we disable the feature to send the admin a notification mail but can be still used using filters
243 $recovery_admin_email_title = '';
244 $recovery_admin_email_title = apply_filters( 'wppb_recover_password_message_title_sent_to_admin', $recovery_admin_email_title, $display_username_email );
245 $recovery_email_from = apply_filters ( 'wppb_recover_password_success_notification_email_from_field', get_bloginfo( 'name' ) );
246 $recovery_admin_email_context = 'email_admin_recover_success';
247 //send mail to the admin notifying him of of a user with a password reset request
248 if (trim($recovery_admin_email_title) != '')
249 wppb_mail(get_option('admin_email'), $recovery_admin_email_title, $recovery_admin_email_message, $recovery_email_from, $recovery_admin_email_context);
250 }
251
252 /**
253 * The function for the recover password shortcode
254 *
255 */
256 function wppb_front_end_password_recovery( $atts ){
257 global $wppb_shortcode_on_front;
258 $wppb_shortcode_on_front = true;
259 global $wppb_password_recovery_shortcode_on_front;
260 $wppb_password_recovery_shortcode_on_front = true;
261 $password_email_sent = false;
262 $password_changed_success = false;
263
264 $atts = shortcode_atts( array(
265 'block' => false,
266 'ajax' => false,
267 ), $atts, 'wppb-recover-password' );
268
269 $is_ajax_form = false;
270 if( defined( 'WPPB_PAID_PLUGIN_DIR' ) && $atts['ajax'] === 'true' && file_exists( WPPB_PAID_PLUGIN_DIR . '/features/ajax/assets/forms-ajax-validation.js' ) ) {
271 wp_enqueue_script( 'wppb-forms-ajax-validation-script', WPPB_PAID_PLUGIN_URL . 'features/ajax/assets/forms-ajax-validation.js', array( 'jquery' ), PROFILE_BUILDER_VERSION, true );
272 wp_localize_script( 'wppb-forms-ajax-validation-script', 'submitButtonData', array( 'processingText' => __('Processing...', 'profile-builder') ) );
273 $is_ajax_form = true;
274 }
275
276 $output = '<div class="wppb_holder" id="wppb-recover-password-container">';
277
278 global $wpdb;
279
280 // check if the form is being displayed in the Elementor editor
281 $is_elementor_edit_mode_or_divi_ajax = false;
282 if( class_exists ( '\Elementor\Plugin' ) ){
283 $is_elementor_edit_mode_or_divi_ajax = \Elementor\Plugin::$instance->editor->is_edit_mode();
284 }
285
286 if ( is_array( $_POST ) && array_key_exists( 'action', $_POST ) && $_POST['action'] === 'wppb_divi_extension_ajax' ) {
287 $is_elementor_edit_mode_or_divi_ajax = true;
288 }
289
290 if( is_user_logged_in() && !( $is_elementor_edit_mode_or_divi_ajax || $atts['block'] ) ) {
291 return apply_filters('wppb_recover_password_already_logged_in', __('You are already logged in. You can change your password on the edit profile form.', 'profile-builder'));
292 }
293
294 //Get general settings
295 $wppb_generalSettings = get_option( 'wppb_general_settings' );
296
297 // If the user entered an email/username, process the request
298 if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'recover_password' && isset( $_POST['password_recovery_nonce_field'] ) && wp_verify_nonce( sanitize_text_field( $_POST['password_recovery_nonce_field'] ),'verify_true_password_recovery') ) {
299 // filter must be applied on the $_POST variable so that the value returned to the form can be corrected too
300
301 if( !empty( $_POST['username_email'] ) )
302 $username_email = apply_filters( 'wppb_before_processing_email_from_forms', sanitize_text_field( $_POST['username_email'] ) ); //we get the raw data
303 else
304 $username_email = '';
305
306 //check to see if it's an e-mail (and if this is valid/present in the database) or is a username
307
308 // if we do not have an email in the posted date we try to get the email for that user
309 if( !is_email( $username_email ) ){
310 // When filter is enabled and login is set to email only, do not allow username for password reset
311 if ( apply_filters( 'wppb_recover_password_require_email_when_login_with_email', false ) && !empty( $wppb_generalSettings['loginWith'] ) && $wppb_generalSettings['loginWith'] == 'email' ) {
312 $warning = __( 'Please enter your email address to request a password reset.', 'profile-builder' );
313 $warning = apply_filters( 'wppb_recover_password_sent_message_username_not_allowed', $warning );
314 $output .= wppb_password_recovery_warning( $warning, 'wppb_recover_password_displayed_message1' );
315 } else {
316 /* make sure it is a username */
317 $username = sanitize_user( $username_email );
318 if ( username_exists($username) ){
319 $query = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE user_login= %s", $username ) );
320 if( !empty( $query[0] ) ){
321 $username_email = $query[0]->user_email;
322 }
323 } else {
324 if( apply_filters( 'wppb_recover_password_use_old_error_messages', false ) ) {
325
326 if( !empty( $wppb_generalSettings['loginWith'] ) ){
327 if( $wppb_generalSettings['loginWith'] == 'email' ){
328 $warning = __( 'The email entered wasn\'t found in the database!', 'profile-builder').'<br/>'.__('Please check that you entered the correct email.', 'profile-builder' );
329
330 }
331 else if( $wppb_generalSettings['loginWith'] == 'username' ) {
332 $warning = __( 'The username entered wasn\'t found in the database!', 'profile-builder').'<br/>'.__('Please check that you entered the correct username.', 'profile-builder' );
333 }
334 else{
335 $warning = __( 'The email/username entered wasn\'t found in the database!', 'profile-builder').'<br/>'.__('Please check that you entered the correct email/username.', 'profile-builder' );
336 }
337 }
338 $warning = apply_filters( 'wppb_recover_password_sent_message4', $warning );
339
340 $output .= wppb_password_recovery_warning( $warning, 'wppb_recover_password_displayed_message1' );
341
342 } else {
343 $warning = __( 'If your information matches an account, a confirmation link will be sent to your email address.', 'profile-builder' );
344 $warning = apply_filters( 'wppb_recover_password_sent_message4', $warning );
345 $output .= wppb_password_recovery_success( $warning, 'wppb_recover_password_displayed_message1' );
346 $password_email_sent = true;
347 }
348 }
349 }
350 }
351
352 // we should have an email by this point
353 if ( is_email( $username_email ) ){
354 if ( email_exists( $username_email ) ){
355 $warning = wppb_check_for_unapproved_user($username_email, 'user_email');
356 if ( $warning != '' ){
357 $output .= wppb_password_recovery_warning( $warning, 'wppb_recover_password_displayed_message1' );
358 } else {
359
360 if( apply_filters( 'wppb_recover_password_use_old_error_messages', false ) ) {
361 $success = __( 'Check your email for the confirmation link.', 'profile-builder' );
362 } else {
363 $success = __( 'If your information matches an account, a confirmation link will be sent to your email address.', 'profile-builder' );
364 }
365
366 $success = apply_filters( 'wppb_recover_password_sent_message1', $success, $username_email );
367
368 if ( $success != 'wppb_recaptcha_error' && $success != 'wppb_turnstile_error' && $success != 'wppb_captcha_error' ) {
369 $output .= wppb_password_recovery_success( $success, 'wppb_recover_password_displayed_message2' );
370 } else {
371 $output .= wppb_password_recovery_warning( $success, 'wppb_recover_password_displayed_message1' );
372 }
373
374 //verify e-mail validity
375 $query = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE user_email= %s", sanitize_email( $username_email ) ) );
376 if( !empty( $query[0] ) ){
377 $user = $query[0];
378
379 //send mail to the user notifying him of the reset request
380 $sent = wppb_send_recovery_email( $user, $success );
381
382 if ( $sent === false ){
383 $warning = '<strong>'. __( 'ERROR:', 'profile-builder' ) .'</strong>' . __( 'There was an error while trying to send the activation link!', 'profile-builder' );
384 $warning = apply_filters( 'wppb_recover_password_sent_message_error_sending', $warning );
385 $output .= wppb_password_recovery_warning( $warning, 'wppb_recover_password_displayed_message1' );
386 } else {
387 $password_email_sent = true;
388 }
389
390 if( !apply_filters( 'wppb_recover_password_use_old_error_messages', false ) ) {
391 // We want to set this to true regardless of the email result, so we can hide the form (to prevent user enumeration).
392 // But ONLY if the captcha was solved successfully.
393 if ( $success != 'wppb_recaptcha_error' && $success != 'wppb_turnstile_error' && $success != 'wppb_captcha_error' ) {
394 $password_email_sent = true;
395 }
396 }
397
398 }
399
400 }
401 } elseif ( !email_exists( $username_email ) ){
402 // check reCAPTCHA
403 $warning = wppb_password_recovery_warning( '', 'wppb_recover_password_displayed_message1' );
404
405 // if there is no reCAPTCHA error show the invalid email address error
406 if( $warning === '' ) {
407 $warning = __('The email address entered wasn\'t found in the database!', 'profile-builder').'<br/>'.__('Please check that you entered the correct email address.', 'profile-builder');
408 $warning = apply_filters('wppb_recover_password_sent_message2', $warning);
409 $output .= '<p class="wppb-warning" role="alert">'.$warning.'</p>';
410 } else {
411 $output .= $warning;
412 }
413 }
414 }
415 }
416 // If the user used the correct key-code, update his/her password
417 elseif ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action2'] ) && $_POST['action2'] === 'recover_password2' && isset( $_POST['password_recovery_nonce_field2'] ) && isset( $_POST['userData'] ) && wp_verify_nonce( sanitize_text_field( $_POST['password_recovery_nonce_field2'] ), 'verify_true_password_recovery2_'.absint( sanitize_text_field( $_POST['userData'] ) ) ) ) {
418
419 $password_change_message = '';
420
421 if( ( !empty( $_POST['passw1'] ) && !empty( $_POST['passw2'] ) ) ){
422
423 //get the login name and key and verify if they match the ones in the database
424 if( isset( $_POST['key'] ) )
425 $key = sanitize_text_field( $_POST['key'] );
426 else
427 $key = '';
428
429 if( empty( $key ) ){
430 $password_change_message = __('The key cannot be empty!', 'profile-builder');
431 $output .= wppb_password_recovery_error( $password_change_message, 'wppb_recover_password_password_changed_message2' );
432 }
433
434 if( isset( $_POST['login'] ) )
435 $login = sanitize_text_field( $_POST['login'] );
436 else
437 $login = '';
438
439 if( empty( $login ) ){
440 $password_change_message = __('Login cannot be empty!', 'profile-builder');
441 $output .= wppb_password_recovery_error( $password_change_message, 'wppb_recover_password_password_changed_message2' );
442 }
443
444 $user = check_password_reset_key( $key, $login );
445
446 if( is_wp_error( $user ) || empty( $user ) || ( !empty( $user ) && $user->ID != absint( $_POST['userData'] ) ) ){
447 $password_change_message = __('Invalid key!', 'profile-builder');
448 $output .= wppb_password_recovery_error( $password_change_message, 'wppb_recover_password_password_changed_message2' );
449 }
450
451 if( $_POST['passw1'] != $_POST['passw2'] ) {
452 $password_change_message = __('The entered passwords don\'t match!', 'profile-builder');
453 $output .= wppb_password_recovery_error( $password_change_message, 'wppb_recover_password_password_changed_message2' );
454 }
455
456 if( !empty( $wppb_generalSettings['minimum_password_length'] ) || ( isset( $_POST['wppb_password_strength'] ) && !empty( $wppb_generalSettings['minimum_password_strength'] ) ) ){
457 if( wppb_check_password_length( $_POST['passw1'] ) ){//phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
458 $password_change_message = sprintf( __( "The password must have the minimum length of %s characters", "profile-builder" ), $wppb_generalSettings['minimum_password_length'] ) . '<br/>';
459 $output .= wppb_password_recovery_error( $password_change_message, 'wppb_recover_password_password_changed_message2' );
460 }
461 if( wppb_check_password_strength() ){
462 $password_change_message = sprintf( __( "The password must have a minimum strength of %s", "profile-builder" ), wppb_check_password_strength() );
463 $output .= wppb_password_recovery_error( $password_change_message, 'wppb_recover_password_password_changed_message2' );
464 }
465 }
466
467 $password_change_message = apply_filters( 'wppb_recover_password_extra_validation', $password_change_message, $user );
468
469 if( !empty( $password_change_message ) ){
470 $output .= wppb_password_recovery_error( $password_change_message, 'wppb_custom_recover_password_validation_message' );
471 }
472
473 if( empty($password_change_message) ){
474
475 $password_change_message = __( 'Your password has been successfully changed!', 'profile-builder' );
476 $output .= wppb_password_recovery_success( $password_change_message, 'wppb_recover_password_password_changed_message1' );
477 $password_changed_success = true;
478
479 $userID = $user->ID;
480 $new_pass = $_POST['passw1']; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
481
482 //update the new password and delete the key
483 do_action( 'wppb_password_reset', $userID, $new_pass );
484 wp_set_password( $new_pass, $userID );
485
486 /* log out of all sessions on password reset */
487 $sessions = WP_Session_Tokens::get_instance( $userID );
488 $sessions->destroy_all();
489
490 $user_info = get_userdata( $userID );
491
492 //send email to user
493 wppb_send_successful_password_reset_email( $user_info, $new_pass );
494
495 //send email to admin
496 wppb_send_admin_password_reset_email( $user_info );
497
498 // CHECK FOR REDIRECT
499 $redirect_url = wppb_get_redirect_url( 'normal', 'after_success_password_reset', '', sanitize_user( $user_info->user_login ) );
500
501 //log the user in if the option was selected
502 if ( apply_filters( 'wppb_recover_password_autologin', false ) ){
503 //use the after_login redirect if no after_success_password_reset redirect is set
504 if( empty( $redirect_url ) ) {
505 $redirect_url = wppb_get_redirect_url( 'normal', 'after_login', '', sanitize_user( $user_info->user_login ) );
506 $redirect_url = apply_filters( 'wppb_after_recover_and_login', $redirect_url );
507 }
508 if( empty( $redirect_url ) ) {
509 $redirect_url = remove_query_arg( 'key', wppb_curpageurl() );
510 }
511
512 $redirect_url = add_query_arg( wppb_get_autologin_query_args( $user_info->ID ), $redirect_url );
513 }
514
515 $redirect_delay = apply_filters( 'wppb_success_password_reset_redirect_delay', 3, sanitize_user( $user_info->user_login ) );
516 $redirect_message = wppb_build_redirect( $redirect_url, $redirect_delay, 'after_success_password_reset' );
517
518 if( isset( $redirect_message ) && ! empty( $redirect_message ) ) {
519 $output .= '<p>' . $redirect_message . '</p>';
520 }
521 }
522 }
523 else{
524 $password_change_message .= __( "The password must not be empty!", "profile-builder" );
525 $output .= wppb_password_recovery_error( $password_change_message, 'wppb_recover_password_password_changed_message2' );
526 }
527 }
528
529 // use this action hook to add extra content before the password recovery form
530 do_action( 'wppb_before_recover_password_fields' );
531
532
533 //this is the part that shows the forms
534 if( isset( $_GET['key'] ) && isset( $_GET['login'] ) ){
535
536 $key = sanitize_text_field( $_GET['key'] );
537 $login = sanitize_text_field( $_GET['login'] );
538
539 if( !empty( $key ) && !empty( $login ) && !$password_changed_success ) {
540
541 $user = check_password_reset_key( $key, $login );
542
543 if( !is_wp_error( $user ) ){
544
545 ob_start();
546 wppb_create_recover_password_form( $user, $_POST, $is_ajax_form );
547 $output .= ob_get_contents();
548 ob_end_clean();
549 }
550 else {
551 $output .= wppb_password_recovery_error('<strong>' . __('ERROR:', 'profile-builder') . '</strong>' . __('Invalid key!', 'profile-builder'), 'wppb_recover_password_invalid_key_message');
552 }
553
554 } elseif ( !$password_changed_success && !$password_email_sent ) {
555 ob_start();
556 wppb_create_generate_password_form($_POST, $is_ajax_form);
557 $output .= ob_get_contents();
558 ob_end_clean();
559 }
560
561 } else {
562 if( !$password_email_sent ) {
563 ob_start();
564 wppb_create_generate_password_form($_POST, $is_ajax_form);
565 $output .= ob_get_contents();
566 ob_end_clean();
567 }
568 }
569
570 // use this action hook to add extra content after the password recovery form.
571 do_action( 'wppb_after_recover_password_fields' );
572
573 $output .= '</div>';
574 return apply_filters( 'wppb_recover_password_before_content_output', $output );
575 }
576
577 /* function for displaying success messages on the recover password page */
578 function wppb_password_recovery_success( $message, $filter ){
579 return apply_filters( $filter, '<p class="wppb-success" role="alert">'.$message.'</p>', $message );
580 }
581
582 /* function for displaying warning messages on the recover password page */
583 function wppb_password_recovery_warning( $message, $filter ){
584 if( $message !== '' ) {
585 return apply_filters( $filter, '<p class="wppb-warning" role="alert">'.$message.'</p>', $message );
586 } else {
587 return apply_filters( $filter, '', $message );
588 }
589 }
590
591 /* function for displaying error messages on the recover password page */
592 function wppb_password_recovery_error( $message, $filter ){
593 return apply_filters( $filter, '<p class="wppb-error" role="alert">'.$message.'</p>', $message );
594 }
595
596
597 // include missing scripts needed on Elementor Pages (Form inside an Elementor Popup)
598 function wppb_recover_password_scripts_and_styles() {
599 if ( is_plugin_active('elementor-pro/elementor-pro.php') && defined( 'WPPB_PAID_PLUGIN_URL' ) )
600 wp_enqueue_script( 'wppb_elementor_popup_script', WPPB_PAID_PLUGIN_URL . 'features/elementor-pro/assets/js/elementor-popup.js', array('jquery') );
601 }
602 add_action( 'elementor/frontend/after_enqueue_scripts', 'wppb_recover_password_scripts_and_styles' );