[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/js/ -> password-strength-meter.js (source)

   1  /**
   2   * @output wp-admin/js/password-strength-meter.js
   3   */
   4  
   5  /* global zxcvbn */
   6  
   7  /** @namespace wp */
   8  window.wp = window.wp || {};
   9  
  10  ( function( $, wp ) {
  11      var __ = wp.i18n.__,
  12          sprintf = wp.i18n.sprintf;
  13  
  14      /**
  15       * Contains functions to determine the password strength.
  16       *
  17       * @since 3.7.0
  18       *
  19       * @namespace
  20       */
  21      wp.passwordStrength = {
  22          /**
  23           * Determines the strength of a given password.
  24           *
  25           * Compares first password to the password confirmation.
  26           *
  27           * @since 3.7.0
  28           *
  29           * @param {string} password1       The subject password.
  30           * @param {Array}  disallowedList An array of words that will lower the entropy of
  31           *                                 the password.
  32           * @param {string} password2       The password confirmation.
  33           *
  34           * @return {number} The password strength score.
  35           */
  36          meter : function( password1, disallowedList, password2 ) {
  37              if ( ! Array.isArray( disallowedList ) )
  38                  disallowedList = [ disallowedList.toString() ];
  39  
  40              if (password1 != password2 && password2 && password2.length > 0)
  41                  return 5;
  42  
  43              if ( 'undefined' === typeof window.zxcvbn ) {
  44                  // Password strength unknown.
  45                  return -1;
  46              }
  47  
  48              var result = zxcvbn( password1, disallowedList );
  49              return result.score;
  50          },
  51  
  52          /**
  53           * Builds an array of words that should be penalized.
  54           *
  55           * Certain words need to be penalized because it would lower the entropy of a
  56           * password if they were used. The disallowedList is based on user input fields such
  57           * as username, first name, email etc.
  58           *
  59           * @since 3.7.0
  60           * @deprecated 5.5.0 Use {@see 'userInputDisallowedList()'} instead.
  61           *
  62           * @return {string[]} The array of words to be disallowed.
  63           */
  64          userInputBlacklist : function() {
  65              window.console.log(
  66                  sprintf(
  67                      /* translators: 1: Deprecated function name, 2: Version number, 3: Alternative function name. */
  68                      __( '%1$s is deprecated since version %2$s! Use %3$s instead. Please consider writing more inclusive code.' ),
  69                      'wp.passwordStrength.userInputBlacklist()',
  70                      '5.5.0',
  71                      'wp.passwordStrength.userInputDisallowedList()'
  72                  )
  73              );
  74  
  75              return wp.passwordStrength.userInputDisallowedList();
  76          },
  77  
  78          /**
  79           * Builds an array of words that should be penalized.
  80           *
  81           * Certain words need to be penalized because it would lower the entropy of a
  82           * password if they were used. The disallowed list is based on user input fields such
  83           * as username, first name, email etc.
  84           *
  85           * @since 5.5.0
  86           *
  87           * @return {string[]} The array of words to be disallowed.
  88           */
  89          userInputDisallowedList : function() {
  90              var i, userInputFieldsLength, rawValuesLength, currentField,
  91                  rawValues       = [],
  92                  disallowedList  = [],
  93                  userInputFields = [ 'user_login', 'first_name', 'last_name', 'nickname', 'display_name', 'email', 'url', 'description', 'weblog_title', 'admin_email' ];
  94  
  95              // Collect all the strings we want to disallow.
  96              rawValues.push( document.title );
  97              rawValues.push( document.URL );
  98  
  99              userInputFieldsLength = userInputFields.length;
 100              for ( i = 0; i < userInputFieldsLength; i++ ) {
 101                  currentField = $( '#' + userInputFields[ i ] );
 102  
 103                  if ( 0 === currentField.length ) {
 104                      continue;
 105                  }
 106  
 107                  rawValues.push( currentField[0].defaultValue );
 108                  rawValues.push( currentField.val() );
 109              }
 110  
 111              /*
 112               * Strip out non-alphanumeric characters and convert each word to an
 113               * individual entry.
 114               */
 115              rawValuesLength = rawValues.length;
 116              for ( i = 0; i < rawValuesLength; i++ ) {
 117                  if ( rawValues[ i ] ) {
 118                      disallowedList = disallowedList.concat( rawValues[ i ].replace( /\W/g, ' ' ).split( ' ' ) );
 119                  }
 120              }
 121  
 122              /*
 123               * Remove empty values, short words and duplicates. Short words are likely to
 124               * cause many false positives.
 125               */
 126              disallowedList = $.grep( disallowedList, function( value, key ) {
 127                  if ( '' === value || 4 > value.length ) {
 128                      return false;
 129                  }
 130  
 131                  return $.inArray( value, disallowedList ) === key;
 132              });
 133  
 134              return disallowedList;
 135          }
 136      };
 137  
 138      // Backward compatibility.
 139  
 140      /**
 141       * Password strength meter function.
 142       *
 143       * @since 2.5.0
 144       * @deprecated 3.7.0 Use wp.passwordStrength.meter instead.
 145       *
 146       * @global
 147       *
 148       * @type {wp.passwordStrength.meter}
 149       */
 150      window.passwordStrength = wp.passwordStrength.meter;
 151  } )( jQuery, window.wp );


Generated : Sun Aug 30 08:20:25 2026 Cross-referenced by PHPXref