メモ:PHPでWooCommerceのパスワード強度チェックをしたい
WC Password Strength Settings
https://ja.wordpress.org/plugins/wc-password-strength-settings/
ZxcvbnPhpライブラリ
https://github.com/bjeavons/zxcvbn-php
functions.php
require_once dirname(__FILE__).'/ZxcvbnPhp/Matcher.php';
require_once dirname(__FILE__).'/ZxcvbnPhp/Feedback.php';
require_once dirname(__FILE__).'/ZxcvbnPhp/TimeEstimator.php';
require_once dirname(__FILE__).'/ZxcvbnPhp/Scorer.php';
require_once dirname(__FILE__).'/ZxcvbnPhp/Zxcvbn.php';
require_once dirname(__FILE__).'/ZxcvbnPhp/Matchers/MatchInterface.php';
require_once dirname(__FILE__).'/ZxcvbnPhp/Matchers/Match.php';
require_once dirname(__FILE__).'/ZxcvbnPhp/Matchers/Bruteforce.php';
require_once dirname(__FILE__).'/ZxcvbnPhp/Matchers/DateMatch.php';
require_once dirname(__FILE__).'/ZxcvbnPhp/Matchers/DictionaryMatch.php';
require_once dirname(__FILE__).'/ZxcvbnPhp/Matchers/L33tMatch.php';
require_once dirname(__FILE__).'/ZxcvbnPhp/Matchers/RepeatMatch.php';
require_once dirname(__FILE__).'/ZxcvbnPhp/Matchers/ReverseDictionaryMatch.php';
require_once dirname(__FILE__).'/ZxcvbnPhp/Matchers/SequenceMatch.php';
require_once dirname(__FILE__).'/ZxcvbnPhp/Matchers/SpatialMatch.php';
require_once dirname(__FILE__).'/ZxcvbnPhp/Matchers/YearMatch.php';
use ZxcvbnPhp\Zxcvbn;
使用方法
$strength_level = apply_filters( 'woocommerce_min_password_strength', 3 );
$zxcvbn = new \ZxcvbnPhp\Zxcvbn();
$strength = $zxcvbn->passwordStrength($password);
$message = '';
if($strength['score'] < $strength_level){
switch($strength['score']){
case 0:
//4文字未満
$message = __(get_option( 'woocommerce_password_strength_label_1', 'パスワードが短いか単純すぎます'));
break;
case 1:
//short
$message = __(get_option( 'woocommerce_password_strength_label_2', null));
break;
case 2:
//bad
$message = __(get_option( 'woocommerce_password_strength_label_3', null));
break;
case 3:
$message = __(get_option( 'woocommerce_password_strength_label_4', null));
break;
}
$validation_errors->add('password_error', $message);
}