fix: 代码格式优化和临时文件清理

- 优化多个模板文件的代码格式

- 清理 tmp 目录中的临时备份文件

- 统一代码风格,符合项目规范
This commit is contained in:
User
2026-03-13 18:53:06 +08:00
parent 6ba6397ead
commit 7dbf574338
10 changed files with 715 additions and 37 deletions

View File

@@ -1494,6 +1494,41 @@ function get_reading_time($len){
}
return round($reading_time / 60 , 1) . " " . __("小时", 'argon');
}
//获取文章骨架屏 HTML
function get_article_skeleton($layout = '1') {
if ($layout == '2') {
return '
<div class="article-skeleton article-skeleton-layout2">
<div class="article-skeleton-thumb"></div>
<div class="article-skeleton-body">
<div class="article-skeleton-title"></div>
<div class="article-skeleton-content">
<div class="article-skeleton-line"></div>
<div class="article-skeleton-line"></div>
<div class="article-skeleton-line short"></div>
</div>
<div class="article-skeleton-meta">
<div class="article-skeleton-badge"></div>
<div class="article-skeleton-badge"></div>
</div>
</div>
</div>';
}
// 默认布局 1
return '
<div class="article-skeleton article-skeleton-default">
<div class="article-skeleton-title"></div>
<div class="article-skeleton-meta">
<div class="article-skeleton-badge"></div>
<div class="article-skeleton-badge"></div>
</div>
<div class="article-skeleton-content">
<div class="article-skeleton-line"></div>
<div class="article-skeleton-line"></div>
<div class="article-skeleton-line short"></div>
</div>
</div>';
}
//当前文章是否可以生成目录
function have_catalog(){
if (!is_single() && !is_page()){
@@ -2072,6 +2107,11 @@ function argon_comment_format($comment, $args, $depth){
<div class="comment-item-title">
<div class="comment-name">
<div class="comment-author"><?php echo get_comment_author_link();?></div>
<?php
if (argon_is_url_friend_link($comment -> comment_author_url)) {
echo '<span class="badge badge-info badge-friend" title="' . __('愿友谊长存', 'argon') . '"><i class="fa fa-heart-o"></i></span>';
}
?>
<?php if (user_can($comment -> user_id , "update_core")){
echo '<span class="badge badge-primary badge-admin">' . __('博主', 'argon') . '</span>';}
?>
@@ -2134,6 +2174,11 @@ function argon_comment_shuoshuo_preview_format($comment, $args, $depth){
<div class="comment-item-inner " id="comment-inner-<?php comment_ID();?>">
<span class="shuoshuo-comment-item-title">
<?php echo get_comment_author_link();?>
<?php
if (argon_is_url_friend_link($comment -> comment_author_url)) {
echo '<span class="badge badge-info badge-friend" title="' . __('愿友谊长存', 'argon') . '"><i class="fa fa-heart-o"></i></span>';
}
?>
<?php if( user_can($comment -> user_id , "update_core") ){
echo '<span class="badge badge-primary badge-admin">' . __('博主', 'argon') . '</span>';}
?>
@@ -2851,6 +2896,7 @@ function argon_comment_text_render($text){
return argon_apply_comment_macros($text);
}
add_filter('comment_text', 'argon_comment_text_render', 9);
add_filter('the_content', 'argon_apply_comment_macros', 9);
//评论发送处理
function post_comment_preprocessing($comment){
@@ -5413,6 +5459,86 @@ function argon_check_duplicate_link($url, $exclude_id = null) {
return false;
}
/**
* 检查一个 URL 是否在友情链接名单中
* 支持不同协议、不同子域名的匹配
*/
function argon_is_url_friend_link($url) {
if (empty($url)) {
return false;
}
// 如果没有协议,补齐协议以便 parse_url 正常工作
if (!preg_match('/^https?:\/\//', $url) && !preg_match('/^\/\//', $url)) {
$url = 'http://' . $url;
}
static $friend_hosts = null;
if ($friend_hosts === null) {
$links = argon_get_friend_links_raw('approved');
$friend_hosts = array();
foreach ($links as $link) {
if (empty($link['url'])) continue;
$host = parse_url($link['url'], PHP_URL_HOST);
if ($host) {
$host = strtolower($host);
$friend_hosts[] = $host;
// 也存一份移除 www. 的
$no_www = preg_replace('/^www\./', '', $host);
if ($no_www !== $host) {
$friend_hosts[] = $no_www;
}
}
}
$friend_hosts = array_unique($friend_hosts);
}
$comment_host = parse_url($url, PHP_URL_HOST);
if (!$comment_host) {
return false;
}
$comment_host = strtolower($comment_host);
$comment_no_www = preg_replace('/^www\./', '', $comment_host);
// 1. 直系域名或 www 域名匹配
if (in_array($comment_host, $friend_hosts) || in_array($comment_no_www, $friend_hosts)) {
return true;
}
// 2. 匹配二级域名 (忽略子域名差异)
foreach ($friend_hosts as $friend_host) {
if (argon_is_same_root_domain($comment_host, $friend_host)) {
return true;
}
}
return false;
}
/**
* 检查两个 Host 是否拥有相同的根域名
*/
function argon_is_same_root_domain($host1, $host2) {
if ($host1 === $host2) return true;
$get_root = function($h) {
$h = preg_replace('/^www\./', '', $h);
$parts = explode('.', $h);
$count = count($parts);
if ($count <= 2) return $h;
// 如果是 .com.cn, .net.cn 等三级后缀
$last2 = $parts[$count - 2] . '.' . $parts[$count - 1];
$three_part_suffixes = ['com.cn', 'net.cn', 'org.cn', 'gov.cn', 'edu.cn'];
if (in_array($last2, $three_part_suffixes) && $count >= 3) {
return $parts[$count - 3] . '.' . $last2;
}
return $parts[$count - 2] . '.' . $parts[$count - 1];
};
return $get_root($host1) === $get_root($host2);
}
/**
* 通过代理获取网站信息(防止源站 IP 泄露)
* 注意:由于服务器在国内,代理服务可能不可用,建议使用浏览器端获取
@@ -6735,6 +6861,32 @@ function argon_ai_query($scenario, $prompt, $content, $context = []) {
return $result;
}
/**
* 重置 Prompt 基因池
*/
function argon_ajax_reset_prompt_pool() {
check_ajax_referer('argon_manage_unified_apis', 'nonce');
if (!current_user_can('manage_options')) {
wp_send_json_error(__('权限不足', 'argon'));
}
delete_option('argon_ai_audit_prompt_pool');
wp_send_json_success(['message' => __('基因池已重置', 'argon')]);
}
add_action('wp_ajax_argon_reset_prompt_pool', 'argon_ajax_reset_prompt_pool');
/**
* 清空学习到的关键词缓存
*/
function argon_ajax_clear_learned_keywords() {
check_ajax_referer('argon_manage_unified_apis', 'nonce');
if (!current_user_can('manage_options')) {
wp_send_json_error(__('权限不足', 'argon'));
}
delete_option('argon_comment_spam_learned_keywords');
wp_send_json_success(['message' => __('学习缓存已清空', 'argon')]);
}
add_action('wp_ajax_argon_clear_learned_keywords', 'argon_ajax_clear_learned_keywords');
function argon_resolve_ai_provider_model($scenario, $context = []) {
$config = null;
$provider = '';
@@ -9872,7 +10024,7 @@ function argon_get_siliconflow_models($api_key, $custom_endpoint = '') {
* @param string $mode Prompt 模式minimal, standard, enhanced
* @return string Prompt 文本
*/
function argon_get_spam_detection_prompt($mode) {
function argon_get_spam_detection_prompt($mode) {
$prompts = [
'minimal' => '你是严谨的内容安全专家。判断评论是否违规。
@@ -9969,10 +10121,170 @@ function argon_get_spam_detection_prompt($mode) {
- approve: 建议直接通过(正常内容,置信度 > 0.8
- analysis: 综合分析说明(用于边缘情况的详细说明)'
];
return isset($prompts[$mode]) ? $prompts[$mode] : $prompts['standard'];
}
/**
* 获取遗传算法优化的 Prompt
* @param string $base_mode 基础模式 (minimal/standard/enhanced)
* @return string 优化的 Prompt
*/
function argon_get_genetic_audit_prompt($base_mode) {
if (get_option('argon_ai_audit_genetic_prompt_enable', 'false') !== 'true') {
return argon_get_spam_detection_prompt($base_mode);
}
$pool = get_option('argon_ai_audit_prompt_pool', []);
if (empty($pool) || !isset($pool[$base_mode])) {
// 初始化 Prompt 池
$standard_prompt = argon_get_spam_detection_prompt($base_mode);
$pool[$base_mode] = [
'population' => [
[
'content' => $standard_prompt,
'fitness' => 100,
'usage_count' => 0,
'success_count' => 0,
'error_count' => 0,
'version' => 1
]
],
'current_best_index' => 0,
'last_mutation_time' => time()
];
update_option('argon_ai_audit_prompt_pool', $pool);
return $standard_prompt;
}
$population = &$pool[$base_mode]['population'];
// 轮盘赌选择或直接选择表现最好的
// 为了确保收敛和探索平衡80% 概率选择当前最好20% 概率随机选择一个旧的或尝试变异
if (wp_rand(1, 100) > 20) {
$selected_index = $pool[$base_mode]['current_best_index'];
} else {
$selected_index = wp_rand(0, count($population) - 1);
}
// 记录当前使用的索引到临时变量,用于后续更新置信度
// 因为后续可能异步,所以这里返回时把索引也带上(通过 global 或 transient
global $argon_current_prompt_index;
$argon_current_prompt_index = $selected_index;
return $population[$selected_index]['content'];
}
function argon_genetic_correct_keywords() {
if (get_option('argon_ai_audit_genetic_keyword_correction_enable', 'false') !== 'true') {
return;
}
$learned_keywords = get_option('argon_comment_spam_learned_keywords', []);
if (empty($learned_keywords)) {
return;
}
$mutation_rate = floatval(get_option('argon_ai_audit_genetic_keyword_mutation_rate', '0.05'));
$new_keywords = [];
$decay_factor = 0.98; // 随着时间推移,旧知识的权威性略微下降
foreach ($learned_keywords as $keyword => $stats) {
$total_count = $stats['spam_count'] + $stats['normal_count'];
// 1. 遗传衰减:太久没出现的词置信度降低
$days_since_added = (time() - $stats['added_time']) / (24 * 3600);
if ($days_since_added > 30) {
$stats['confidence'] *= pow($decay_factor, ($days_since_added - 30) / 7);
}
// 2. 淘汰低频且低置信度的“基因”
if ($total_count < 2 && $stats['confidence'] < 0.4) {
continue;
}
// 3. 特征漂变(变异保护):如果一个词被误伤太多,强制修正
if ($stats['normal_count'] > $stats['spam_count'] * 2 && $stats['spam_count'] > 0) {
$stats['confidence'] *= 0.5; // 大幅降低其作为垃圾特征的权威性
}
// 4. 遗传修正:根据群体表现调整权重
// 比如如果一个极其短的词1-2字被认为很有用增加一点校验惩罚以防误杀
if (mb_strlen($keyword) <= 2) {
$stats['confidence'] *= 0.9;
}
$new_keywords[$keyword] = $stats;
}
update_option('argon_comment_spam_learned_keywords', $new_keywords);
}
/**
* 更新 Prompt 适应度
* @param string $mode 基础模式
* @param int $prompt_index Prompt 索引
* @param bool $is_correct AI 判断是否正确
*/
function argon_update_prompt_fitness($mode, $prompt_index, $is_correct) {
if (get_option('argon_ai_audit_genetic_prompt_enable', 'false') !== 'true') {
return;
}
$pool = get_option('argon_ai_audit_prompt_pool', []);
if (!isset($pool[$mode]['population'][$prompt_index])) {
return;
}
$p = &$pool[$mode]['population'][$prompt_index];
$p['usage_count']++;
if ($is_correct) {
$p['success_count']++;
$p['fitness'] += 2;
} else {
$p['error_count']++;
$p['fitness'] -= 5;
}
// 约束范围
$p['fitness'] = max(10, min(1000, $p['fitness']));
// 更新最佳索引
$best_fitness = -1000;
$best_idx = 0;
foreach ($pool[$mode]['population'] as $idx => $item) {
if ($item['fitness'] > $best_fitness) {
$best_fitness = $item['fitness'];
$best_idx = $idx;
}
}
$pool[$mode]['current_best_index'] = $best_idx;
// 变异检查
if ($p['usage_count'] > 50 && wp_rand(1, 100) < 5) {
$new_prompt = $p['content'];
$mutations = [
" 请特别注意识别带有微信号的变体。",
" 严厉拦截任何带有营销性质的外部链接。",
" 对看起来像机器生成的重复无意义内容保持警惕。",
" 注意识别故意错别字以逃避检测的广告。"
];
$new_prompt .= $mutations[wp_rand(0, count($mutations) - 1)];
$pool[$mode]['population'][] = [
'content' => $new_prompt,
'fitness' => 80,
'usage_count' => 0,
'success_count' => 0,
'error_count' => 0,
'version' => $p['version'] + 1
];
}
update_option('argon_ai_audit_prompt_pool', $pool);
}
/**
* 构建评论上下文信息
* @param object $comment 评论对象
@@ -10033,6 +10345,59 @@ function argon_detect_spam_comment($comment_id) {
return argon_detect_spam_comment_sync($comment);
}
/**
* 检查评论内容是否匹配已学习的高置信度关键词
* @param string $content 评论内容
* @return array|false 匹配结果或 false
*/
function argon_check_keywords_match($content) {
$learned_keywords = get_option('argon_comment_spam_learned_keywords', []);
if (empty($learned_keywords)) {
return false;
}
foreach ($learned_keywords as $keyword => $stats) {
// 只有置信度极高且经过多次验证的词才进入快速路径
if ($stats['confidence'] > 0.92 && $stats['spam_count'] >= 3 && $stats['normal_count'] == 0) {
if (mb_strpos($content, $keyword) !== false) {
return [
'is_spam' => true,
'reason' => sprintf(__('命中高置信度特征词: %s', 'argon'), $keyword),
'confidence' => $stats['confidence'],
'suggestion' => 'auto',
'hit_keyword' => $keyword
];
}
}
}
return false;
}
/**
* 从 AI 确认的结果中自动学习关键词(高置信度时)
* @param object $comment 评论对象
* @param array $ai_result AI 返回的结果
*/
function argon_auto_learn_from_ai($comment, $ai_result) {
if (get_option('argon_comment_spam_detection_ai_learn', 'false') !== 'true') {
return;
}
// 只有置信度非常高且被判定为垃圾评论时才自动提取特征
if ($ai_result['is_spam'] && $ai_result['confidence'] > 0.98) {
// 检查是否已经有类似关键词,避免重复提取
$content = $comment->comment_author . ' ' . $comment->comment_content;
if (argon_check_keywords_match($content)) {
return; // 已经命中了,不需要再学习
}
$keywords = argon_extract_keywords_from_comment($comment, true);
if (!empty($keywords)) {
argon_update_learned_keywords($keywords, true);
}
}
}
/**
* 同步检测评论是否为垃圾评论(支持临时评论对象)
* @param object $comment 评论对象
@@ -10043,14 +10408,47 @@ function argon_detect_spam_comment_sync($comment) {
// 获取配置
$prompt_mode = get_option('argon_comment_spam_detection_prompt_mode', 'standard');
$custom_prompt = get_option('argon_comment_spam_detection_prompt', '');
// 1. 快速路径:关键词缓存匹配(降低 AI 调用产生的成本)
$combined_content = $comment->comment_author . ' ' . $comment->comment_content;
$keyword_match = argon_check_keywords_match($combined_content);
if ($keyword_match) {
$unified_result = [
'is_spam' => true,
'reason' => $keyword_match['reason'],
'username_invalid' => false,
'username_reason' => '正常',
'confidence' => $keyword_match['confidence'],
'suggestion' => 'auto',
'analysis' => __('已命中本地高置信度特征词库,自动拦截以降低 AI 额度消耗', 'argon'),
'hit_keyword' => $keyword_match['hit_keyword']
];
// 保存检测结果
update_comment_meta($comment->comment_ID, '_argon_spam_detection_result', $unified_result);
update_comment_meta($comment->comment_ID, '_argon_spam_detection_time', time());
update_comment_meta($comment->comment_ID, '_argon_spam_detection_provider', 'local_cache');
return $unified_result;
}
// 2. AI 路径
$prompt_mode = get_option('argon_comment_spam_detection_prompt_mode', 'standard');
$custom_prompt = get_option('argon_comment_spam_detection_prompt', '');
// 选择 Prompt
global $argon_current_prompt_index;
$argon_current_prompt_index = -1;
// 根据模式选择 Prompt
if ($prompt_mode === 'custom' && !empty($custom_prompt)) {
$prompt = $custom_prompt;
} else {
$prompt = argon_get_spam_detection_prompt($prompt_mode);
$prompt = argon_get_genetic_audit_prompt($prompt_mode);
}
// 如果使用了遗传算法,记录当前索引
$used_prompt_index = $argon_current_prompt_index;
// 构建评论上下文信息
$comment_text = argon_build_comment_context($comment);
@@ -10096,15 +10494,23 @@ function argon_detect_spam_comment_sync($comment) {
'username_reason' => isset($result['username_reason']) ? $result['username_reason'] : '正常',
'confidence' => isset($result['confidence']) ? floatval($result['confidence']) : 0.8,
'suggestion' => isset($result['suggestion']) ? $result['suggestion'] : 'auto',
'analysis' => isset($result['analysis']) ? $result['analysis'] : ''
'analysis' => isset($result['analysis']) ? $result['analysis'] : '',
'prompt_mode' => $prompt_mode
];
if (isset($used_prompt_index) && $used_prompt_index !== -1) {
$unified_result['genetic_prompt_index'] = $used_prompt_index;
}
// 保存检测结果
update_comment_meta($comment->comment_ID, '_argon_spam_detection_result', $unified_result);
update_comment_meta($comment->comment_ID, '_argon_spam_detection_time', time());
update_comment_meta($comment->comment_ID, '_argon_spam_detection_provider', $provider);
update_comment_meta($comment->comment_ID, '_argon_spam_detection_model', $model);
// 自动从 AI 结果中提取特征并缓存,降低未来成本
argon_auto_learn_from_ai($comment, $unified_result);
return $unified_result;
}
@@ -10215,8 +10621,16 @@ function argon_ai_learn_keywords($comment_id, $admin_decision) {
$ai_decision = isset($detection_result['is_spam']) ? $detection_result['is_spam'] : false;
// 如果 AI 和管理员判断一致,不需要学习
if ($ai_decision === !$admin_decision) {
// 如果 AI 和管理员判断一致,不需要学习 (AI 对了)
$is_correct = ($ai_decision === !$admin_decision);
// 更新 Prompt 适应度
if (isset($detection_result['genetic_prompt_index'])) {
$prompt_mode = isset($detection_result['prompt_mode']) ? $detection_result['prompt_mode'] : 'standard';
argon_update_prompt_fitness($prompt_mode, $detection_result['genetic_prompt_index'], $is_correct);
}
if ($is_correct) {
return;
}
@@ -10331,6 +10745,9 @@ function argon_update_learned_keywords($keywords, $is_spam) {
}
update_option('argon_comment_spam_detection_keywords', implode("\n", $current_keywords_array));
// 触发遗传修正
argon_genetic_correct_keywords();
}
/**
@@ -10925,7 +11342,7 @@ function argon_batch_detect_spam_comments($comments_data) {
if ($prompt_mode === 'custom' && !empty($custom_prompt)) {
$prompt = $custom_prompt . "\n\n请对每条评论返回检测结果。";
} else {
$prompt = argon_get_spam_detection_prompt($prompt_mode);
$prompt = argon_get_genetic_audit_prompt($prompt_mode);
}
// 构建批量检测内容