From c0fa3d2352a4a6f1499bbb88ee154433ac2e5577 Mon Sep 17 00:00:00 2001
From: nanhaoluo <3075912108@qq.com>
Date: Thu, 22 Jan 2026 13:14:23 +0800
Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E8=AF=84=E8=AE=BA?=
=?UTF-8?q?=E5=8F=91=E9=80=81=E9=80=9F=E5=BA=A6=E5=92=8C=E9=82=AE=E4=BB=B6?=
=?UTF-8?q?=E9=80=9A=E7=9F=A5=E7=B3=BB=E7=BB=9F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- AI 垃圾评论检测改为异步执行(延迟 2 秒),避免阻塞评论发送
- 站长评论通知改用邮件模板系统发送
- 站长评论通知邮件包含 AI 审核信息(识别结果、理由、识别码)
- 禁用 WordPress 默认的评论通知邮件
- 邮件模板系统支持 Mustache 条件语法({{#variable}}...{{/variable}})
- 评论发送、AI 审核、邮件通知全部异步并行处理
---
email-templates/base.php | 36 +++++++++++++++++++++++++++++-
email-templates/comment-notify.php | 15 +++++++++++++
functions.php | 34 ++++++++++++++++++++++++++--
3 files changed, 82 insertions(+), 3 deletions(-)
diff --git a/email-templates/base.php b/email-templates/base.php
index 5d5227a..4321acb 100644
--- a/email-templates/base.php
+++ b/email-templates/base.php
@@ -23,6 +23,14 @@ function argon_get_email_types() {
+{{#ai_spam_check}}
+
+
🤖 AI 内容审核
+
识别结果:{{#ai_is_spam}}疑似垃圾评论{{/ai_is_spam}}{{^ai_is_spam}}正常评论{{/ai_is_spam}}
+ {{#ai_spam_reason}}
识别理由:{{ai_spam_reason}}
{{/ai_spam_reason}}
+ {{#ai_detection_code}}
识别码:{{ai_detection_code}}
{{/ai_detection_code}}
+
+{{/ai_spam_check}}
查看评论
',
@@ -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];
diff --git a/email-templates/comment-notify.php b/email-templates/comment-notify.php
index 294323b..eb602e2 100644
--- a/email-templates/comment-notify.php
+++ b/email-templates/comment-notify.php
@@ -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);
}
diff --git a/functions.php b/functions.php
index a8278cc..796e968 100644
--- a/functions.php
+++ b/functions.php
@@ -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);