wp_send_user_request()
云策文档标注
概述
wp_send_user_request() 函数用于发送确认请求邮件,以验证用户操作。它处理请求验证、本地化、邮件内容生成和发送,并返回成功或错误状态。
关键要点
- 函数参数为 $request_id(整数,必需),通过 wp_create_user_request() 创建的请求 ID。
- 返回 true 表示成功,WP_Error 表示失败。
- 内部流程包括:验证请求、切换用户或站点本地化、生成确认密钥、构建邮件数据、应用过滤器、发送邮件。
- 支持多个过滤器钩子,如 user_request_action_email_subject、user_request_action_email_content 和 user_request_action_email_headers,用于自定义邮件主题、内容和头部。
- 邮件内容包含占位符(如 ###DESCRIPTION###、###CONFIRM_URL###),在发送前被替换为实际数据。
代码示例
function wp_send_user_request( $request_id ) {
$request_id = absint( $request_id );
$request = wp_get_user_request( $request_id );
if ( ! $request ) {
return new WP_Error( 'invalid_request', __( 'Invalid personal data request.' ) );
}
// 本地化处理
if ( ! empty( $request->user_id ) ) {
$switched_locale = switch_to_user_locale( $request->user_id );
} else {
$switched_locale = switch_to_locale( get_locale() );
}
$request->confirm_key = wp_generate_user_request_key( $request_id );
$email_data = array(
'request' => $request,
'email' => $request->email,
'description' => wp_user_request_action_description( $request->action_name ),
'confirm_url' => add_query_arg(
array(
'action' => 'confirmaction',
'request_id' => $request_id,
'confirm_key' => $request->confirm_key,
),
wp_login_url()
),
'sitename' => wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ),
'siteurl' => home_url(),
);
$subject = sprintf( __( '[%1$s] Confirm Action: %2$s' ), $email_data['sitename'], $email_data['description'] );
$subject = apply_filters( 'user_request_action_email_subject', $subject, $email_data['sitename'], $email_data );
$content = __( 'Howdy,nnA request has been made to perform the following action on your account:nn ###DESCRIPTION###nnTo confirm this, please click on the following link:n###CONFIRM_URL###nnYou can safely ignore and delete this email if you do not want tontake this action.nnRegards,nAll at ###SITENAME###n###SITEURL###' );
$content = apply_filters( 'user_request_action_email_content', $content, $email_data );
$content = str_replace( '###DESCRIPTION###', $email_data['description'], $content );
$content = str_replace( '###CONFIRM_URL###', sanitize_url( $email_data['confirm_url'] ), $content );
$content = str_replace( '###EMAIL###', $email_data['email'], $content );
$content = str_replace( '###SITENAME###', $email_data['sitename'], $content );
$content = str_replace( '###SITEURL###', sanitize_url( $email_data['siteurl'] ), $content );
$headers = '';
$headers = apply_filters( 'user_request_action_email_headers', $headers, $subject, $content, $request_id, $email_data );
$email_sent = wp_mail( $email_data['email'], $subject, $content, $headers );
if ( $switched_locale ) {
restore_previous_locale();
}
if ( ! $email_sent ) {
return new WP_Error( 'privacy_email_error', __( 'Unable to send personal data export confirmation email.' ) );
}
return true;
}注意事项
- 函数在 WordPress 4.9.6 版本引入,主要用于隐私数据请求处理。
- 邮件发送失败时返回 WP_Error,错误代码为 'privacy_email_error'。
- 使用前需确保请求通过 wp_create_user_request() 创建且有效。
- 过滤器钩子允许开发者自定义邮件内容,增强灵活性。
原文内容
Send a confirmation request email to confirm an action.
Description
If the request is not already pending, it will be updated.
Parameters
$request_idintrequired-
ID of the request created via wp_create_user_request() .
Source
function wp_send_user_request( $request_id ) {
$request_id = absint( $request_id );
$request = wp_get_user_request( $request_id );
if ( ! $request ) {
return new WP_Error( 'invalid_request', __( 'Invalid personal data request.' ) );
}
// Localize message content for user; fallback to site default for visitors.
if ( ! empty( $request->user_id ) ) {
$switched_locale = switch_to_user_locale( $request->user_id );
} else {
$switched_locale = switch_to_locale( get_locale() );
}
/*
* Generate the new user request key first, as it is used by both the $request
* object and the confirm_url array.
* See https://core.trac.wordpress.org/ticket/44940
*/
$request->confirm_key = wp_generate_user_request_key( $request_id );
$email_data = array(
'request' => $request,
'email' => $request->email,
'description' => wp_user_request_action_description( $request->action_name ),
'confirm_url' => add_query_arg(
array(
'action' => 'confirmaction',
'request_id' => $request_id,
'confirm_key' => $request->confirm_key,
),
wp_login_url()
),
'sitename' => wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ),
'siteurl' => home_url(),
);
/* translators: Confirm privacy data request notification email subject. 1: Site title, 2: Name of the action. */
$subject = sprintf( __( '[%1$s] Confirm Action: %2$s' ), $email_data['sitename'], $email_data['description'] );
/**
* Filters the subject of the email sent when an account action is attempted.
*
* @since 4.9.6
*
* @param string $subject The email subject.
* @param string $sitename The name of the site.
* @param array $email_data {
* Data relating to the account action email.
*
* @type WP_User_Request $request User request object.
* @type string $email The email address this is being sent to.
* @type string $description Description of the action being performed so the user knows what the email is for.
* @type string $confirm_url The link to click on to confirm the account action.
* @type string $sitename The site name sending the mail.
* @type string $siteurl The site URL sending the mail.
* }
*/
$subject = apply_filters( 'user_request_action_email_subject', $subject, $email_data['sitename'], $email_data );
/* translators: Do not translate DESCRIPTION, CONFIRM_URL, SITENAME, SITEURL: those are placeholders. */
$content = __(
'Howdy,
A request has been made to perform the following action on your account:
###DESCRIPTION###
To confirm this, please click on the following link:
###CONFIRM_URL###
You can safely ignore and delete this email if you do not want to
take this action.
Regards,
All at ###SITENAME###
###SITEURL###'
);
/**
* Filters the text of the email sent when an account action is attempted.
*
* The following strings have a special meaning and will get replaced dynamically:
*
* - `###DESCRIPTION###` Description of the action being performed so the user knows what the email is for.
* - `###CONFIRM_URL###` The link to click on to confirm the account action.
* - `###SITENAME###` The name of the site.
* - `###SITEURL###` The URL to the site.
*
* @since 4.9.6
*
* @param string $content Text in the email.
* @param array $email_data {
* Data relating to the account action email.
*
* @type WP_User_Request $request User request object.
* @type string $email The email address this is being sent to.
* @type string $description Description of the action being performed so the user knows what the email is for.
* @type string $confirm_url The link to click on to confirm the account action.
* @type string $sitename The site name sending the mail.
* @type string $siteurl The site URL sending the mail.
* }
*/
$content = apply_filters( 'user_request_action_email_content', $content, $email_data );
$content = str_replace( '###DESCRIPTION###', $email_data['description'], $content );
$content = str_replace( '###CONFIRM_URL###', sanitize_url( $email_data['confirm_url'] ), $content );
$content = str_replace( '###EMAIL###', $email_data['email'], $content );
$content = str_replace( '###SITENAME###', $email_data['sitename'], $content );
$content = str_replace( '###SITEURL###', sanitize_url( $email_data['siteurl'] ), $content );
$headers = '';
/**
* Filters the headers of the email sent when an account action is attempted.
*
* @since 5.4.0
*
* @param string|array $headers The email headers.
* @param string $subject The email subject.
* @param string $content The email content.
* @param int $request_id The request ID.
* @param array $email_data {
* Data relating to the account action email.
*
* @type WP_User_Request $request User request object.
* @type string $email The email address this is being sent to.
* @type string $description Description of the action being performed so the user knows what the email is for.
* @type string $confirm_url The link to click on to confirm the account action.
* @type string $sitename The site name sending the mail.
* @type string $siteurl The site URL sending the mail.
* }
*/
$headers = apply_filters( 'user_request_action_email_headers', $headers, $subject, $content, $request_id, $email_data );
$email_sent = wp_mail( $email_data['email'], $subject, $content, $headers );
if ( $switched_locale ) {
restore_previous_locale();
}
if ( ! $email_sent ) {
return new WP_Error( 'privacy_email_error', __( 'Unable to send personal data export confirmation email.' ) );
}
return true;
}
Hooks
- apply_filters( ‘user_request_action_email_content’, string $content, array $email_data )
-
Filters the text of the email sent when an account action is attempted.
- apply_filters( ‘user_request_action_email_headers’, string|array $headers, string $subject, string $content, int $request_id, array $email_data )
-
Filters the headers of the email sent when an account action is attempted.
- apply_filters( ‘user_request_action_email_subject’, string $subject, string $sitename, array $email_data )
-
Filters the subject of the email sent when an account action is attempted.
Changelog
| Version | Description |
|---|---|
| 4.9.6 | Introduced. |