| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
/* handle field output */ |
| 5 |
function wppb_password_handler( $output, $form_location, $field, $user_id, $field_check_errors, $request_data ){ |
| 6 |
$current_password_error = wppb_password_has_current_password_error( $field, $form_location, $request_data, $user_id ); |
| 7 |
|
| 8 |
$item_title = apply_filters( 'wppb_'.$form_location.'_password_item_title', wppb_icl_t( 'plugin profile-builder-pro', 'default_field_'.$field['id'].'_title_translation', $field['field-title'], true ) ); |
| 9 |
$item_description = wppb_icl_t( 'plugin profile-builder-pro', 'default_field_'.$field['id'].'_description_translation', $field['description'], true ); |
| 10 |
|
| 11 |
if ( $form_location != 'back_end' ){ |
| 12 |
$error_mark = ( ( $field['required'] == 'Yes' ) ? ( $form_location != 'edit_profile' ? '<span class="wppb-required" title="'.wppb_required_field_error($field["field-title"]).'">*</span>' : '' ) : '' ); |
| 13 |
|
| 14 |
if ( array_key_exists( $field['id'], $field_check_errors ) && ! $current_password_error ) |
| 15 |
$error_mark = '<img src="'.WPPB_PLUGIN_URL.'assets/images/pencil_delete.png" title="'.wppb_required_field_error($field["field-title"]).'"/>'; |
| 16 |
|
| 17 |
$extra_attr = apply_filters( 'wppb_extra_attribute', '', $field, $form_location ); |
| 18 |
|
| 19 |
$autocomplete = 'off'; |
| 20 |
|
| 21 |
if( $form_location == 'edit_profile' ) |
| 22 |
$autocomplete = 'new-password'; |
| 23 |
|
| 24 |
$subfields_output = ''; |
| 25 |
|
| 26 |
if ( wppb_password_should_ask_for_current_password( $field, $form_location, $user_id ) ) { |
| 27 |
$subfields_output .= ' |
| 28 |
<div class="wppb-password-subfield wppb-current-password-subfield' . ( $current_password_error ? ' wppb-current-password-subfield-error' : '' ) . '"> |
| 29 |
<label for="wppb_current_passw">' . __( 'Current Password', 'profile-builder' ) . '</label> |
| 30 |
<span class="wppb-password-field-container"> |
| 31 |
<input class="text-input '. apply_filters( 'wppb_fields_extra_css_class', '', $field ) .'" name="wppb_current_passw" maxlength="'. apply_filters( 'wppb_maximum_character_length', 70, $field ) .'" type="password" id="wppb_current_passw" value="" autocomplete="current-password" '. $extra_attr .'/> |
| 32 |
'. wppb_password_visibility_toggle_html() .' <!-- add the HTML for the visibility toggle --> |
| 33 |
</span> |
| 34 |
</div>'; |
| 35 |
} |
| 36 |
|
| 37 |
$subfields_output .= ' |
| 38 |
<div class="wppb-password-subfield wppb-new-password-subfield"> |
| 39 |
<label for="passw1">' . $item_title.$error_mark . '</label> |
| 40 |
<span class="wppb-password-field-container"> |
| 41 |
<input class="text-input '. apply_filters( 'wppb_fields_extra_css_class', '', $field ) .'" name="passw1" maxlength="'. apply_filters( 'wppb_maximum_character_length', 70, $field ) .'" type="password" id="passw1" value="" autocomplete="'. esc_attr( $autocomplete ) .'" '. $extra_attr .'/> |
| 42 |
'. wppb_password_visibility_toggle_html() .' <!-- add the HTML for the visibility toggle --> |
| 43 |
</span> |
| 44 |
</div>'; |
| 45 |
|
| 46 |
$output = '<div class="wppb-password-group">' . $subfields_output . '</div>'; |
| 47 |
|
| 48 |
if( ! empty( $item_description ) ) |
| 49 |
$output .= '<span class="wppb-description-delimiter">'. $item_description .' '. wppb_password_length_text() .' '. wppb_password_strength_description() .'</span>'; |
| 50 |
else |
| 51 |
$output .= '<span class="wppb-description-delimiter">'. wppb_password_length_text() .' '. wppb_password_strength_description() .'</span>'; |
| 52 |
|
| 53 |
/* if we have active the password strength checker */ |
| 54 |
$output .= wppb_password_strength_checker_html(); |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
return apply_filters( 'wppb_'.$form_location.'_password', $output, $form_location, $field, $user_id, $field_check_errors, $request_data ); |
| 59 |
} |
| 60 |
add_filter( 'wppb_output_form_field_default-password', 'wppb_password_handler', 10, 6 ); |
| 61 |
|
| 62 |
/* handle field validation */ |
| 63 |
function wppb_check_password_value( $message, $field, $request_data, $form_location, $field_check_errors = '', $user_id = 0 ){ |
| 64 |
|
| 65 |
if ( $form_location == 'register' ){ |
| 66 |
if ( ( isset( $request_data['passw1'] ) && ( trim( $request_data['passw1'] ) == '' ) ) && ( $field['required'] == 'Yes' ) ) |
| 67 |
return wppb_required_field_error($field["field-title"]); |
| 68 |
|
| 69 |
elseif ( !isset( $request_data['passw1'] ) && ( $field['required'] == 'Yes' ) ) |
| 70 |
return wppb_required_field_error($field["field-title"]); |
| 71 |
} |
| 72 |
|
| 73 |
if ( wppb_password_should_ask_for_current_password( $field, $form_location, $user_id ) && isset( $request_data['passw1'] ) && trim( $request_data['passw1'] ) !== '' && !wppb_password_was_changed_in_current_request() ) { |
| 74 |
$current_password = isset( $request_data['wppb_current_passw'] ) ? trim( wp_unslash( (string) $request_data['wppb_current_passw'] ) ) : ''; |
| 75 |
|
| 76 |
if ( empty( $request_data['wppb_current_passw'] ) ) { |
| 77 |
return __( 'Please enter your current password to change your password.', 'profile-builder' ); |
| 78 |
} |
| 79 |
|
| 80 |
$edited_user_id = ! empty( $user_id ) ? absint( $user_id ) : get_current_user_id(); |
| 81 |
$user = get_userdata( $edited_user_id ); |
| 82 |
|
| 83 |
if ( empty( $user ) || ! wp_check_password( $current_password, $user->data->user_pass, $edited_user_id ) ) { |
| 84 |
return __( 'The current password you entered is incorrect.', 'profile-builder' ); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if ( isset( $request_data['passw1'] ) && trim( $request_data['passw1'] ) != '' ){ |
| 89 |
$wppb_generalSettings = get_option( 'wppb_general_settings' ); |
| 90 |
|
| 91 |
if( wppb_check_password_length( $request_data['passw1'] ) ) |
| 92 |
return '<br/>'. sprintf( __( "The password must have the minimum length of %s characters", "profile-builder" ), $wppb_generalSettings['minimum_password_length'] ); |
| 93 |
|
| 94 |
|
| 95 |
if( wppb_check_password_strength() ){ |
| 96 |
return '<br/>' . sprintf( __( "The password must have a minimum strength of %s", "profile-builder" ), wppb_check_password_strength() ); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
return $message; |
| 101 |
} |
| 102 |
add_filter( 'wppb_check_form_field_default-password', 'wppb_check_password_value', 10, 6 ); |
| 103 |
|
| 104 |
/* handle field save */ |
| 105 |
function wppb_userdata_add_password( $userdata, $global_request, $form_args ){ |
| 106 |
if( wppb_field_exists_in_form( 'Default - Password', $form_args ) ) { |
| 107 |
if (isset($global_request['passw1']) && (trim($global_request['passw1']) != '')) |
| 108 |
$userdata['user_pass'] = trim($global_request['passw1']); |
| 109 |
} |
| 110 |
|
| 111 |
return $userdata; |
| 112 |
} |
| 113 |
add_filter( 'wppb_build_userdata', 'wppb_userdata_add_password', 10, 3 ); |
| 114 |
|
| 115 |
function wppb_password_should_ask_for_current_password( $field, $form_location, $user_id = 0 ) { |
| 116 |
if ( $form_location !== 'edit_profile' ) { |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
if ( empty( $field['ask-current-password'] ) || $field['ask-current-password'] !== 'yes' ) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
$edited_user_id = ! empty( $user_id ) ? absint( $user_id ) : get_current_user_id(); |
| 125 |
|
| 126 |
return $edited_user_id === get_current_user_id(); |
| 127 |
} |
| 128 |
|
| 129 |
function wppb_password_has_current_password_error( $field, $form_location, $request_data, $user_id = 0 ) { |
| 130 |
if ( ! wppb_password_should_ask_for_current_password( $field, $form_location, $user_id ) ) { |
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
if ( wppb_password_was_changed_in_current_request() ) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
if ( empty( $request_data['passw1'] ) || trim( $request_data['passw1'] ) === '' ) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
if ( empty( $request_data['wppb_current_passw'] ) ) { |
| 143 |
return true; |
| 144 |
} |
| 145 |
|
| 146 |
$current_password = isset( $request_data['wppb_current_passw'] ) ? trim( wp_unslash( (string) $request_data['wppb_current_passw'] ) ) : ''; |
| 147 |
$edited_user_id = ! empty( $user_id ) ? absint( $user_id ) : get_current_user_id(); |
| 148 |
$user = get_userdata( $edited_user_id ); |
| 149 |
|
| 150 |
return empty( $user ) || ! wp_check_password( $current_password, $user->data->user_pass, $edited_user_id ); |
| 151 |
} |
| 152 |
|
| 153 |
function wppb_password_was_changed_in_current_request() { |
| 154 |
return did_action( 'wppb_edit_profile_password_changed' ) > 0; |
| 155 |
} |
| 156 |
|
| 157 |
function wppb_password_add_current_password_error_class( $classes, $field ) { |
| 158 |
if ( empty( $field['field'] ) || $field['field'] !== 'Default - Password' ) { |
| 159 |
return $classes; |
| 160 |
} |
| 161 |
|
| 162 |
if ( ! is_user_logged_in() || empty( $_REQUEST['action'] ) || $_REQUEST['action'] !== 'edit_profile' ) { |
| 163 |
return $classes; |
| 164 |
} |
| 165 |
|
| 166 |
$user_id = get_current_user_id(); |
| 167 |
|
| 168 |
if ( ( ! is_multisite() && current_user_can( 'edit_users' ) ) || ( is_multisite() && ( current_user_can( 'remove_users' ) || current_user_can( 'manage_options' ) ) ) ) { |
| 169 |
if ( ! empty( $_REQUEST['edit_user'] ) ) { |
| 170 |
$user_id = absint( $_REQUEST['edit_user'] ); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
if ( wppb_password_has_current_password_error( $field, 'edit_profile', $_REQUEST, $user_id ) ) { |
| 175 |
$classes .= ' wppb-current-password-field-error'; |
| 176 |
} |
| 177 |
|
| 178 |
return $classes; |
| 179 |
} |
| 180 |
add_filter( 'wppb_field_css_class', 'wppb_password_add_current_password_error_class', 10, 2 ); |
| 181 |
|