From 4002fc8f60102cdccc08577b078dc6cac78ff449 Mon Sep 17 00:00:00 2001 From: nanhaoluo <3075912108@qq.com> Date: Thu, 15 Jan 2026 15:45:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E7=83=AD=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增热更新开关设置(位于高级设置-杂项) - 主题文件更新后自动清理所有缓存(对象缓存、主题更新缓存、transient缓存) - 后台显示更新通知,提示版本变化 - 可选在前台显示更新通知(仅管理员可见) - 提供手动清理缓存按钮 - 保留最近10条更新历史记录 --- functions.php | 190 ++++++++++++++++++++++++++++++++++++++++++++++++++ settings.php | 86 +++++++++++++++++++++++ 2 files changed, 276 insertions(+) diff --git a/functions.php b/functions.php index e36a72b..9e88a91 100644 --- a/functions.php +++ b/functions.php @@ -148,6 +148,196 @@ switch ($argon_update_source) { ); } +//热更新功能 +function argon_hot_reload_init() { + // 检查是否启用热更新 + if (get_option('argon_enable_hot_reload', 'false') != 'true') { + return; + } + + // 记录当前主题版本 + $current_version = $GLOBALS['theme_version']; + $last_known_version = get_option('argon_hot_reload_last_version', ''); + + // 如果版本发生变化,清理缓存并记录更新 + if (!empty($last_known_version) && $last_known_version !== $current_version) { + argon_clear_all_caches(); + argon_record_hot_reload_update($last_known_version, $current_version); + } + + // 更新记录的版本 + update_option('argon_hot_reload_last_version', $current_version); +} + +// 清理所有缓存 +function argon_clear_all_caches() { + // 清理 WordPress 对象缓存 + if (function_exists('wp_cache_flush')) { + wp_cache_flush(); + } + + // 清理主题更新检查器缓存 + delete_site_transient('update_themes'); + delete_transient('argon_update_info'); + + // 清理可能存在的其他主题相关缓存 + global $wpdb; + $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_argon_%'"); + $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_timeout_argon_%'"); + $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_site_transient_puc_%'"); + $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_site_transient_timeout_puc_%'"); + + // 触发缓存清理钩子,允许其他插件响应 + do_action('argon_cache_cleared'); +} + +// 记录热更新 +function argon_record_hot_reload_update($old_version, $new_version) { + $update_history = get_option('argon_hot_reload_history', array()); + + // 添加新的更新记录 + $update_history[] = array( + 'old_version' => $old_version, + 'new_version' => $new_version, + 'time' => current_time('timestamp'), + 'dismissed' => false + ); + + // 只保留最近 10 条记录 + if (count($update_history) > 10) { + $update_history = array_slice($update_history, -10); + } + + update_option('argon_hot_reload_history', $update_history); +} + +// 获取未读的更新通知 +function argon_get_pending_update_notices() { + $update_history = get_option('argon_hot_reload_history', array()); + $pending = array(); + + foreach ($update_history as $index => $update) { + if (empty($update['dismissed'])) { + $pending[$index] = $update; + } + } + + return $pending; +} + +// 标记更新通知为已读 +function argon_dismiss_update_notice() { + check_ajax_referer('argon_dismiss_update_notice', 'nonce'); + + $index = isset($_POST['index']) ? intval($_POST['index']) : -1; + $update_history = get_option('argon_hot_reload_history', array()); + + if ($index === -1) { + // 标记所有为已读 + foreach ($update_history as &$update) { + $update['dismissed'] = true; + } + } else if (isset($update_history[$index])) { + $update_history[$index]['dismissed'] = true; + } + + update_option('argon_hot_reload_history', $update_history); + wp_send_json_success(); +} +add_action('wp_ajax_argon_dismiss_update_notice', 'argon_dismiss_update_notice'); + +// 手动触发缓存清理 +function argon_manual_clear_cache() { + check_ajax_referer('argon_clear_cache', 'nonce'); + + if (!current_user_can('manage_options')) { + wp_send_json_error(__('权限不足', 'argon')); + } + + argon_clear_all_caches(); + wp_send_json_success(__('缓存已清理', 'argon')); +} +add_action('wp_ajax_argon_clear_cache', 'argon_manual_clear_cache'); + +// 在后台显示更新通知 +function argon_admin_hot_reload_notice() { + if (get_option('argon_enable_hot_reload', 'false') != 'true') { + return; + } + + $pending_notices = argon_get_pending_update_notices(); + if (empty($pending_notices)) { + return; + } + + $latest = end($pending_notices); + $latest_index = key($pending_notices); + ?> +
+
+ ' . esc_html($latest['old_version']) . '',
+ '' . esc_html($latest['new_version']) . ''
+ ); ?>
+
+ ' . esc_html($latest['old_version']) . '',
+ '' . esc_html($latest['new_version']) . ''
+ ); ?>
+