feat: 添加强制刷新缓存功能
- 在设置页杂项部分添加强制刷新缓存按钮 - 启用后所有资源文件(CSS/JS)将附加时间戳参数 - 1小时后自动关闭,避免服务器压力 - 解决手机端浏览器缓存导致样式或功能无法更新的问题 - 支持实时倒计时显示剩余时间
This commit is contained in:
@@ -30,7 +30,8 @@
|
||||
</div>
|
||||
|
||||
<!-- 加载主题核心 JS -->
|
||||
<script src="<?php echo $GLOBALS['assets_path']; ?>/argontheme.js?v=<?php echo $GLOBALS['theme_version']; ?>"></script>
|
||||
<?php $assets_version = function_exists('argon_get_assets_version') ? argon_get_assets_version() : $GLOBALS['theme_version']; ?>
|
||||
<script src="<?php echo $GLOBALS['assets_path']; ?>/argontheme.js?v=<?php echo $assets_version; ?>"></script>
|
||||
|
||||
<?php if (get_option('argon_math_render') == 'mathjax3') { /*Mathjax V3*/?>
|
||||
|
||||
|
||||
@@ -35,6 +35,93 @@ $GLOBALS['theme_version'] = $argon_version;
|
||||
// 强制使用本地资源,避免 CDN 加载问题
|
||||
$GLOBALS['assets_path'] = get_bloginfo('template_url');
|
||||
|
||||
// ==================== 强制刷新缓存功能 ====================
|
||||
|
||||
/**
|
||||
* 检查强制刷新缓存是否启用
|
||||
* 启用后 1 小时自动关闭
|
||||
*/
|
||||
function argon_is_force_refresh_enabled() {
|
||||
$enabled_time = get_option('argon_force_refresh_enabled_time', 0);
|
||||
if ($enabled_time == 0) {
|
||||
return false;
|
||||
}
|
||||
// 检查是否超过 1 小时
|
||||
if (time() - $enabled_time > 3600) {
|
||||
// 自动关闭
|
||||
update_option('argon_force_refresh_enabled_time', 0);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取资源版本号
|
||||
* 如果启用了强制刷新,返回当前时间戳
|
||||
*/
|
||||
function argon_get_assets_version() {
|
||||
if (argon_is_force_refresh_enabled()) {
|
||||
return $GLOBALS['theme_version'] . '.' . time();
|
||||
}
|
||||
return $GLOBALS['theme_version'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 启用强制刷新缓存
|
||||
*/
|
||||
function argon_enable_force_refresh() {
|
||||
check_ajax_referer('argon_force_refresh', 'nonce');
|
||||
|
||||
if (!current_user_can('manage_options')) {
|
||||
wp_send_json_error(__('权限不足', 'argon'));
|
||||
}
|
||||
|
||||
update_option('argon_force_refresh_enabled_time', time());
|
||||
wp_send_json_success(array(
|
||||
'message' => __('强制刷新已启用,将在 1 小时后自动关闭', 'argon'),
|
||||
'expires_at' => time() + 3600
|
||||
));
|
||||
}
|
||||
add_action('wp_ajax_argon_enable_force_refresh', 'argon_enable_force_refresh');
|
||||
|
||||
/**
|
||||
* 关闭强制刷新缓存
|
||||
*/
|
||||
function argon_disable_force_refresh() {
|
||||
check_ajax_referer('argon_force_refresh', 'nonce');
|
||||
|
||||
if (!current_user_can('manage_options')) {
|
||||
wp_send_json_error(__('权限不足', 'argon'));
|
||||
}
|
||||
|
||||
update_option('argon_force_refresh_enabled_time', 0);
|
||||
wp_send_json_success(array(
|
||||
'message' => __('强制刷新已关闭', 'argon')
|
||||
));
|
||||
}
|
||||
add_action('wp_ajax_argon_disable_force_refresh', 'argon_disable_force_refresh');
|
||||
|
||||
/**
|
||||
* 获取强制刷新状态
|
||||
*/
|
||||
function argon_get_force_refresh_status() {
|
||||
check_ajax_referer('argon_force_refresh', 'nonce');
|
||||
|
||||
$enabled_time = get_option('argon_force_refresh_enabled_time', 0);
|
||||
$is_enabled = argon_is_force_refresh_enabled();
|
||||
$remaining = 0;
|
||||
|
||||
if ($is_enabled && $enabled_time > 0) {
|
||||
$remaining = max(0, 3600 - (time() - $enabled_time));
|
||||
}
|
||||
|
||||
wp_send_json_success(array(
|
||||
'enabled' => $is_enabled,
|
||||
'remaining' => $remaining
|
||||
));
|
||||
}
|
||||
add_action('wp_ajax_argon_get_force_refresh_status', 'argon_get_force_refresh_status');
|
||||
|
||||
//翻译 Hook
|
||||
function argon_locate_filter($locate){
|
||||
if (substr($locate, 0, 2) == 'zh'){
|
||||
|
||||
13
header.php
13
header.php
@@ -265,12 +265,15 @@
|
||||
<?php endif; ?>
|
||||
|
||||
<?php
|
||||
// 获取资源版本号(支持强制刷新)
|
||||
$assets_version = function_exists('argon_get_assets_version') ? argon_get_assets_version() : $GLOBALS['theme_version'];
|
||||
|
||||
// CSS 加载 - 添加 media 属性确保移动端正确加载
|
||||
wp_enqueue_style("argon_css_merged", $GLOBALS['assets_path'] . "/assets/argon_css_merged.css", array(), $GLOBALS['theme_version'], 'all');
|
||||
wp_enqueue_style("style", $GLOBALS['assets_path'] . "/style.css", array('argon_css_merged'), $GLOBALS['theme_version'], 'all');
|
||||
wp_enqueue_style("argon_css_merged", $GLOBALS['assets_path'] . "/assets/argon_css_merged.css", array(), $assets_version, 'all');
|
||||
wp_enqueue_style("style", $GLOBALS['assets_path'] . "/style.css", array('argon_css_merged'), $assets_version, 'all');
|
||||
|
||||
// 集成外部资源备用机制
|
||||
wp_enqueue_script("resource_loader", $GLOBALS['assets_path'] . "/assets/vendor/external/resource-loader.js", array(), $GLOBALS['theme_version'], false);
|
||||
wp_enqueue_script("resource_loader", $GLOBALS['assets_path'] . "/assets/vendor/external/resource-loader.js", array(), $assets_version, false);
|
||||
|
||||
if (get_option('argon_disable_googlefont') != 'true') {
|
||||
// 使用备用机制加载Google Fonts
|
||||
@@ -284,7 +287,7 @@
|
||||
}
|
||||
|
||||
// 加载 argon_js_merged(包含 jQuery 和其他库)- 在头部同步加载以确保后续脚本可用
|
||||
wp_enqueue_script("argon_js_merged", $GLOBALS['assets_path'] . "/assets/argon_js_merged.js", array(), $GLOBALS['theme_version'], false);
|
||||
wp_enqueue_script("argon_js_merged", $GLOBALS['assets_path'] . "/assets/argon_js_merged.js", array(), $assets_version, false);
|
||||
?>
|
||||
|
||||
<?php wp_head(); ?>
|
||||
@@ -323,7 +326,7 @@
|
||||
</script>
|
||||
|
||||
<!-- Argon 修复补丁 - 必须在 wp_head() 之后立即执行 -->
|
||||
<script src="<?php echo get_template_directory_uri(); ?>/assets/js/argon.min.js?ver=<?php echo $GLOBALS['theme_version']; ?>"></script>
|
||||
<script src="<?php echo get_template_directory_uri(); ?>/assets/js/argon.min.js?ver=<?php echo $assets_version; ?>"></script>
|
||||
|
||||
<?php $GLOBALS['wp_path'] = get_option('argon_wp_path') == '' ? '/' : get_option('argon_wp_path'); ?>
|
||||
|
||||
|
||||
207
settings.php
207
settings.php
@@ -4731,6 +4731,213 @@ window.pjaxLoaded = function(){
|
||||
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
|
||||
<th><label><?php _e('强制刷新缓存', 'argon');?></label></th>
|
||||
|
||||
<td>
|
||||
|
||||
<?php
|
||||
$force_refresh_enabled = function_exists('argon_is_force_refresh_enabled') ? argon_is_force_refresh_enabled() : false;
|
||||
$enabled_time = get_option('argon_force_refresh_enabled_time', 0);
|
||||
$remaining = 0;
|
||||
if ($force_refresh_enabled && $enabled_time > 0) {
|
||||
$remaining = max(0, 3600 - (time() - $enabled_time));
|
||||
}
|
||||
?>
|
||||
|
||||
<div id="argon_force_refresh_container">
|
||||
|
||||
<div id="argon_force_refresh_status" style="margin-bottom:15px;padding:12px 15px;border-radius:6px;<?php echo $force_refresh_enabled ? 'background:#d4edda;border:1px solid #c3e6cb;' : 'background:#f8f9fa;border:1px solid #e9ecef;'; ?>">
|
||||
|
||||
<span id="argon_force_refresh_status_text">
|
||||
|
||||
<?php if ($force_refresh_enabled): ?>
|
||||
|
||||
<span style="color:#155724;">✓ <?php _e('强制刷新已启用', 'argon'); ?></span>
|
||||
|
||||
<span id="argon_force_refresh_countdown" style="margin-left:10px;color:#666;"><?php echo sprintf(__('剩余 %s', 'argon'), '<span id="argon_countdown_time">' . floor($remaining / 60) . ':' . str_pad($remaining % 60, 2, '0', STR_PAD_LEFT) . '</span>'); ?></span>
|
||||
|
||||
<?php else: ?>
|
||||
|
||||
<span style="color:#666;"><?php _e('强制刷新未启用', 'argon'); ?></span>
|
||||
|
||||
<?php endif; ?>
|
||||
|
||||
</span>
|
||||
|
||||
</div>
|
||||
|
||||
<button type="button" class="button <?php echo $force_refresh_enabled ? 'button-secondary' : 'button-primary'; ?>" id="argon_force_refresh_btn">
|
||||
|
||||
<?php echo $force_refresh_enabled ? __('关闭强制刷新', 'argon') : __('启用强制刷新 (1小时)', 'argon'); ?>
|
||||
|
||||
</button>
|
||||
|
||||
</div>
|
||||
|
||||
<p class="description" style="margin-top:10px;">
|
||||
|
||||
<?php _e('启用后,所有访客在刷新页面时将强制重新获取所有资源文件(CSS、JS等),解决手机端缓存导致的样式或功能异常问题。', 'argon'); ?><br/>
|
||||
|
||||
<?php _e('该功能会在 1 小时后自动关闭,以避免对服务器造成持续压力。', 'argon'); ?>
|
||||
|
||||
</p>
|
||||
|
||||
<script>
|
||||
|
||||
jQuery(document).ready(function($) {
|
||||
|
||||
var isEnabled = <?php echo $force_refresh_enabled ? 'true' : 'false'; ?>;
|
||||
|
||||
var remainingSeconds = <?php echo $remaining; ?>;
|
||||
|
||||
var countdownTimer = null;
|
||||
|
||||
|
||||
|
||||
function updateCountdown() {
|
||||
|
||||
if (remainingSeconds <= 0) {
|
||||
|
||||
clearInterval(countdownTimer);
|
||||
|
||||
isEnabled = false;
|
||||
|
||||
updateUI();
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
remainingSeconds--;
|
||||
|
||||
var mins = Math.floor(remainingSeconds / 60);
|
||||
|
||||
var secs = remainingSeconds % 60;
|
||||
|
||||
$('#argon_countdown_time').text(mins + ':' + (secs < 10 ? '0' : '') + secs);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function updateUI() {
|
||||
|
||||
var statusDiv = $('#argon_force_refresh_status');
|
||||
|
||||
var statusText = $('#argon_force_refresh_status_text');
|
||||
|
||||
var btn = $('#argon_force_refresh_btn');
|
||||
|
||||
|
||||
|
||||
if (isEnabled) {
|
||||
|
||||
statusDiv.css({'background': '#d4edda', 'border': '1px solid #c3e6cb'});
|
||||
|
||||
var mins = Math.floor(remainingSeconds / 60);
|
||||
|
||||
var secs = remainingSeconds % 60;
|
||||
|
||||
statusText.html('<span style="color:#155724;">✓ <?php _e('强制刷新已启用', 'argon'); ?></span><span id="argon_force_refresh_countdown" style="margin-left:10px;color:#666;"><?php _e('剩余', 'argon'); ?> <span id="argon_countdown_time">' + mins + ':' + (secs < 10 ? '0' : '') + secs + '</span></span>');
|
||||
|
||||
btn.removeClass('button-primary').addClass('button-secondary').text('<?php _e('关闭强制刷新', 'argon'); ?>');
|
||||
|
||||
if (!countdownTimer) {
|
||||
|
||||
countdownTimer = setInterval(updateCountdown, 1000);
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
statusDiv.css({'background': '#f8f9fa', 'border': '1px solid #e9ecef'});
|
||||
|
||||
statusText.html('<span style="color:#666;"><?php _e('强制刷新未启用', 'argon'); ?></span>');
|
||||
|
||||
btn.removeClass('button-secondary').addClass('button-primary').text('<?php _e('启用强制刷新 (1小时)', 'argon'); ?>');
|
||||
|
||||
if (countdownTimer) {
|
||||
|
||||
clearInterval(countdownTimer);
|
||||
|
||||
countdownTimer = null;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 初始化倒计时
|
||||
|
||||
if (isEnabled && remainingSeconds > 0) {
|
||||
|
||||
countdownTimer = setInterval(updateCountdown, 1000);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
$('#argon_force_refresh_btn').on('click', function() {
|
||||
|
||||
var btn = $(this);
|
||||
|
||||
btn.prop('disabled', true);
|
||||
|
||||
|
||||
|
||||
var action = isEnabled ? 'argon_disable_force_refresh' : 'argon_enable_force_refresh';
|
||||
|
||||
|
||||
|
||||
$.post(ajaxurl, {
|
||||
|
||||
action: action,
|
||||
|
||||
nonce: '<?php echo wp_create_nonce('argon_force_refresh'); ?>'
|
||||
|
||||
}, function(response) {
|
||||
|
||||
btn.prop('disabled', false);
|
||||
|
||||
if (response.success) {
|
||||
|
||||
isEnabled = !isEnabled;
|
||||
|
||||
if (isEnabled) {
|
||||
|
||||
remainingSeconds = 3600;
|
||||
|
||||
} else {
|
||||
|
||||
remainingSeconds = 0;
|
||||
|
||||
}
|
||||
|
||||
updateUI();
|
||||
|
||||
} else {
|
||||
|
||||
alert(response.data || '<?php _e('操作失败', 'argon'); ?>');
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
</script>
|
||||
|
||||
</td>
|
||||
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
|
||||
<th><label><?php _e('调试控制台', 'argon');?></label></th>
|
||||
|
||||
Reference in New Issue
Block a user