wp_privacy_send_personal_data_export_email()
云策文档标注
概述
wp_privacy_send_personal_data_export_email() 函数用于向用户发送包含个人数据导出文件链接的电子邮件。它处理请求验证、本地化、邮件内容构建和发送,并支持多个过滤器进行自定义。
关键要点
- 参数:接受一个必需的整数 $request_id,表示个人数据导出请求的 ID。
- 返回值:成功时返回 true,失败时返回 WP_Error 对象。
- 功能:验证请求、设置本地化、构建邮件内容(包括过期日期和文件链接)、应用过滤器,并通过 wp_mail() 发送邮件。
- 过滤器:包括 wp_privacy_personal_data_email_to(收件人)、wp_privacy_personal_data_email_subject(主题)、wp_privacy_personal_data_email_content(内容)和 wp_privacy_personal_data_email_headers(邮件头),允许开发者自定义邮件细节。
- 错误处理:无效请求或邮件发送失败时会返回 WP_Error。
代码示例
// 示例:发送个人数据导出邮件
$request_id = 123; // 假设的请求 ID
$result = wp_privacy_send_personal_data_export_email( $request_id );
if ( is_wp_error( $result ) ) {
// 处理错误
echo $result->get_error_message();
} else {
// 邮件发送成功
echo '邮件已发送。';
}注意事项
- 确保 $request_id 对应一个有效的 'export_personal_data' 请求,否则函数会返回 WP_Error。
- 邮件内容中的占位符(如 ###EXPIRATION###、###LINK###)会被自动替换,开发者可通过过滤器修改文本。
- 使用 wp_privacy_personal_data_email_to 过滤器更改收件人时需谨慎,以避免数据泄露。
- 函数内部处理本地化切换,发送后恢复原始区域设置,无需手动干预。
原文内容
Send an email to the user with a link to the personal data export file
Parameters
$request_idintrequired-
The request ID for this personal data export.
Source
function wp_privacy_send_personal_data_export_email( $request_id ) {
// Get the request.
$request = wp_get_user_request( $request_id );
if ( ! $request || 'export_personal_data' !== $request->action_name ) {
return new WP_Error( 'invalid_request', __( 'Invalid request ID when sending personal data export email.' ) );
}
// 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() );
}
/** This filter is documented in wp-includes/functions.php */
$expiration = apply_filters( 'wp_privacy_export_expiration', 3 * DAY_IN_SECONDS );
$expiration_date = date_i18n( get_option( 'date_format' ), time() + $expiration );
$exports_url = wp_privacy_exports_url();
$export_file_name = get_post_meta( $request_id, '_export_file_name', true );
$export_file_url = $exports_url . $export_file_name;
$site_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
$site_url = home_url();
/**
* Filters the recipient of the personal data export email notification.
* Should be used with great caution to avoid sending the data export link to the wrong email.
*
* @since 5.3.0
*
* @param string $request_email The email address of the notification recipient.
* @param WP_User_Request $request The request that is initiating the notification.
*/
$request_email = apply_filters( 'wp_privacy_personal_data_email_to', $request->email, $request );
$email_data = array(
'request' => $request,
'expiration' => $expiration,
'expiration_date' => $expiration_date,
'message_recipient' => $request_email,
'export_file_url' => $export_file_url,
'sitename' => $site_name,
'siteurl' => $site_url,
);
/* translators: Personal data export notification email subject. %s: Site title. */
$subject = sprintf( __( '[%s] Personal Data Export' ), $site_name );
/**
* Filters the subject of the email sent when an export request is completed.
*
* @since 5.3.0
*
* @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 int $expiration The time in seconds until the export file expires.
* @type string $expiration_date The localized date and time when the export file expires.
* @type string $message_recipient The address that the email will be sent to. Defaults
* to the value of `$request->email`, but can be changed
* by the `wp_privacy_personal_data_email_to` filter.
* @type string $export_file_url The export file URL.
* @type string $sitename The site name sending the mail.
* @type string $siteurl The site URL sending the mail.
* }
*/
$subject = apply_filters( 'wp_privacy_personal_data_email_subject', $subject, $site_name, $email_data );
/* translators: Do not translate EXPIRATION, LINK, SITENAME, SITEURL: those are placeholders. */
$email_text = __(
'Howdy,
Your request for an export of personal data has been completed. You may
download your personal data by clicking on the link below. For privacy
and security, we will automatically delete the file on ###EXPIRATION###,
so please download it before then.
###LINK###
Regards,
All at ###SITENAME###
###SITEURL###'
);
/**
* Filters the text of the email sent with a personal data export file.
*
* The following strings have a special meaning and will get replaced dynamically:
*
* - `###EXPIRATION###` The date when the URL will be automatically deleted.
* - `###LINK###` URL of the personal data export file for the user.
* - `###SITENAME###` The name of the site.
* - `###SITEURL###` The URL to the site.
*
* @since 4.9.6
* @since 5.3.0 Introduced the `$email_data` array.
*
* @param string $email_text Text in the email.
* @param int $request_id The request ID for this personal data export.
* @param array $email_data {
* Data relating to the account action email.
*
* @type WP_User_Request $request User request object.
* @type int $expiration The time in seconds until the export file expires.
* @type string $expiration_date The localized date and time when the export file expires.
* @type string $message_recipient The address that the email will be sent to. Defaults
* to the value of `$request->email`, but can be changed
* by the `wp_privacy_personal_data_email_to` filter.
* @type string $export_file_url The export file URL.
* @type string $sitename The site name sending the mail.
* @type string $siteurl The site URL sending the mail.
*/
$content = apply_filters( 'wp_privacy_personal_data_email_content', $email_text, $request_id, $email_data );
$content = str_replace( '###EXPIRATION###', $expiration_date, $content );
$content = str_replace( '###LINK###', sanitize_url( $export_file_url ), $content );
$content = str_replace( '###EMAIL###', $request_email, $content );
$content = str_replace( '###SITENAME###', $site_name, $content );
$content = str_replace( '###SITEURL###', sanitize_url( $site_url ), $content );
$headers = '';
/**
* Filters the headers of the email sent with a personal data export file.
*
* @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 int $expiration The time in seconds until the export file expires.
* @type string $expiration_date The localized date and time when the export file expires.
* @type string $message_recipient The address that the email will be sent to. Defaults
* to the value of `$request->email`, but can be changed
* by the `wp_privacy_personal_data_email_to` filter.
* @type string $export_file_url The export file URL.
* @type string $sitename The site name sending the mail.
* @type string $siteurl The site URL sending the mail.
* }
*/
$headers = apply_filters( 'wp_privacy_personal_data_email_headers', $headers, $subject, $content, $request_id, $email_data );
$mail_success = wp_mail( $request_email, $subject, $content, $headers );
if ( $switched_locale ) {
restore_previous_locale();
}
if ( ! $mail_success ) {
return new WP_Error( 'privacy_email_error', __( 'Unable to send personal data export email.' ) );
}
return true;
}
Hooks
- apply_filters( ‘wp_privacy_export_expiration’, int $expiration )
-
Filters the lifetime, in seconds, of a personal data export file.
- apply_filters( ‘wp_privacy_personal_data_email_content’, string $email_text, int $request_id, array $email_data )
-
Filters the text of the email sent with a personal data export file.
- apply_filters( ‘wp_privacy_personal_data_email_headers’, string|array $headers, string $subject, string $content, int $request_id, array $email_data )
-
Filters the headers of the email sent with a personal data export file.
- apply_filters( ‘wp_privacy_personal_data_email_subject’, string $subject, string $sitename, array $email_data )
-
Filters the subject of the email sent when an export request is completed.
- apply_filters( ‘wp_privacy_personal_data_email_to’, string $request_email, WP_User_Request $request )
-
Filters the recipient of the personal data export email notification.
Changelog
| Version | Description |
|---|---|
| 4.9.6 | Introduced. |