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(); ?>

🧪 AI SEO Generator 测试报告

测试概览

总体状态:

测试时间:

连接测试

状态:

消息:

响应时间: ms

内容生成测试

状态:

消息:

响应时间: ms

生成的内容:

标题:

描述:

关键词:

系统信息

WordPress版本
PHP版本
cURL支持
JSON支持
内存限制
执行时间限制

插件信息

插件版本
API Key配置
API URL配置
数据库版本
run_full_test(); wp_send_json($results); } ?>