Files

240 lines
9.1 KiB
PHP

<?php
/**
* 右侧悬浮按钮前台输出(结构对齐参考主题 zib_float_right())。
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
if (!function_exists('aksrt_float_btn_style_attr')) :
/**
* 自定义颜色 → 内联 CSS 变量。
*
* @param mixed $color
*/
function aksrt_float_btn_style_attr($color): string {
$color = is_array($color) ? $color : [];
$style = '';
if (!empty($color['color'])) {
$style .= '--this-color:' . $color['color'] . ';';
}
if (!empty($color['bg'])) {
$style .= '--this-bg:' . $color['bg'] . ';';
}
return $style !== '' ? ' style="' . esc_attr($style) . '"' : '';
}
endif;
if (!function_exists('aksrt_float_btn_tooltip_attr')) :
/**
* PC 端 tooltip(参考主题仅 PC 输出 data-toggle)。
*/
function aksrt_float_btn_tooltip_attr(string $title, bool $is_mobile): string {
$attr = !$is_mobile ? ' data-toggle="tooltip"' : '';
return $attr . ' data-placement="left" title="' . esc_attr($title) . '"';
}
endif;
if (!function_exists('aksrt_get_float_pay_vip_html')) :
/**
* VIP 购买按钮:登录且未满级 → .pay-vip;访客 → .signin-loader。
*/
function aksrt_get_float_pay_vip_html(string $style, bool $is_mobile): string {
if (!class_exists('\AKSRT\Settings\Settings') || \AKSRT\Settings\Settings::close_sign()) {
return '';
}
$vip = \AKSRT\Settings\Settings::get_payment_vip_settings();
$vip1_s = !empty($vip['levels'][1]['enabled']);
$vip2_s = !empty($vip['levels'][2]['enabled']);
if (!$vip1_s && !$vip2_s) {
return '';
}
$icon = '<svg class="icon" aria-hidden="true"><use xlink:href="#icon-vip_1"></use></svg>';
$user_id = get_current_user_id();
if (!$user_id) {
return '<a' . $style . ' class="float-btn signin-loader"' . aksrt_float_btn_tooltip_attr(__('开通会员', 'aksrt'), $is_mobile) . ' href="javascript:;">' . $icon . '</a>';
}
$level = function_exists('aksrt_get_user_vip_level') ? (int) aksrt_get_user_vip_level($user_id) : 0;
if ($level && !($level < 2 && $vip2_s)) {
return '';
}
$title = 1 === $level ? __('升级会员', 'aksrt') : __('开通会员', 'aksrt');
$open_level = $vip2_s ? 2 : 1;
return '<a' . $style . ' class="float-btn pay-vip" vip-level="' . esc_attr((string) $open_level) . '"' . aksrt_float_btn_tooltip_attr($title, $is_mobile) . ' href="javascript:;">' . $icon . '</a>';
}
endif;
if (!function_exists('aksrt_get_float_add_html')) :
/**
* 发布按钮:当前仅文章投稿,对应参考主题单按钮分支(直接链接)。
*/
function aksrt_get_float_add_html(string $icon, string $style, bool $is_mobile): string {
if (!function_exists('aksrt_get_new_add_actions')) {
return '';
}
if (class_exists('\AKSRT\Settings\Settings') && \AKSRT\Settings\Settings::close_sign()) {
return '';
}
$actions = aksrt_get_new_add_actions(['post']);
if (!$actions) {
return '';
}
$a = $actions[0];
$icon_html = $icon !== '' ? aksrt_icon_markup($icon) : '';
if ($icon_html === '') {
$icon_html = '<i class="fa fas fa-plus" aria-hidden="true"></i>';
}
return '<a' . $style . ' rel="nofollow" class="newadd-btns float-btn add-btn"' . aksrt_float_btn_tooltip_attr((string) $a['label'], $is_mobile) . ' href="' . esc_url($a['url']) . '">' . $icon_html . '</a>';
}
endif;
if (!function_exists('aksrt_get_float_more_html')) :
/**
* 更多(自定义)按钮组。
*/
function aksrt_get_float_more_html(array $buttons, bool $is_mobile): string {
$html = '';
foreach ($buttons as $opt) {
if (!is_array($opt)) {
continue;
}
if (!(($is_mobile && !empty($opt['m_s'])) || (!$is_mobile && !empty($opt['pc_s'])))) {
continue;
}
$style = aksrt_float_btn_style_attr($opt['color'] ?? []);
$class = 'float-btn more-btn';
$hover = '';
$tag = 'a';
$tooltip = true;
$hover_html = (string) ($opt['hover'] ?? '');
if ($hover_html !== '') {
$tag = 'span';
$tooltip = false;
$class .= ' hover-show';
$width = (int) ($opt['hover_width'] ?? 200);
$hover_style = $width && 200 !== $width ? ' style="width:' . $width . 'px;"' : '';
$hover = '<div' . $hover_style . ' class="hover-show-con dropdown-menu">' . wp_kses_post($hover_html) . '</div>';
}
$icon = aksrt_icon_markup((string) ($opt['icon'] ?? ''));
if ($icon === '') {
$icon = '<i class="fa fas fa-heart" aria-hidden="true"></i>';
}
$url = (string) ($opt['link']['url'] ?? '');
$href = ($tag === 'a' && $url !== '') ? esc_url($url) : 'javascript:;';
$target = ($tag === 'a' && $url !== '' && ($opt['link']['target'] ?? '') === '_blank') ? ' target="_blank" rel="noopener"' : '';
$desc = (string) ($opt['desc'] ?? '');
$title = $desc !== '' ? ($tooltip ? aksrt_float_btn_tooltip_attr($desc, $is_mobile) : ' title="' . esc_attr($desc) . '"') : '';
if ('javascript:;' === $href) {
$class .= ' nowave';
}
$href_attr = $tag === 'a' ? ' href="' . $href . '"' : '';
$html .= '<' . $tag . $style . ' class="' . esc_attr($class) . '"' . $target . $title . $href_attr . '>' . $icon . $hover . '</' . $tag . '>';
}
return $html;
}
endif;
if (!function_exists('aksrt_float_right')) :
/**
* 输出右侧悬浮按钮。
*/
function aksrt_float_right(): void {
if (!class_exists('\AKSRT\Settings\Settings') || is_page_template('templates/pages/page-submit.php')) {
return;
}
$settings = \AKSRT\Settings\Settings::get_floating_buttons_settings();
$items = is_array($settings['items'] ?? null) ? $settings['items'] : [];
$is_mobile = wp_is_mobile();
$btn = '';
foreach ($items as $opt) {
$type = (string) ($opt['type'] ?? '');
if ('more' !== $type && ((empty($opt['pc_s']) && !$is_mobile) || (empty($opt['m_s']) && $is_mobile))) {
continue;
}
$style = aksrt_float_btn_style_attr($opt['color'] ?? []);
switch ($type) {
case 'theme_mode':
$btn .= '<a' . $style . ' class="float-btn toggle-theme hover-show"' . aksrt_float_btn_tooltip_attr(__('切换主题', 'aksrt'), $is_mobile) . ' href="javascript:;"><i class="fa fas fa-toggle-theme"></i></a>';
break;
case 'back_top':
$btn .= '<a' . $style . ' class="float-btn ontop fade"' . aksrt_float_btn_tooltip_attr(__('返回顶部', 'aksrt'), $is_mobile) . ' href="javascript:(scrollTopTo());"><i class="fa fas fa-angle-up em12"></i></a>';
break;
case 'service_qq':
$qq = (string) ($opt['qq'] ?? '');
if ($qq !== '') {
$href = 'https://wpa.qq.com/msgrd?v=3&uin=' . $qq . '&site=qq&menu=yes';
$btn .= '<a' . $style . ' class="float-btn service-qq"' . aksrt_float_btn_tooltip_attr(__('QQ联系', 'aksrt'), $is_mobile) . ' target="_blank" rel="noopener" href="' . esc_url($href) . '"><i class="fa fab fa-qq"></i></a>';
}
break;
case 'pay_vip':
$btn .= aksrt_get_float_pay_vip_html($style, $is_mobile);
break;
case 'service_wechat':
$img = (string) ($opt['wechat_img'] ?? '');
if ($img !== '') {
$alt = sprintf(__('扫码添加微信%s', 'aksrt'), ' - ' . get_bloginfo('name'));
$img_attr = function_exists('aksrt_get_lazy_attr')
? aksrt_get_lazy_attr('lazy_other', $img, 'radius4 relative')
: ' class="radius4 relative" src="' . esc_url($img) . '"';
$btn .= '<a' . $style . ' class="float-btn service-wechat hover-show nowave" title="' . esc_attr__('扫码添加微信', 'aksrt') . '" href="javascript:;"><i class="fa fab fa-weixin"></i>'
. '<div class="hover-show-con dropdown-menu"><img width="100%"' . $img_attr . ' alt="' . esc_attr($alt) . '"></div></a>';
}
break;
case 'qrcode':
$desc = (string) ($opt['desc'] ?? '');
$desc = $desc !== '' ? '<div class="mt6 px12 muted-color">' . esc_html($desc) . '</div>' : '';
$btn .= '<span' . $style . ' class="float-btn qrcode-btn hover-show service-wechat"><i class="fa fas fa-qrcode"></i>'
. '<div class="hover-show-con dropdown-menu"><div class="qrcode" data-size="100"></div>' . $desc . '</div></span>';
break;
case 'add':
// 当前仅支持文章投稿,发布类型固定为 post。
$btn .= aksrt_get_float_add_html((string) ($opt['icon'] ?? ''), $style, $is_mobile);
break;
case 'more':
$btn .= aksrt_get_float_more_html((array) ($opt['buttons'] ?? []), $is_mobile);
break;
}
}
if ($btn === '') {
return;
}
// 扩展点(对齐参考主题 zib_float_right filter):模块可注入自定义悬浮按钮。
$btn = (string) apply_filters('aksrt_float_right_buttons', $btn, $is_mobile);
if ($btn === '') {
return;
}
$filter = (array) ($settings['filter_blur'] ?? []);
$class = ($settings['style'] ?? 'round') . ' position-' . ($settings['position'] ?? 'bottom');
$class .= (($is_mobile && in_array('m_s', $filter, true)) || (!$is_mobile && in_array('pc_s', $filter, true))) ? ' filter' : '';
$class .= !empty($settings['scroll_hide']) ? ' scrolling-hide' : '';
$class .= !empty($settings['scroll_down_hide']) ? ' scroll-down-hide' : '';
echo '<div class="float-right ' . esc_attr($class) . '">' . $btn . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- 各片段已转义
}
add_action('wp_footer', 'aksrt_float_right');
endif;