EditProfileBuilder.php
5 years ago
FieldsShortcodeCallback.php
4 years ago
FrontendProfileBuilder.php
4 years ago
GlobalShortcodes.php
4 years ago
LoginFormBuilder.php
5 years ago
PasswordResetBuilder.php
5 years ago
RegistrationFormBuilder.php
5 years ago
index.php
5 years ago
PasswordResetBuilder.php
365 lines
| 1 | <?php namespace ProfilePress\Core\ShortcodeParser\Builder; |
| 2 | |
| 3 | use ProfilePress\Core\Classes\FormRepository; |
| 4 | |
| 5 | class PasswordResetBuilder |
| 6 | { |
| 7 | public function __construct() |
| 8 | { |
| 9 | add_shortcode('user-login', array($this, 'user_login')); |
| 10 | add_shortcode('reset-submit', array($this, 'submit_button')); |
| 11 | |
| 12 | add_shortcode('enter-password', array($this, 'enter_password')); |
| 13 | add_shortcode('re-enter-password', array($this, 're_enter_password')); |
| 14 | add_shortcode('password-reset-submit', array($this, 'password_reset_submit')); |
| 15 | |
| 16 | add_shortcode('reset-password-meter', array($this, 'password_meter')); |
| 17 | |
| 18 | do_action('ppress_register_password_reset_form_shortcode'); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * parse the [user-login] shortcode |
| 23 | * |
| 24 | * @param array $atts |
| 25 | * |
| 26 | * @return string |
| 27 | */ |
| 28 | public function user_login($atts) |
| 29 | { |
| 30 | // grab unofficial attributes |
| 31 | $other_atts_html = ppress_other_field_atts($atts); |
| 32 | |
| 33 | $placeholder = esc_html__('Username or Email', 'wp-user-avatar'); |
| 34 | |
| 35 | $atts = shortcode_atts( |
| 36 | array( |
| 37 | 'class' => '', |
| 38 | 'id' => '', |
| 39 | 'value' => '', |
| 40 | 'title' => $placeholder, |
| 41 | 'placeholder' => $placeholder, |
| 42 | ), |
| 43 | $atts |
| 44 | ); |
| 45 | |
| 46 | $atts = apply_filters('ppress_password_reset_username_field_atts', $atts); |
| 47 | |
| 48 | $class = 'class="' . $atts['class'] . '"'; |
| 49 | $placeholder = 'placeholder="' . $atts['placeholder'] . '"'; |
| 50 | $id = ! empty($atts['id']) ? 'id="' . $atts['id'] . '"' : null; |
| 51 | $value = isset($_POST['user_login']) ? 'value="' . esc_attr($_POST['user_login']) . '"' : 'value=""'; |
| 52 | |
| 53 | $title = 'title="' . $atts['title'] . '"'; |
| 54 | |
| 55 | $html = "<input name=\"user_login\" type='text' $title $value $class $id $placeholder $other_atts_html required='required'/>"; |
| 56 | |
| 57 | return apply_filters('ppress_password_reset_username_field', $html, $atts); |
| 58 | } |
| 59 | |
| 60 | protected function get_processing_label($atts) |
| 61 | { |
| 62 | $form_type = FormRepository::PASSWORD_RESET_TYPE; |
| 63 | $form_id = isset($GLOBALS['pp_password_reset_form_id']) ? $GLOBALS['pp_password_reset_form_id'] : 0; |
| 64 | |
| 65 | if (isset($GLOBALS['pp_melange_form_id'])) { |
| 66 | $form_id = $GLOBALS['pp_melange_form_id']; |
| 67 | $form_type = FormRepository::MELANGE_TYPE; |
| 68 | } |
| 69 | |
| 70 | return ! empty($atts['processing_label']) ? $atts['processing_label'] : FormRepository::get_processing_label($form_id, $form_type); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Password reset submit button. |
| 75 | * |
| 76 | * @param $atts array shortcode param |
| 77 | * |
| 78 | * @return string HTML submit button |
| 79 | */ |
| 80 | public function submit_button($atts) |
| 81 | { |
| 82 | // grab unofficial attributes |
| 83 | $other_atts_html = ppress_other_field_atts($atts); |
| 84 | |
| 85 | $atts = shortcode_atts( |
| 86 | array( |
| 87 | 'class' => '', |
| 88 | 'id' => '', |
| 89 | 'value' => '', |
| 90 | 'title' => '', |
| 91 | 'processing_label' => '', |
| 92 | 'name' => 'password_reset_submit', |
| 93 | ), |
| 94 | $atts |
| 95 | ); |
| 96 | |
| 97 | |
| 98 | $atts = apply_filters('ppress_password_reset_submit_field_atts', $atts); |
| 99 | |
| 100 | $name = 'name="' . $atts['name'] . '"'; |
| 101 | $class = 'class="pp-submit-form ' . $atts['class'] . '"'; |
| 102 | $id = ! empty($atts['id']) ? 'id="' . $atts['id'] . '"' : ''; |
| 103 | |
| 104 | $value = ! empty($atts['value']) ? $atts['value'] : esc_html__('Get New Password', 'wp-user-avatar'); |
| 105 | |
| 106 | $title = 'title="' . $atts['title'] . '"'; |
| 107 | |
| 108 | $html = sprintf( |
| 109 | '<input data-pp-submit-label="%2$s" data-pp-processing-label="%3$s" type="submit" value="%2$s" %1$s>', |
| 110 | "$name $title $class $id $other_atts_html", |
| 111 | $value, |
| 112 | $this->get_processing_label($atts) |
| 113 | ); |
| 114 | |
| 115 | return apply_filters('ppress_password_reset_submit_field', $html, $atts); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | /** |
| 120 | * parse the [enter-password] shortcode |
| 121 | * |
| 122 | * @param array $atts |
| 123 | * |
| 124 | * @return string |
| 125 | */ |
| 126 | public function enter_password($atts) |
| 127 | { |
| 128 | // grab unofficial attributes |
| 129 | $other_atts_html = ppress_other_field_atts($atts); |
| 130 | |
| 131 | $atts = shortcode_atts( |
| 132 | array( |
| 133 | 'class' => '', |
| 134 | 'id' => '', |
| 135 | 'value' => '', |
| 136 | 'title' => '', |
| 137 | 'placeholder' => '', |
| 138 | ), |
| 139 | $atts |
| 140 | ); |
| 141 | |
| 142 | $atts = apply_filters('ppress_password_reset_handler_password1_field_atts', $atts); |
| 143 | |
| 144 | $class = 'class="' . $atts['class'] . '"'; |
| 145 | $placeholder = 'placeholder="' . $atts['placeholder'] . '"'; |
| 146 | $id = ! empty($atts['id']) ? 'id="' . $atts['id'] . '"' : null; |
| 147 | $value = 'value=""'; |
| 148 | |
| 149 | $title = 'title="' . $atts['title'] . '"'; |
| 150 | |
| 151 | $html = "<input name=\"password1\" type='password' $title $value $class $id $placeholder $other_atts_html autocomplete='off'>"; |
| 152 | |
| 153 | return apply_filters('ppress_password_reset_handler_password1_field', $html, $atts); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /** |
| 158 | * parse the [re-enter-password] shortcode |
| 159 | * |
| 160 | * @param array $atts |
| 161 | * |
| 162 | * @return string |
| 163 | */ |
| 164 | function re_enter_password($atts) |
| 165 | { |
| 166 | // grab unofficial attributes |
| 167 | $other_atts_html = ppress_other_field_atts($atts); |
| 168 | |
| 169 | $atts = shortcode_atts( |
| 170 | array( |
| 171 | 'class' => '', |
| 172 | 'id' => '', |
| 173 | 'value' => '', |
| 174 | 'title' => '', |
| 175 | 'placeholder' => '', |
| 176 | ), |
| 177 | $atts |
| 178 | ); |
| 179 | |
| 180 | $atts = apply_filters('ppress_password_reset_handler_password2_field_atts', $atts); |
| 181 | |
| 182 | $class = 'class="' . $atts['class'] . '"'; |
| 183 | $placeholder = 'placeholder="' . $atts['placeholder'] . '"'; |
| 184 | $id = ! empty($atts['id']) ? 'id="' . $atts['id'] . '"' : null; |
| 185 | $value = 'value=""'; |
| 186 | |
| 187 | $title = 'title="' . $atts['title'] . '"'; |
| 188 | |
| 189 | $html = "<input name=\"password2\" type='password' $title $value $class $id $placeholder $other_atts_html autocomplete='off'>"; |
| 190 | |
| 191 | return apply_filters('ppress_password_reset_handler_password2_field', $html, $atts); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Password strength meter field for password reset handler form. |
| 196 | * @see http://code.tutsplus.com/articles/using-the-included-password-strength-meter-script-in-wordpress--wp-34736 |
| 197 | */ |
| 198 | public static function password_meter($atts) |
| 199 | { |
| 200 | // grab unofficial attributes |
| 201 | $other_atts_html = ppress_other_field_atts($atts); |
| 202 | |
| 203 | $atts = shortcode_atts( |
| 204 | array( |
| 205 | 'class' => '', |
| 206 | 'enforce' => 'true', |
| 207 | ), |
| 208 | $atts |
| 209 | ); |
| 210 | |
| 211 | $atts = apply_filters('ppress_password_reset_handler_meter_field_atts', $atts); |
| 212 | |
| 213 | wp_localize_script('password-strength-meter', 'pwsL10n', array( |
| 214 | 'empty' => esc_html__('Strength indicator'), |
| 215 | 'short' => esc_html__('Very weak'), |
| 216 | 'bad' => esc_html__('Weak'), |
| 217 | 'good' => _x('Medium', 'password strength'), |
| 218 | 'strong' => esc_html__('Strong'), |
| 219 | 'mismatch' => esc_html__('Mismatch'), |
| 220 | )); |
| 221 | |
| 222 | ob_start(); ?> |
| 223 | <?php if ('true' == $atts['enforce']) : ?> |
| 224 | <input type="hidden" name="pp_enforce_password_meter" value="true"> |
| 225 | <?php endif; ?> |
| 226 | <div id="pp-pass-strength-result" <?php echo 'class="' . $atts['class'] . '"' . $other_atts_html; ?>><?php _e('Strength indicator'); ?></div> |
| 227 | <script type="text/javascript"> |
| 228 | var pass_strength = 0; |
| 229 | jQuery(document).ready(function ($) { |
| 230 | var password1 = $('input[name=password1]'); |
| 231 | var password2 = $('input[name=password2]'); |
| 232 | var submitButton = $('input[name=reset_password]'); |
| 233 | var strengthMeterId = $('#pp-pass-strength-result'); |
| 234 | |
| 235 | $('body').on('keyup', 'input[name=password1], input[name=password2]', |
| 236 | function (event) { |
| 237 | pp_checkPasswordStrength(password1, password2, strengthMeterId, submitButton, []); |
| 238 | } |
| 239 | ); |
| 240 | |
| 241 | if (password1.val() != '') { |
| 242 | // trigger 'keyup' event to check password strength when password field isn't empty. |
| 243 | $('body input[name=password1]').trigger('keyup'); |
| 244 | } |
| 245 | |
| 246 | submitButton.click(function () { |
| 247 | $('input[name=pp_enforce_password_meter]').val(pass_strength); |
| 248 | }); |
| 249 | }); |
| 250 | |
| 251 | function pp_checkPasswordStrength($pass1, $pass2, $strengthResult, $submitButton, blacklistArray) { |
| 252 | |
| 253 | var min_password_strength = <?php echo apply_filters('ppress_min_password_strength', 4); ?>; |
| 254 | var pass1 = $pass1.val(); |
| 255 | var pass2 = $pass2.val(); |
| 256 | |
| 257 | <?php if('true' == $atts['enforce']) : ?> |
| 258 | // Reset the form & meter |
| 259 | $submitButton.attr('disabled', 'disabled').css("opacity", ".4"); |
| 260 | <?php endif; ?> |
| 261 | $strengthResult.removeClass('short bad good strong'); |
| 262 | |
| 263 | // Extend our blacklist array with those from the inputs & site data |
| 264 | blacklistArray = blacklistArray.concat(wp.passwordStrength.userInputBlacklist()); |
| 265 | |
| 266 | // Get the password strength |
| 267 | var strength = wp.passwordStrength.meter(pass1, blacklistArray, pass2); |
| 268 | |
| 269 | // Add the strength meter results |
| 270 | switch (strength) { |
| 271 | |
| 272 | case 2: |
| 273 | $strengthResult.addClass('bad').html(pwsL10n.bad); |
| 274 | break; |
| 275 | case 3: |
| 276 | $strengthResult.addClass('good').html(pwsL10n.good); |
| 277 | if (min_password_strength === 3) { |
| 278 | pass_strength = 1; |
| 279 | } |
| 280 | break; |
| 281 | case 4: |
| 282 | $strengthResult.addClass('strong').html(pwsL10n.strong); |
| 283 | if (min_password_strength === 4) { |
| 284 | pass_strength = 1; |
| 285 | } |
| 286 | break; |
| 287 | case 5: |
| 288 | $strengthResult.addClass('short').html(pwsL10n.mismatch); |
| 289 | break; |
| 290 | default: |
| 291 | $strengthResult.addClass('short').html(pwsL10n.short); |
| 292 | } |
| 293 | |
| 294 | // The meter function returns a result even if pass2 is empty, |
| 295 | // enable only the submit button if the password is strong |
| 296 | <?php if('true' == $atts['enforce']) : ?> |
| 297 | if (min_password_strength <= strength) { |
| 298 | $submitButton.removeAttr('disabled').css("opacity", ""); |
| 299 | } |
| 300 | <?php endif; ?> |
| 301 | |
| 302 | return strength; |
| 303 | } |
| 304 | </script> |
| 305 | <?php |
| 306 | return apply_filters('ppress_registration_password_meter_field', ob_get_clean(), $atts); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Password reset handler submit button. |
| 311 | * |
| 312 | * @param $atts array shortcode param |
| 313 | * |
| 314 | * @return string HTML submit button |
| 315 | */ |
| 316 | function password_reset_submit($atts) |
| 317 | { |
| 318 | // grab unofficial attributes |
| 319 | $other_atts_html = ppress_other_field_atts($atts); |
| 320 | |
| 321 | $atts = shortcode_atts( |
| 322 | array( |
| 323 | 'class' => '', |
| 324 | 'id' => '', |
| 325 | 'title' => '', |
| 326 | 'processing_label' => '', |
| 327 | 'name' => 'reset_password', |
| 328 | ), |
| 329 | $atts |
| 330 | ); |
| 331 | |
| 332 | $atts = apply_filters('ppress_password_reset_handler_submit_field_atts', $atts); |
| 333 | |
| 334 | $name = 'name="' . $atts['name'] . '"'; |
| 335 | $class = 'class="pp-submit-form ' . $atts['class'] . '"'; |
| 336 | |
| 337 | $value = ! empty($atts['value']) ? $atts['value'] : esc_html__('Get New Password', 'wp-user-avatar'); |
| 338 | |
| 339 | $id = ! empty($atts['id']) ? 'id="' . $atts['id'] . '"' : ''; |
| 340 | |
| 341 | $title = 'title="' . $atts['title'] . '"'; |
| 342 | |
| 343 | $html = sprintf( |
| 344 | '<input data-pp-submit-label="%2$s" data-pp-processing-label="%3$s" type="submit" value="%2$s" %1$s>', |
| 345 | "$name $title $class $id $other_atts_html", |
| 346 | $value, |
| 347 | $this->get_processing_label($atts) |
| 348 | ); |
| 349 | |
| 350 | return apply_filters('ppress_password_reset_handler_submit_field', $html, $atts); |
| 351 | } |
| 352 | |
| 353 | |
| 354 | /** Singleton poop */ |
| 355 | static function get_instance() |
| 356 | { |
| 357 | static $instance = false; |
| 358 | if ( ! $instance) { |
| 359 | $instance = new self; |
| 360 | } |
| 361 | |
| 362 | return $instance; |
| 363 | } |
| 364 | } |
| 365 |