Files
wordpress-theme-aksrt/inc/classes/settings/class-aksrt-settings-mail.php
T

119 lines
3.9 KiB
PHP

<?php
/**
* Settings mail/event helper.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
class AKSRT_Settings_Mail {
/** 所有邮件事件类型常量 */
public const EMAIL_EVENTS = [
'reg_verify_link',
'reg_verify_code',
'reg_success',
'password_reset',
'change_code',
'change_notice',
];
/**
* 获取邮件事件配置
*
* @param string $event 事件类型,留空返回全部
* @return array
*/
public static function get_email_event_config(string $event = ''): array {
$s = AKSRT_Settings::get_all();
$global = [
'footer_text' => sanitize_text_field((string) ($s['email_footer_text'] ?? '')),
'site_branding' => !isset($s['email_site_branding']) || !empty($s['email_site_branding']),
'template_mode' => in_array($s['email_template_mode'] ?? 'plain', ['plain', 'html'], true)
? ($s['email_template_mode'] ?? 'plain') : 'plain',
];
if ($event === '') {
$events = [];
foreach (self::EMAIL_EVENTS as $ev) {
$events[$ev] = self::build_email_event_entry($s, $ev);
}
return ['global' => $global, 'events' => $events];
}
if (!in_array($event, self::EMAIL_EVENTS, true)) {
return ['enabled' => true, 'subject' => '', 'content' => '', 'html' => false, 'global' => $global];
}
return array_merge(self::build_email_event_entry($s, $event), ['global' => $global]);
}
/**
* 邮件事件是否启用
*/
public static function is_email_event_enabled(string $event): bool {
if (!in_array($event, self::EMAIL_EVENTS, true)) {
return true;
}
$s = AKSRT_Settings::get_all();
return !isset($s["email_{$event}_enabled"]) || !empty($s["email_{$event}_enabled"]);
}
/**
* 通过邮件事件体系发送邮件
*/
public static function send_event_email(
string $event,
string $to,
string $default_subject,
string $default_content,
array $placeholders = []
): bool {
if (!self::is_email_event_enabled($event)) {
return false;
}
$cfg = self::get_email_event_config($event);
$subject = ($cfg['subject'] !== '') ? $cfg['subject'] : $default_subject;
$subject = strtr($subject, $placeholders);
$content = ($cfg['content'] !== '') ? $cfg['content'] : $default_content;
$content = strtr($content, $placeholders);
$branding = AKSRT_Settings::get_mail_branding();
$footer = $cfg['global']['footer_text'];
if ($footer === '' && $branding['description'] !== '') {
$footer = $branding['description'];
}
if ($footer === '') {
$footer = get_bloginfo('name');
}
if ($footer !== '') {
$content .= "\n\n— " . $footer;
if ($branding['description'] !== '' && $footer !== $branding['description']) {
$content .= "\n" . $branding['description'];
}
}
$is_html = $cfg['html'] || ($cfg['global']['template_mode'] === 'html');
$headers = [$is_html ? 'Content-Type: text/html; charset=UTF-8' : 'Content-Type: text/plain; charset=UTF-8'];
return wp_mail($to, $subject, $content, $headers);
}
/**
* 构建单个邮件事件条目
*/
private static function build_email_event_entry(array $s, string $event): array {
return [
'enabled' => !isset($s["email_{$event}_enabled"]) || !empty($s["email_{$event}_enabled"]),
'subject' => sanitize_text_field((string) ($s["email_{$event}_subject"] ?? '')),
'content' => sanitize_textarea_field((string) ($s["email_{$event}_content"] ?? '')),
'html' => !empty($s["email_{$event}_html"]),
];
}
}