2016-01-27 10:53:59 +00:00
|
|
|
/* global wp, pwsL10n, wc_password_strength_meter_params */
|
2015-10-09 16:31:47 +00:00
|
|
|
jQuery( function( $ ) {
|
|
|
|
|
|
|
|
/**
|
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 )
|
2016-01-27 10:53:59 +00:00
|
|
|
.on( 'keyup', 'form.register #reg_password, form.checkout #account_password, form.edit-account #password_1, form.lost_reset_password #password_1', this.strengthMeter )
|
|
|
|
.on( 'submit', 'form.register, form.edit-account, form.lost_reset_password', this.onSubmit );
|
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() {
|
2016-01-22 19:37:34 +00:00
|
|
|
var wrapper = $( 'form.register, form.checkout, form.edit-account, form.lost_reset_password' ),
|
2015-10-09 16:31:47 +00:00
|
|
|
submit = $( 'input[type="submit"]', wrapper ),
|
2016-01-22 19:33:35 +00:00
|
|
|
field = $( '#reg_password, #account_password, #password_1', wrapper ),
|
2015-10-09 16:31:47 +00:00
|
|
|
strength = 1;
|
|
|
|
|
|
|
|
wc_password_strength_meter.includeMeter( wrapper, field );
|
|
|
|
|
|
|
|
strength = wc_password_strength_meter.checkPasswordStrength( field );
|
|
|
|
|
2016-01-27 10:53:59 +00:00
|
|
|
// Add class to wrapper
|
|
|
|
if ( 3 === strength || 4 === strength ) {
|
|
|
|
wrapper.removeClass( 'has-weak-password' );
|
|
|
|
} else {
|
|
|
|
wrapper.addClass( 'has-weak-password' );
|
|
|
|
}
|
|
|
|
|
2016-01-22 19:46:15 +00:00
|
|
|
// Stop form if password is weak... But not in checkout form!
|
2015-10-09 16:31:47 +00:00
|
|
|
if ( 3 === strength || 4 === strength ) {
|
2016-01-27 10:53:59 +00:00
|
|
|
submit.removeClass( 'disabled' );
|
2016-01-22 19:46:15 +00:00
|
|
|
} else if ( ! wrapper.hasClass( 'checkout' ) ) {
|
2016-01-27 10:53:59 +00:00
|
|
|
submit.addClass( 'disabled' );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-01-27 11:07:44 +00:00
|
|
|
/**
|
|
|
|
* When the form is submitted, prevent if weak.
|
|
|
|
*/
|
2016-01-27 10:53:59 +00:00
|
|
|
onSubmit: function() {
|
|
|
|
$( '.woocommerce-password-error' ).remove();
|
|
|
|
|
|
|
|
if ( $( this ).is( '.has-weak-password' ) ) {
|
|
|
|
$( this ).prepend( '<div class="woocommerce-error woocommerce-password-error">' + wc_password_strength_meter_params.i18n_password_error + '</div>' );
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
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
|
|
|
|
|
|
|
if ( 0 === meter.length ) {
|
2015-10-13 20:42:26 +00:00
|
|
|
field.after( '<div class="woocommerce-password-strength" aria-live="polite"></div>' );
|
2015-10-09 16:31:47 +00:00
|
|
|
} else if ( '' === field.val() ) {
|
|
|
|
meter.remove();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
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}
|
|
|
|
*/
|
|
|
|
checkPasswordStrength: function( field ) {
|
2016-01-27 10:53:59 +00:00
|
|
|
var meter = $( '.woocommerce-password-strength' );
|
|
|
|
var hint = $( '.woocommerce-password-hint' );
|
|
|
|
var hint_html = '<small class="woocommerce-password-hint">' + wc_password_strength_meter_params.i18n_password_hint + '</small>';
|
|
|
|
var strength = wp.passwordStrength.meter( field.val(), wp.passwordStrength.userInputBlacklist() );
|
2015-10-09 16:31:47 +00:00
|
|
|
|
2016-01-27 10:53:59 +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
|
|
|
|
|
|
|
switch ( strength ) {
|
|
|
|
case 2 :
|
|
|
|
meter.addClass( 'bad' ).html( pwsL10n.bad );
|
2016-01-27 10:53:59 +00:00
|
|
|
meter.after( hint_html );
|
2015-10-09 16:31:47 +00:00
|
|
|
break;
|
|
|
|
case 3 :
|
|
|
|
meter.addClass( 'good' ).html( pwsL10n.good );
|
|
|
|
break;
|
|
|
|
case 4 :
|
|
|
|
meter.addClass( 'strong' ).html( pwsL10n.strong );
|
|
|
|
break;
|
|
|
|
case 5 :
|
|
|
|
meter.addClass( 'short' ).html( pwsL10n.mismatch );
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
meter.addClass( 'short' ).html( pwsL10n['short'] );
|
2016-01-27 10:53:59 +00:00
|
|
|
meter.after( hint_html );
|
2015-10-09 16:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return strength;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
wc_password_strength_meter.init();
|
|
|
|
});
|