fix: 优化评论发送速度和邮件通知系统

- AI 垃圾评论检测改为异步执行(延迟 2 秒),避免阻塞评论发送
- 站长评论通知改用邮件模板系统发送
- 站长评论通知邮件包含 AI 审核信息(识别结果、理由、识别码)
- 禁用 WordPress 默认的评论通知邮件
- 邮件模板系统支持 Mustache 条件语法({{#variable}}...{{/variable}})
- 评论发送、AI 审核、邮件通知全部异步并行处理
This commit is contained in:
2026-01-22 13:14:23 +08:00
parent 5150b67339
commit c0fa3d2352
3 changed files with 82 additions and 3 deletions

View File

@@ -23,6 +23,14 @@ function argon_get_email_types() {
<div style="background: #f6f9fc; border-left: 4px solid {{theme_color}}; padding: 16px; border-radius: 4px; margin: 0 0 20px 0;">
<p style="margin: 0; color: #525f7f; line-height: 1.6;">{{comment_content}}</p>
</div>
{{#ai_spam_check}}
<div style="background: {{#ai_is_spam}}#fff5f5{{/ai_is_spam}}{{^ai_is_spam}}#f0fdf4{{/ai_is_spam}}; border-left: 4px solid {{#ai_is_spam}}#f56565{{/ai_is_spam}}{{^ai_is_spam}}#48bb78{{/ai_is_spam}}; padding: 12px 16px; border-radius: 4px; margin: 0 0 20px 0;">
<p style="margin: 0 0 8px 0; color: #2d3748; font-size: 14px; font-weight: 600;">🤖 AI 内容审核</p>
<p style="margin: 0 0 4px 0; color: #4a5568; font-size: 13px;">识别结果:{{#ai_is_spam}}<span style="color: #e53e3e; font-weight: 600;">疑似垃圾评论</span>{{/ai_is_spam}}{{^ai_is_spam}}<span style="color: #38a169; font-weight: 600;">正常评论</span>{{/ai_is_spam}}</p>
{{#ai_spam_reason}}<p style="margin: 0 0 4px 0; color: #4a5568; font-size: 13px;">识别理由:{{ai_spam_reason}}</p>{{/ai_spam_reason}}
{{#ai_detection_code}}<p style="margin: 0; color: #718096; font-size: 12px;">识别码:{{ai_detection_code}}</p>{{/ai_detection_code}}
</div>
{{/ai_spam_check}}
<p style="margin: 0;">
<a href="{{comment_url}}" style="display: inline-block; background: {{theme_color}}; color: #ffffff; padding: 10px 20px; border-radius: 4px; text-decoration: none; font-size: 14px;">查看评论</a>
</p>',
@@ -35,6 +43,10 @@ function argon_get_email_types() {
'comment_content' => __('评论内容', 'argon'),
'comment_url' => __('评论链接', 'argon'),
'comment_date' => __('评论时间', 'argon'),
'ai_spam_check' => __('是否进行了 AI 审核', 'argon'),
'ai_is_spam' => __('AI 判断是否为垃圾评论', 'argon'),
'ai_spam_reason' => __('AI 识别理由', 'argon'),
'ai_detection_code' => __('AI 识别码', 'argon'),
'theme_color' => __('主题色', 'argon'),
),
),
@@ -314,7 +326,29 @@ function argon_replace_placeholders($template, $vars, $escape = false) {
$vars = array_merge($global_vars, $vars);
// 处理条件语句 {{#if variable}}...{{/if}}
// 处理 Mustache 风格的条件语句 {{#variable}}...{{/variable}}
$template = preg_replace_callback('/\{\{#(\w+)\}\}(.*?)\{\{\/\1\}\}/s', function($matches) use ($vars) {
$var_name = $matches[1];
$content = $matches[2];
// 如果变量存在且为真值,返回内容
if (isset($vars[$var_name]) && $vars[$var_name]) {
return $content;
}
return '';
}, $template);
// 处理 Mustache 风格的反向条件语句 {{^variable}}...{{/variable}}
$template = preg_replace_callback('/\{\{\^(\w+)\}\}(.*?)\{\{\/\1\}\}/s', function($matches) use ($vars) {
$var_name = $matches[1];
$content = $matches[2];
// 如果变量不存在或为假值,返回内容
if (!isset($vars[$var_name]) || !$vars[$var_name]) {
return $content;
}
return '';
}, $template);
// 处理旧版条件语句 {{#if variable}}...{{/if}}(向后兼容)
$template = preg_replace_callback('/\{\{#if\s+(\w+)\}\}(.*?)\{\{\/if\}\}/s', function($matches) use ($vars) {
$var_name = $matches[1];
$content = $matches[2];

View File

@@ -45,6 +45,21 @@ function argon_send_comment_notify_email($comment) {
'comment_date' => get_comment_date(get_option('date_format') . ' ' . get_option('time_format'), $comment),
);
// 检查是否有 AI 审核信息
$spam_detection_result = get_comment_meta($comment->comment_ID, '_argon_spam_detection_result', true);
if (!empty($spam_detection_result) && is_array($spam_detection_result)) {
$vars['ai_spam_check'] = true;
$vars['ai_is_spam'] = $spam_detection_result['is_spam'];
$vars['ai_spam_reason'] = isset($spam_detection_result['reason']) ? $spam_detection_result['reason'] : '';
$vars['ai_spam_action'] = isset($spam_detection_result['action']) ? $spam_detection_result['action'] : '';
// 生成内容识别码(评论 ID + 时间戳的 MD5
$detection_time = get_comment_meta($comment->comment_ID, '_argon_spam_detection_time', true);
$vars['ai_detection_code'] = strtoupper(substr(md5($comment->comment_ID . '-' . $detection_time), 0, 8));
} else {
$vars['ai_spam_check'] = false;
}
// 发送邮件
return argon_send_email($admin_email, 'comment_notify', $vars);
}

View File

@@ -2934,6 +2934,36 @@ function post_comment_updatemetas($id){
add_action('comment_post' , 'post_comment_updatemetas');
add_action('comment_unapproved_to_approved', 'comment_mail_notify');
add_rewrite_rule('^unsubscribe-comment-mailnotice/?(.*)$', '/wp-content/themes/argon/unsubscribe-comment-mailnotice.php$1', 'top');
/**
* 发送新评论通知给站长(异步)
*/
function argon_notify_admin_new_comment($comment_id) {
$comment = get_comment($comment_id);
if (!$comment) {
return;
}
// 使用邮件模板系统发送通知
argon_send_comment_notify_email($comment);
}
add_action('argon_async_admin_comment_notify', 'argon_notify_admin_new_comment');
/**
* 评论发布后异步通知站长
*/
function argon_schedule_admin_comment_notify($comment_id, $comment_approved) {
// 延迟 1 秒发送,让评论元数据先保存
wp_schedule_single_event(time() + 1, 'argon_async_admin_comment_notify', [$comment_id]);
}
add_action('comment_post', 'argon_schedule_admin_comment_notify', 20, 2);
/**
* 禁用 WordPress 默认的评论通知邮件
*/
add_filter('notify_post_author', '__return_false');
add_filter('notify_moderator', '__return_false');
//编辑评论
function user_edit_comment(){
header('Content-Type:application/json; charset=utf-8');
@@ -7493,8 +7523,8 @@ function argon_auto_detect_spam_on_comment($comment_id, $comment_approved) {
}
if ($should_check) {
// 异步检测
wp_schedule_single_event(time(), 'argon_async_spam_detection', [$comment_id]);
// 异步检测(延迟 2 秒执行,避免阻塞评论发送)
wp_schedule_single_event(time() + 2, 'argon_async_spam_detection', [$comment_id]);
}
}
add_action('comment_post', 'argon_auto_detect_spam_on_comment', 10, 2);