PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 4.0.3
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v4.0.3
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.3, at front-end/recover.php

609 lines 32.7 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" novalidate 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" novalidate 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 ) ).'" required '. 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( $username_email === '' ){
309 if( !empty( $wppb_generalSettings['loginWith'] ) && $wppb_generalSettings['loginWith'] == 'email' )
310 $warning = __( 'Please enter your email address.', 'profile-builder' );
311 else
312 $warning = __( 'Please enter your username or email address.', 'profile-builder' );
313 $warning = apply_filters( 'wppb_recover_password_sent_message_empty', $warning );
314 $output .= wppb_password_recovery_warning( $warning, 'wppb_recover_password_displayed_message1' );
315 } elseif ( !is_email( $username_email ) ){
316 // if we do not have an email in the posted date we try to get the email for that user
317 // When filter is enabled and login is set to email only, do not allow username for password reset
318 if ( apply_filters( 'wppb_recover_password_require_email_when_login_with_email', false ) && !empty( $wppb_generalSettings['loginWith'] ) && $wppb_generalSettings['loginWith'] == 'email' ) {
319 $warning = __( 'Please enter your email address to request a password reset.', 'profile-builder' );
320 $warning = apply_filters( 'wppb_recover_password_sent_message_username_not_allowed', $warning );
321 $output .= wppb_password_recovery_warning( $warning, 'wppb_recover_password_displayed_message1' );
322 } else {
323 /* make sure it is a username */
324 $username = sanitize_user( $username_email );
325 if ( username_exists($username) ){
326 $query = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE user_login= %s", $username ) );
327 if( !empty( $query[0] ) ){
328 $username_email = $query[0]->user_email;
329 }
330 } else {
331 if( apply_filters( 'wppb_recover_password_use_old_error_messages', false ) ) {
332
333 if( !empty( $wppb_generalSettings['loginWith'] ) ){
334 if( $wppb_generalSettings['loginWith'] == 'email' ){
335 $warning = __( 'The email entered wasn\'t found in the database!', 'profile-builder').'<br/>'.__('Please check that you entered the correct email.', 'profile-builder' );
336
337 }
338 else if( $wppb_generalSettings['loginWith'] == 'username' ) {
339 $warning = __( 'The username entered wasn\'t found in the database!', 'profile-builder').'<br/>'.__('Please check that you entered the correct username.', 'profile-builder' );
340 }
341 else{
342 $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' );
343 }
344 }
345 $warning = apply_filters( 'wppb_recover_password_sent_message4', $warning );
346
347 $output .= wppb_password_recovery_warning( $warning, 'wppb_recover_password_displayed_message1' );
348
349 } else {
350 $warning = __( 'If your information matches an account, a confirmation link will be sent to your email address.', 'profile-builder' );
351 $warning = apply_filters( 'wppb_recover_password_sent_message4', $warning );
352 $output .= wppb_password_recovery_success( $warning, 'wppb_recover_password_displayed_message1' );
353 $password_email_sent = true;
354 }
355 }
356 }
357 }
358
359 // we should have an email by this point
360 if ( is_email( $username_email ) ){
361 if ( email_exists( $username_email ) ){
362 $warning = wppb_check_for_unapproved_user($username_email, 'user_email');
363 if ( $warning != '' ){
364 $output .= wppb_password_recovery_warning( $warning, 'wppb_recover_password_displayed_message1' );
365 } else {
366
367 if( apply_filters( 'wppb_recover_password_use_old_error_messages', false ) ) {
368 $success = __( 'Check your email for the confirmation link.', 'profile-builder' );
369 } else {
370 $success = __( 'If your information matches an account, a confirmation link will be sent to your email address.', 'profile-builder' );
371 }
372
373 $success = apply_filters( 'wppb_recover_password_sent_message1', $success, $username_email );
374
375 if ( $success != 'wppb_recaptcha_error' && $success != 'wppb_turnstile_error' && $success != 'wppb_captcha_error' ) {
376 $output .= wppb_password_recovery_success( $success, 'wppb_recover_password_displayed_message2' );
377 } else {
378 $output .= wppb_password_recovery_warning( $success, 'wppb_recover_password_displayed_message1' );
379 }
380
381 //verify e-mail validity
382 $query = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE user_email= %s", sanitize_email( $username_email ) ) );
383 if( !empty( $query[0] ) ){
384 $user = $query[0];
385
386 //send mail to the user notifying him of the reset request
387 $sent = wppb_send_recovery_email( $user, $success );
388
389 if ( $sent === false ){
390 $warning = '<strong>'. __( 'ERROR:', 'profile-builder' ) .'</strong>' . __( 'There was an error while trying to send the activation link!', 'profile-builder' );
391 $warning = apply_filters( 'wppb_recover_password_sent_message_error_sending', $warning );
392 $output .= wppb_password_recovery_warning( $warning, 'wppb_recover_password_displayed_message1' );
393 } else {
394 $password_email_sent = true;
395 }
396
397 if( !apply_filters( 'wppb_recover_password_use_old_error_messages', false ) ) {
398 // We want to set this to true regardless of the email result, so we can hide the form (to prevent user enumeration).
399 // But ONLY if the captcha was solved successfully.
400 if ( $success != 'wppb_recaptcha_error' && $success != 'wppb_turnstile_error' && $success != 'wppb_captcha_error' ) {
401 $password_email_sent = true;
402 }
403 }
404
405 }
406
407 }
408 } elseif ( !email_exists( $username_email ) ){
409 // check reCAPTCHA
410 $warning = wppb_password_recovery_warning( '', 'wppb_recover_password_displayed_message1' );
411
412 // if there is no reCAPTCHA error show the invalid email address error
413 if( $warning === '' ) {
414 $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');
415 $warning = apply_filters('wppb_recover_password_sent_message2', $warning);
416 $output .= '<p class="wppb-warning" role="alert">'.$warning.'</p>';
417 } else {
418 $output .= $warning;
419 }
420 }
421 }
422 }
423 // If the user used the correct key-code, update his/her password
424 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'] ) ) ) ) {
425
426 $password_change_message = '';
427
428 if( ( !empty( $_POST['passw1'] ) && !empty( $_POST['passw2'] ) ) ){
429
430 //get the login name and key and verify if they match the ones in the database
431 if( isset( $_POST['key'] ) )
432 $key = sanitize_text_field( $_POST['key'] );
433 else
434 $key = '';
435
436 if( empty( $key ) ){
437 $password_change_message = __('The key cannot be empty!', 'profile-builder');
438 $output .= wppb_password_recovery_error( $password_change_message, 'wppb_recover_password_password_changed_message2' );
439 }
440
441 if( isset( $_POST['login'] ) )
442 $login = sanitize_text_field( $_POST['login'] );
443 else
444 $login = '';
445
446 if( empty( $login ) ){
447 $password_change_message = __('Login cannot be empty!', 'profile-builder');
448 $output .= wppb_password_recovery_error( $password_change_message, 'wppb_recover_password_password_changed_message2' );
449 }
450
451 $user = check_password_reset_key( $key, $login );
452
453 if( is_wp_error( $user ) || empty( $user ) || ( !empty( $user ) && $user->ID != absint( $_POST['userData'] ) ) ){
454 $password_change_message = __('Invalid key!', 'profile-builder');
455 $output .= wppb_password_recovery_error( $password_change_message, 'wppb_recover_password_password_changed_message2' );
456 }
457
458 if( $_POST['passw1'] != $_POST['passw2'] ) {
459 $password_change_message = __('The entered passwords don\'t match!', 'profile-builder');
460 $output .= wppb_password_recovery_error( $password_change_message, 'wppb_recover_password_password_changed_message2' );
461 }
462
463 if( !empty( $wppb_generalSettings['minimum_password_length'] ) || ( isset( $_POST['wppb_password_strength'] ) && !empty( $wppb_generalSettings['minimum_password_strength'] ) ) ){
464 if( wppb_check_password_length( $_POST['passw1'] ) ){//phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
465 $password_change_message = sprintf( __( "The password must have the minimum length of %s characters", "profile-builder" ), $wppb_generalSettings['minimum_password_length'] ) . '<br/>';
466 $output .= wppb_password_recovery_error( $password_change_message, 'wppb_recover_password_password_changed_message2' );
467 }
468 if( wppb_check_password_strength() ){
469 $password_change_message = sprintf( __( "The password must have a minimum strength of %s", "profile-builder" ), wppb_check_password_strength() );
470 $output .= wppb_password_recovery_error( $password_change_message, 'wppb_recover_password_password_changed_message2' );
471 }
472 }
473
474 $password_change_message = apply_filters( 'wppb_recover_password_extra_validation', $password_change_message, $user );
475
476 if( !empty( $password_change_message ) ){
477 $output .= wppb_password_recovery_error( $password_change_message, 'wppb_custom_recover_password_validation_message' );
478 }
479
480 if( empty($password_change_message) ){
481
482 $password_change_message = __( 'Your password has been successfully changed!', 'profile-builder' );
483 $output .= wppb_password_recovery_success( $password_change_message, 'wppb_recover_password_password_changed_message1' );
484 $password_changed_success = true;
485
486 $userID = $user->ID;
487 $new_pass = $_POST['passw1']; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
488
489 //update the new password and delete the key
490 do_action( 'wppb_password_reset', $userID, $new_pass );
491 wp_set_password( $new_pass, $userID );
492
493 /* log out of all sessions on password reset */
494 $sessions = WP_Session_Tokens::get_instance( $userID );
495 $sessions->destroy_all();
496
497 $user_info = get_userdata( $userID );
498
499 //send email to user
500 wppb_send_successful_password_reset_email( $user_info, $new_pass );
501
502 //send email to admin
503 wppb_send_admin_password_reset_email( $user_info );
504
505 // CHECK FOR REDIRECT
506 $redirect_url = wppb_get_redirect_url( 'normal', 'after_success_password_reset', '', sanitize_user( $user_info->user_login ) );
507
508 //log the user in if the option was selected
509 if ( apply_filters( 'wppb_recover_password_autologin', false ) ){
510 //use the after_login redirect if no after_success_password_reset redirect is set
511 if( empty( $redirect_url ) ) {
512 $redirect_url = wppb_get_redirect_url( 'normal', 'after_login', '', sanitize_user( $user_info->user_login ) );
513 $redirect_url = apply_filters( 'wppb_after_recover_and_login', $redirect_url );
514 }
515 if( empty( $redirect_url ) ) {
516 $redirect_url = remove_query_arg( 'key', wppb_curpageurl() );
517 }
518
519 $redirect_url = add_query_arg( wppb_get_autologin_query_args( $user_info->ID ), $redirect_url );
520 }
521
522 $redirect_delay = apply_filters( 'wppb_success_password_reset_redirect_delay', 3, sanitize_user( $user_info->user_login ) );
523 $redirect_message = wppb_build_redirect( $redirect_url, $redirect_delay, 'after_success_password_reset' );
524
525 if( isset( $redirect_message ) && ! empty( $redirect_message ) ) {
526 $output .= '<p>' . $redirect_message . '</p>';
527 }
528 }
529 }
530 else{
531 $password_change_message .= __( "The password must not be empty!", "profile-builder" );
532 $output .= wppb_password_recovery_error( $password_change_message, 'wppb_recover_password_password_changed_message2' );
533 }
534 }
535
536 // use this action hook to add extra content before the password recovery form
537 do_action( 'wppb_before_recover_password_fields' );
538
539
540 //this is the part that shows the forms
541 if( isset( $_GET['key'] ) && isset( $_GET['login'] ) ){
542
543 $key = sanitize_text_field( $_GET['key'] );
544 $login = sanitize_text_field( $_GET['login'] );
545
546 if( !empty( $key ) && !empty( $login ) && !$password_changed_success ) {
547
548 $user = check_password_reset_key( $key, $login );
549
550 if( !is_wp_error( $user ) ){
551
552 ob_start();
553 wppb_create_recover_password_form( $user, $_POST, $is_ajax_form );
554 $output .= ob_get_contents();
555 ob_end_clean();
556 }
557 else {
558 $output .= wppb_password_recovery_error('<strong>' . __('ERROR:', 'profile-builder') . '</strong>' . __('Invalid key!', 'profile-builder'), 'wppb_recover_password_invalid_key_message');
559 }
560
561 } elseif ( !$password_changed_success && !$password_email_sent ) {
562 ob_start();
563 wppb_create_generate_password_form($_POST, $is_ajax_form);
564 $output .= ob_get_contents();
565 ob_end_clean();
566 }
567
568 } else {
569 if( !$password_email_sent ) {
570 ob_start();
571 wppb_create_generate_password_form($_POST, $is_ajax_form);
572 $output .= ob_get_contents();
573 ob_end_clean();
574 }
575 }
576
577 // use this action hook to add extra content after the password recovery form.
578 do_action( 'wppb_after_recover_password_fields' );
579
580 $output .= '</div>';
581 return apply_filters( 'wppb_recover_password_before_content_output', $output );
582 }
583
584 /* function for displaying success messages on the recover password page */
585 function wppb_password_recovery_success( $message, $filter ){
586 return apply_filters( $filter, '<p class="wppb-success" role="alert">'.$message.'</p>', $message );
587 }
588
589 /* function for displaying warning messages on the recover password page */
590 function wppb_password_recovery_warning( $message, $filter ){
591 if( $message !== '' ) {
592 return apply_filters( $filter, '<p class="wppb-warning" role="alert">'.$message.'</p>', $message );
593 } else {
594 return apply_filters( $filter, '', $message );
595 }
596 }
597
598 /* function for displaying error messages on the recover password page */
599 function wppb_password_recovery_error( $message, $filter ){
600 return apply_filters( $filter, '<p class="wppb-error" role="alert">'.$message.'</p>', $message );
601 }
602
603
604 // include missing scripts needed on Elementor Pages (Form inside an Elementor Popup)
605 function wppb_recover_password_scripts_and_styles() {
606 if ( is_plugin_active('elementor-pro/elementor-pro.php') && defined( 'WPPB_PAID_PLUGIN_URL' ) )
607 wp_enqueue_script( 'wppb_elementor_popup_script', WPPB_PAID_PLUGIN_URL . 'features/elementor-pro/assets/js/elementor-popup.js', array('jquery') );
608 }
609 add_action( 'elementor/frontend/after_enqueue_scripts', 'wppb_recover_password_scripts_and_styles' );