Merge pull request #23811 from woocommerce/feature/23807

Allow enforce password strength meter on checkout
This commit is contained in:
Rodrigo Primo 2019-06-27 11:39:00 -03:00 committed by GitHub
commit 68f5ccabc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 8 deletions

View File

@ -11,7 +11,12 @@
*/ */
init: function() { init: function() {
$( document.body ) $( document.body )
.on( 'keyup change', 'form.register #reg_password, form.checkout #account_password, form.edit-account #password_1, form.lost_reset_password #password_1', this.strengthMeter ); .on(
'keyup change',
'form.register #reg_password, form.checkout #account_password, ' +
'form.edit-account #password_1, form.lost_reset_password #password_1',
this.strengthMeter
);
$( 'form.checkout #createaccount' ).change(); $( 'form.checkout #createaccount' ).change();
}, },
@ -23,13 +28,24 @@
submit = $( 'button[type="submit"]', wrapper ), submit = $( 'button[type="submit"]', wrapper ),
field = $( '#reg_password, #account_password, #password_1', wrapper ), field = $( '#reg_password, #account_password, #password_1', wrapper ),
strength = 1, strength = 1,
fieldValue = field.val(); fieldValue = field.val(),
stop_checkout = ! wrapper.is( 'form.checkout' ); // By default is disabled on checkout.
wc_password_strength_meter.includeMeter( wrapper, field ); wc_password_strength_meter.includeMeter( wrapper, field );
strength = wc_password_strength_meter.checkPasswordStrength( wrapper, field ); strength = wc_password_strength_meter.checkPasswordStrength( wrapper, field );
if ( fieldValue.length > 0 && strength < wc_password_strength_meter_params.min_password_strength && ! wrapper.is( 'form.checkout' ) && -1 !== strength ) { // Allow password strength meter stop checkout.
if ( wc_password_strength_meter_params.stop_checkout ) {
stop_checkout = true;
}
if (
fieldValue.length > 0 &&
strength < wc_password_strength_meter_params.min_password_strength &&
-1 !== strength &&
stop_checkout
) {
submit.attr( 'disabled', 'disabled' ).addClass( 'disabled' ); submit.attr( 'disabled', 'disabled' ).addClass( 'disabled' );
} else { } else {
submit.removeAttr( 'disabled', 'disabled' ).removeClass( 'disabled' ); submit.removeAttr( 'disabled', 'disabled' ).removeClass( 'disabled' );

View File

@ -1 +1 @@
!function(a){"use strict";var d={init:function(){a(document.body).on("keyup change","form.register #reg_password, form.checkout #account_password, form.edit-account #password_1, form.lost_reset_password #password_1",this.strengthMeter),a("form.checkout #createaccount").change()},strengthMeter:function(){var s,r=a("form.register, form.checkout, form.edit-account, form.lost_reset_password"),e=a('button[type="submit"]',r),t=a("#reg_password, #account_password, #password_1",r),o=t.val();d.includeMeter(r,t),s=d.checkPasswordStrength(r,t),0<o.length&&s<wc_password_strength_meter_params.min_password_strength&&!r.is("form.checkout")&&-1!==s?e.attr("disabled","disabled").addClass("disabled"):e.removeAttr("disabled","disabled").removeClass("disabled")},includeMeter:function(s,r){var e=s.find(".woocommerce-password-strength");""===r.val()?(e.hide(),a(document.body).trigger("wc-password-strength-hide")):0===e.length?(r.after('<div class="woocommerce-password-strength" aria-live="polite"></div>'),a(document.body).trigger("wc-password-strength-added")):(e.show(),a(document.body).trigger("wc-password-strength-show"))},checkPasswordStrength:function(s,r){var e=s.find(".woocommerce-password-strength"),t=s.find(".woocommerce-password-hint"),o='<small class="woocommerce-password-hint">'+wc_password_strength_meter_params.i18n_password_hint+"</small>",a=wp.passwordStrength.meter(r.val(),wp.passwordStrength.userInputBlacklist()),d="";if(e.removeClass("short bad good strong"),t.remove(),e.is(":hidden"))return a;switch(a<wc_password_strength_meter_params.min_password_strength&&(d=" - "+wc_password_strength_meter_params.i18n_password_error),a){case 0:e.addClass("short").html(pwsL10n["short"]+d),e.after(o);break;case 1:case 2:e.addClass("bad").html(pwsL10n.bad+d),e.after(o);break;case 3:e.addClass("good").html(pwsL10n.good+d);break;case 4:e.addClass("strong").html(pwsL10n.strong+d);break;case 5:e.addClass("short").html(pwsL10n.mismatch)}return a}};d.init()}(jQuery); !function(d){"use strict";var n={init:function(){d(document.body).on("keyup change","form.register #reg_password, form.checkout #account_password, form.edit-account #password_1, form.lost_reset_password #password_1",this.strengthMeter),d("form.checkout #createaccount").change()},strengthMeter:function(){var s,r=d("form.register, form.checkout, form.edit-account, form.lost_reset_password"),e=d('button[type="submit"]',r),t=d("#reg_password, #account_password, #password_1",r),o=t.val(),a=!r.is("form.checkout");n.includeMeter(r,t),s=n.checkPasswordStrength(r,t),wc_password_strength_meter_params.stop_checkout&&(a=!0),0<o.length&&s<wc_password_strength_meter_params.min_password_strength&&-1!==s&&a?e.attr("disabled","disabled").addClass("disabled"):e.removeAttr("disabled","disabled").removeClass("disabled")},includeMeter:function(s,r){var e=s.find(".woocommerce-password-strength");""===r.val()?(e.hide(),d(document.body).trigger("wc-password-strength-hide")):0===e.length?(r.after('<div class="woocommerce-password-strength" aria-live="polite"></div>'),d(document.body).trigger("wc-password-strength-added")):(e.show(),d(document.body).trigger("wc-password-strength-show"))},checkPasswordStrength:function(s,r){var e=s.find(".woocommerce-password-strength"),t=s.find(".woocommerce-password-hint"),o='<small class="woocommerce-password-hint">'+wc_password_strength_meter_params.i18n_password_hint+"</small>",a=wp.passwordStrength.meter(r.val(),wp.passwordStrength.userInputBlacklist()),d="";if(e.removeClass("short bad good strong"),t.remove(),e.is(":hidden"))return a;switch(a<wc_password_strength_meter_params.min_password_strength&&(d=" - "+wc_password_strength_meter_params.i18n_password_error),a){case 0:e.addClass("short").html(pwsL10n["short"]+d),e.after(o);break;case 1:case 2:e.addClass("bad").html(pwsL10n.bad+d),e.after(o);break;case 3:e.addClass("good").html(pwsL10n.good+d);break;case 4:e.addClass("strong").html(pwsL10n.strong+d);break;case 5:e.addClass("short").html(pwsL10n.mismatch)}return a}};n.init()}(jQuery);

View File

@ -583,6 +583,7 @@ class WC_Frontend_Scripts {
case 'wc-password-strength-meter': case 'wc-password-strength-meter':
$params = array( $params = array(
'min_password_strength' => apply_filters( 'woocommerce_min_password_strength', 3 ), 'min_password_strength' => apply_filters( 'woocommerce_min_password_strength', 3 ),
'stop_checkout' => apply_filters( 'woocommerce_enforce_password_strength_meter_on_checkout', false ),
'i18n_password_error' => esc_attr__( 'Please enter a stronger password.', 'woocommerce' ), 'i18n_password_error' => esc_attr__( 'Please enter a stronger password.', 'woocommerce' ),
'i18n_password_hint' => esc_attr( wp_get_password_hint() ), 'i18n_password_hint' => esc_attr( wp_get_password_hint() ),
); );