2016-01-27 10:53:59 +00:00
|
|
|
/* global wp, pwsL10n, wc_password_strength_meter_params */
|
2017-10-13 12:23:26 +00:00
|
|
|
( function( $ ) {
|
|
|
|
'use strict';
|
2015-10-09 16:31:47 +00:00
|
|
|
/**
|
2016-01-22 19:33:35 +00:00
|
|
|
* Password Strength Meter class.
|
2015-10-09 16:31:47 +00:00
|
|
|
*/
|
|
|
|
var wc_password_strength_meter = {
|
|
|
|
|
|
|
|
/**
|
2016-01-22 19:33:35 +00:00
|
|
|
* Initialize strength meter actions.
|
2015-10-09 16:31:47 +00:00
|
|
|
*/
|
|
|
|
init: function() {
|
2015-10-09 16:43:08 +00:00
|
|
|
$( document.body )
|
2019-05-24 18:27:26 +00:00
|
|
|
.on(
|
|
|
|
'keyup change',
|
|
|
|
'form.register #reg_password, form.checkout #account_password, ' +
|
|
|
|
'form.edit-account #password_1, form.lost_reset_password #password_1',
|
|
|
|
this.strengthMeter
|
|
|
|
);
|
2015-10-09 16:43:08 +00:00
|
|
|
$( 'form.checkout #createaccount' ).change();
|
2015-10-09 16:31:47 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-01-22 19:33:35 +00:00
|
|
|
* Strength Meter.
|
2015-10-09 16:31:47 +00:00
|
|
|
*/
|
|
|
|
strengthMeter: function() {
|
2019-05-24 18:27:26 +00:00
|
|
|
var wrapper = $( 'form.register, form.checkout, form.edit-account, form.lost_reset_password' ),
|
|
|
|
submit = $( 'button[type="submit"]', wrapper ),
|
|
|
|
field = $( '#reg_password, #account_password, #password_1', wrapper ),
|
|
|
|
strength = 1,
|
|
|
|
fieldValue = field.val(),
|
|
|
|
stop_checkout = ! wrapper.is( 'form.checkout' ); // By default is disabled on checkout.
|
2015-10-09 16:31:47 +00:00
|
|
|
|
|
|
|
wc_password_strength_meter.includeMeter( wrapper, field );
|
|
|
|
|
2016-03-21 16:26:33 +00:00
|
|
|
strength = wc_password_strength_meter.checkPasswordStrength( wrapper, field );
|
2015-10-09 16:31:47 +00:00
|
|
|
|
2019-05-24 18:27:26 +00:00
|
|
|
// 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
|
|
|
|
) {
|
2016-01-28 15:53:08 +00:00
|
|
|
submit.attr( 'disabled', 'disabled' ).addClass( 'disabled' );
|
2016-01-27 10:53:59 +00:00
|
|
|
} else {
|
2016-01-28 15:53:08 +00:00
|
|
|
submit.removeAttr( 'disabled', 'disabled' ).removeClass( 'disabled' );
|
2015-10-09 16:31:47 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-01-22 19:33:35 +00:00
|
|
|
* Include meter HTML.
|
2015-10-09 16:31:47 +00:00
|
|
|
*
|
|
|
|
* @param {Object} wrapper
|
|
|
|
* @param {Object} field
|
|
|
|
*/
|
|
|
|
includeMeter: function( wrapper, field ) {
|
2015-10-13 20:42:26 +00:00
|
|
|
var meter = wrapper.find( '.woocommerce-password-strength' );
|
2015-10-09 16:31:47 +00:00
|
|
|
|
2016-01-28 15:53:08 +00:00
|
|
|
if ( '' === field.val() ) {
|
2018-05-08 17:10:26 +00:00
|
|
|
meter.hide();
|
|
|
|
$( document.body ).trigger( 'wc-password-strength-hide' );
|
2016-01-28 15:53:08 +00:00
|
|
|
} else if ( 0 === meter.length ) {
|
|
|
|
field.after( '<div class="woocommerce-password-strength" aria-live="polite"></div>' );
|
2016-02-03 15:24:11 +00:00
|
|
|
$( document.body ).trigger( 'wc-password-strength-added' );
|
2018-05-08 17:10:26 +00:00
|
|
|
} else {
|
|
|
|
meter.show();
|
|
|
|
$( document.body ).trigger( 'wc-password-strength-show' );
|
2015-10-09 16:31:47 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-01-22 19:33:35 +00:00
|
|
|
* Check password strength.
|
2015-10-09 16:31:47 +00:00
|
|
|
*
|
2016-01-22 19:33:35 +00:00
|
|
|
* @param {Object} field
|
2015-10-09 16:31:47 +00:00
|
|
|
*
|
|
|
|
* @return {Int}
|
|
|
|
*/
|
2016-03-21 16:26:33 +00:00
|
|
|
checkPasswordStrength: function( wrapper, field ) {
|
2018-05-08 17:10:26 +00:00
|
|
|
var meter = wrapper.find( '.woocommerce-password-strength' ),
|
|
|
|
hint = wrapper.find( '.woocommerce-password-hint' ),
|
|
|
|
hint_html = '<small class="woocommerce-password-hint">' + wc_password_strength_meter_params.i18n_password_hint + '</small>',
|
|
|
|
strength = wp.passwordStrength.meter( field.val(), wp.passwordStrength.userInputBlacklist() ),
|
|
|
|
error = '';
|
2015-10-09 16:31:47 +00:00
|
|
|
|
2018-05-08 17:10:26 +00:00
|
|
|
// Reset.
|
2015-10-09 16:31:47 +00:00
|
|
|
meter.removeClass( 'short bad good strong' );
|
2016-01-27 10:53:59 +00:00
|
|
|
hint.remove();
|
2015-10-09 16:31:47 +00:00
|
|
|
|
2018-05-08 17:10:26 +00:00
|
|
|
if ( meter.is( ':hidden' ) ) {
|
|
|
|
return strength;
|
|
|
|
}
|
|
|
|
|
2016-01-28 15:36:24 +00:00
|
|
|
// Error to append
|
2016-01-28 15:53:08 +00:00
|
|
|
if ( strength < wc_password_strength_meter_params.min_password_strength ) {
|
2016-01-28 15:36:24 +00:00
|
|
|
error = ' - ' + wc_password_strength_meter_params.i18n_password_error;
|
|
|
|
}
|
|
|
|
|
2015-10-09 16:31:47 +00:00
|
|
|
switch ( strength ) {
|
2016-01-28 15:36:24 +00:00
|
|
|
case 0 :
|
|
|
|
meter.addClass( 'short' ).html( pwsL10n['short'] + error );
|
|
|
|
meter.after( hint_html );
|
|
|
|
break;
|
|
|
|
case 1 :
|
|
|
|
meter.addClass( 'bad' ).html( pwsL10n.bad + error );
|
|
|
|
meter.after( hint_html );
|
|
|
|
break;
|
2015-10-09 16:31:47 +00:00
|
|
|
case 2 :
|
2016-01-28 15:36:24 +00:00
|
|
|
meter.addClass( 'bad' ).html( pwsL10n.bad + error );
|
2016-01-27 10:53:59 +00:00
|
|
|
meter.after( hint_html );
|
2015-10-09 16:31:47 +00:00
|
|
|
break;
|
|
|
|
case 3 :
|
2016-01-28 15:36:24 +00:00
|
|
|
meter.addClass( 'good' ).html( pwsL10n.good + error );
|
2015-10-09 16:31:47 +00:00
|
|
|
break;
|
|
|
|
case 4 :
|
2016-01-28 15:36:24 +00:00
|
|
|
meter.addClass( 'strong' ).html( pwsL10n.strong + error );
|
2015-10-09 16:31:47 +00:00
|
|
|
break;
|
|
|
|
case 5 :
|
|
|
|
meter.addClass( 'short' ).html( pwsL10n.mismatch );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return strength;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
wc_password_strength_meter.init();
|
2017-10-27 17:40:05 +00:00
|
|
|
})( jQuery );
|