refactor: 移除智能预审查,改为先保存后检测

- 移除所有智能预审查相关的设置和代码
- 评论先保存到数据库并标记为待审核状态
- AI 检测在评论保存后异步进行
- 检测完成后根据结果更新评论状态
- 避免误杀正常评论,提升用户体验
- 所有需要检测的评论都会显示为审核中
This commit is contained in:
2026-01-23 18:32:54 +08:00
parent 244b1725af
commit a38f168909
2 changed files with 48 additions and 184 deletions

View File

@@ -2836,18 +2836,18 @@ function post_comment_preprocessing($comment){
$comment['comment_content'] = comment_markdown_parse($comment['comment_content']);
}
// AI 智能审查:在评论保存前进行检测和处理
// AI 垃圾评论检测:如果启用了检测,将评论标记为待审核
if (get_option('argon_comment_spam_detection_enable', 'false') === 'true') {
$mode = get_option('argon_comment_spam_detection_mode', 'manual');
// 只有在启用了实时检测模式时才进行预审查
// 只有在启用了实时检测模式时才需要审核
if ($mode !== 'manual') {
// 检查是否为已登录用户(可跳过)
$skip_logged_in = get_option('argon_comment_spam_detection_exclude_logged_in', 'true') === 'true';
$is_logged_in = is_user_logged_in();
if (!$is_logged_in || !$skip_logged_in) {
// 创建临时评论对象用于检
// 创建临时评论对象用于检
$temp_comment = (object) [
'comment_author' => $comment['comment_author'],
'comment_author_email' => $comment['comment_author_email'],
@@ -2861,106 +2861,37 @@ function post_comment_preprocessing($comment){
// 检查白名单
if (!argon_is_comment_in_whitelist($temp_comment)) {
$should_check = false;
$check_reason = '';
// 优先级1检查是否触发关键字
// 检查是否触发关键字
$keyword_check = argon_check_spam_keywords($temp_comment);
if ($keyword_check && $keyword_check['triggered']) {
$should_check = true;
$check_reason = 'keyword';
}
// 优先级2全量检测模式
// 全量检测模式
elseif ($mode === 'all') {
$should_check = true;
$check_reason = 'all';
}
// 优先级3抽查模式根据概率
// 关键字必查模式(只检测触发关键字的
elseif ($mode === 'keyword') {
// 已在上面检查过关键字
}
// 抽查模式(根据概率)
elseif ($mode === 'sample') {
$check_probability = argon_get_user_spam_check_probability($temp_comment);
if (rand(1, 100) <= $check_probability) {
$should_check = true;
$check_reason = 'sample';
}
}
// 如果需要检测,立即进行同步检测
// 如果需要检测,将评论标记为待审核
if ($should_check) {
$result = argon_detect_spam_comment_sync($temp_comment);
// 强制设置为待审核状态
$comment['comment_approved'] = 0;
if ($result && isset($result['is_spam'])) {
$content_spam = $result['is_spam'];
$username_invalid = isset($result['username_invalid']) ? $result['username_invalid'] : false;
$confidence = isset($result['confidence']) ? floatval($result['confidence']) : 0.8;
$suggestion = isset($result['suggestion']) ? $result['suggestion'] : 'auto';
$confidence_threshold = floatval(get_option('argon_comment_spam_detection_confidence_threshold', 0.85));
// 保存标记,表示需要 AI 检测
$_POST['_argon_needs_spam_check'] = 'true';
// 情况1内容违规且置信度高 - 直接拒绝评论
if ($content_spam && $confidence >= $confidence_threshold && $suggestion === 'auto') {
wp_die(
'<div style="max-width:600px;margin:50px auto;padding:30px;background:#fff;border-radius:8px;box-shadow:0 2px 12px rgba(0,0,0,0.1);">
<h2 style="color:#f5365c;margin-bottom:20px;">⚠️ ' . __('评论审核未通过', 'argon') . '</h2>
<p style="font-size:16px;line-height:1.8;color:#525f7f;">' . __('您的评论因以下原因未能通过审核:', 'argon') . '</p>
<div style="background:#f7fafc;padding:15px;border-left:4px solid #f5365c;margin:20px 0;">
<strong>' . esc_html($result['reason']) . '</strong>
</div>
<p style="font-size:14px;color:#8898aa;">' . __('如果您认为这是误判,请联系网站管理员。', 'argon') . '</p>
<div style="margin-top:30px;text-align:center;">
<a href="javascript:history.back()" style="display:inline-block;padding:12px 30px;background:#5e72e4;color:#fff;text-decoration:none;border-radius:4px;">' . __('返回修改', 'argon') . '</a>
</div>
</div>',
__('评论审核未通过', 'argon'),
['response' => 403, 'back_link' => true]
);
}
// 情况2用户名违规且没有邮箱 - 直接拒绝评论
if ($username_invalid && empty($comment['comment_author_email'])) {
wp_die(
'<div style="max-width:600px;margin:50px auto;padding:30px;background:#fff;border-radius:8px;box-shadow:0 2px 12px rgba(0,0,0,0.1);">
<h2 style="color:#f5365c;margin-bottom:20px;">⚠️ ' . __('用户名审核未通过', 'argon') . '</h2>
<p style="font-size:16px;line-height:1.8;color:#525f7f;">' . __('您的用户名因以下原因未能通过审核:', 'argon') . '</p>
<div style="background:#f7fafc;padding:15px;border-left:4px solid #f5365c;margin:20px 0;">
<strong>' . esc_html($result['username_reason']) . '</strong>
</div>
<p style="font-size:14px;color:#8898aa;">' . __('请修改用户名后重试,或填写邮箱以便我们为您自动生成合规的用户名。', 'argon') . '</p>
<div style="margin-top:30px;text-align:center;">
<a href="javascript:history.back()" style="display:inline-block;padding:12px 30px;background:#5e72e4;color:#fff;text-decoration:none;border-radius:4px;">' . __('返回修改', 'argon') . '</a>
</div>
</div>',
__('用户名审核未通过', 'argon'),
['response' => 403, 'back_link' => true]
);
}
// 情况3用户名违规但有邮箱 - 自动修改用户名
if ($username_invalid && !empty($comment['comment_author_email'])) {
$original_username = $comment['comment_author'];
$new_username = argon_generate_unique_username(
$original_username,
$comment['comment_author_email'],
$comment['comment_author_IP'],
isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''
);
// 修改评论数组中的用户名
$comment['comment_author'] = $new_username;
// 保存原始用户名和检测结果到 $_POST稍后在 post_comment_updatemetas 中保存
$_POST['_argon_original_username'] = $original_username;
$_POST['_argon_username_changed'] = 'true';
$_POST['_argon_username_change_reason'] = $result['username_reason'];
$_POST['_argon_spam_detection_result'] = json_encode($result);
}
// 情况4内容违规但置信度不足 - 标记为待审核
if ($content_spam && ($confidence < $confidence_threshold || $suggestion !== 'auto')) {
// 保存检测结果,稍后标记为待审核
$_POST['_argon_spam_low_confidence'] = 'true';
$_POST['_argon_spam_detection_result'] = json_encode($result);
}
// 保存检测原因
$_POST['_argon_spam_check_reason'] = $check_reason;
// 保存触发的关键字
if ($keyword_check && $keyword_check['triggered']) {
$_POST['_argon_spam_triggered_keywords'] = json_encode($keyword_check['keywords']);
}
@@ -2969,7 +2900,6 @@ function post_comment_preprocessing($comment){
}
}
}
}
return $comment;
}
@@ -3109,6 +3039,19 @@ function post_comment_updatemetas($id){
update_comment_meta($id, "qq_number", $_POST['qq']);
}
}
// 保存 AI 垃圾评论检测相关的元数据
if (isset($_POST['_argon_needs_spam_check']) && $_POST['_argon_needs_spam_check'] === 'true') {
update_comment_meta($id, '_argon_needs_spam_check', 'true');
}
if (isset($_POST['_argon_spam_triggered_keywords'])) {
update_comment_meta($id, '_argon_spam_triggered_keywords', $_POST['_argon_spam_triggered_keywords']);
// 解析并保存检测原因
$keywords = json_decode($_POST['_argon_spam_triggered_keywords'], true);
if (!empty($keywords)) {
update_comment_meta($id, '_argon_spam_check_reason', 'keyword');
}
}
}
add_action('comment_post' , 'post_comment_updatemetas');
add_action('comment_unapproved_to_approved', 'comment_mail_notify');
@@ -8189,9 +8132,10 @@ function argon_auto_detect_spam_on_comment($comment_id, $comment_approved) {
return;
}
$mode = get_option('argon_comment_spam_detection_mode', 'manual');
if ($mode === 'manual') {
return;
// 检查是否标记为需要检测
$needs_check = get_comment_meta($comment_id, '_argon_needs_spam_check', true);
if ($needs_check !== 'true') {
return; // 没有标记,跳过
}
$comment = get_comment($comment_id);
@@ -8205,67 +8149,20 @@ function argon_auto_detect_spam_on_comment($comment_id, $comment_approved) {
return; // 已检测过,跳过
}
// 跳过已登录用户
if (get_option('argon_comment_spam_detection_exclude_logged_in', 'true') === 'true' && $comment->user_id > 0) {
return;
// 获取检测原因
$check_reason = get_comment_meta($comment_id, '_argon_spam_check_reason', true);
if (empty($check_reason)) {
$check_reason = 'unknown';
}
// 检查白名单
if (argon_is_comment_in_whitelist($comment)) {
return;
}
// 判断是否需要检测
$should_check = false;
$check_reason = '';
// 检查是否触发关键字
$keyword_check = argon_check_spam_keywords($comment);
$keyword_triggered = $keyword_check && $keyword_check['triggered'];
// 根据检测模式决定是否检测
if ($mode === 'keyword') {
// 关键字必查模式:只检测触发关键字的评论
if ($keyword_triggered) {
$should_check = true;
$check_reason = 'keyword';
update_comment_meta($comment_id, '_argon_spam_triggered_keywords', $keyword_check['keywords']);
}
} elseif ($mode === 'all') {
// 全量检测模式:检测所有评论
$should_check = true;
$check_reason = $keyword_triggered ? 'keyword' : 'all';
if ($keyword_triggered) {
update_comment_meta($comment_id, '_argon_spam_triggered_keywords', $keyword_check['keywords']);
}
} elseif ($mode === 'sample') {
// 智能抽查模式:关键字触发或随机抽查
if ($keyword_triggered) {
$should_check = true;
$check_reason = 'keyword';
update_comment_meta($comment_id, '_argon_spam_triggered_keywords', $keyword_check['keywords']);
} else {
// 根据用户历史通过率动态调整概率
$check_probability = argon_get_user_spam_check_probability($comment);
if (rand(1, 100) <= $check_probability) {
$should_check = true;
$check_reason = 'sample';
}
}
}
if ($should_check) {
// 保存检测原因
update_comment_meta($comment_id, '_argon_spam_check_reason', $check_reason);
// 如果是关键字触发,立即同步检测(不延迟)
// 立即进行异步检测
// 如果是关键字触发,立即同步检测
if ($check_reason === 'keyword') {
argon_async_spam_detection_handler($comment_id);
} else {
// 其他情况异步检测(延迟 1 秒执行,让评论元数据先保存)
wp_schedule_single_event(time() + 1, 'argon_async_spam_detection', [$comment_id]);
}
}
}
add_action('comment_post', 'argon_auto_detect_spam_on_comment', 10, 2);

View File

@@ -3908,7 +3908,7 @@ window.pjaxLoaded = function(){
</p>
</div>
<div style="margin-bottom: 15px;">
<div>
<label style="display: block; margin-bottom: 10px; font-weight: 600;">
<?php _e('智能抽查概率', 'argon');?>
</label>
@@ -3920,18 +3920,6 @@ window.pjaxLoaded = function(){
<?php _e('• 关键字必查模式下此设置无效', 'argon');?>
</p>
</div>
<div>
<?php $argon_comment_spam_detection_pre_check = get_option('argon_comment_spam_detection_pre_check', 'true'); ?>
<label style="display: block; margin-bottom: 8px;">
<input type="checkbox" name="argon_comment_spam_detection_pre_check" value="true" <?php if ($argon_comment_spam_detection_pre_check=='true'){echo 'checked';}?>/>
<strong><?php _e('启用智能预审查(推荐)', 'argon');?></strong>
</label>
<p class="description" style="margin-left: 24px;">
<?php _e('在评论保存前进行 AI 审查,高置信度垃圾评论直接拒绝,用户名违规自动修改。', 'argon');?><br/>
<?php _e('关闭后,评论会先保存,然后异步检测(可能已发送邮件通知)。', 'argon');?>
</p>
</div>
</td>
</tr>
@@ -4154,26 +4142,6 @@ window.pjaxLoaded = function(){
</td>
</tr>
<tr>
<th><label><?php _e('智能预审查', 'argon');?></label></th>
<td>
<?php $argon_comment_spam_detection_pre_check = get_option('argon_comment_spam_detection_pre_check', 'true'); ?>
<label style="display: block; margin-bottom: 8px;">
<input type="checkbox" name="argon_comment_spam_detection_pre_check" value="true" <?php if ($argon_comment_spam_detection_pre_check=='true'){echo 'checked';}?>/>
<?php _e('在评论保存前进行 AI 审查', 'argon');?>
</label>
<p class="description">
<strong><?php _e('推荐开启', 'argon');?></strong><br/>
<?php _e('开启后,评论会在保存到数据库前先进行 AI 审查:', 'argon');?><br/>
<?php _e('• 高置信度垃圾评论:直接拒绝,不保存到数据库', 'argon');?><br/>
<?php _e('• 用户名违规且无邮箱:直接拒绝', 'argon');?><br/>
<?php _e('• 用户名违规但有邮箱:自动修改用户名后保存', 'argon');?><br/>
<?php _e('• 低置信度垃圾评论:标记为待审核', 'argon');?><br/>
<?php _e('关闭后,评论会先保存,然后异步检测(可能已发送邮件通知)', 'argon');?>
</p>
</td>
</tr>
<tr>
<th><label><?php _e('手动批量检测', 'argon');?></label></th>
<td>
@@ -6844,7 +6812,6 @@ function argon_update_themeoptions(){
argon_update_option_checkbox('argon_comment_spam_detection_exclude_logged_in');
argon_update_option('argon_comment_spam_detection_keywords');
argon_update_option_checkbox('argon_comment_spam_detection_ai_learn');
argon_update_option_checkbox('argon_comment_spam_detection_pre_check');
}