前半部分の続きです。
パスワードの取得とリセット、新規ユーザ登録のための関数を定義する部分まで。後半に続きます。
パスワード取得、retrieve_password()関数の定義
/** * Handles sending password retrieval email to user. */
ユーザにパスワード取得メールを送信する処理。
以下、定義。
グローバル宣言とエラーのチェック
function retrieve_password() {
global $wpdb, $current_site;
$errors = new WP_Error();
if ( empty( $_POST['user_login'] ) && empty( $_POST['user_email'] ) )
$errors->add('empty_username', __('<strong>ERROR</strong>: Enter a username or e-mail address.'));
if ( strpos($_POST['user_login'], '@') ) {
$user_data = get_user_by_email(trim($_POST['user_login']));
if ( empty($user_data) )
$errors->add('invalid_email', __('<strong>ERROR</strong>: There is no user registered with that email address.'));
} else {
$login = trim($_POST['user_login']);
$user_data = get_userdatabylogin($login);
}
do_action('lostpassword_post');
if ( $errors->get_error_code() )
return $errors;
if ( !$user_data ) {
$errors->add('invalidcombo', __('<strong>ERROR</strong>: Invalid username or e-mail.'));
return $errors;
}
- ユーザ名orメールアドレスが記入されてない→「記入して」
- ユーザ名に@が含まれる→get_user_by_email関数でDB照会→なければ「いないよ」
- ユーザ名に@が含まれない→get_userdatabylogin関数でDB照会→なければ「ダメです」
変数の定義確認
// redefining user_login ensures we return the right case in the email
$user_login = $user_data->user_login;
$user_email = $user_data->user_email;
do_action('retreive_password', $user_login); // Misspelled and deprecated
do_action('retrieve_password', $user_login);
そもそもパスワード復旧を許すのか
$allow = apply_filters('allow_password_reset', true, $user_data->ID);
if ( ! $allow )
return new WP_Error('no_password_reset', __('Password reset is not allowed for this user'));
else if ( is_wp_error($allow) )
return $allow;
パスワード確認ともしなければ生成、$messageを保持
$key = $wpdb->get_var($wpdb->prepare("SELECT user_activation_key FROM $wpdb->users WHERE user_login = %s", $user_login));
if ( empty($key) ) {
// Generate something random for a key...
$key = wp_generate_password(20, false);
do_action('retrieve_password_key', $user_login, $key);
// Now insert the new md5 key into the db
$wpdb->update($wpdb->users, array('user_activation_key' => $key), array('user_login' => $user_login));
}
$message = __('Someone has asked to reset the password for the following site and username.') . "¥r¥n¥r¥n";
$message .= network_site_url() . "¥r¥n¥r¥n";
$message .= sprintf(__('Username: %s'), $user_login) . "¥r¥n¥r¥n";
$message .= __('To reset your password visit the following address, otherwise just ignore this email and nothing will happen.') . "¥r¥n¥r¥n";
$message .= network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . "¥r¥n";
get_var()関数は、DBにアクセスするためのもの。
prepare()関数は、SQL文を含んでおいて、実行を待っている状態を作り出す。ここでは、$keyを実行したときに、SQL文も実行される。
SQL文は、user_loginカラムに$user_loginを持つところのuser_activation_keyをセレクトする、の意味。
if文では、もし$keyが空であれば、wp_generate_password()関数でパスワードを生成して、それをmd5変換したものをusersテーブルにUPDATEしています。
あとで使うと思われる$messagaeを複数行保持させておきます。
マルチサイトの時は
if ( is_multisite() )
$blogname = $GLOBALS['current_site']->site_name;
else
// The blogname option is escaped with esc_html on the way into the database in sanitize_option
// we want to reverse this for the plain text arena of emails.
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$blognameは個別サイトの名前にします。
また、メールのメッセージで使える形にしています。
タイトルなどを保持
$title = sprintf( __('[%s] Password Reset'), $blogname );
$title = apply_filters('retrieve_password_title', $title);
$message = apply_filters('retrieve_password_message', $message, $key);
件名:パスワードリセット、ブログ名
本文:retrieve_password_messageから取ってくる
if ( $message && !wp_mail($user_email, $title, $message) )
wp_die( __('The e-mail could not be sent.') . "<br />¥n" . __('Possible reason: your host may have disabled the mail() function...') );
return true;
}
メールが送れなかったら、エラーメッセージを出力して処理終了。
trueを返しておく。
ここまでで、
retrieve_password()の定義が完了。
パスワードリセットreset_password($key, $login) 関数の定義
/** * Handles resetting the user's password. */
パスワードリセットの処理です。
以下定義です。
下準備
function reset_password($key, $login) {
global $wpdb;
$key = preg_replace('/[^a-z0-9]/i', '', $key);
if ( empty( $key ) || !is_string( $key ) )
return new WP_Error('invalid_key', __('Invalid key'));
if ( empty($login) || !is_string($login) )
return new WP_Error('invalid_key', __('Invalid key'));
$user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE user_activation_key = %s AND user_login = %s", $key, $login));
if ( empty( $user ) )
return new WP_Error('invalid_key', __('Invalid key'));
グローバル宣言。
$keyを置換します。
$keyや$login、$userが空だったり文字列でなかった場合にエラーメッセージ表示。
仮のパスワード的なものを発行
// Generate something random for a password...
$new_pass = wp_generate_password();
do_action('password_reset', $user, $new_pass);
$new_pass に仮のものを発行しておきます。
アクションフック挿入。
メール文
wp_set_password($new_pass, $user->ID);
update_user_option($user->ID, 'default_password_nag', true, true); //Set up the Password change nag.
$message = sprintf(__('Username: %s'), $user->user_login) . "¥r¥n";
$message .= sprintf(__('Password: %s'), $new_pass) . "¥r¥n";
$message .= site_url('wp-login.php', 'login') . "¥r¥n";
default_password_nagがなんなのか、よく分からず。
$messageに保持。
マルチサイト
if ( is_multisite() )
$blogname = $GLOBALS['current_site']->site_name;
else
// The blogname option is escaped with esc_html on the way into the database in sanitize_option
// we want to reverse this for the plain text arena of emails.
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
マルチサイトの場合のサイト名は個別サイト名で。
マルチでなければ、デコードしたもの。
件名など、
$title = sprintf( __('[%s] Your new password'), $blogname );
$title = apply_filters('password_reset_title', $title);
$message = apply_filters('password_reset_message', $message, $new_pass);
if ( $message && !wp_mail($user->user_email, $title, $message) )
wp_die( __('The e-mail could not be sent.') . "<br />¥n" . __('Possible reason: your host may have disabled the mail() function...') );
wp_password_change_notification($user);
return true;
}
件名と本文の設定。
エラーメッセージの設定。
wp_password_change_notification()は、管理者へのパス変更の通知。
以上で、reset_password()関数の定義が完了。
新規登録
新規にユーザを登録させるための処理。
/** * Handles registering a new user. */
以下、register_new_user( $user_login, $user_email )の定義です。
下準備
function register_new_user( $user_login, $user_email ) {
$errors = new WP_Error();
$sanitized_user_login = sanitize_user( $user_login );
$user_email = apply_filters( 'user_registration_email', $user_email );
エラーの新しいインスタンス作成。
フック。
ユーザ名、メールのチェック
// Check the username
if ( $sanitized_user_login == '' ) {
$errors->add( 'empty_username', __( '<strong>ERROR</strong>: Please enter a username.' ) );
} elseif ( ! validate_username( $user_login ) ) {
$errors->add( 'invalid_username', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) );
$sanitized_user_login = '';
} elseif ( username_exists( $sanitized_user_login ) ) {
$errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered, please choose another one.' ) );
}
// Check the e-mail address
if ( $user_email == '' ) {
$errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please type your e-mail address.' ) );
} elseif ( ! is_email( $user_email ) ) {
$errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn’t correct.' ) );
$user_email = '';
} elseif ( email_exists( $user_email ) ) {
$errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.' ) );
}
入力されたユーザ名とメールアドレスが、空、不正、かぶってる場合にはエラーを出す。
フックとエラー関連
do_action( 'register_post', $sanitized_user_login, $user_email, $errors ); $errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email ); if ( $errors->get_error_code() ) return $errors;
パスワード生成、ユーザ名の作成
$user_pass = wp_generate_password();
$user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email );
if ( ! $user_id ) {
$errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn’t register you... please contact the <a href="mailto:%s">webmaster</a> !' ), get_option( 'admin_email' ) ) );
return $errors;
}
パスワード生成、ユーザIDを作る、エラー。
update_user_option( $user_id, 'default_password_nag', true, true ); //Set up the Password change nag. wp_new_user_notification( $user_id, $user_pass ); return $user_id; }
メール送信等。
最後は$user_idを返して、定義が完了。

コメント