shortcode_atts = $banner_title_shortcode_atts;
}
/**
* 获取标题
*/
public function getTitle() {
$title = !empty($this->shortcode_atts['title'])
? $this->shortcode_atts['title']
: get_theme_mod('banner_title_main_title', 'ABOUT US');
// 根据title参数返回合适的显示文本
switch (strtolower($title)) {
case 'cases':
return 'CASES';
case 'news':
return 'NEWS';
case 'about':
return 'ABOUT US';
case 'contact':
return 'CONTACT US';
default:
return strtoupper($title);
}
}
/**
* 获取描述
*/
public function getDescription() {
return !empty($this->shortcode_atts['description'])
? $this->shortcode_atts['description']
: get_theme_mod('banner_title_description', 'Powering Sustainable Futures with Advanced Energy Storage Solutions');
}
/**
* 获取背景图片
*/
public function getBackgroundImage() {
$bg_image = !empty($this->shortcode_atts['bg_image'])
? $this->shortcode_atts['bg_image']
: get_theme_mod('banner_title_bg_image', '');
// 如果没有设置背景图片或图片无效,使用默认图片
if (empty($bg_image)) {
$bg_image = get_template_directory_uri() . '/assets/images/about-bg.webp';
} else {
// 验证图片路径
$is_valid_url = filter_var($bg_image, FILTER_VALIDATE_URL);
$is_valid_file = false;
// 只对本地路径进行文件存在性检查
if (!$is_valid_url && is_string($bg_image)) {
$file_path = str_replace(get_site_url(), ABSPATH, $bg_image);
$is_valid_file = file_exists($file_path);
}
// 如果既不是有效URL也不是有效文件,使用默认图片
if (!$is_valid_url && !$is_valid_file) {
$bg_image = get_template_directory_uri() . '/assets/images/about-bg.webp';
}
}
return $bg_image;
}
/**
* 获取元素ID
*/
public function getElementId() {
return !empty($this->shortcode_atts['id'])
? $this->shortcode_atts['id']
: 'banner-title-' . uniqid();
}
/**
* 获取CSS类
*/
public function getCssClasses() {
$classes = array('nenghui-banner-title', 'banner-title-modern');
if (!empty($this->shortcode_atts['class']) && is_string($this->shortcode_atts['class'])) {
// 清理CSS类名,移除潜在的危险字符
$custom_class = preg_replace('/[^a-zA-Z0-9\-_\s]/', '', $this->shortcode_atts['class']);
if (!empty($custom_class)) {
$classes[] = $custom_class;
}
}
// 动画类
$show_animation = isset($this->shortcode_atts['show_animation'])
? $this->shortcode_atts['show_animation']
: 'true';
if ($show_animation === 'true' || $show_animation === true) {
$classes[] = 'has-animation';
}
return implode(' ', array_filter($classes));
}
/**
* 获取高度设置
*/
public function getHeight() {
$height = !empty($this->shortcode_atts['height'])
? $this->shortcode_atts['height']
: get_theme_mod('banner_title_height', '60vh');
// 验证高度值格式
if (!preg_match('/^\d+(\.\d+)?(px|vh|vw|%|em|rem)$/', $height)) {
$height = '70vh'; // 默认值
}
return $height;
}
/**
* 获取遮罩透明度
*/
public function getOverlayOpacity() {
$opacity = !empty($this->shortcode_atts['overlay_opacity'])
? $this->shortcode_atts['overlay_opacity']
: get_theme_mod('banner_title_overlay_opacity', '0.4');
// 验证透明度值(0-1之间的数字)
$opacity = floatval($opacity);
if ($opacity < 0 || $opacity > 1) {
$opacity = 0.4; // 默认值
}
return $opacity;
}
}
}
// 初始化配置
try {
$config = new BannerTitleConfig();
$element_id = $config->getElementId();
$title = $config->getTitle();
$description = $config->getDescription();
$bg_image = $config->getBackgroundImage();
$css_classes = $config->getCssClasses();
$height = $config->getHeight();
$overlay_opacity = $config->getOverlayOpacity();
} catch (Exception $e) {
// 错误处理:使用默认值
$element_id = 'banner-title-' . uniqid();
$title = 'ABOUT US';
$description = 'Powering Sustainable Futures with Advanced Energy Storage Solutions';
$bg_image = get_template_directory_uri() . '/assets/images/about-bg.webp';
$css_classes = 'nenghui-banner-title banner-title-modern';
$height = '60vh';
$overlay_opacity = '0.4';
// 记录错误(如果错误处理器可用)
if (function_exists('nenghui_log_foreach_error')) {
nenghui_log_foreach_error('BannerTitleConfig initialization error: ' . $e->getMessage(), __FILE__, __LINE__);
}
}
// 验证输出变量
$element_id = !empty($element_id) ? $element_id : 'banner-title-' . uniqid();
$title = !empty($title) ? $title : 'ABOUT US';
$description = is_string($description) ? $description : '';
$bg_image = !empty($bg_image) ? $bg_image : get_template_directory_uri() . '/assets/images/about-bg.webp';
$css_classes = !empty($css_classes) ? $css_classes : 'nenghui-banner-title banner-title-modern';
$height = !empty($height) ? $height : '60vh';
$overlay_opacity = is_numeric($overlay_opacity) ? $overlay_opacity : '0.4';
?>