You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
/**
* 主题初始化配置文件
* 包含服务器资源限制、内存管理等初始化设置
*/
// 防止直接访问
if (!defined('ABSPATH')) {
exit;
}
/**
* 设置合理的服务器资源限制
* 在主题初始化时配置内存、执行时间和文件上传限制
*/
function nenghui_performance_init() {
// 设置内存限制(根据服务器配置调整)
if (!ini_get('memory_limit') || ini_get('memory_limit') < '256M') {
ini_set('memory_limit', '256M');
}
// 设置执行时间限制
if (!ini_get('max_execution_time') || ini_get('max_execution_time') < 300) {
set_time_limit(300); // 5分钟
}
// 设置上传文件大小限制
if (!ini_get('upload_max_filesize') || parse_size(ini_get('upload_max_filesize')) < parse_size('50M')) {
ini_set('upload_max_filesize', '50M');
}
// 设置POST数据最大大小限制为52MB需要比upload_max_filesize稍大以容纳表单数据
if (!ini_get('post_max_size') || parse_size(ini_get('post_max_size')) < parse_size('52M')) {
ini_set('post_max_size', '52M');
}
}
/**
* 解析文件大小字符串为字节数
* 将如"50M"、"1G"等格式转换为字节数
* @param string $size 文件大小字符串
* @return int 字节数
*/
function parse_size($size) {
$unit = preg_replace('/[^bkmgtpezy]/i', '', $size);
$size = preg_replace('/[^0-9\.]/', '', $size);
if ($unit) {
return round($size * pow(1024, stripos('bkmgtpezy', $unit[0])));
}
return round($size);
}
/**
* 启用图片压缩和缩略图生成
* 设置JPEG压缩质量和WebP支持
*/
function nenghui_enable_image_compression() {
// 设置JPEG压缩质量
add_filter('jpeg_quality', function() { return 85; });
add_filter('wp_editor_set_quality', function() { return 85; });
// 启用WebP支持如果服务器支持
if (function_exists('imagewebp')) {
add_filter('wp_generate_attachment_metadata', 'nenghui_generate_webp_images', 10, 2);
}
}
/**
* 生成WebP格式图片
* 为上传的JPEG和PNG图片自动生成WebP版本
* @param array $metadata 图片元数据
* @param int $attachment_id 附件ID
* @return array 处理后的元数据
*/
function nenghui_generate_webp_images($metadata, $attachment_id) {
// 确保 $metadata 是数组
if (!is_array($metadata)) {
return $metadata;
}
$file = get_attached_file($attachment_id);
if (!$file) {
return $metadata;
}
$info = pathinfo($file);
if (isset($info['extension']) && in_array(strtolower($info['extension']), array('jpg', 'jpeg', 'png'))) {
$webp_file = $info['dirname'] . '/' . $info['filename'] . '.webp';
switch (strtolower($info['extension'])) {
case 'jpg':
case 'jpeg':
$image = imagecreatefromjpeg($file);
break;
case 'png':
$image = imagecreatefrompng($file);
break;
}
if (isset($image)) {
imagewebp($image, $webp_file, 85);
imagedestroy($image);
}
}
return $metadata;
}
/**
* 监控内存使用情况
* 在调试模式下记录内存使用情况
*/
function nenghui_memory_monitor() {
if (WP_DEBUG) {
$memory_usage = memory_get_usage(true);
$memory_peak = memory_get_peak_usage(true);
$memory_limit = ini_get('memory_limit');
error_log(sprintf(
'Nenghui Theme Memory Usage: Current: %s, Peak: %s, Limit: %s',
size_format($memory_usage),
size_format($memory_peak),
$memory_limit
));
}
}
add_action('wp_footer', 'nenghui_memory_monitor');
/**
* 清理临时文件
* 定期清理上传目录中的临时文件
*/
function nenghui_cleanup_temp_files() {
$upload_dir = wp_upload_dir();
// 确保 $upload_dir 是数组且包含 basedir
if (!is_array($upload_dir) || !isset($upload_dir['basedir'])) {
return;
}
$temp_dir = $upload_dir['basedir'] . '/temp/';
if (is_dir($temp_dir)) {
$files = glob($temp_dir . '*');
$now = time();
if (is_array($files) && !empty($files)) {
foreach ($files as $file) {
if (is_file($file) && ($now - filemtime($file)) > 3600) { // 删除1小时前的临时文件
unlink($file);
}
}
}
}
}
// 每小时清理一次临时文件
if (!wp_next_scheduled('nenghui_cleanup_temp_files')) {
wp_schedule_event(time(), 'hourly', 'nenghui_cleanup_temp_files');
}
add_action('nenghui_cleanup_temp_files', 'nenghui_cleanup_temp_files');
// 初始化性能优化
add_action('init', 'nenghui_performance_init');
add_action('init', 'nenghui_enable_image_compression');
?>