|
|
<?php
|
|
|
/**
|
|
|
* 资源加载管理器
|
|
|
* 用于管理和加载项目所需的CSS、JS等资源文件
|
|
|
*/
|
|
|
|
|
|
// 防止直接访问
|
|
|
if (!defined('ABSPATH')) {
|
|
|
exit;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 加载前端资源
|
|
|
*/
|
|
|
function load_theme_assets() {
|
|
|
// 获取主题目录URL和路径
|
|
|
$theme_url = get_template_directory_uri();
|
|
|
$theme_path = get_template_directory();
|
|
|
|
|
|
// 确保使用WordPress内置的jQuery
|
|
|
wp_enqueue_script('jquery');
|
|
|
|
|
|
// 安全加载CSS文件
|
|
|
$css_files = array(
|
|
|
'theme-reset-css' => '/assets/css/reset.css',
|
|
|
'font-awesome' => '/assets/fontawesome/css/all.min.css',
|
|
|
'theme-header-css' => '/assets/css/header.css',
|
|
|
'theme-index-css' => '/assets/css/index.css',
|
|
|
// 'animate-css' => '/assets/css/animate.min.css',
|
|
|
// 'custom-animations' => '/assets/css/animations.css',
|
|
|
// 'scroll-animations-css' => '/assets/css/scroll-animations.css',
|
|
|
// 'smooth-scroll-css' => '/assets/css/smooth-scroll.css',
|
|
|
'search-css' => '/assets/css/search.css',
|
|
|
'navigation-menu-css' => '/assets/css/navigation-menu.css',
|
|
|
'footer-css' => '/assets/css/footer.css',
|
|
|
// 'video-banner-css' => '/assets/css/video-banner.css', // 移除全局加载,改为按需加载
|
|
|
'products-services-css' => '/assets/css/products-services.css',
|
|
|
'features-css' => '/assets/css/features.css',
|
|
|
'solution-css' => '/assets/css/solution.css',
|
|
|
'block-news-css' => '/assets/css/block-news.css',
|
|
|
'article-display-css' => '/assets/css/article-display.css',
|
|
|
'single-sidebar-css' => '/assets/css/single-sidebar.css',
|
|
|
'simple-carousel-css' => '/assets/css/simple-carousel.css',
|
|
|
'about-timeline-css' => '/assets/css/about-timeline.css',
|
|
|
'social-activities-css' => '/assets/css/social-activities.css',
|
|
|
'business-process-css' => '/assets/css/business-process.css',
|
|
|
// 'henghui-core-css' => '/assets/css/henghui-core.css' // 移至页面单独加载
|
|
|
);
|
|
|
|
|
|
foreach ($css_files as $handle => $file_path) {
|
|
|
// 在生产环境中跳过文件检查以提高性能
|
|
|
if ((!defined('WP_DEBUG') || !WP_DEBUG) || file_exists($theme_path . $file_path)) {
|
|
|
wp_enqueue_style($handle, $theme_url . $file_path, array(), '1.0.0');
|
|
|
} elseif (defined('WP_DEBUG') && WP_DEBUG) {
|
|
|
error_log('Missing CSS file: ' . $theme_path . $file_path);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 安全加载JavaScript文件 - 优化GSAP和Swiper兼容性
|
|
|
$js_files = array(
|
|
|
// 基础库 - 最先加载
|
|
|
// 'smoothscroll' => array('file' => '/assets/js/smoothscroll.min.js', 'deps' => array(), 'ver' => '1.4.10'),
|
|
|
// 'smooth-scroll-init' => array('file' => '/assets/js/smooth-scroll-init.js', 'deps' => array('smoothscroll'), 'ver' => '1.0.0'),
|
|
|
|
|
|
// GSAP核心库 - 已启用,优化加载顺序
|
|
|
'gsap' => array('file' => '/assets/js/gsap.min.js', 'deps' => array('jquery'), 'ver' => '3.12.2'),
|
|
|
'gsap-scrolltrigger' => array('file' => '/assets/js/ScrollTrigger.min.js', 'deps' => array('gsap'), 'ver' => '3.12.2'),
|
|
|
'gsap-textplugin' => array('file' => '/assets/js/TextPlugin.min.js', 'deps' => array('gsap'), 'ver' => '3.12.2'),
|
|
|
|
|
|
// GSAP与Elementor兼容性管理器 - 必须在所有GSAP相关脚本之后加载
|
|
|
'gsap-elementor-compatibility' => array('file' => '/assets/js/gsap-elementor-compatibility.js', 'deps' => array('gsap', 'gsap-scrolltrigger', 'gsap-textplugin'), 'ver' => '1.0.1'),
|
|
|
|
|
|
// GSAP与Elementor初始化管理器 - 确保正确的初始化顺序
|
|
|
'gsap-elementor-init' => array('file' => '/assets/js/gsap-elementor-init.js', 'deps' => array('gsap-elementor-compatibility'), 'ver' => '1.0.0'),
|
|
|
|
|
|
// 滚动动画系统 - 基于GSAP ScrollTrigger的动画控制器
|
|
|
'scroll-animations' => array('file' => '/assets/js/scroll-animations.js', 'deps' => array('gsap', 'gsap-scrolltrigger', 'gsap-elementor-init'), 'ver' => '1.0.0'),
|
|
|
|
|
|
// Elementor调试工具 - 仅在调试模式下加载
|
|
|
'elementor-debug' => array('file' => '/assets/js/elementor-debug.js', 'deps' => array('gsap-elementor-compatibility'), 'ver' => '1.0.0'),
|
|
|
|
|
|
// 组件脚本 - GSAP依赖配置,确保兼容性管理器优先加载
|
|
|
// 'video-banner-js' => array('file' => '/assets/js/video-banner.js', 'deps' => array('jquery', 'gsap'), 'ver' => '1.0.0'), // 移除全局加载,改为按需加载
|
|
|
'products-services-js' => array('file' => '/assets/js/products-services.js', 'deps' => array('jquery', 'gsap-elementor-init'), 'ver' => '1.0.0'),
|
|
|
'features-js' => array('file' => '/assets/js/features.js', 'deps' => array('jquery', 'gsap-elementor-init'), 'ver' => '1.0.0'),
|
|
|
'solution-js' => array('file' => '/assets/js/solution.js', 'deps' => array('jquery', 'gsap-elementor-init'), 'ver' => '1.0.0'),
|
|
|
'search-js' => array('file' => '/assets/js/search.js', 'deps' => array('jquery', 'gsap-elementor-init'), 'ver' => '1.0.0'),
|
|
|
|
|
|
// 其他独立库
|
|
|
'tinytyper' => array('file' => '/assets/js/tinytyper.min.js', 'deps' => array('jquery'), 'ver' => '1.0.0'),
|
|
|
|
|
|
// SimpleCarousel库 - 轻量级轮播组件
|
|
|
'simple-carousel-js' => array('file' => '/assets/js/simple-carousel.js', 'deps' => array('jquery'), 'ver' => '1.0.0'),
|
|
|
|
|
|
// 区块组件脚本
|
|
|
'about-timeline-js' => array('file' => '/assets/js/about-timeline.js', 'deps' => array('jquery', 'simple-carousel-js'), 'ver' => '1.0.0'),
|
|
|
'social-activities-js' => array('file' => '/assets/js/social-activities.js', 'deps' => array('jquery', 'simple-carousel-js'), 'ver' => '1.0.0'),
|
|
|
'business-process-js' => array('file' => '/assets/js/business-process.js', 'deps' => array('jquery'), 'ver' => '1.0.0'),
|
|
|
// 'professional-fields-js' => array('file' => '/assets/js/professional-fields.js', 'deps' => array('jquery', 'simple-carousel-js'), 'ver' => '1.0.0'), // 移至页面单独加载
|
|
|
// 'henghui-core-js' => array('file' => '/assets/js/henghui-core.js', 'deps' => array('jquery', 'gsap', 'gsap-scrolltrigger', 'simple-carousel-js'), 'ver' => '1.0.0'), // 移至页面单独加载
|
|
|
|
|
|
// 主题脚本
|
|
|
'dropdown-menu-js' => array('file' => '/assets/js/dropdown-menu.js', 'deps' => array('jquery'), 'ver' => '1.0.0'),
|
|
|
'theme-index-js' => array('file' => '/assets/js/index.js', 'deps' => array('jquery', 'tinytyper'), 'ver' => '1.0.0')
|
|
|
);
|
|
|
|
|
|
foreach ($js_files as $handle => $config) {
|
|
|
// 调试工具只在调试模式下加载
|
|
|
if ($handle === 'elementor-debug' && !isset($_GET['debug'])) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
// 在生产环境中跳过文件检查以提高性能
|
|
|
if ((!defined('WP_DEBUG') || !WP_DEBUG) || file_exists($theme_path . $config['file'])) {
|
|
|
wp_enqueue_script($handle, $theme_url . $config['file'], $config['deps'], $config['ver'], true);
|
|
|
} elseif (defined('WP_DEBUG') && WP_DEBUG) {
|
|
|
error_log('Missing JS file: ' . $theme_path . $config['file']);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 为主题JS文件传递一些有用的数据
|
|
|
wp_localize_script('theme-index-js', 'themeData', array(
|
|
|
'ajaxUrl' => admin_url('admin-ajax.php'),
|
|
|
'nonce' => wp_create_nonce('theme_nonce'),
|
|
|
'themeUrl' => $theme_url
|
|
|
));
|
|
|
}
|
|
|
add_action('wp_enqueue_scripts', 'load_theme_assets');
|
|
|
|
|
|
/**
|
|
|
* 加载后台管理资源
|
|
|
*/
|
|
|
function load_admin_assets($hook) {
|
|
|
$theme_url = get_template_directory_uri();
|
|
|
|
|
|
// 加载后台CSS
|
|
|
wp_enqueue_style('theme-admin-css', $theme_url . '/assets/css/admin.css', array(), '1.0.0');
|
|
|
}
|
|
|
add_action('admin_enqueue_scripts', 'load_admin_assets');
|
|
|
|
|
|
/**
|
|
|
* 优化资源加载性能
|
|
|
*/
|
|
|
function optimize_asset_loading() {
|
|
|
// 移除WordPress默认的jQuery,使用我们自己的版本(如果需要)
|
|
|
// wp_deregister_script('jquery');
|
|
|
|
|
|
// 添加资源预加载提示
|
|
|
echo '<link rel="preload" href="' . get_template_directory_uri() . '/assets/js/gsap.min.js" as="script">';
|
|
|
echo '<link rel="preload" href="' . get_template_directory_uri() . '/assets/css/index.css" as="style">';
|
|
|
}
|
|
|
add_action('wp_head', 'optimize_asset_loading', 1);
|
|
|
|
|
|
/**
|
|
|
* 添加资源版本控制
|
|
|
*/
|
|
|
function add_asset_version($src, $handle) {
|
|
|
// 为本地资源添加文件修改时间作为版本号
|
|
|
if (strpos($src, get_template_directory_uri()) !== false) {
|
|
|
// 开发模式下检查文件修改时间,生产模式直接使用主题版本号,减少IO操作
|
|
|
if (defined('WP_DEBUG') && WP_DEBUG) {
|
|
|
$file_path = str_replace(get_template_directory_uri(), get_template_directory(), $src);
|
|
|
if (file_exists($file_path)) {
|
|
|
$version = filemtime($file_path);
|
|
|
$src = add_query_arg('v', $version, $src);
|
|
|
}
|
|
|
} else {
|
|
|
// 生产环境使用主题版本号
|
|
|
$theme = wp_get_theme();
|
|
|
$src = add_query_arg('v', $theme->get('Version'), $src);
|
|
|
}
|
|
|
}
|
|
|
return $src;
|
|
|
}
|
|
|
add_filter('style_loader_src', 'add_asset_version', 10, 2);
|
|
|
add_filter('script_loader_src', 'add_asset_version', 10, 2);
|
|
|
|
|
|
/**
|
|
|
* 条件加载资源
|
|
|
* 根据页面类型加载不同的资源
|
|
|
*/
|
|
|
function conditional_asset_loading() {
|
|
|
$theme_url = get_template_directory_uri();
|
|
|
|
|
|
// 仅在首页加载特定的动画资源(文件存在时)
|
|
|
if (is_front_page() && ((!defined('WP_DEBUG') || !WP_DEBUG) || file_exists(get_template_directory() . '/assets/js/homepage-animations.js'))) {
|
|
|
wp_enqueue_script('homepage-animations', $theme_url . '/assets/js/homepage-animations.js', array('gsap'), '1.0.0', true);
|
|
|
}
|
|
|
|
|
|
// 仅在单篇文章页面加载相关资源(文件存在时)
|
|
|
if (is_single() && ((!defined('WP_DEBUG') || !WP_DEBUG) || file_exists(get_template_directory() . '/assets/js/single-post.js'))) {
|
|
|
wp_enqueue_script('single-post-enhancements', $theme_url . '/assets/js/single-post.js', array('gsap'), '1.0.0', true);
|
|
|
}
|
|
|
|
|
|
// 恒辉核心页面专用脚本
|
|
|
if (is_page_template('page-henghui-core.php')) {
|
|
|
if ((!defined('WP_DEBUG') || !WP_DEBUG) || file_exists(get_template_directory() . '/assets/js/henghui-core.js')) {
|
|
|
wp_enqueue_script('henghui-core-js', $theme_url . '/assets/js/henghui-core.js', array('jquery'), '1.0.0', true);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
add_action('wp_enqueue_scripts', 'conditional_asset_loading', 20);
|
|
|
|
|
|
/**
|
|
|
* 内联关键CSS(可选)
|
|
|
*/
|
|
|
function inline_critical_css() {
|
|
|
$critical_css_file = get_template_directory() . '/assets/css/critical.css';
|
|
|
if (file_exists($critical_css_file)) {
|
|
|
echo '<style id="critical-css">';
|
|
|
echo file_get_contents($critical_css_file);
|
|
|
echo '</style>';
|
|
|
}
|
|
|
}
|
|
|
// add_action('wp_head', 'inline_critical_css', 2);
|
|
|
|
|
|
/**
|
|
|
* 资源完整性检查
|
|
|
*/
|
|
|
function check_asset_integrity() {
|
|
|
// 仅在调试模式下检查,避免生产环境产生不必要的IO开销
|
|
|
if (!defined('WP_DEBUG') || !WP_DEBUG) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
$required_assets = array(
|
|
|
get_template_directory() . '/assets/js/gsap.min.js',
|
|
|
get_template_directory() . '/assets/js/index.js',
|
|
|
get_template_directory() . '/assets/css/index.css'
|
|
|
);
|
|
|
|
|
|
foreach ($required_assets as $asset) {
|
|
|
if (!file_exists($asset)) {
|
|
|
error_log('Missing required asset: ' . $asset);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
add_action('init', 'check_asset_integrity');
|
|
|
?>
|