feat: 新增AI SEO助手插件,支持自动生成SEO内容和导航主题适配

添加完整的AI SEO助手WordPress插件,主要功能包括:
- 集成Dify API自动生成SEO优化的标题、描述和关键词
- 支持导航主题的自定义字段适配
- 提供管理界面设置API和文章类型支持
- 包含前端SEO标签自动应用功能
- 添加详细的测试和调试功能
This commit is contained in:
2025-08-13 18:52:35 +08:00
commit 1ae6823bd5
9 changed files with 2536 additions and 0 deletions

View File

@@ -0,0 +1,345 @@
<?php
/**
* AI SEO Generator API测试文件
* 用于测试Dify API连接和功能验证
*/
// 防止直接访问
if (!defined('ABSPATH')) {
exit;
}
/**
* API测试类
*/
class AiSeoApiTest {
private $api_key;
private $api_url;
/**
* 构造函数
*/
public function __construct() {
$this->api_key = get_option('ai_seo_api_key', '');
$this->api_url = get_option('ai_seo_api_url', '');
}
/**
* 测试API连接
*/
public function test_connection() {
$test_data = array(
'status' => 'unknown',
'message' => '',
'response_time' => 0,
'details' => array()
);
$start_time = microtime(true);
try {
$response = wp_remote_get($this->api_url, array(
'headers' => array(
'Authorization' => 'Bearer ' . $this->api_key,
'Content-Type' => 'application/json'
),
'timeout' => 10
));
$end_time = microtime(true);
$test_data['response_time'] = round(($end_time - $start_time) * 1000, 2);
if (is_wp_error($response)) {
$test_data['status'] = 'error';
$test_data['message'] = $response->get_error_message();
} else {
$status_code = wp_remote_retrieve_response_code($response);
$test_data['details']['status_code'] = $status_code;
if ($status_code >= 200 && $status_code < 300) {
$test_data['status'] = 'success';
$test_data['message'] = 'API连接成功';
} else {
$test_data['status'] = 'error';
$test_data['message'] = 'API返回错误状态码: ' . $status_code;
}
}
} catch (Exception $e) {
$test_data['status'] = 'error';
$test_data['message'] = '连接异常: ' . $e->getMessage();
}
return $test_data;
}
/**
* 测试SEO内容生成
*/
public function test_seo_generation() {
$test_content = array(
'title' => 'WordPress插件开发指南',
'content' => 'WordPress是世界上最流行的内容管理系统之一拥有强大的插件生态系统。本文将介绍如何开发一个WordPress插件包括基本结构、钩子系统、数据库操作等核心概念。我们还将探讨最佳实践和安全考虑帮助开发者创建高质量的插件。'
);
$test_data = array(
'status' => 'unknown',
'message' => '',
'generated_content' => null,
'response_time' => 0
);
$start_time = microtime(true);
try {
$prompt = "请根据以下文章标题和内容生成SEO优化的标题(Title)、描述(Description)和关键词(Keywords)\n\n标题:{$test_content['title']}\n\n内容:{$test_content['content']}\n\n请以JSON格式返回包含title、description、keywords字段。";
$response = wp_remote_post($this->api_url . '/chat-messages', array(
'headers' => array(
'Authorization' => 'Bearer ' . $this->api_key,
'Content-Type' => 'application/json'
),
'body' => json_encode(array(
'inputs' => array(),
'query' => $prompt,
'response_mode' => 'blocking',
'user' => 'test-user'
)),
'timeout' => 30
));
$end_time = microtime(true);
$test_data['response_time'] = round(($end_time - $start_time) * 1000, 2);
if (is_wp_error($response)) {
$test_data['status'] = 'error';
$test_data['message'] = $response->get_error_message();
} else {
$status_code = wp_remote_retrieve_response_code($response);
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if ($status_code === 200 && isset($data['answer'])) {
// 尝试从AI回答中提取JSON
$answer = $data['answer'];
preg_match('/\{.*\}/s', $answer, $matches);
if (!empty($matches[0])) {
$seo_data = json_decode($matches[0], true);
if ($seo_data && isset($seo_data['title'], $seo_data['description'], $seo_data['keywords'])) {
$test_data['status'] = 'success';
$test_data['message'] = 'SEO内容生成成功';
$test_data['generated_content'] = $seo_data;
} else {
$test_data['status'] = 'error';
$test_data['message'] = '无法解析AI返回的JSON格式';
$test_data['raw_response'] = $answer;
}
} else {
$test_data['status'] = 'error';
$test_data['message'] = 'AI返回格式不正确';
$test_data['raw_response'] = $answer;
}
} else {
$test_data['status'] = 'error';
$test_data['message'] = 'API返回错误: ' . $status_code;
$test_data['raw_response'] = $body;
}
}
} catch (Exception $e) {
$test_data['status'] = 'error';
$test_data['message'] = '生成异常: ' . $e->getMessage();
}
return $test_data;
}
/**
* 运行完整测试套件
*/
public function run_full_test() {
$results = array(
'connection_test' => $this->test_connection(),
'generation_test' => $this->test_seo_generation(),
'system_info' => $this->get_system_info(),
'plugin_info' => $this->get_plugin_info()
);
// 计算总体状态
$overall_status = 'success';
if ($results['connection_test']['status'] === 'error' || $results['generation_test']['status'] === 'error') {
$overall_status = 'error';
}
$results['overall_status'] = $overall_status;
$results['test_time'] = current_time('Y-m-d H:i:s');
return $results;
}
/**
* 获取系统信息
*/
private function get_system_info() {
global $wp_version;
return array(
'wordpress_version' => $wp_version,
'php_version' => PHP_VERSION,
'curl_enabled' => extension_loaded('curl'),
'json_enabled' => extension_loaded('json'),
'memory_limit' => ini_get('memory_limit'),
'max_execution_time' => ini_get('max_execution_time'),
'upload_max_filesize' => ini_get('upload_max_filesize')
);
}
/**
* 获取插件信息
*/
private function get_plugin_info() {
return array(
'plugin_version' => AI_SEO_VERSION,
'plugin_path' => AI_SEO_PLUGIN_PATH,
'plugin_url' => AI_SEO_PLUGIN_URL,
'api_key_configured' => !empty($this->api_key),
'api_url_configured' => !empty($this->api_url),
'database_version' => get_option('ai_seo_db_version', '0')
);
}
/**
* 生成测试报告HTML
*/
public function generate_test_report() {
$results = $this->run_full_test();
ob_start();
?>
<div class="ai-seo-test-report">
<h2>🧪 AI SEO Generator 测试报告</h2>
<div class="test-summary">
<h3>测试概览</h3>
<p><strong>总体状态:</strong>
<span class="status-<?php echo $results['overall_status']; ?>">
<?php echo $results['overall_status'] === 'success' ? '✅ 通过' : '❌ 失败'; ?>
</span>
</p>
<p><strong>测试时间:</strong> <?php echo $results['test_time']; ?></p>
</div>
<div class="test-details">
<h3>连接测试</h3>
<div class="test-item">
<p><strong>状态:</strong>
<span class="status-<?php echo $results['connection_test']['status']; ?>">
<?php echo $results['connection_test']['status'] === 'success' ? '✅ 成功' : '❌ 失败'; ?>
</span>
</p>
<p><strong>消息:</strong> <?php echo esc_html($results['connection_test']['message']); ?></p>
<p><strong>响应时间:</strong> <?php echo $results['connection_test']['response_time']; ?>ms</p>
</div>
<h3>内容生成测试</h3>
<div class="test-item">
<p><strong>状态:</strong>
<span class="status-<?php echo $results['generation_test']['status']; ?>">
<?php echo $results['generation_test']['status'] === 'success' ? '✅ 成功' : '❌ 失败'; ?>
</span>
</p>
<p><strong>消息:</strong> <?php echo esc_html($results['generation_test']['message']); ?></p>
<p><strong>响应时间:</strong> <?php echo $results['generation_test']['response_time']; ?>ms</p>
<?php if (isset($results['generation_test']['generated_content'])): ?>
<h4>生成的内容:</h4>
<div class="generated-content">
<p><strong>标题:</strong> <?php echo esc_html($results['generation_test']['generated_content']['title']); ?></p>
<p><strong>描述:</strong> <?php echo esc_html($results['generation_test']['generated_content']['description']); ?></p>
<p><strong>关键词:</strong> <?php echo esc_html($results['generation_test']['generated_content']['keywords']); ?></p>
</div>
<?php endif; ?>
</div>
</div>
<div class="system-info">
<h3>系统信息</h3>
<table class="widefat">
<tbody>
<tr><td>WordPress版本</td><td><?php echo $results['system_info']['wordpress_version']; ?></td></tr>
<tr><td>PHP版本</td><td><?php echo $results['system_info']['php_version']; ?></td></tr>
<tr><td>cURL支持</td><td><?php echo $results['system_info']['curl_enabled'] ? '✅ 是' : '❌ 否'; ?></td></tr>
<tr><td>JSON支持</td><td><?php echo $results['system_info']['json_enabled'] ? '✅ 是' : '❌ 否'; ?></td></tr>
<tr><td>内存限制</td><td><?php echo $results['system_info']['memory_limit']; ?></td></tr>
<tr><td>执行时间限制</td><td><?php echo $results['system_info']['max_execution_time']; ?>秒</td></tr>
</tbody>
</table>
</div>
<div class="plugin-info">
<h3>插件信息</h3>
<table class="widefat">
<tbody>
<tr><td>插件版本</td><td><?php echo $results['plugin_info']['plugin_version']; ?></td></tr>
<tr><td>API Key配置</td><td><?php echo $results['plugin_info']['api_key_configured'] ? '✅ 已配置' : '❌ 未配置'; ?></td></tr>
<tr><td>API URL配置</td><td><?php echo $results['plugin_info']['api_url_configured'] ? '✅ 已配置' : '❌ 未配置'; ?></td></tr>
<tr><td>数据库版本</td><td><?php echo $results['plugin_info']['database_version']; ?></td></tr>
</tbody>
</table>
</div>
</div>
<style>
.ai-seo-test-report {
background: white;
padding: 20px;
border: 1px solid #ddd;
border-radius: 8px;
margin: 20px 0;
}
.test-summary {
background: #f9f9f9;
padding: 15px;
border-radius: 5px;
margin-bottom: 20px;
}
.test-item {
background: #f9f9f9;
padding: 15px;
border-radius: 5px;
margin-bottom: 15px;
}
.status-success {
color: #28a745;
font-weight: bold;
}
.status-error {
color: #dc3545;
font-weight: bold;
}
.generated-content {
background: #e9ecef;
padding: 10px;
border-radius: 4px;
margin-top: 10px;
}
.widefat td {
padding: 8px 12px;
}
</style>
<?php
return ob_get_clean();
}
}
// 如果是AJAX请求返回JSON结果
if (defined('DOING_AJAX') && DOING_AJAX && isset($_POST['action']) && $_POST['action'] === 'ai_seo_run_test') {
$tester = new AiSeoApiTest();
$results = $tester->run_full_test();
wp_send_json($results);
}
?>