添加完整的AI SEO助手WordPress插件,主要功能包括: - 集成Dify API自动生成SEO优化的标题、描述和关键词 - 支持导航主题的自定义字段适配 - 提供管理界面设置API和文章类型支持 - 包含前端SEO标签自动应用功能 - 添加详细的测试和调试功能
346 lines
10 KiB
PHP
346 lines
10 KiB
PHP
<?php
|
||
/**
|
||
* AI SEO Generator 安装脚本
|
||
* 用于插件的安装、升级和卸载操作
|
||
*/
|
||
|
||
// 防止直接访问
|
||
if (!defined('ABSPATH')) {
|
||
exit;
|
||
}
|
||
|
||
/**
|
||
* 插件安装类
|
||
*/
|
||
class AiSeoInstaller {
|
||
|
||
/**
|
||
* 插件激活时执行
|
||
*/
|
||
public static function activate() {
|
||
self::create_database_tables();
|
||
self::set_default_options();
|
||
self::create_directories();
|
||
self::schedule_events();
|
||
|
||
// 刷新重写规则
|
||
flush_rewrite_rules();
|
||
|
||
// 记录激活时间
|
||
update_option('ai_seo_activated_time', current_time('timestamp'));
|
||
|
||
// 显示欢迎消息
|
||
set_transient('ai_seo_activation_notice', true, 30);
|
||
}
|
||
|
||
/**
|
||
* 插件停用时执行
|
||
*/
|
||
public static function deactivate() {
|
||
// 清理计划任务
|
||
wp_clear_scheduled_hook('ai_seo_cleanup_task');
|
||
|
||
// 刷新重写规则
|
||
flush_rewrite_rules();
|
||
|
||
// 记录停用时间
|
||
update_option('ai_seo_deactivated_time', current_time('timestamp'));
|
||
}
|
||
|
||
/**
|
||
* 插件卸载时执行
|
||
*/
|
||
public static function uninstall() {
|
||
// 删除选项
|
||
self::remove_options();
|
||
|
||
// 删除用户meta
|
||
self::remove_user_meta();
|
||
|
||
// 删除文章meta
|
||
self::remove_post_meta();
|
||
|
||
// 删除数据库表(可选)
|
||
// self::drop_database_tables();
|
||
|
||
// 清理计划任务
|
||
wp_clear_scheduled_hook('ai_seo_cleanup_task');
|
||
}
|
||
|
||
/**
|
||
* 创建数据库表
|
||
*/
|
||
private static function create_database_tables() {
|
||
global $wpdb;
|
||
|
||
$charset_collate = $wpdb->get_charset_collate();
|
||
|
||
// SEO日志表
|
||
$table_name = $wpdb->prefix . 'ai_seo_logs';
|
||
|
||
$sql = "CREATE TABLE $table_name (
|
||
id mediumint(9) NOT NULL AUTO_INCREMENT,
|
||
post_id bigint(20) NOT NULL,
|
||
action varchar(50) NOT NULL,
|
||
old_value text,
|
||
new_value text,
|
||
user_id bigint(20) NOT NULL,
|
||
created_at datetime DEFAULT CURRENT_TIMESTAMP,
|
||
PRIMARY KEY (id),
|
||
KEY post_id (post_id),
|
||
KEY user_id (user_id),
|
||
KEY created_at (created_at)
|
||
) $charset_collate;";
|
||
|
||
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
|
||
dbDelta($sql);
|
||
|
||
// 更新数据库版本
|
||
update_option('ai_seo_db_version', '1.0');
|
||
}
|
||
|
||
/**
|
||
* 设置默认选项
|
||
* 自动检测并支持所有公开的文章类型
|
||
*/
|
||
private static function set_default_options() {
|
||
// 获取所有公开的文章类型作为默认支持类型
|
||
$all_post_types = get_post_types(array('public' => true), 'names');
|
||
$default_supported_types = !empty($all_post_types) ? $all_post_types : array('post', 'page');
|
||
|
||
$default_options = array(
|
||
'ai_seo_api_key' => '',
|
||
'ai_seo_api_url' => '',
|
||
'ai_seo_auto_generate' => false,
|
||
'ai_seo_enable_logging' => true,
|
||
'ai_seo_cache_duration' => 3600, // 1小时
|
||
'ai_seo_max_retries' => 3,
|
||
'ai_seo_timeout' => 30,
|
||
'ai_seo_supported_post_types' => $default_supported_types,
|
||
'ai_seo_title_template' => '{title}',
|
||
'ai_seo_description_template' => '{description}',
|
||
'ai_seo_keywords_template' => '{keywords}',
|
||
'ai_seo_enable_frontend' => true,
|
||
'ai_seo_enable_admin_notices' => true
|
||
);
|
||
|
||
foreach ($default_options as $option_name => $default_value) {
|
||
if (get_option($option_name) === false) {
|
||
add_option($option_name, $default_value);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 创建必要的目录
|
||
*/
|
||
private static function create_directories() {
|
||
$upload_dir = wp_upload_dir();
|
||
$ai_seo_dir = $upload_dir['basedir'] . '/ai-seo';
|
||
|
||
if (!file_exists($ai_seo_dir)) {
|
||
wp_mkdir_p($ai_seo_dir);
|
||
|
||
// 创建.htaccess文件保护目录
|
||
$htaccess_content = "Order deny,allow\nDeny from all";
|
||
file_put_contents($ai_seo_dir . '/.htaccess', $htaccess_content);
|
||
|
||
// 创建index.php文件
|
||
file_put_contents($ai_seo_dir . '/index.php', '<?php // Silence is golden');
|
||
}
|
||
|
||
// 创建日志目录
|
||
$logs_dir = $ai_seo_dir . '/logs';
|
||
if (!file_exists($logs_dir)) {
|
||
wp_mkdir_p($logs_dir);
|
||
file_put_contents($logs_dir . '/index.php', '<?php // Silence is golden');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 安排计划任务
|
||
*/
|
||
private static function schedule_events() {
|
||
if (!wp_next_scheduled('ai_seo_cleanup_task')) {
|
||
wp_schedule_event(time(), 'daily', 'ai_seo_cleanup_task');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 删除插件选项
|
||
*/
|
||
private static function remove_options() {
|
||
$options_to_remove = array(
|
||
'ai_seo_api_key',
|
||
'ai_seo_api_url',
|
||
'ai_seo_auto_generate',
|
||
'ai_seo_enable_logging',
|
||
'ai_seo_cache_duration',
|
||
'ai_seo_max_retries',
|
||
'ai_seo_timeout',
|
||
'ai_seo_supported_post_types',
|
||
'ai_seo_title_template',
|
||
'ai_seo_description_template',
|
||
'ai_seo_keywords_template',
|
||
'ai_seo_enable_frontend',
|
||
'ai_seo_enable_admin_notices',
|
||
'ai_seo_activated_time',
|
||
'ai_seo_deactivated_time',
|
||
'ai_seo_db_version'
|
||
);
|
||
|
||
foreach ($options_to_remove as $option) {
|
||
delete_option($option);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 删除用户meta数据
|
||
*/
|
||
private static function remove_user_meta() {
|
||
global $wpdb;
|
||
|
||
$wpdb->query(
|
||
"DELETE FROM {$wpdb->usermeta}
|
||
WHERE meta_key LIKE 'ai_seo_%'"
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 删除文章meta数据
|
||
*/
|
||
private static function remove_post_meta() {
|
||
global $wpdb;
|
||
|
||
$meta_keys = array(
|
||
'_ai_seo_title',
|
||
'_ai_seo_description',
|
||
'_ai_seo_keywords',
|
||
'_ai_seo_generated_at',
|
||
'_ai_seo_last_updated'
|
||
);
|
||
|
||
foreach ($meta_keys as $meta_key) {
|
||
$wpdb->query(
|
||
$wpdb->prepare(
|
||
"DELETE FROM {$wpdb->postmeta} WHERE meta_key = %s",
|
||
$meta_key
|
||
)
|
||
);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 删除数据库表
|
||
*/
|
||
private static function drop_database_tables() {
|
||
global $wpdb;
|
||
|
||
$table_name = $wpdb->prefix . 'ai_seo_logs';
|
||
$wpdb->query("DROP TABLE IF EXISTS $table_name");
|
||
}
|
||
|
||
/**
|
||
* 检查系统要求
|
||
*/
|
||
public static function check_requirements() {
|
||
$requirements = array(
|
||
'php_version' => '7.4',
|
||
'wp_version' => '5.0',
|
||
'extensions' => array('curl', 'json')
|
||
);
|
||
|
||
$errors = array();
|
||
|
||
// 检查PHP版本
|
||
if (version_compare(PHP_VERSION, $requirements['php_version'], '<')) {
|
||
$errors[] = sprintf(
|
||
'需要PHP版本 %s 或更高,当前版本:%s',
|
||
$requirements['php_version'],
|
||
PHP_VERSION
|
||
);
|
||
}
|
||
|
||
// 检查WordPress版本
|
||
global $wp_version;
|
||
if (version_compare($wp_version, $requirements['wp_version'], '<')) {
|
||
$errors[] = sprintf(
|
||
'需要WordPress版本 %s 或更高,当前版本:%s',
|
||
$requirements['wp_version'],
|
||
$wp_version
|
||
);
|
||
}
|
||
|
||
// 检查PHP扩展
|
||
foreach ($requirements['extensions'] as $extension) {
|
||
if (!extension_loaded($extension)) {
|
||
$errors[] = sprintf('需要PHP扩展:%s', $extension);
|
||
}
|
||
}
|
||
|
||
return $errors;
|
||
}
|
||
|
||
/**
|
||
* 数据库升级
|
||
*/
|
||
public static function upgrade_database() {
|
||
$current_version = get_option('ai_seo_db_version', '0');
|
||
|
||
if (version_compare($current_version, '1.0', '<')) {
|
||
self::create_database_tables();
|
||
}
|
||
|
||
// 未来版本的升级逻辑可以在这里添加
|
||
}
|
||
|
||
/**
|
||
* 创建示例内容
|
||
*/
|
||
public static function create_sample_content() {
|
||
// 可以在这里创建示例文章或页面
|
||
// 用于演示插件功能
|
||
}
|
||
|
||
/**
|
||
* 导入默认设置
|
||
*/
|
||
public static function import_default_settings() {
|
||
$settings_file = AI_SEO_PLUGIN_PATH . 'config/default-settings.json';
|
||
|
||
if (file_exists($settings_file)) {
|
||
$settings = json_decode(file_get_contents($settings_file), true);
|
||
|
||
if ($settings) {
|
||
foreach ($settings as $key => $value) {
|
||
update_option($key, $value);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// 注册激活、停用和卸载钩子
|
||
register_activation_hook(__FILE__, array('AiSeoInstaller', 'activate'));
|
||
register_deactivation_hook(__FILE__, array('AiSeoInstaller', 'deactivate'));
|
||
register_uninstall_hook(__FILE__, array('AiSeoInstaller', 'uninstall'));
|
||
|
||
// 检查数据库版本并升级
|
||
add_action('plugins_loaded', function() {
|
||
$current_version = get_option('ai_seo_db_version', '0');
|
||
if (version_compare($current_version, '1.0', '<')) {
|
||
AiSeoInstaller::upgrade_database();
|
||
}
|
||
});
|
||
|
||
// 显示激活通知
|
||
add_action('admin_notices', function() {
|
||
if (get_transient('ai_seo_activation_notice')) {
|
||
echo '<div class="notice notice-success is-dismissible">';
|
||
echo '<p><strong>AI SEO Generator</strong> 插件已成功激活!请前往 <a href="' . admin_url('options-general.php?page=ai-seo-generator') . '">设置页面</a> 配置API密钥。</p>';
|
||
echo '</div>';
|
||
delete_transient('ai_seo_activation_notice');
|
||
}
|
||
});
|
||
|
||
?>
|