Files
wordpress-theme-aksrt/inc/helpers/homepage/header-components.php
T

440 lines
18 KiB
PHP

<?php
/**
* Zibll header slider visibility and rendering.
*
* This is the only header-slider rendering path. It consumes the original
* Zibll option IDs and the serialized page_config post meta contract.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
if (!function_exists('aksrt_header_slider_device_is_visible')) {
/** @param mixed $value */
function aksrt_header_slider_device_is_visible($value): bool {
$devices = array_map('sanitize_key', (array) $value);
return wp_is_mobile()
? in_array('m_s', $devices, true)
: in_array('pc_s', $devices, true);
}
}
if (!function_exists('aksrt_header_slide_excluded_page_templates')) {
/** @return list<string> */
function aksrt_header_slide_excluded_page_templates(): array {
return [
'templates/pages/page-sign.php',
'templates/pages/page-login.php',
'templates/pages/page-register.php',
'templates/pages/page-reset-password.php',
'templates/pages/page-bind-account.php',
'templates/pages/page-verify-email.php',
'templates/pages/page-download.php',
'templates/pages/page-download-file.php',
'templates/pages/page-forums.php',
];
}
}
if (!function_exists('aksrt_get_page_header_slider_config')) {
/** @return array<string,mixed> */
function aksrt_get_page_header_slider_config(): array {
if (!is_page()) {
return [];
}
$config = get_post_meta(get_queried_object_id(), 'page_config', true);
return is_array($config) && !empty($config['header_slider_show']) ? $config : [];
}
}
if (!function_exists('aksrt_header_slide_matches_query')) {
function aksrt_header_slide_matches_query(string $type): bool {
$type = sanitize_key($type);
switch ($type) {
case 'home':
return is_home() || is_front_page();
case 'category':
return is_category();
case 'tag':
return is_tag();
case 'author':
return is_author();
case 'single':
return is_singular('post');
case 'search':
return is_search();
case 'topics':
return is_tax('topics');
case 'forum_home':
return is_page_template('templates/pages/page-forums.php');
case 'forum_plate':
return is_singular('plate') || is_post_type_archive('plate');
case 'forum_post':
return is_singular('forum_post');
case 'page':
if (!is_page()) {
return false;
}
foreach (aksrt_header_slide_excluded_page_templates() as $template) {
if (is_page_template($template)) {
return false;
}
}
return true;
}
return false;
}
}
if (!function_exists('aksrt_header_slide_is_show')) {
function aksrt_header_slide_is_show(): bool {
global $wp_query;
if (is_admin() || !($wp_query instanceof WP_Query)) {
return false;
}
if ((int) $wp_query->get('paged') > 1 || (int) $wp_query->get('page') > 1) {
return false;
}
$page_config = aksrt_get_page_header_slider_config();
if ($page_config) {
$show_type = sanitize_key((string) ($page_config['header_slider_show_type'] ?? ''));
return !($show_type === 'only_pc' && wp_is_mobile())
&& !($show_type === 'only_sm' && !wp_is_mobile());
}
$settings = class_exists('\\AKSRT\\Settings\\Settings')
? \AKSRT\Settings\Settings::get_header_slider_settings()
: [];
$show_type = sanitize_key((string) ($settings['show_type'] ?? ''));
if (($show_type === 'only_pc' && wp_is_mobile()) || ($show_type === 'only_sm' && !wp_is_mobile())) {
return false;
}
foreach ((array) ($settings['show_on_pages'] ?? []) as $type) {
if (aksrt_header_slide_matches_query((string) $type)) {
return true;
}
}
return false;
}
}
if (!function_exists('aksrt_get_header_slider_args')) {
/** @return array<string,mixed> */
function aksrt_get_header_slider_args(): array {
$page_config = aksrt_get_page_header_slider_config();
if ($page_config) {
return [
'header_slider' => (array) ($page_config['header_slider'] ?? []),
'header_slider_option' => (array) ($page_config['header_slider_option'] ?? []),
'header_slider_show_type' => (string) ($page_config['header_slider_show_type'] ?? ''),
'header_slider_nav_color' => (string) ($page_config['header_slider_nav_color'] ?? ''),
'header_slider_search_s' => (array) ($page_config['header_slider_search_s'] ?? []),
'header_slider_search_option' => (array) ($page_config['header_slider_search_option'] ?? []),
'header_slider_card_s' => (array) ($page_config['header_slider_card_s'] ?? []),
'header_slider_card_option' => (array) ($page_config['header_slider_card_option'] ?? []),
];
}
$settings = class_exists('\\AKSRT\\Settings\\Settings')
? \AKSRT\Settings\Settings::get_header_slider_settings()
: [];
return [
'header_slider' => (array) ($settings['slides'] ?? []),
'header_slider_option' => (array) ($settings['slider_config'] ?? []),
'header_slider_show_type' => (string) ($settings['show_type'] ?? ''),
'header_slider_nav_color' => (string) ($settings['nav_color'] ?? ''),
'header_slider_search_s' => (array) ($settings['search_show'] ?? []),
'header_slider_search_option' => (array) ($settings['search_config'] ?? []),
'header_slider_card_s' => (array) ($settings['card_show'] ?? []),
'header_slider_card_option' => (array) ($settings['card_config'] ?? []),
];
}
}
if (!function_exists('aksrt_header_slider_allowed_html')) {
/** @return array<string,array<string,bool>> */
function aksrt_header_slider_allowed_html(): array {
return [
'a' => ['href' => true, 'target' => true, 'rel' => true, 'class' => true],
'b' => ['class' => true],
'badge' => ['class' => true],
'br' => [],
'div' => ['class' => true],
'em' => ['class' => true],
'i' => ['class' => true, 'aria-hidden' => true],
'small' => ['class' => true],
'span' => ['class' => true],
'strong' => ['class' => true],
];
}
}
if (!function_exists('aksrt_get_header_slider_markup')) {
/** @param array<string,mixed> $options */
function aksrt_get_header_slider_markup(array $options): string {
$options = wp_parse_args($options, [
'class' => 'slide-index slide-header',
'direction' => 'horizontal',
'pagination' => true,
'pagination_type' => '',
'effect' => 'slide',
'button' => true,
'loop' => true,
'scale_height' => false,
'blur_bg' => false,
'gradient_bg' => false,
'scale' => 40,
'auto_height' => false,
'm_height' => 280,
'pc_height' => 550,
'autoplay' => true,
'interval' => 6,
'speed' => 0,
'spacebetween' => 0,
'slides' => [],
]);
if (empty($options['slides'][0])) {
return '';
}
$is_mobile = wp_is_mobile();
$slides_html = '';
$slide_count = 0;
$allow = aksrt_header_slider_allowed_html();
$site_suffix = ' - ' . get_bloginfo('name');
foreach ((array) $options['slides'] as $slide) {
if (!is_array($slide)) {
continue;
}
$hide = sanitize_key((string) ($slide['hide'] ?? ''));
if ((!$is_mobile && $hide === 'pc') || ($is_mobile && $hide === 'm')) {
continue;
}
$background = esc_url((string) ($slide['background'] ?? ''));
$background_video = esc_url((string) ($slide['background_video'] ?? ''));
$video = esc_url((string) ($slide['video'] ?? ''));
$text = is_array($slide['text'] ?? null) ? $slide['text'] : [];
$link = is_array($slide['link'] ?? null) ? $slide['link'] : [];
$title = (string) ($text['title'] ?? '');
$alt = trim(wp_strip_all_tags($title));
$alt = $alt !== '' ? $alt . $site_suffix : __('幻灯片', 'aksrt') . $site_suffix;
$slide_class = trim((string) ($slide['class'] ?? ''));
$background_html = '';
if ($background_video !== '' && !$is_mobile) {
$slide_class .= ' slide-background-video';
$background_html = '<video autoplay loop muted playsinline class="fit-cover radius8" src="' . esc_url($background_video) . '"'
. ($background !== '' ? ' poster="' . esc_url($background) . '"' : '') . '></video><div class="absolute"></div>';
} elseif ($video !== '') {
$slide_class .= ' slide-dplayer';
$background_html = '<video controls playsinline preload="metadata" class="fit-cover radius8" src="' . esc_url($video) . '"'
. ($background !== '' ? ' poster="' . esc_url($background) . '"' : '') . '></video>';
if ($background !== '') {
$background_html .= '<div class="absolute dplayer-poster"><img class="no-imgbox radius8" src="' . esc_url($background) . '" alt="' . esc_attr($alt) . '"><div class="absolute flex jc play-icon"><span class="toggle-radius"><i class="fa fas fa-play" aria-hidden="true"></i></span></div></div>';
}
} elseif ($background !== '') {
$background_html = '<img class="radius8" src="' . esc_url($background) . '" alt="' . esc_attr($alt) . '">';
}
if ($background_html === '') {
continue;
}
$blur_html = '';
if (!empty($options['blur_bg']) && $background !== '') {
$blur_html = '<div class="slide-blur-bg"><img class="no-imgbox" src="' . esc_url($background) . '" alt="' . esc_attr($alt) . '"></div>';
}
$layers_html = '';
foreach ((array) ($slide['image_layer'] ?? []) as $layer) {
if (!is_array($layer) || empty($layer['image'])) {
continue;
}
$layer_class = 'absolute';
if (!empty($layer['free_size'])) {
$align = in_array(($layer['align'] ?? ''), ['left', 'center', 'right'], true) ? $layer['align'] : 'center';
$layer_class .= ' slide-layer text-' . $align;
}
$layer_attrs = '';
$parallax = (int) ($layer['parallax'] ?? 0);
if ($parallax !== 0) {
$layer_attrs .= ' data-swiper-parallax="' . esc_attr((string) $parallax) . '%"';
$opacity = max(1, min(100, (int) ($layer['parallax_opacity'] ?? 100)));
if ($opacity !== 100) {
$layer_attrs .= ' data-swiper-parallax-opacity="' . esc_attr((string) ($opacity / 100)) . '"';
}
}
$scale = max(1, min(200, (int) ($layer['parallax_scale'] ?? 100)));
if ($scale !== 100) {
$layer_attrs .= ' data-swiper-parallax-scale="' . esc_attr((string) ($scale / 100)) . '"';
}
$layers_html .= '<div class="' . esc_attr($layer_class) . '"' . $layer_attrs . '><img class="radius8" src="' . esc_url((string) $layer['image']) . '" alt="' . esc_attr($alt) . '"></div>';
}
$text_html = '';
if ($title !== '') {
$align = sanitize_html_class((string) ($text['text_align'] ?? 'left-bottom'));
$text_style = '--text-size-pc:' . max(12, min(50, (int) ($text['text_size_pc'] ?? 30))) . 'px;'
. '--text-size-m:' . max(12, min(50, (int) ($text['text_size_m'] ?? 20))) . 'px;';
$text_html = '<div class="abs-center slide-text ' . esc_attr($align) . '" style="' . esc_attr($text_style) . '"><div class="slide-title">'
. wp_kses($title, $allow) . '</div>';
if (!empty($text['desc'])) {
$text_html .= '<div class="slide-desc">' . wp_kses((string) $text['desc'], $allow) . '</div>';
}
$text_html .= '</div>';
$parallax = (int) ($text['parallax'] ?? 0);
if ($parallax !== 0) {
$text_html = '<div class="absolute" data-swiper-parallax="' . esc_attr((string) $parallax) . '">' . $text_html . '</div>';
}
}
$slide_attrs = '';
if (!empty($options['gradient_bg'])) {
$slide_class .= ' gradient-bg';
$slide_attrs .= ' data-opacity="0.2"';
}
$url = esc_url((string) ($link['url'] ?? ''));
$target = !empty($link['target']) ? ' target="_blank" rel="noopener noreferrer"' : '';
$slides_html .= '<div class="swiper-slide ' . esc_attr(trim($slide_class)) . '"' . $slide_attrs . '>' . $blur_html;
$slides_html .= $url !== '' ? '<a href="' . esc_url($url) . '"' . $target . '>' : '<span>';
$slides_html .= $background_html . $layers_html . $text_html;
$slides_html .= $url !== '' ? '</a>' : '</span>';
$slides_html .= '</div>';
$slide_count++;
}
if ($slide_count === 0) {
return '';
}
$class = trim((string) $options['class']);
$class .= !empty($options['blur_bg']) ? ' blur-background' : '';
$class .= !empty($options['gradient_bg']) ? ' gradient-background' : '';
$class .= $slide_count === 1 ? ' swiper-container-initialized' : '';
$attrs = ' data-direction="' . esc_attr((string) $options['direction']) . '" data-effect="' . esc_attr((string) $options['effect']) . '"';
$attrs .= !empty($options['loop']) ? ' data-loop="true"' : '';
$attrs .= !empty($options['autoplay']) ? ' data-autoplay="1" data-interval="' . esc_attr((string) ((int) $options['interval'] < 999 ? (int) $options['interval'] * 1000 : (int) $options['interval'])) . '"' : '';
$attrs .= (int) $options['speed'] > 299 ? ' data-speed="' . esc_attr((string) (int) $options['speed']) . '"' : '';
$attrs .= isset($options['spacebetween']) ? ' data-spaceBetween="' . esc_attr((string) (int) $options['spacebetween']) . '"' : '';
if (!empty($options['pagination_type']) && !empty($options['pagination']) && $slide_count > 1) {
$attrs .= ' data-pagination-type="' . esc_attr((string) $options['pagination_type']) . '"';
}
if (!empty($options['scale_height']) && $options['direction'] !== 'vertical') {
$class .= ' scale-height';
$style = '--scale-height :' . max(10, min(300, (int) $options['scale'])) . '%';
} elseif (!empty($options['auto_height']) && $options['direction'] !== 'vertical') {
$attrs .= ' auto-height="true"';
$style = '--max-height :' . max(120, (int) ($options['max_height'] ?? 500)) . 'px;--min-height :' . max(100, (int) ($options['min_height'] ?? 180)) . 'px;';
} else {
$style = '--m-height :' . max(100, (int) $options['m_height']) . 'px;--pc-height :' . max(120, (int) $options['pc_height']) . 'px;';
}
$controls = '';
if ($slide_count > 1 && !empty($options['button'])) {
$controls .= '<div class="swiper-button-prev"></div><div class="swiper-button-next"></div>';
}
if ($slide_count > 1 && !empty($options['pagination'])) {
$pagination_class = !empty($options['pagination_type'])
? ' scroll-x no-scrollbar pagination-type-' . sanitize_html_class((string) $options['pagination_type'])
: '';
$controls .= '<div class="swiper-pagination kaoyou' . esc_attr($pagination_class) . '"></div>';
}
return '<div class="relative zib-slider"><div class="new-swiper ' . esc_attr(trim($class)) . '"' . $attrs . ' style="' . esc_attr($style) . '"><div class="swiper-wrapper">'
. $slides_html . '</div>' . $controls . '</div></div>';
}
}
if (!function_exists('aksrt_header_slide')) {
function aksrt_header_slide(): void {
if (!aksrt_header_slide_is_show()) {
return;
}
$args = aksrt_get_header_slider_args();
$slides = (array) ($args['header_slider'] ?? []);
$options = (array) ($args['header_slider_option'] ?? []);
if (empty($slides[0])) {
return;
}
$options['slides'] = $slides;
$options['class'] = 'slide-index slide-header';
$search_html = '';
if (aksrt_header_slider_device_is_visible($args['header_slider_search_s'] ?? [])) {
$search_options = wp_parse_args((array) ($args['header_slider_search_option'] ?? []), [
'class' => '', 'show_keywords' => false, 'show_history' => false,
'show_posts' => false, 'show_input_cat' => false, 'more_cats' => [],
'in_cat' => '', 'show_type' => false, 'in_type' => 'post', 'in_user' => '',
'before_html' => '', 'after_html' => '',
]);
if (wp_is_mobile()) {
$search_options['show_keywords'] = false;
}
$before = $search_options['before_html'] !== '' ? '<div class="header-slider-search-more text-center before">' . wp_kses((string) $search_options['before_html'], aksrt_header_slider_allowed_html()) . '</div>' : '';
$after = $search_options['after_html'] !== '' ? '<div class="header-slider-search-more after">' . wp_kses((string) $search_options['after_html'], aksrt_header_slider_allowed_html()) . '</div>' : '';
unset($search_options['before_html'], $search_options['after_html']);
$box = function_exists('aksrt_get_search_box') ? (string) aksrt_get_search_box($search_options) : '';
$search_html = '<div class="header-slider-search abs-center">' . $before . $box . $after . '</div>';
}
$cards_html = '';
$card_count = 0;
if (aksrt_header_slider_device_is_visible($args['header_slider_card_s'] ?? [])) {
$card_options = wp_parse_args((array) ($args['header_slider_card_option'] ?? []), [
'show_widget_bg' => false, 'icon_radius4' => false, 'cards' => [],
]);
$items = '';
foreach ((array) $card_options['cards'] as $card) {
if (!is_array($card) || (empty($card['icon']) && empty($card['customize_icon']))) {
continue;
}
$card['class'] = !empty($card_options['show_widget_bg']) ? 'padding-10' : 'zib-widget';
$card['icon_class'] = trim((string) ($card['icon_class'] ?? '') . (!empty($card_options['icon_radius4']) ? ' radius4' : ''));
$inner = function_exists('aksrt_icon_cover_card') ? aksrt_icon_cover_card($card) : '';
if ($inner === '') {
continue;
}
$items .= !empty($card_options['show_widget_bg']) ? '<div class="relative-h">' : '<div class="flex1">';
$items .= $inner . '</div>';
$card_count++;
}
if ($items !== '') {
$cards_html = '<div class="header-slider-card container">'
. (!empty($card_options['show_widget_bg']) ? '<div class="zib-widget padding-10">' : '<div class="">')
. '<div class="flex jse flex-row gutters-5 flex-col-sm-2">' . $items . '</div></div></div>';
}
}
$nav_color = sanitize_hex_color((string) ($args['header_slider_nav_color'] ?? ''));
if ($nav_color && $nav_color !== '#ffffff') {
echo '<style>body.nav-fixed:not(.body-scroll):not(.mobile-navbar-show) .header.show-slide{--header-color:' . esc_html($nav_color) . ';}</style>';
}
$settings = class_exists('\\AKSRT\\Settings\\Settings') ? \AKSRT\Settings\Settings::get_header_slider_settings() : [];
$filter_blur = aksrt_header_slider_device_is_visible($settings['filter_blur'] ?? []);
$slider_html = aksrt_get_header_slider_markup($options);
if ($slider_html === '') {
return;
}
echo '<div class="header-slider-container relative mb20 card-' . (int) $card_count . ($filter_blur ? ' filter-blur' : '') . '">';
echo $slider_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $search_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $cards_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '</div>';
}
}