Guest User

Untitled

a guest
Feb 21st, 2018
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. // functions.php
  2.  
  3. add_action( 'user_profile_update_errors', 'validateProfileUpdate', 10, 3 );
  4. add_filter( 'registration_errors', 'validateRegistration', 10, 3 );
  5. add_action( 'validate_password_reset', 'validatePasswordReset', 10, 2 );
  6.  
  7. /**
  8. * validate profile update
  9. *
  10. * @author Joe Sexton <joe.@webtipblog.com>
  11. * @param WP_Error $errors
  12. * @param boolean $update
  13. * @param object $user raw user object not a WP_User
  14. */
  15. public function validateProfileUpdate( WP_Error &$errors, $update, &$user ) {
  16. return validateComplexPassword( $errors );
  17. }
  18.  
  19. /**
  20. * validate registration
  21. *
  22. * @author Joe Sexton <joe.@webtipblog.com>
  23. * @param WP_Error $errors
  24. * @param string $sanitized_user_login
  25. * @param string $user_email
  26. * @return WP_Error
  27. */
  28. function validateRegistration( WP_Error &$errors, $sanitized_user_login, $user_email ) {
  29. return validateComplexPassword( $errors );
  30. }
  31.  
  32. /**
  33. * validate password reset
  34. *
  35. * @author Joe Sexton <joe.@webtipblog.com>
  36. * @param WP_Error $errors
  37. * @param stdClass $userData
  38. * @return WP_Error
  39. */
  40. function validatePasswordReset( WP_Error &$errors, $userData ) {
  41. return validateComplexPassword( $errors );
  42. }
  43.  
  44. /**
  45. * validate complex password
  46. *
  47. * @author Joe Sexton <joe.@webtipblog.com>
  48. * @param WP_Error $errors
  49. * @param stdClass $userData
  50. * @return WP_Error
  51. */
  52. function validateComplexPassword( $errors ) {
  53. $password = ( isset( $_POST[ 'pass1' ] ) && trim( $_POST[ 'pass1' ] ) ) ? $_POST[ 'pass1' ] : null;
  54.  
  55. // no password or already has password error
  56. if ( empty( $password ) || ( $errors->get_error_data( 'pass' ) ) )
  57. return $errors;
  58.  
  59. // validate
  60. if ( ! isStrongPassword( $password ) )
  61. $errors->add( 'pass', '<strong>ERROR</strong>: Your password must contain at least 8 characters.' ); // your complex password error message
  62.  
  63. return $errors;
  64. }
  65.  
  66. /**
  67. * isStrongPassword
  68. *
  69. * @author Joe Sexton <joe.@webtipblog.com>
  70. * @param string $password
  71. * @return boolean
  72. */
  73. function isStrongPassword( $password ) {
  74. return strlen( $password ) >= 8; // your complex password algorithm
  75. }
  76.  
  77. wp_enqueue_script( 'password-strength-meter' );
  78.  
  79. <form>
  80. <input type="password" name="password" />
  81. <input type="password" name="password_retyped" />
  82. <span id="password-strength"></span>
  83. <input type="submit" disabled="disabled" value="Submit" />
  84. </form>
  85.  
  86. function checkPasswordStrength( $pass1,
  87. $pass2,
  88. $strengthResult,
  89. $submitButton,
  90. blacklistArray ) {
  91. var pass1 = $pass1.val();
  92. var pass2 = $pass2.val();
  93.  
  94. // Reset the form & meter
  95. $submitButton.attr( 'disabled', 'disabled' );
  96. $strengthResult.removeClass( 'short bad good strong' );
  97.  
  98. // Extend our blacklist array with those from the inputs & site data
  99. blacklistArray = blacklistArray.concat( wp.passwordStrength.userInputBlacklist() )
  100.  
  101. // Get the password strength
  102. var strength = wp.passwordStrength.meter( pass1, blacklistArray, pass2 );
  103.  
  104. // Add the strength meter results
  105. switch ( strength ) {
  106.  
  107. case 2:
  108. $strengthResult.addClass( 'bad' ).html( pwsL10n.bad );
  109. break;
  110.  
  111. case 3:
  112. $strengthResult.addClass( 'good' ).html( pwsL10n.good );
  113. break;
  114.  
  115. case 4:
  116. $strengthResult.addClass( 'strong' ).html( pwsL10n.strong );
  117. break;
  118.  
  119. case 5:
  120. $strengthResult.addClass( 'short' ).html( pwsL10n.mismatch );
  121. break;
  122.  
  123. default:
  124. $strengthResult.addClass( 'short' ).html( pwsL10n.short );
  125.  
  126. }
  127.  
  128. // The meter function returns a result even if pass2 is empty,
  129. // enable only the submit button if the password is strong and
  130. // both passwords are filled up
  131. if ( 4 === strength && '' !== pass2.trim() ) {
  132. $submitButton.removeAttr( 'disabled' );
  133. }
  134.  
  135. return strength;
  136. }
  137.  
  138. jQuery( document ).ready( function( $ ) {
  139. // Binding to trigger checkPasswordStrength
  140. $( 'body' ).on( 'keyup', 'input[name=password1], input[name=password2]',
  141. function( event ) {
  142. checkPasswordStrength(
  143. $('input[name=password]'), // First password field
  144. $('input[name=password_retyped]'), // Second password field
  145. $('#password-strength'), // Strength meter
  146. $('input[type=submit]'), // Submit button
  147. ['black', 'listed', 'word'] // Blacklisted words
  148. );
  149. }
  150. );
  151. });
  152.  
  153. add_filter( 'gettext_with_context', 'wpse199813_change_password_indicatior', 10, 4 );
  154.  
  155. function wpse199813_change_password_indicatior($translations, $text, $context, $domain){
  156.  
  157. if( $text == "Weak" && $context == "password strength")
  158. return "OK";
  159.  
  160. return $translations;
  161. }
  162.  
  163. add_filter('gettext', 'translate_text');
  164. add_filter('ngettext', 'translate_text');
  165.  
  166. function translate_text($translated) {
  167. $translated = str_ireplace('Very Weak', 'Bad', $translated);
  168. $translated = str_ireplace('Weak', 'OK', $translated);
  169.  
  170. return $translated;
  171. }
  172.  
  173. add_action( 'wp_enqueue_scripts', 'my_strength_meter_localize_script' );
  174. function my_strength_meter_localize_script() {
  175. wp_localize_script( 'password-strength-meter', 'pwsL10n', array(
  176. 'empty' => __( 'But... it's empty!', 'theme-domain' ),
  177. 'short' => __( 'Too short!', 'theme-domain' ),
  178. 'bad' => __( 'Not even close!', 'theme-domain' ),
  179. 'good' => __( 'You are getting closer...', 'theme-domain' ),
  180. 'strong' => __( 'Now, that's a password!', 'theme-domain' ),
  181. 'mismatch' => __( 'They are completely different, come on!', 'theme-domain' )
  182. ) );
  183. }
Add Comment
Please, Sign In to add comment