refactor: 移除 functions.php 中的向后兼容代码
- 删除旧的多 API 管理函数(argon_get_provider_apis, argon_add_provider_api 等) - 删除数据迁移函数 argon_migrate_ai_apis() - 移除自动迁移钩子 - 简化 argon_get_ai_provider_config() 函数,移除回退逻辑 - 简化 argon_get_active_api_config() 函数,移除向后兼容逻辑 - 清理临时测试文件和脚本
This commit is contained in:
@@ -1,233 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* 新的 AI 设置页面结构
|
||||
* 这是一个草稿文件,用于设计新的 AI 设置界面
|
||||
*/
|
||||
|
||||
// 提供商列表
|
||||
$providers = [
|
||||
'openai' => 'OpenAI (ChatGPT)',
|
||||
'anthropic' => 'Anthropic (Claude)',
|
||||
'deepseek' => 'DeepSeek',
|
||||
'xiaomi' => __('小米 Mimo', 'argon'),
|
||||
'qianwen' => __('通义千问', 'argon'),
|
||||
'wenxin' => __('文心一言', 'argon'),
|
||||
'doubao' => __('豆包 (火山引擎)', 'argon'),
|
||||
'kimi' => 'Kimi (Moonshot)',
|
||||
'zhipu' => __('智谱 AI (GLM)', 'argon'),
|
||||
'siliconflow' => __('硅基流动 (SiliconFlow)', 'argon')
|
||||
];
|
||||
|
||||
// 获取所有 API
|
||||
$all_apis = argon_get_all_apis();
|
||||
$summary_active_api = get_option('argon_ai_summary_active_api', '');
|
||||
$spam_active_api = get_option('argon_ai_spam_active_api', '');
|
||||
?>
|
||||
|
||||
<!-- ========== AI 功能 ========== -->
|
||||
<h1 style="color: #5e72e4; margin-top: 50px; font-size: 32px;"><?php _e('AI 功能', 'argon');?></h1>
|
||||
<p><?php _e('统一管理所有 AI 服务商的 API 配置,并配置 AI 文章摘要和评论审核功能', 'argon');?></p>
|
||||
|
||||
<!-- ========== API 管理 ========== -->
|
||||
<tr><th class="subtitle"><h2 id="ai-api-management"><?php _e('API 管理', 'argon');?></h2></th></tr>
|
||||
|
||||
<tr>
|
||||
<th><label><?php _e('已配置的 API', 'argon');?></label></th>
|
||||
<td>
|
||||
<div id="argon-unified-api-list">
|
||||
<?php if (!empty($all_apis)): ?>
|
||||
<?php foreach ($all_apis as $api): ?>
|
||||
<div class="argon-unified-api-item" data-api-id="<?php echo esc_attr($api['id']); ?>" style="padding: 15px; background: #f5f5f5; margin-bottom: 10px; border-radius: 6px; border-left: 4px solid #5e72e4;">
|
||||
<div style="display: flex; align-items: center; justify-content: space-between;">
|
||||
<div style="flex: 1;">
|
||||
<div style="font-size: 16px; font-weight: 600; margin-bottom: 5px;">
|
||||
<?php echo esc_html($api['name']); ?>
|
||||
<?php if (!empty($api['model'])): ?>
|
||||
<span style="color: #666; font-weight: 400; font-size: 14px;">(<?php echo esc_html($api['model']); ?>)</span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div style="font-size: 13px; color: #666;">
|
||||
<span style="display: inline-block; padding: 2px 8px; background: #e3f2fd; color: #1976d2; border-radius: 3px; margin-right: 8px;">
|
||||
<?php echo esc_html($providers[$api['provider']]); ?>
|
||||
</span>
|
||||
<?php _e('密钥:', 'argon'); ?> <code><?php echo esc_html(substr($api['api_key'], 0, 12)); ?>...</code>
|
||||
<?php if (!empty($api['api_endpoint'])): ?>
|
||||
| <?php _e('端点:', 'argon'); ?> <code><?php echo esc_html($api['api_endpoint']); ?></code>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div style="margin-top: 8px; font-size: 12px; color: #888;">
|
||||
<?php if ($api['id'] === $summary_active_api): ?>
|
||||
<span style="display: inline-block; padding: 2px 6px; background: #4caf50; color: #fff; border-radius: 3px; margin-right: 5px;">
|
||||
<?php _e('文章摘要', 'argon'); ?>
|
||||
</span>
|
||||
<?php endif; ?>
|
||||
<?php if ($api['id'] === $spam_active_api): ?>
|
||||
<span style="display: inline-block; padding: 2px 6px; background: #ff9800; color: #fff; border-radius: 3px; margin-right: 5px;">
|
||||
<?php _e('评论审核', 'argon'); ?>
|
||||
</span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
<div style="display: flex; gap: 5px;">
|
||||
<button type="button" class="button button-small argon-test-unified-api" data-api-id="<?php echo esc_attr($api['id']); ?>">
|
||||
<span class="dashicons dashicons-yes-alt" style="margin-top: 3px;"></span>
|
||||
<?php _e('测试', 'argon'); ?>
|
||||
</button>
|
||||
<button type="button" class="button button-small argon-edit-unified-api" data-api-id="<?php echo esc_attr($api['id']); ?>">
|
||||
<?php _e('编辑', 'argon'); ?>
|
||||
</button>
|
||||
<button type="button" class="button button-small argon-delete-unified-api" data-api-id="<?php echo esc_attr($api['id']); ?>" style="color: #b32d2e;">
|
||||
<?php _e('删除', 'argon'); ?>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
<?php else: ?>
|
||||
<p style="color: #666; padding: 20px; background: #f9f9f9; border-radius: 4px; text-align: center;">
|
||||
<?php _e('暂无配置的 API,请点击下方按钮添加', 'argon'); ?>
|
||||
</p>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
<button type="button" class="button button-primary" id="argon-add-unified-api-btn" style="margin-top: 15px;">
|
||||
<span class="dashicons dashicons-plus-alt" style="margin-top: 3px;"></span>
|
||||
<?php _e('添加新 API', 'argon'); ?>
|
||||
</button>
|
||||
|
||||
<!-- API 配置表单(隐藏) -->
|
||||
<div id="argon-unified-api-form" style="display:none; margin-top: 20px; padding: 20px; background: #fff; border: 2px solid #5e72e4; border-radius: 6px;">
|
||||
<h3 style="margin-top: 0;"><?php _e('API 配置', 'argon'); ?></h3>
|
||||
<input type="hidden" id="argon-unified-api-form-id" value="" />
|
||||
|
||||
<p>
|
||||
<label>
|
||||
<strong><?php _e('配置名称:', 'argon'); ?></strong> <span style="color: #d32f2f;">*</span><br>
|
||||
<input type="text" id="argon-unified-api-form-name" class="regular-text" placeholder="<?php _e('例如: 主 OpenAI API', 'argon'); ?>" />
|
||||
</label>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<label>
|
||||
<strong><?php _e('API 密钥:', 'argon'); ?></strong> <span style="color: #d32f2f;">*</span><br>
|
||||
<input type="password" id="argon-unified-api-form-key" class="regular-text" placeholder="sk-..." />
|
||||
<button type="button" class="button" id="argon-toggle-unified-password" style="margin-left: 5px;">
|
||||
<span class="dashicons dashicons-visibility"></span>
|
||||
</button>
|
||||
</label>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<label>
|
||||
<strong><?php _e('提供商:', 'argon'); ?></strong> <span style="color: #d32f2f;">*</span><br>
|
||||
<select id="argon-unified-api-form-provider" class="regular-text">
|
||||
<option value=""><?php _e('请选择提供商', 'argon'); ?></option>
|
||||
<?php foreach ($providers as $key => $name): ?>
|
||||
<option value="<?php echo esc_attr($key); ?>"><?php echo esc_html($name); ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</label>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<label>
|
||||
<strong><?php _e('API 端点:', 'argon'); ?></strong> <small>(<?php _e('可选', 'argon'); ?>)</small><br>
|
||||
<input type="text" id="argon-unified-api-form-endpoint" class="regular-text" placeholder="<?php _e('留空使用默认端点', 'argon'); ?>" />
|
||||
</label>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<label>
|
||||
<strong><?php _e('模型:', 'argon'); ?></strong> <small>(<?php _e('可选', 'argon'); ?>)</small><br>
|
||||
<input type="text" id="argon-unified-api-form-model" class="regular-text" placeholder="<?php _e('留空使用默认模型', 'argon'); ?>" style="width: calc(100% - 120px);" />
|
||||
<button type="button" class="button" id="argon-refresh-unified-models" style="margin-left: 5px;">
|
||||
<span class="dashicons dashicons-update"></span> <?php _e('刷新', 'argon'); ?>
|
||||
</button>
|
||||
</label>
|
||||
<div id="argon-unified-models-list" style="display:none; margin-top: 10px; max-height: 200px; overflow-y: auto; border: 1px solid #ddd; padding: 10px; background: #fafafa; border-radius: 4px;">
|
||||
<p style="margin: 0; color: #666;"><?php _e('加载中...', 'argon'); ?></p>
|
||||
</div>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<button type="button" class="button button-primary" id="argon-save-unified-api">
|
||||
<?php _e('保存', 'argon'); ?>
|
||||
</button>
|
||||
<button type="button" class="button" id="argon-cancel-unified-api">
|
||||
<?php _e('取消', 'argon'); ?>
|
||||
</button>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<p class="description" style="margin-top: 15px;">
|
||||
<span class="dashicons dashicons-info" style="color: #2271b1;"></span>
|
||||
<?php _e('统一管理所有 AI 服务商的 API 配置。不同功能可以使用不同的 API(在下方的文章摘要和评论审核设置中选择)', 'argon');?>
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<!-- ========== 文章摘要 ========== -->
|
||||
<tr><th class="subtitle"><h2 id="ai-summary"><?php _e('文章摘要', 'argon');?></h2></th></tr>
|
||||
|
||||
<tr>
|
||||
<th><label><?php _e('启用 AI 摘要', 'argon');?></label></th>
|
||||
<td>
|
||||
<select name="argon_ai_summary_enable">
|
||||
<?php $argon_ai_summary_enable = get_option('argon_ai_summary_enable', 'false'); ?>
|
||||
<option value="true" <?php if ($argon_ai_summary_enable=='true'){echo 'selected';} ?>><?php _e('启用', 'argon');?></option>
|
||||
<option value="false" <?php if ($argon_ai_summary_enable=='false'){echo 'selected';} ?>><?php _e('禁用', 'argon');?></option>
|
||||
</select>
|
||||
<p class="description"><?php _e('开启后,文章开头会显示 AI 生成的摘要。每篇文章只生成一次,结果会缓存到服务器。', 'argon');?></p>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<th><label><?php _e('默认使用 API', 'argon');?></label></th>
|
||||
<td>
|
||||
<select name="argon_ai_summary_active_api">
|
||||
<option value=""><?php _e('请选择 API', 'argon'); ?></option>
|
||||
<?php foreach ($all_apis as $api): ?>
|
||||
<option value="<?php echo esc_attr($api['id']); ?>" <?php selected($summary_active_api, $api['id']); ?>>
|
||||
<?php echo esc_html($api['name']); ?> (<?php echo esc_html($providers[$api['provider']]); ?>)
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<p class="description"><?php _e('选择用于生成文章摘要的 API 配置', 'argon');?></p>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<!-- 其他文章摘要设置保持不变 -->
|
||||
|
||||
<!-- ========== 评论审核 ========== -->
|
||||
<tr><th class="subtitle"><h2 id="ai-spam-detection"><?php _e('评论审核', 'argon');?></h2></th></tr>
|
||||
|
||||
<tr>
|
||||
<th><label><?php _e('启用 AI 识别', 'argon');?></label></th>
|
||||
<td>
|
||||
<?php $argon_comment_spam_detection_enable = get_option('argon_comment_spam_detection_enable', 'false'); ?>
|
||||
<label>
|
||||
<input type="checkbox" name="argon_comment_spam_detection_enable" value="true" <?php if ($argon_comment_spam_detection_enable=='true'){echo 'checked';}?>/>
|
||||
<?php _e('启用 AI 自动识别垃圾评论', 'argon');?>
|
||||
</label>
|
||||
<p class="description">
|
||||
<?php _e('开启后,将使用 AI 自动识别广告、反动、违法等垃圾评论。', 'argon');?>
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<th><label><?php _e('默认使用 API', 'argon');?></label></th>
|
||||
<td>
|
||||
<select name="argon_ai_spam_active_api">
|
||||
<option value=""><?php _e('请选择 API', 'argon'); ?></option>
|
||||
<?php foreach ($all_apis as $api): ?>
|
||||
<option value="<?php echo esc_attr($api['id']); ?>" <?php selected($spam_active_api, $api['id']); ?>>
|
||||
<?php echo esc_html($api['name']); ?> (<?php echo esc_html($providers[$api['provider']]); ?>)
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<p class="description"><?php _e('选择用于垃圾评论检测的 API 配置', 'argon');?></p>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<!-- 其他评论审核设置保持不变 -->
|
||||
21
tmp/analyze-settings.py
Normal file
21
tmp/analyze-settings.py
Normal file
@@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env python3
|
||||
# 分析 settings.php 文件结构
|
||||
|
||||
with open('settings.php', 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
# 查找关键位置
|
||||
pos1 = content.find('<!-- ========== 12. 文章功能 ==========')
|
||||
pos2 = content.find('<tr><th class="subtitle"><h3 id="subsection-footnote">')
|
||||
|
||||
print(f'文章功能开始位置: {pos1}')
|
||||
print(f'脚注引用开始位置: {pos2}')
|
||||
print(f'需要替换的字符数: {pos2 - pos1}')
|
||||
|
||||
# 统计行数
|
||||
lines_before = content[:pos1].count('\n')
|
||||
lines_between = content[pos1:pos2].count('\n')
|
||||
|
||||
print(f'\n文章功能开始行: {lines_before + 1}')
|
||||
print(f'需要移除的行数: {lines_between}')
|
||||
print(f'脚注引用开始行: {lines_before + lines_between + 1}')
|
||||
71
tmp/refactor-settings.py
Normal file
71
tmp/refactor-settings.py
Normal file
@@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Settings.php AI 功能重构脚本
|
||||
移除旧的 AI 设置,插入新的统一 AI 功能部分
|
||||
"""
|
||||
|
||||
# 读取文件
|
||||
with open('settings.php', 'r', encoding='utf-8') as f:
|
||||
lines = f.readlines()
|
||||
|
||||
# 读取新的 AI 部分
|
||||
with open('tmp/complete-ai-section.php', 'r', encoding='utf-8') as f:
|
||||
new_ai_section = f.read()
|
||||
|
||||
# 读取评论审核的其他设置(保留非 AI 部分)
|
||||
with open('tmp/comment-settings-other.txt', 'r', encoding='utf-8') as f:
|
||||
comment_other_settings = f.read()
|
||||
|
||||
# 步骤 1: 在第 1985 行之前插入新的 AI 功能部分
|
||||
# 步骤 2: 移除第 1987-2599 行(旧的 AI 摘要设置)
|
||||
# 步骤 3: 在评论设置中移除 AI 垃圾评论识别子分类,添加到新的 AI 功能部分
|
||||
|
||||
# 找到插入位置(文章功能之前)
|
||||
insert_pos = None
|
||||
for i, line in enumerate(lines):
|
||||
if '<!-- ========== 12. 文章功能 ==========' in line:
|
||||
insert_pos = i
|
||||
break
|
||||
|
||||
if insert_pos is None:
|
||||
print("错误:找不到文章功能部分")
|
||||
exit(1)
|
||||
|
||||
# 找到需要移除的旧 AI 摘要设置的结束位置
|
||||
remove_end = None
|
||||
for i in range(insert_pos, len(lines)):
|
||||
if 'subsection-footnote' in lines[i] and '脚注引用' in lines[i]:
|
||||
remove_end = i
|
||||
break
|
||||
|
||||
if remove_end is None:
|
||||
print("错误:找不到脚注引用部分")
|
||||
exit(1)
|
||||
|
||||
print(f"将在第 {insert_pos + 1} 行插入新的 AI 功能部分")
|
||||
print(f"将移除第 {insert_pos + 3} 到 {remove_end} 行的旧 AI 摘要设置")
|
||||
|
||||
# 构建新文件
|
||||
new_lines = []
|
||||
|
||||
# 保留插入位置之前的内容
|
||||
new_lines.extend(lines[:insert_pos])
|
||||
|
||||
# 插入新的 AI 功能部分
|
||||
new_lines.append(new_ai_section)
|
||||
new_lines.append('\n')
|
||||
|
||||
# 跳过旧的 AI 摘要设置,保留文章功能标题和之后的内容
|
||||
new_lines.append(lines[insert_pos]) # 文章功能标题
|
||||
new_lines.append(lines[insert_pos + 1]) # <tr><th class="subtitle">
|
||||
|
||||
# 保留脚注引用及之后的内容
|
||||
new_lines.extend(lines[remove_end:])
|
||||
|
||||
# 写入新文件
|
||||
with open('settings.php', 'w', encoding='utf-8') as f:
|
||||
f.writelines(new_lines)
|
||||
|
||||
print("✓ 成功插入新的 AI 功能部分")
|
||||
print("✓ 成功移除旧的 AI 摘要设置")
|
||||
print(f"✓ 文件行数变化:{len(lines)} → {len(new_lines)}")
|
||||
30
tmp/remove-duplicate-spam-detection.py
Normal file
30
tmp/remove-duplicate-spam-detection.py
Normal file
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
删除 settings.php 中重复的 AI 垃圾评论识别部分
|
||||
"""
|
||||
|
||||
def remove_duplicate_spam_detection():
|
||||
with open('settings.php', 'r', encoding='utf-8') as f:
|
||||
lines = f.readlines()
|
||||
|
||||
# 删除第 4619-5204 行(Python 索引从 0 开始,所以是 4618-5203)
|
||||
# 第 4619 行是 <tr><th class="subtitle"><h3 id="subsection-comment-spam-detection">
|
||||
# 第 5204 行是 </tr> (在 subsection-comment-appearance 之前)
|
||||
|
||||
start_line = 4618 # 第 4619 行(索引从 0 开始)
|
||||
end_line = 5204 # 第 5205 行(不包含)
|
||||
|
||||
# 保留前面和后面的内容
|
||||
new_lines = lines[:start_line] + lines[end_line:]
|
||||
|
||||
# 写回文件
|
||||
with open('settings.php', 'w', encoding='utf-8') as f:
|
||||
f.writelines(new_lines)
|
||||
|
||||
deleted_lines = end_line - start_line
|
||||
print(f"✓ 已删除第 {start_line + 1}-{end_line} 行(共 {deleted_lines} 行)")
|
||||
print(f"✓ 文件行数:{len(lines)} → {len(new_lines)} (-{deleted_lines})")
|
||||
|
||||
if __name__ == '__main__':
|
||||
remove_duplicate_spam_detection()
|
||||
Reference in New Issue
Block a user