create_options(); } /** * 插件停用时执行 */ public function deactivate() { // 清理工作 } /** * 创建默认选项 */ private function create_options() { add_option('ai_seo_api_key', ''); add_option('ai_seo_api_url', ''); } /** * 添加管理菜单 */ public function add_admin_menu() { add_options_page( 'AI SEO Generator 设置', 'AI SEO Generator', 'manage_options', 'ai-seo-generator', array($this, 'admin_page') ); // 添加测试页面子菜单 add_submenu_page( 'options-general.php', 'AI SEO 测试', 'AI SEO 测试', 'manage_options', 'ai-seo-test', array($this, 'test_page') ); } /** * 管理页面内容 * 处理设置保存,包括API配置和支持的文章类型 */ public function admin_page() { if (isset($_POST['submit'])) { // 保存API设置 update_option('ai_seo_api_key', sanitize_text_field($_POST['api_key'])); update_option('ai_seo_api_url', esc_url_raw($_POST['api_url'])); // 保存支持的文章类型设置 $supported_post_types = array(); if (isset($_POST['supported_post_types']) && is_array($_POST['supported_post_types'])) { // 获取所有公开的post类型进行验证 $all_post_types = get_post_types(array('public' => true), 'names'); foreach ($_POST['supported_post_types'] as $post_type) { $post_type = sanitize_text_field($post_type); // 只保存有效的公开post类型 if (in_array($post_type, $all_post_types)) { $supported_post_types[] = $post_type; } } } // 如果没有选择任何类型,默认支持post和page if (empty($supported_post_types)) { $supported_post_types = array('post', 'page'); } update_option('ai_seo_supported_post_types', $supported_post_types); echo '

设置已保存!支持的文章类型:' . implode(', ', $supported_post_types) . '

'; } $api_key = get_option('ai_seo_api_key', ''); $api_url = get_option('ai_seo_api_url', ''); include AI_SEO_PLUGIN_PATH . 'admin/settings.php'; } /** * 测试页面内容 */ public function test_page() { if (!class_exists('AiSeoApiTest')) { echo '

测试功能不可用

测试类未加载。

'; return; } echo '
'; echo '

🧪 AI SEO Generator 测试

'; echo '

此页面用于测试API连接和功能验证。

'; if (isset($_POST['run_test'])) { $tester = new AiSeoApiTest(); echo $tester->generate_test_report(); } else { echo '
'; echo '

'; echo '
'; echo '
'; echo '

测试内容包括:

'; echo ''; echo '
'; } echo '
'; } /** * 添加meta boxes到编辑页面 * 支持所有公开的post类型,包括自定义post类型 */ public function add_meta_boxes() { // 获取所有公开的post类型 $post_types = get_post_types(array('public' => true), 'names'); // 从设置中获取支持的post类型,如果没有设置则使用所有公开类型 $supported_post_types = get_option('ai_seo_supported_post_types', $post_types); // 确保支持的类型都是公开的 $supported_post_types = array_intersect($supported_post_types, $post_types); // 为每个支持的post类型添加meta box foreach ($supported_post_types as $post_type) { add_meta_box( 'ai-seo-generator', 'AI SEO Generator', array($this, 'meta_box_callback'), $post_type, 'normal', 'high' ); } } /** * Meta box回调函数 */ public function meta_box_callback($post) { wp_nonce_field('ai_seo_meta_box', 'ai_seo_meta_box_nonce'); $seo_title = get_post_meta($post->ID, '_ai_seo_title', true); $seo_description = get_post_meta($post->ID, '_ai_seo_description', true); $seo_keywords = get_post_meta($post->ID, '_ai_seo_keywords', true); include AI_SEO_PLUGIN_PATH . 'admin/meta-box.php'; } /** * 保存文章meta数据 */ public function save_post_meta($post_id) { if (!isset($_POST['ai_seo_meta_box_nonce']) || !wp_verify_nonce($_POST['ai_seo_meta_box_nonce'], 'ai_seo_meta_box')) { return; } if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } if (!current_user_can('edit_post', $post_id)) { return; } if (isset($_POST['ai_seo_title'])) { update_post_meta($post_id, '_ai_seo_title', sanitize_text_field($_POST['ai_seo_title'])); } if (isset($_POST['ai_seo_description'])) { update_post_meta($post_id, '_ai_seo_description', sanitize_textarea_field($_POST['ai_seo_description'])); } if (isset($_POST['ai_seo_keywords'])) { update_post_meta($post_id, '_ai_seo_keywords', sanitize_text_field($_POST['ai_seo_keywords'])); } } /** * 加载管理页面脚本和样式 */ public function enqueue_admin_scripts($hook) { if ('post.php' == $hook || 'post-new.php' == $hook || 'settings_page_ai-seo-generator' == $hook) { wp_enqueue_script('ai-seo-admin', AI_SEO_PLUGIN_URL . 'assets/admin.js', array('jquery'), AI_SEO_VERSION, true); wp_enqueue_style('ai-seo-admin', AI_SEO_PLUGIN_URL . 'assets/admin.css', array(), AI_SEO_VERSION); wp_localize_script('ai-seo-admin', 'aiSeoAjax', array( 'ajax_url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('ai_seo_nonce') )); } } /** * AJAX处理生成SEO内容 * 增强错误处理和调试信息 */ public function ajax_generate_seo_content() { // 增加PHP执行时间限制,防止长时间AI生成被中断 set_time_limit(180); // 设置为3分钟 check_ajax_referer('ai_seo_nonce', 'nonce'); $post_id = intval($_POST['post_id']); $post = get_post($post_id); if (!$post) { wp_send_json_error('文章不存在'); return; } $content = $post->post_content; $title = $post->post_title; // 读取导航主题的自定义字段 $sites_link = get_post_meta($post_id, '_sites_link', true); $sites_describe = get_post_meta($post_id, '_sites_sescribe', true); // 构建完整的内容信息,包含自定义字段 $full_content = $content; if (!empty($sites_link)) { $full_content .= "\n\n网站链接: " . $sites_link; } if (!empty($sites_describe)) { $full_content .= "\n\n网站描述: " . $sites_describe; } // 记录请求信息 error_log('AI SEO Request - Post ID: ' . $post_id . ', Title: ' . $title . ', Sites Link: ' . $sites_link . ', Sites Describe: ' . $sites_describe); // 调用AI API生成SEO内容 $seo_data = $this->generate_seo_with_ai($title, $full_content); if ($seo_data && is_array($seo_data)) { // 记录成功信息 error_log('AI SEO Success: ' . json_encode($seo_data)); wp_send_json_success($seo_data); } else { // 记录失败信息 error_log('AI SEO Failed: seo_data = ' . var_export($seo_data, true)); wp_send_json_error('生成SEO内容失败,请查看错误日志获取详细信息'); } } /** * 调用AI API生成SEO内容 * 增强调试和错误处理功能 */ private function generate_seo_with_ai($title, $content) { $api_key = get_option('ai_seo_api_key'); $api_url = get_option('ai_seo_api_url'); // 记录配置信息 error_log('AI SEO Config - API URL: ' . $api_url . ', API Key: ' . (empty($api_key) ? 'Empty' : 'Set')); if (empty($api_key) || empty($api_url)) { error_log('AI SEO Error: API Key or URL not configured'); return false; } // 检测是否包含导航网站信息 $is_navigation_site = (strpos($content, '网站链接:') !== false || strpos($content, '网站描述:') !== false); if ($is_navigation_site) { $prompt = "请根据以下导航网站信息,生成SEO优化的标题(Title)、描述(Description)和关键词(Keywords)。这是一个网站导航页面,请重点关注网站的功能、特色和用途:\n\n标题:{$title}\n\n内容:" . wp_strip_all_tags($content) . "\n\n请针对导航类网站的特点,生成吸引用户点击的SEO内容。以JSON格式返回,包含title、description、keywords字段。"; } else { $prompt = "请根据以下文章标题和内容,生成SEO优化的标题(Title)、描述(Description)和关键词(Keywords):\n\n标题:{$title}\n\n内容:" . wp_strip_all_tags($content) . "\n\n请以JSON格式返回,包含title、description、keywords字段。"; } $request_body = array( 'inputs' => array(), 'query' => $prompt, 'response_mode' => 'blocking', 'user' => 'wordpress-user' ); // 记录请求信息 error_log('AI SEO Request Body: ' . json_encode($request_body)); $response = wp_remote_post($api_url . '/chat-messages', array( 'headers' => array( 'Authorization' => 'Bearer ' . $api_key, 'Content-Type' => 'application/json' ), 'body' => json_encode($request_body), 'timeout' => 120 // 增加超时时间到120秒(2分钟) )); if (is_wp_error($response)) { error_log('AI SEO WP Error: ' . $response->get_error_message()); return false; } $response_code = wp_remote_retrieve_response_code($response); error_log('AI SEO Response Code: ' . $response_code); $body = wp_remote_retrieve_body($response); // 记录原始响应 error_log('AI SEO Raw Response: ' . $body); if (empty($body)) { error_log('AI SEO Error: Empty response body'); return false; } $data = json_decode($body, true); $json_error = json_last_error(); if ($json_error !== JSON_ERROR_NONE) { error_log('AI SEO JSON Error: ' . json_last_error_msg()); return false; } error_log('AI SEO Parsed Data: ' . json_encode($data)); // 方法1: 检查是否直接返回了SEO数据结构 if (isset($data['title'], $data['description'], $data['keywords'])) { error_log('AI SEO: Found direct SEO data structure'); return array( 'title' => $data['title'], 'description' => $data['description'], 'keywords' => $data['keywords'] ); } // 方法2: 检查answer字段中的JSON数据 if (isset($data['answer'])) { $answer = $data['answer']; error_log('AI SEO: Found answer field: ' . $answer); // 尝试直接解析answer作为JSON $seo_data = json_decode($answer, true); if ($seo_data && isset($seo_data['title'], $seo_data['description'], $seo_data['keywords'])) { error_log('AI SEO: Successfully parsed answer as JSON'); return $seo_data; } // 尝试从answer中提取JSON字符串 preg_match('/\{.*\}/s', $answer, $matches); if (!empty($matches[0])) { error_log('AI SEO: Extracted JSON from answer: ' . $matches[0]); $seo_data = json_decode($matches[0], true); if ($seo_data && isset($seo_data['title'], $seo_data['description'], $seo_data['keywords'])) { error_log('AI SEO: Successfully parsed extracted JSON'); return $seo_data; } } } // 方法3: 检查data字段中的内容 if (isset($data['data'])) { $data_content = $data['data']; error_log('AI SEO: Found data field: ' . json_encode($data_content)); if (is_array($data_content) && isset($data_content['title'], $data_content['description'], $data_content['keywords'])) { error_log('AI SEO: Successfully found SEO data in data field'); return $data_content; } } error_log('AI SEO: No valid SEO data found in response'); return false; } /** * 自定义页面标题 */ public function custom_wp_title($title, $sep) { if (is_single() || is_page()) { global $post; $custom_title = get_post_meta($post->ID, '_ai_seo_title', true); if (!empty($custom_title)) { return $custom_title; } } return $title; } /** * 添加meta标签到页面头部 * 使用Open Graph标签避免与WordPress默认meta标签冲突 */ public function add_meta_tags() { if (is_single() || is_page()) { global $post; $seo_title = get_post_meta($post->ID, '_ai_seo_title', true); $description = get_post_meta($post->ID, '_ai_seo_description', true); $keywords = get_post_meta($post->ID, '_ai_seo_keywords', true); // 输出Open Graph标题 if (!empty($seo_title)) { echo '' . "\n"; } // 输出Open Graph描述 if (!empty($description)) { echo '' . "\n"; } // 保留keywords标签(Open Graph没有对应标签) if (!empty($keywords)) { echo '' . "\n"; } // 添加其他常用的Open Graph标签 echo '' . "\n"; echo '' . "\n"; // 如果文章有特色图片,添加og:image if (has_post_thumbnail($post->ID)) { $thumbnail_url = get_the_post_thumbnail_url($post->ID, 'large'); echo '' . "\n"; } } } } // 初始化插件 new AiSeoGenerator(); ?>