- 删除 7 个死模板文件 (products/services/quality/process/projects/customization/rfq) - 删除 13 个旧图片素材 (jpg/png/webp) - 新增 CDN SRI 完整性校验 (Bootstrap CSS/JS) - 新增面包屑导航模板组件 - 新增代码清理方案与代码审查标准文档 - 更新 README.md
109 lines
5.0 KiB
PHP
109 lines
5.0 KiB
PHP
<?php
|
|
/**
|
|
* Theme setup and frontend assets.
|
|
*
|
|
* @package Fragrance_Trade
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/** Register theme features and menus. */
|
|
function fragrance_trade_setup() {
|
|
load_theme_textdomain( 'fragrance-trade', get_template_directory() . '/languages' );
|
|
add_theme_support( 'title-tag' );
|
|
add_theme_support( 'custom-logo', array( 'height' => 96, 'width' => 260, 'flex-height' => true, 'flex-width' => true ) );
|
|
add_theme_support( 'post-thumbnails' );
|
|
add_theme_support( 'automatic-feed-links' );
|
|
add_theme_support( 'responsive-embeds' );
|
|
add_theme_support( 'html5', array( 'comment-list', 'comment-form', 'search-form', 'gallery', 'caption', 'style', 'script' ) );
|
|
add_image_size( 'fragrance-capability', 760, 720, true );
|
|
|
|
register_nav_menus(
|
|
array(
|
|
'primary' => __( '主导航', 'fragrance-trade' ),
|
|
)
|
|
);
|
|
}
|
|
add_action( 'after_setup_theme', 'fragrance_trade_setup' );
|
|
|
|
/**
|
|
* Add Bootstrap 5 nav-item / nav-link classes to primary menu.
|
|
*
|
|
* Without these classes the offcanvas mobile menu items may not render
|
|
* properly because Bootstrap's offcanvas CSS relies on them for display.
|
|
*/
|
|
function fragrance_trade_nav_menu_css_class( $classes, $item, $args ) {
|
|
if ( 'primary' === $args->theme_location ) {
|
|
$classes[] = 'nav-item';
|
|
}
|
|
return $classes;
|
|
}
|
|
add_filter( 'nav_menu_css_class', 'fragrance_trade_nav_menu_css_class', 10, 3 );
|
|
|
|
function fragrance_trade_nav_menu_link_attributes( $atts, $item, $args ) {
|
|
if ( 'primary' === $args->theme_location ) {
|
|
$atts['class'] = isset( $atts['class'] ) ? $atts['class'] . ' nav-link' : 'nav-link';
|
|
}
|
|
return $atts;
|
|
}
|
|
add_filter( 'nav_menu_link_attributes', 'fragrance_trade_nav_menu_link_attributes', 10, 3 );
|
|
|
|
/**
|
|
* Return a cache-busting version for a local theme asset.
|
|
*
|
|
* @param string $relative_path Path relative to the theme root.
|
|
* @param string $fallback Version used when the file cannot be read.
|
|
* @return string
|
|
*/
|
|
function fragrance_trade_asset_version( $relative_path, $fallback ) {
|
|
$file = get_theme_file_path( '/' . ltrim( $relative_path, '/' ) );
|
|
return file_exists( $file ) ? (string) filemtime( $file ) : $fallback;
|
|
}
|
|
|
|
/** Load theme styles and scripts through the WordPress dependency system. */
|
|
function fragrance_trade_enqueue_assets() {
|
|
$version = wp_get_theme()->get( 'Version' );
|
|
|
|
wp_enqueue_style( 'fragrance-bootstrap', 'https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.0-alpha3/css/bootstrap.min.css', array(), '5.3.0-alpha3' );
|
|
wp_enqueue_style( 'fragrance-fonts', 'https://fonts.loli.net/css2?family=Josefin+Sans:wght@200;300;400;500;600;700&family=Jost:wght@200;300;400;500&family=Noto+Sans+SC:wght@300;400;500;700&display=swap', array(), null );
|
|
wp_enqueue_style( 'fragrance-normalize', get_template_directory_uri() . '/assets/css/normalize.css', array(), fragrance_trade_asset_version( 'assets/css/normalize.css', $version ) );
|
|
wp_enqueue_style( 'fragrance-vendor', get_template_directory_uri() . '/assets/css/vendor.css', array(), fragrance_trade_asset_version( 'assets/css/vendor.css', $version ) );
|
|
wp_enqueue_style( 'fragrance-base', get_stylesheet_uri(), array( 'fragrance-bootstrap', 'fragrance-vendor' ), fragrance_trade_asset_version( 'style.css', $version ) );
|
|
wp_enqueue_style( 'fragrance-theme', get_template_directory_uri() . '/assets/css/theme.css', array( 'fragrance-base' ), fragrance_trade_asset_version( 'assets/css/theme.css', $version ) );
|
|
|
|
wp_enqueue_script( 'fragrance-bootstrap', 'https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.0-alpha3/js/bootstrap.bundle.min.js', array(), '5.3.0-alpha3', true );
|
|
wp_enqueue_script( 'fragrance-plugins', get_template_directory_uri() . '/assets/js/plugins.js', array( 'jquery' ), fragrance_trade_asset_version( 'assets/js/plugins.js', $version ), true );
|
|
wp_enqueue_script( 'fragrance-theme', get_template_directory_uri() . '/assets/js/theme.js', array( 'jquery', 'fragrance-bootstrap', 'fragrance-plugins' ), fragrance_trade_asset_version( 'assets/js/theme.js', $version ), true );
|
|
}
|
|
add_action( 'wp_enqueue_scripts', 'fragrance_trade_enqueue_assets' );
|
|
|
|
/**
|
|
* SRI integrity hashes for CDN-hosted resources.
|
|
*
|
|
* Computed 2026-07-24 from cdn.bootcdn.net.
|
|
* Verify/update via: curl -sL <url> | openssl dgst -sha384 -binary | openssl base64 -A
|
|
*/
|
|
function fragrance_trade_style_sri( $html, $handle ) {
|
|
$sri = array(
|
|
'fragrance-bootstrap' => 'sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ',
|
|
);
|
|
if ( isset( $sri[ $handle ] ) ) {
|
|
$html = str_replace( '/>', ' integrity="' . esc_attr( $sri[ $handle ] ) . '" crossorigin="anonymous" />', $html );
|
|
}
|
|
return $html;
|
|
}
|
|
add_filter( 'style_loader_tag', 'fragrance_trade_style_sri', 10, 2 );
|
|
|
|
function fragrance_trade_script_sri( $html, $handle ) {
|
|
$sri = array(
|
|
'fragrance-bootstrap' => 'sha384-ENjdO4Dr2bkBIFxQpeoTz1HIcje39Wm4jDKdf19U8gI4ddQ3GYNS7NTKfAdVQSZe',
|
|
);
|
|
if ( isset( $sri[ $handle ] ) ) {
|
|
$html = str_replace( '></script>', ' integrity="' . esc_attr( $sri[ $handle ] ) . '" crossorigin="anonymous"></script>', $html );
|
|
}
|
|
return $html;
|
|
}
|
|
add_filter( 'script_loader_tag', 'fragrance_trade_script_sri', 10, 2 );
|