|
|
<?php
|
|
|
/**
|
|
|
* HTML Compression WordPress Integration
|
|
|
* WordPress HTML压缩功能集成
|
|
|
*
|
|
|
* @package Nenghui Energy Theme
|
|
|
* @since 1.0.0
|
|
|
*/
|
|
|
|
|
|
// 防止直接访问
|
|
|
if (!defined('ABSPATH')) {
|
|
|
exit;
|
|
|
}
|
|
|
|
|
|
// 引入HTML压缩核心类
|
|
|
require_once get_template_directory() . '/inc/html-compressor.php';
|
|
|
|
|
|
class NenghuiHTMLCompressionIntegration {
|
|
|
|
|
|
/**
|
|
|
* HTML压缩器实例
|
|
|
*/
|
|
|
private $compressor;
|
|
|
|
|
|
/**
|
|
|
* 压缩统计数据
|
|
|
*/
|
|
|
private $compression_stats = array();
|
|
|
|
|
|
/**
|
|
|
* 是否启用压缩
|
|
|
*/
|
|
|
private $compression_enabled = true;
|
|
|
|
|
|
/**
|
|
|
* 排除压缩的页面
|
|
|
*/
|
|
|
private $excluded_pages = array();
|
|
|
|
|
|
/**
|
|
|
* 构造函数
|
|
|
*/
|
|
|
public function __construct() {
|
|
|
// 初始化HTML压缩器
|
|
|
$this->compressor = new NenghuiHTMLCompressor();
|
|
|
|
|
|
// 设置压缩选项
|
|
|
$this->setupCompressionOptions();
|
|
|
|
|
|
// 初始化WordPress钩子
|
|
|
$this->initHooks();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置压缩选项
|
|
|
*/
|
|
|
private function setupCompressionOptions() {
|
|
|
$options = array(
|
|
|
'remove_comments' => get_theme_mod('nenghui_compression_remove_comments', true),
|
|
|
'remove_whitespace' => get_theme_mod('nenghui_compression_remove_whitespace', true),
|
|
|
'compress_css' => get_theme_mod('nenghui_compression_compress_css', true),
|
|
|
'compress_js' => get_theme_mod('nenghui_compression_compress_js', true),
|
|
|
'preserve_line_breaks' => get_theme_mod('nenghui_compression_preserve_line_breaks', false),
|
|
|
'preserve_pre_content' => true,
|
|
|
'preserve_textarea_content' => true,
|
|
|
'preserve_script_content' => true,
|
|
|
'preserve_style_content' => true
|
|
|
);
|
|
|
|
|
|
$this->compressor->setOptions($options);
|
|
|
|
|
|
// 设置是否启用压缩
|
|
|
$this->compression_enabled = get_theme_mod('nenghui_compression_enabled', true);
|
|
|
|
|
|
// 设置排除的页面
|
|
|
$excluded_pages = get_theme_mod('nenghui_compression_excluded_pages', '');
|
|
|
if (!empty($excluded_pages)) {
|
|
|
$this->excluded_pages = array_map('trim', explode(',', $excluded_pages));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 初始化WordPress钩子
|
|
|
*/
|
|
|
private function initHooks() {
|
|
|
// 只在前端启用压缩
|
|
|
if (!is_admin() && $this->compression_enabled) {
|
|
|
add_action('template_redirect', array($this, 'startOutputBuffering'), 1);
|
|
|
add_action('wp_footer', array($this, 'addCompressionStats'), 999);
|
|
|
}
|
|
|
|
|
|
// 添加自定义器选项
|
|
|
add_action('customize_register', array($this, 'addCustomizerOptions'));
|
|
|
|
|
|
// 添加管理员菜单
|
|
|
add_action('admin_menu', array($this, 'addAdminMenu'));
|
|
|
|
|
|
// 添加AJAX处理
|
|
|
add_action('wp_ajax_nenghui_test_compression', array($this, 'testCompression'));
|
|
|
add_action('wp_ajax_nenghui_clear_compression_cache', array($this, 'clearCompressionCache'));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 开始输出缓冲
|
|
|
*/
|
|
|
public function startOutputBuffering() {
|
|
|
// 检查是否应该排除当前页面
|
|
|
if ($this->shouldExcludePage()) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 检查是否为AJAX请求
|
|
|
if (defined('DOING_AJAX') && DOING_AJAX) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 检查是否为REST API请求
|
|
|
if (defined('REST_REQUEST') && REST_REQUEST) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 开始输出缓冲
|
|
|
ob_start(array($this, 'compressOutput'));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 压缩输出内容
|
|
|
*
|
|
|
* @param string $buffer
|
|
|
* @return string
|
|
|
*/
|
|
|
public function compressOutput($buffer) {
|
|
|
// 检查缓冲区是否为空
|
|
|
if (empty($buffer)) {
|
|
|
return $buffer;
|
|
|
}
|
|
|
|
|
|
// 检查是否为HTML内容
|
|
|
if (!$this->isHTMLContent($buffer)) {
|
|
|
return $buffer;
|
|
|
}
|
|
|
|
|
|
// 记录原始大小
|
|
|
$original_size = strlen($buffer);
|
|
|
|
|
|
try {
|
|
|
// 执行压缩
|
|
|
$compressed_buffer = $this->compressor->compress($buffer);
|
|
|
|
|
|
// 记录压缩统计
|
|
|
$this->compression_stats = $this->compressor->getCompressionStats($buffer, $compressed_buffer);
|
|
|
|
|
|
// 添加压缩信息注释(仅在开发模式下)
|
|
|
if (defined('WP_DEBUG') && WP_DEBUG) {
|
|
|
$compressed_buffer = $this->addCompressionComment($compressed_buffer);
|
|
|
}
|
|
|
|
|
|
return $compressed_buffer;
|
|
|
|
|
|
} catch (Exception $e) {
|
|
|
// 压缩失败时返回原始内容
|
|
|
error_log('Nenghui HTML Compression Error: ' . $e->getMessage());
|
|
|
return $buffer;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 检查是否应该排除当前页面
|
|
|
*
|
|
|
* @return bool
|
|
|
*/
|
|
|
private function shouldExcludePage() {
|
|
|
global $wp;
|
|
|
|
|
|
// 获取当前页面URL
|
|
|
$current_url = home_url($wp->request);
|
|
|
|
|
|
// 检查排除列表
|
|
|
foreach ($this->excluded_pages as $excluded_page) {
|
|
|
if (strpos($current_url, $excluded_page) !== false) {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 检查特殊页面
|
|
|
if (is_feed() || is_robots() || is_trackback()) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
// 检查XML内容
|
|
|
if (strpos($_SERVER['REQUEST_URI'], '.xml') !== false) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 检查是否为HTML内容
|
|
|
*
|
|
|
* @param string $buffer
|
|
|
* @return bool
|
|
|
*/
|
|
|
private function isHTMLContent($buffer) {
|
|
|
// 检查内容类型
|
|
|
$headers = headers_list();
|
|
|
foreach ($headers as $header) {
|
|
|
if (stripos($header, 'content-type') !== false) {
|
|
|
if (stripos($header, 'text/html') === false) {
|
|
|
return false;
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 检查是否包含HTML标签
|
|
|
return preg_match('/<html|<head|<body|<!DOCTYPE/i', $buffer);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 添加压缩信息注释
|
|
|
*
|
|
|
* @param string $buffer
|
|
|
* @return string
|
|
|
*/
|
|
|
private function addCompressionComment($buffer) {
|
|
|
if (!empty($this->compression_stats)) {
|
|
|
$comment = sprintf(
|
|
|
"\n<!-- Nenghui HTML Compression: Original: %s bytes, Compressed: %s bytes, Saved: %s bytes (%.2f%%) -->",
|
|
|
number_format($this->compression_stats['original_size']),
|
|
|
number_format($this->compression_stats['compressed_size']),
|
|
|
number_format($this->compression_stats['saved_bytes']),
|
|
|
$this->compression_stats['compression_ratio']
|
|
|
);
|
|
|
|
|
|
// 在</body>标签前插入注释
|
|
|
$buffer = str_replace('</body>', $comment . "\n</body>", $buffer);
|
|
|
}
|
|
|
|
|
|
return $buffer;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 添加压缩统计信息到页面底部
|
|
|
*/
|
|
|
public function addCompressionStats() {
|
|
|
if (defined('WP_DEBUG') && WP_DEBUG && !empty($this->compression_stats)) {
|
|
|
echo '<script>';
|
|
|
echo 'console.log("Nenghui HTML Compression Stats:", ' . json_encode($this->compression_stats) . ');';
|
|
|
echo '</script>';
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 添加自定义器选项
|
|
|
*
|
|
|
* @param WP_Customize_Manager $wp_customize
|
|
|
*/
|
|
|
public function addCustomizerOptions($wp_customize) {
|
|
|
// 添加压缩设置面板
|
|
|
$wp_customize->add_panel('nenghui_performance_panel', array(
|
|
|
'title' => __('性能优化', 'nenghui-energy-theme'),
|
|
|
'description' => __('网站性能优化相关设置', 'nenghui-energy-theme'),
|
|
|
'priority' => 160,
|
|
|
));
|
|
|
|
|
|
// 添加HTML压缩设置部分
|
|
|
$wp_customize->add_section('nenghui_compression_settings', array(
|
|
|
'title' => __('HTML压缩设置', 'nenghui-energy-theme'),
|
|
|
'description' => __('通过压缩HTML代码来提升网站加载速度', 'nenghui-energy-theme'),
|
|
|
'panel' => 'nenghui_performance_panel',
|
|
|
'priority' => 10,
|
|
|
));
|
|
|
|
|
|
// 启用压缩 - 主开关
|
|
|
$wp_customize->add_setting('nenghui_compression_enabled', array(
|
|
|
'default' => true,
|
|
|
'sanitize_callback' => 'wp_validate_boolean',
|
|
|
'transport' => 'refresh',
|
|
|
));
|
|
|
|
|
|
$wp_customize->add_control('nenghui_compression_enabled', array(
|
|
|
'label' => __('启用HTML压缩', 'nenghui-energy-theme'),
|
|
|
'description' => __('开启后将自动压缩HTML输出,显著减少页面大小并提升加载速度', 'nenghui-energy-theme'),
|
|
|
'section' => 'nenghui_compression_settings',
|
|
|
'type' => 'checkbox',
|
|
|
'priority' => 10,
|
|
|
));
|
|
|
|
|
|
// 添加分隔符
|
|
|
$wp_customize->add_setting('nenghui_compression_separator_1', array(
|
|
|
'sanitize_callback' => 'wp_kses_post',
|
|
|
));
|
|
|
|
|
|
$wp_customize->add_control(new WP_Customize_Control($wp_customize, 'nenghui_compression_separator_1', array(
|
|
|
'type' => 'hidden',
|
|
|
'section' => 'nenghui_compression_settings',
|
|
|
'priority' => 15,
|
|
|
)));
|
|
|
|
|
|
// 移除注释
|
|
|
$wp_customize->add_setting('nenghui_compression_remove_comments', array(
|
|
|
'default' => true,
|
|
|
'sanitize_callback' => 'wp_validate_boolean',
|
|
|
));
|
|
|
|
|
|
$wp_customize->add_control('nenghui_compression_remove_comments', array(
|
|
|
'label' => __('移除HTML注释', 'nenghui-energy-theme'),
|
|
|
'description' => __('移除HTML中的注释内容。注意:某些插件可能依赖HTML注释', 'nenghui-energy-theme'),
|
|
|
'section' => 'nenghui_compression_settings',
|
|
|
'type' => 'checkbox',
|
|
|
'priority' => 20,
|
|
|
'active_callback' => function() {
|
|
|
return get_theme_mod('nenghui_compression_enabled', true);
|
|
|
},
|
|
|
));
|
|
|
|
|
|
// 移除空白字符
|
|
|
$wp_customize->add_setting('nenghui_compression_remove_whitespace', array(
|
|
|
'default' => true,
|
|
|
'sanitize_callback' => 'wp_validate_boolean',
|
|
|
));
|
|
|
|
|
|
$wp_customize->add_control('nenghui_compression_remove_whitespace', array(
|
|
|
'label' => __('压缩空白字符', 'nenghui-energy-theme'),
|
|
|
'description' => __('移除多余的空格、制表符和换行符,保留必要的空白字符', 'nenghui-energy-theme'),
|
|
|
'section' => 'nenghui_compression_settings',
|
|
|
'type' => 'checkbox',
|
|
|
'priority' => 30,
|
|
|
'active_callback' => function() {
|
|
|
return get_theme_mod('nenghui_compression_enabled', true);
|
|
|
},
|
|
|
));
|
|
|
|
|
|
// 压缩CSS
|
|
|
$wp_customize->add_setting('nenghui_compression_compress_css', array(
|
|
|
'default' => true,
|
|
|
'sanitize_callback' => 'wp_validate_boolean',
|
|
|
));
|
|
|
|
|
|
$wp_customize->add_control('nenghui_compression_compress_css', array(
|
|
|
'label' => __('压缩内联CSS', 'nenghui-energy-theme'),
|
|
|
'description' => __('压缩<style>标签内的CSS代码,移除注释和多余空白', 'nenghui-energy-theme'),
|
|
|
'section' => 'nenghui_compression_settings',
|
|
|
'type' => 'checkbox',
|
|
|
'priority' => 40,
|
|
|
'active_callback' => function() {
|
|
|
return get_theme_mod('nenghui_compression_enabled', true);
|
|
|
},
|
|
|
));
|
|
|
|
|
|
// 压缩JavaScript
|
|
|
$wp_customize->add_setting('nenghui_compression_compress_js', array(
|
|
|
'default' => true,
|
|
|
'sanitize_callback' => 'wp_validate_boolean',
|
|
|
));
|
|
|
|
|
|
$wp_customize->add_control('nenghui_compression_compress_js', array(
|
|
|
'label' => __('压缩内联JavaScript', 'nenghui-energy-theme'),
|
|
|
'description' => __('压缩<script>标签内的JavaScript代码,移除注释和多余空白', 'nenghui-energy-theme'),
|
|
|
'section' => 'nenghui_compression_settings',
|
|
|
'type' => 'checkbox',
|
|
|
'priority' => 50,
|
|
|
'active_callback' => function() {
|
|
|
return get_theme_mod('nenghui_compression_enabled', true);
|
|
|
},
|
|
|
));
|
|
|
|
|
|
// 添加分隔符
|
|
|
$wp_customize->add_setting('nenghui_compression_separator_2', array(
|
|
|
'sanitize_callback' => 'wp_kses_post',
|
|
|
));
|
|
|
|
|
|
$wp_customize->add_control(new WP_Customize_Control($wp_customize, 'nenghui_compression_separator_2', array(
|
|
|
'type' => 'hidden',
|
|
|
'section' => 'nenghui_compression_settings',
|
|
|
'priority' => 55,
|
|
|
)));
|
|
|
|
|
|
// 排除页面
|
|
|
$wp_customize->add_setting('nenghui_compression_excluded_pages', array(
|
|
|
'default' => 'wp-admin,wp-login,xmlrpc',
|
|
|
'sanitize_callback' => 'sanitize_textarea_field',
|
|
|
));
|
|
|
|
|
|
$wp_customize->add_control('nenghui_compression_excluded_pages', array(
|
|
|
'label' => __('排除的页面', 'nenghui-energy-theme'),
|
|
|
'description' => __('输入不需要压缩的页面URL片段,用逗号分隔。例如:wp-admin, wp-login, xmlrpc', 'nenghui-energy-theme'),
|
|
|
'section' => 'nenghui_compression_settings',
|
|
|
'type' => 'textarea',
|
|
|
'priority' => 60,
|
|
|
'active_callback' => function() {
|
|
|
return get_theme_mod('nenghui_compression_enabled', true);
|
|
|
},
|
|
|
));
|
|
|
|
|
|
// 添加性能提示部分
|
|
|
$wp_customize->add_section('nenghui_performance_tips', array(
|
|
|
'title' => __('性能优化建议', 'nenghui-energy-theme'),
|
|
|
'description' => __('其他提升网站性能的建议和技巧', 'nenghui-energy-theme'),
|
|
|
'panel' => 'nenghui_performance_panel',
|
|
|
'priority' => 20,
|
|
|
));
|
|
|
|
|
|
// 性能提示内容
|
|
|
$wp_customize->add_setting('nenghui_performance_tips_content', array(
|
|
|
'default' => '',
|
|
|
'sanitize_callback' => 'wp_kses_post',
|
|
|
));
|
|
|
|
|
|
$wp_customize->add_control(new WP_Customize_Control($wp_customize, 'nenghui_performance_tips_content', array(
|
|
|
'label' => __('性能优化提示', 'nenghui-energy-theme'),
|
|
|
'description' => __('
|
|
|
<div style="background: #f0f8ff; padding: 15px; border-radius: 5px; margin: 10px 0;">
|
|
|
<h4 style="margin-top: 0;">💡 性能优化建议:</h4>
|
|
|
<ul style="margin: 0; padding-left: 20px;">
|
|
|
<li>启用HTML压缩可减少20-40%的页面大小</li>
|
|
|
<li>建议同时使用缓存插件以获得最佳效果</li>
|
|
|
<li>定期检查压缩统计以监控性能提升</li>
|
|
|
<li>在生产环境中测试所有功能正常工作</li>
|
|
|
</ul>
|
|
|
<p style="margin-bottom: 0;"><strong>注意:</strong>某些插件可能与HTML压缩冲突,如遇问题请在排除页面中添加相关URL。</p>
|
|
|
</div>
|
|
|
', 'nenghui-energy-theme'),
|
|
|
'section' => 'nenghui_performance_tips',
|
|
|
'type' => 'hidden',
|
|
|
)));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 添加管理员菜单
|
|
|
*/
|
|
|
public function addAdminMenu() {
|
|
|
add_theme_page(
|
|
|
__('HTML压缩设置', 'nenghui-energy-theme'),
|
|
|
__('HTML压缩', 'nenghui-energy-theme'),
|
|
|
'manage_options',
|
|
|
'nenghui-html-compression',
|
|
|
array($this, 'adminPage')
|
|
|
);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 管理员页面
|
|
|
*/
|
|
|
public function adminPage() {
|
|
|
// 处理表单提交
|
|
|
if (isset($_POST['submit']) && wp_verify_nonce($_POST['compression_nonce'], 'save_compression_settings')) {
|
|
|
$this->saveSettings();
|
|
|
echo '<div class="notice notice-success is-dismissible"><p>' . __('设置已保存!', 'nenghui-energy-theme') . '</p></div>';
|
|
|
}
|
|
|
|
|
|
// 获取当前设置
|
|
|
$settings = $this->getCurrentSettings();
|
|
|
?>
|
|
|
<div class="wrap">
|
|
|
<h1><?php _e('HTML压缩设置', 'nenghui-energy-theme'); ?></h1>
|
|
|
|
|
|
<form method="post" action="">
|
|
|
<?php wp_nonce_field('save_compression_settings', 'compression_nonce'); ?>
|
|
|
|
|
|
<div class="card">
|
|
|
<h2><?php _e('基本设置', 'nenghui-energy-theme'); ?></h2>
|
|
|
<table class="form-table">
|
|
|
<tr>
|
|
|
<th scope="row"><?php _e('启用HTML压缩', 'nenghui-energy-theme'); ?></th>
|
|
|
<td>
|
|
|
<label>
|
|
|
<input type="checkbox" name="compression_enabled" value="1" <?php checked($settings['compression_enabled']); ?>>
|
|
|
<?php _e('启用HTML压缩功能以提升网站性能', 'nenghui-energy-theme'); ?>
|
|
|
</label>
|
|
|
<p class="description"><?php _e('启用后将自动压缩HTML输出,减少页面大小', 'nenghui-energy-theme'); ?></p>
|
|
|
</td>
|
|
|
</tr>
|
|
|
<tr>
|
|
|
<th scope="row"><?php _e('移除HTML注释', 'nenghui-energy-theme'); ?></th>
|
|
|
<td>
|
|
|
<label>
|
|
|
<input type="checkbox" name="remove_comments" value="1" <?php checked($settings['remove_comments']); ?>>
|
|
|
<?php _e('移除HTML中的注释内容', 'nenghui-energy-theme'); ?>
|
|
|
</label>
|
|
|
<p class="description"><?php _e('注意:某些插件可能依赖HTML注释,请谨慎使用', 'nenghui-energy-theme'); ?></p>
|
|
|
</td>
|
|
|
</tr>
|
|
|
<tr>
|
|
|
<th scope="row"><?php _e('压缩空白字符', 'nenghui-energy-theme'); ?></th>
|
|
|
<td>
|
|
|
<label>
|
|
|
<input type="checkbox" name="remove_whitespace" value="1" <?php checked($settings['remove_whitespace']); ?>>
|
|
|
<?php _e('移除多余的空格、制表符和换行符', 'nenghui-energy-theme'); ?>
|
|
|
</label>
|
|
|
<p class="description"><?php _e('保留必要的空白字符,确保页面正常显示', 'nenghui-energy-theme'); ?></p>
|
|
|
</td>
|
|
|
</tr>
|
|
|
<tr>
|
|
|
<th scope="row"><?php _e('压缩内联CSS', 'nenghui-energy-theme'); ?></th>
|
|
|
<td>
|
|
|
<label>
|
|
|
<input type="checkbox" name="compress_css" value="1" <?php checked($settings['compress_css']); ?>>
|
|
|
<?php _e('压缩<style>标签内的CSS代码', 'nenghui-energy-theme'); ?>
|
|
|
</label>
|
|
|
<p class="description"><?php _e('移除CSS注释和多余空白,减少样式表大小', 'nenghui-energy-theme'); ?></p>
|
|
|
</td>
|
|
|
</tr>
|
|
|
<tr>
|
|
|
<th scope="row"><?php _e('压缩内联JavaScript', 'nenghui-energy-theme'); ?></th>
|
|
|
<td>
|
|
|
<label>
|
|
|
<input type="checkbox" name="compress_js" value="1" <?php checked($settings['compress_js']); ?>>
|
|
|
<?php _e('压缩<script>标签内的JavaScript代码', 'nenghui-energy-theme'); ?>
|
|
|
</label>
|
|
|
<p class="description"><?php _e('移除JS注释和多余空白,减少脚本大小', 'nenghui-energy-theme'); ?></p>
|
|
|
</td>
|
|
|
</tr>
|
|
|
<tr>
|
|
|
<th scope="row"><?php _e('排除页面', 'nenghui-energy-theme'); ?></th>
|
|
|
<td>
|
|
|
<textarea name="excluded_pages" rows="3" cols="50" class="large-text"><?php echo esc_textarea($settings['excluded_pages']); ?></textarea>
|
|
|
<p class="description"><?php _e('输入不需要压缩的页面URL片段,每行一个。例如:wp-admin, wp-login, xmlrpc', 'nenghui-energy-theme'); ?></p>
|
|
|
</td>
|
|
|
</tr>
|
|
|
</table>
|
|
|
|
|
|
<?php submit_button(__('保存设置', 'nenghui-energy-theme')); ?>
|
|
|
</div>
|
|
|
</form>
|
|
|
|
|
|
<div class="card">
|
|
|
<h2><?php _e('压缩状态', 'nenghui-energy-theme'); ?></h2>
|
|
|
<div class="compression-status">
|
|
|
<?php $this->displayCompressionStatus(); ?>
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
<div class="card">
|
|
|
<h2><?php _e('压缩测试', 'nenghui-energy-theme'); ?></h2>
|
|
|
<p><?php _e('测试HTML压缩功能的效果', 'nenghui-energy-theme'); ?></p>
|
|
|
<button id="test-compression" class="button button-primary"><?php _e('测试压缩', 'nenghui-energy-theme'); ?></button>
|
|
|
<div id="compression-test-result" style="margin-top: 15px;"></div>
|
|
|
</div>
|
|
|
|
|
|
<div class="card">
|
|
|
<h2><?php _e('压缩统计', 'nenghui-energy-theme'); ?></h2>
|
|
|
<p><?php _e('查看最近的压缩统计信息', 'nenghui-energy-theme'); ?></p>
|
|
|
<div id="compression-stats">
|
|
|
<?php $this->displayCompressionStats(); ?>
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
<div class="card">
|
|
|
<h2><?php _e('缓存管理', 'nenghui-energy-theme'); ?></h2>
|
|
|
<p><?php _e('清除压缩相关的缓存', 'nenghui-energy-theme'); ?></p>
|
|
|
<button id="clear-cache" class="button"><?php _e('清除缓存', 'nenghui-energy-theme'); ?></button>
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
<script>
|
|
|
jQuery(document).ready(function($) {
|
|
|
$('#test-compression').click(function() {
|
|
|
var button = $(this);
|
|
|
button.prop('disabled', true).text('<?php _e('测试中...', 'nenghui-energy-theme'); ?>');
|
|
|
|
|
|
$.ajax({
|
|
|
url: ajaxurl,
|
|
|
type: 'POST',
|
|
|
data: {
|
|
|
action: 'nenghui_test_compression',
|
|
|
nonce: '<?php echo wp_create_nonce('nenghui_compression_test'); ?>'
|
|
|
},
|
|
|
success: function(response) {
|
|
|
$('#compression-test-result').html(response.data);
|
|
|
},
|
|
|
complete: function() {
|
|
|
button.prop('disabled', false).text('<?php _e('测试压缩', 'nenghui-energy-theme'); ?>');
|
|
|
}
|
|
|
});
|
|
|
});
|
|
|
|
|
|
$('#clear-cache').click(function() {
|
|
|
var button = $(this);
|
|
|
button.prop('disabled', true).text('<?php _e('清除中...', 'nenghui-energy-theme'); ?>');
|
|
|
|
|
|
$.ajax({
|
|
|
url: ajaxurl,
|
|
|
type: 'POST',
|
|
|
data: {
|
|
|
action: 'nenghui_clear_compression_cache',
|
|
|
nonce: '<?php echo wp_create_nonce('nenghui_compression_clear'); ?>'
|
|
|
},
|
|
|
success: function(response) {
|
|
|
alert(response.data);
|
|
|
},
|
|
|
complete: function() {
|
|
|
button.prop('disabled', false).text('<?php _e('清除缓存', 'nenghui-energy-theme'); ?>');
|
|
|
}
|
|
|
});
|
|
|
});
|
|
|
});
|
|
|
</script>
|
|
|
<?php
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 保存设置
|
|
|
*/
|
|
|
private function saveSettings() {
|
|
|
$settings = array(
|
|
|
'nenghui_compression_enabled' => isset($_POST['compression_enabled']) ? true : false,
|
|
|
'nenghui_compression_remove_comments' => isset($_POST['remove_comments']) ? true : false,
|
|
|
'nenghui_compression_remove_whitespace' => isset($_POST['remove_whitespace']) ? true : false,
|
|
|
'nenghui_compression_compress_css' => isset($_POST['compress_css']) ? true : false,
|
|
|
'nenghui_compression_compress_js' => isset($_POST['compress_js']) ? true : false,
|
|
|
'nenghui_compression_excluded_pages' => sanitize_textarea_field($_POST['excluded_pages'])
|
|
|
);
|
|
|
|
|
|
foreach ($settings as $key => $value) {
|
|
|
set_theme_mod($key, $value);
|
|
|
}
|
|
|
|
|
|
// 重新设置压缩选项
|
|
|
$this->setupCompressionOptions();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取当前设置
|
|
|
*/
|
|
|
private function getCurrentSettings() {
|
|
|
return array(
|
|
|
'compression_enabled' => get_theme_mod('nenghui_compression_enabled', true),
|
|
|
'remove_comments' => get_theme_mod('nenghui_compression_remove_comments', true),
|
|
|
'remove_whitespace' => get_theme_mod('nenghui_compression_remove_whitespace', true),
|
|
|
'compress_css' => get_theme_mod('nenghui_compression_compress_css', true),
|
|
|
'compress_js' => get_theme_mod('nenghui_compression_compress_js', true),
|
|
|
'excluded_pages' => get_theme_mod('nenghui_compression_excluded_pages', 'wp-admin,wp-login,xmlrpc')
|
|
|
);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 显示压缩状态
|
|
|
*/
|
|
|
private function displayCompressionStatus() {
|
|
|
$settings = $this->getCurrentSettings();
|
|
|
$status_class = $settings['compression_enabled'] ? 'notice-success' : 'notice-warning';
|
|
|
$status_text = $settings['compression_enabled'] ? __('已启用', 'nenghui-energy-theme') : __('已禁用', 'nenghui-energy-theme');
|
|
|
|
|
|
echo '<div class="notice ' . $status_class . ' inline">';
|
|
|
echo '<p><strong>' . __('压缩状态:', 'nenghui-energy-theme') . '</strong>' . $status_text . '</p>';
|
|
|
echo '</div>';
|
|
|
|
|
|
if ($settings['compression_enabled']) {
|
|
|
echo '<ul>';
|
|
|
echo '<li>' . ($settings['remove_comments'] ? '✅' : '❌') . ' ' . __('移除HTML注释', 'nenghui-energy-theme') . '</li>';
|
|
|
echo '<li>' . ($settings['remove_whitespace'] ? '✅' : '❌') . ' ' . __('压缩空白字符', 'nenghui-energy-theme') . '</li>';
|
|
|
echo '<li>' . ($settings['compress_css'] ? '✅' : '❌') . ' ' . __('压缩内联CSS', 'nenghui-energy-theme') . '</li>';
|
|
|
echo '<li>' . ($settings['compress_js'] ? '✅' : '❌') . ' ' . __('压缩内联JavaScript', 'nenghui-energy-theme') . '</li>';
|
|
|
echo '</ul>';
|
|
|
|
|
|
if (!empty($settings['excluded_pages'])) {
|
|
|
echo '<p><strong>' . __('排除的页面:', 'nenghui-energy-theme') . '</strong></p>';
|
|
|
$excluded = array_map('trim', explode(',', $settings['excluded_pages']));
|
|
|
echo '<ul>';
|
|
|
foreach ($excluded as $page) {
|
|
|
if (!empty($page)) {
|
|
|
echo '<li>' . esc_html($page) . '</li>';
|
|
|
}
|
|
|
}
|
|
|
echo '</ul>';
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 显示压缩统计信息
|
|
|
*/
|
|
|
private function displayCompressionStats() {
|
|
|
$stats = get_option('nenghui_compression_stats', array());
|
|
|
|
|
|
if (empty($stats)) {
|
|
|
echo '<p>' . __('暂无压缩统计数据', 'nenghui-energy-theme') . '</p>';
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
echo '<table class="wp-list-table widefat fixed striped">';
|
|
|
echo '<thead><tr>';
|
|
|
echo '<th>' . __('时间', 'nenghui-energy-theme') . '</th>';
|
|
|
echo '<th>' . __('原始大小', 'nenghui-energy-theme') . '</th>';
|
|
|
echo '<th>' . __('压缩后大小', 'nenghui-energy-theme') . '</th>';
|
|
|
echo '<th>' . __('节省空间', 'nenghui-energy-theme') . '</th>';
|
|
|
echo '<th>' . __('压缩率', 'nenghui-energy-theme') . '</th>';
|
|
|
echo '</tr></thead><tbody>';
|
|
|
|
|
|
foreach (array_slice($stats, -10) as $stat) {
|
|
|
echo '<tr>';
|
|
|
echo '<td>' . date('Y-m-d H:i:s', $stat['timestamp']) . '</td>';
|
|
|
echo '<td>' . size_format($stat['original_size']) . '</td>';
|
|
|
echo '<td>' . size_format($stat['compressed_size']) . '</td>';
|
|
|
echo '<td>' . size_format($stat['saved_bytes']) . '</td>';
|
|
|
echo '<td>' . $stat['compression_ratio'] . '%</td>';
|
|
|
echo '</tr>';
|
|
|
}
|
|
|
|
|
|
echo '</tbody></table>';
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* AJAX测试压缩
|
|
|
*/
|
|
|
public function testCompression() {
|
|
|
check_ajax_referer('nenghui_compression_test', 'nonce');
|
|
|
|
|
|
$test_html = '<!DOCTYPE html>
|
|
|
<html>
|
|
|
<head>
|
|
|
<title>Test Page</title>
|
|
|
<style>
|
|
|
body { margin: 0; padding: 20px; }
|
|
|
.container { max-width: 1200px; margin: 0 auto; }
|
|
|
</style>
|
|
|
</head>
|
|
|
<body>
|
|
|
<!-- This is a test comment -->
|
|
|
<div class="container">
|
|
|
<h1>Test Page</h1>
|
|
|
<p>This is a test paragraph with some content.</p>
|
|
|
<script>
|
|
|
console.log("Test script");
|
|
|
var test = "Hello World";
|
|
|
</script>
|
|
|
</div>
|
|
|
</body>
|
|
|
</html>';
|
|
|
|
|
|
$compressed = $this->compressor->compress($test_html);
|
|
|
$stats = $this->compressor->getCompressionStats($test_html, $compressed);
|
|
|
|
|
|
$result = '<div class="notice notice-success"><p><strong>' . __('压缩测试成功!', 'nenghui-energy-theme') . '</strong></p>';
|
|
|
$result .= '<p>' . sprintf(__('原始大小: %s 字节', 'nenghui-energy-theme'), number_format($stats['original_size'])) . '</p>';
|
|
|
$result .= '<p>' . sprintf(__('压缩后大小: %s 字节', 'nenghui-energy-theme'), number_format($stats['compressed_size'])) . '</p>';
|
|
|
$result .= '<p>' . sprintf(__('节省空间: %s 字节 (%.2f%%)', 'nenghui-energy-theme'), number_format($stats['saved_bytes']), $stats['compression_ratio']) . '</p>';
|
|
|
$result .= '</div>';
|
|
|
|
|
|
wp_send_json_success($result);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* AJAX清除缓存
|
|
|
*/
|
|
|
public function clearCompressionCache() {
|
|
|
check_ajax_referer('nenghui_compression_clear', 'nonce');
|
|
|
|
|
|
// 清除相关缓存
|
|
|
delete_option('nenghui_compression_stats');
|
|
|
|
|
|
// 清除对象缓存
|
|
|
wp_cache_flush();
|
|
|
|
|
|
wp_send_json_success(__('缓存已清除', 'nenghui-energy-theme'));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 初始化HTML压缩集成
|
|
|
new NenghuiHTMLCompressionIntegration();
|