wp-includes/ms-functions.phpにあるwpmu_signup_user_notification($user, $user_email, $key, $meta = '')と、
wp-admin/user-new.phpにあるadmin_created_user_email( $text )という関数とその周辺を読みました。
wpmu_signup_user_notification($user, $user_email, $key, $meta = ”)関数のコード
ユーザが新規に作成される時、メールを飛ばしている関数です。メールの文面にアクティベーションのためのURLが含まれていて、それがクリックされるとユーザが作成される処理に進むものです。
function wpmu_signup_user_notification($user, $user_email, $key, $meta = '') {
if ( !apply_filters('wpmu_signup_user_notification', $user, $user_email, $key, $meta) )
return false;
// Send email with activation link.
$admin_email = get_site_option( 'admin_email' );
if ( $admin_email == '' )
$admin_email = 'support@' . $_SERVER['SERVER_NAME'];
$from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
$message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
$message = sprintf(
apply_filters( 'wpmu_signup_user_notification_email',
__( "To activate your user, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login.\n\n" ),
$user, $user_email, $key, $meta
),
site_url( "wp-activate.php?key=$key" )
);
// TODO: Don't hard code activation link.
$subject = sprintf(
apply_filters( 'wpmu_signup_user_notification_subject',
__( '[%1$s] Activate %2$s' ),
$user, $user_email, $key, $meta
),
$from_name,
$user
);
wp_mail($user_email, $subject, $message, $message_headers);
return true;
}
引数
- $user
- 入力されたログイン名
- $user_email
- 入力されたユーザのメールアドレス
- $key
$key = substr( md5( time() . rand() . $user_email ), 0, 16 );という風に生成されたものが入ります。その瞬間のタイムスタンプ、ランダムな数、ユーザのメールアドレスを使ってmd5というハッシュ値を作り、そのハッシュ値の0文字目から16個目の文字までを保存しています。- $meta
- ユーザに持たせるメタデータです。権限が保存されたり、独自のユーザデータを持たせることができます。あとでユーザメタに保存される?optionsテーブルに保存させることもできたはず。
無効化したい時
以下では、wpmu_signup_user_notificationというフィルターフックをやってみて、falseが返ってきたら、この関数自体をパスしています。
if ( !apply_filters('wpmu_signup_user_notification', $user, $user_email, $key, $meta) )
return false;
具体的には、wp-admin/user-new.phpの中のマルチサイトでユーザを作成する箇所で、
if ( isset( $_POST[ 'noconfirmation' ] ) && is_super_admin() ) {
add_filter( 'wpmu_signup_user_notification', '__return_false' ); // Disable confirmation email
}
というコードがあります。
noconfirmationというポストにチェックが入っていて(値がtrueで)、ユーザがネットワークアドミンである場合には、上記のフックにfalseを返しています。メールが飛びません。
メールの情報を定義
$admin_email = get_site_option( 'admin_email' );
if ( $admin_email == '' )
$admin_email = 'support@' . $_SERVER['SERVER_NAME'];
$from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
$message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
$message = sprintf(
apply_filters( 'wpmu_signup_user_notification_email',
__( "To activate your user, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login.\n\n" ),
$user, $user_email, $key, $meta
),
site_url( "wp-activate.php?key=$key" )
);
// TODO: Don't hard code activation link.
$subject = sprintf(
apply_filters( 'wpmu_signup_user_notification_subject',
__( '[%1$s] Activate %2$s' ),
$user, $user_email, $key, $meta
),
$from_name,
$user
);
$admin_emailは、サイト管理者のアドレスで、もしなければsupport@サーバー名になります。
$from_nameは、サイト名で、もしなければWordPressになります。
$message_headersは、メール送信時の送信者ヘッダ。サイト名、管理者アドレス、文字コードが埋め込まれます。
$messageは、本文です。wpmu_signup_user_notification_emailというフックが用意されているので、ここで上書きすることが可能です。実際、user-new.phpの中で上書きのための文章が定義された上でここにフックしています。
この、wpmu_signup_user_notification_emailというフックは、引数にメール本文のメッセージ、$user、$user_email、$key、$metaを取るので、おそらくいろんな情報を上書きできるはずです。また、本文中にsite_url( "wp-activate.php?key=$key" )が有効化のためのリンクとして埋め込まれます。
$subjectは、件名です。これも上書き可能です。
送信
上記までで揃った(上書きされた)情報を元に、メールを送信します。
wp_mail($user_email, $subject, $message, $message_headers); return true;
wp_mail(送信先、件名、本文、送信ヘッダー)という形の関数です。
最後にtrueを返します。
結局このコードがやっていること
- 入力されたログイン名、メールアドレス、ハッシュ値の前半、メタ情報を受け取り
- 関数の先頭でfalseを受け取るとこの関数自体を抜けてしまうフックを通し
- メール送信用の情報を取得して
- 実際にメールを送る
admin_created_user_emailの周辺のコード
if ( is_multisite() ) {
function admin_created_user_email( $text ) {
/* translators: 1: Site name, 2: site URL, 3: role */
return sprintf( __( 'Hi,
You\'ve been invited to join \'%1$s\' at
%2$s as a %3$s.
If you do not want to join this site please ignore
this email. This invitation will expire in a few days.
Please click the following link to activate your user account:
%%s' ), get_bloginfo('name'), site_url(), esc_html( $_REQUEST[ 'role' ] ) );
}
add_filter( 'wpmu_signup_user_notification_email', 'admin_created_user_email' );
function admin_created_user_subject( $text ) {
return sprintf( __( '[%s] Your site invite' ), get_bloginfo( 'name' ) );
}
}
全体がif ( is_multisite() ) { }で囲われているので、マルチサイトの場合だけです。
文章を書き換えている
長いですが、admin_created_user_email( $text )を定義した直後に、すぐにフィルターにフックしています。つまり、マルチサイトの場合は、上で見たメール送信プログラムの中のメールの本文を書き換える処理です。
admin_created_user_subjectは何をしている?
関数を定義したまま、どこにもフックしてないんですけど、これは一体何をしているの??

コメント