feat(widgets): simplify locations and paid sidebar
Remove deprecated widget locations and widgets, standardize sidebar labels, and render a compact paid-post purchase card in the article sidebar.
This commit is contained in:
1 parent
51d2ab3aa7
commit
bd7d3046a5
43 files changed
+294
-3581
No files matched your search
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* Header and mobile navigation language controls.
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* Header language dropdown (zibll zib_get_translate_language_btn).
|
||||
* Uses SPA i18n_available_langs when i18n enabled and position includes header.
|
||||
*
|
||||
* @param string $class Extra classes.
|
||||
*/
|
||||
function aksrt_get_translate_language_btn(string $class = ''): string {
|
||||
if (!class_exists('\AKSRT\Settings\Settings')) {
|
||||
return '';
|
||||
}
|
||||
$cfg = \AKSRT\Settings\Settings::get_i18n_config();
|
||||
if (empty($cfg['enabled'])) {
|
||||
return '';
|
||||
}
|
||||
$pos = (string) ($cfg['switcherPosition'] ?? 'footer');
|
||||
if (!in_array($pos, ['header', 'both'], true)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$raw = (string) ($cfg['availableLangs'] ?? '');
|
||||
$buttons = [];
|
||||
foreach (preg_split('/\r\n|\r|\n/', $raw) as $line) {
|
||||
$line = trim($line);
|
||||
if ($line === '' || strpos($line, '|') === false) {
|
||||
continue;
|
||||
}
|
||||
[$code, $label] = array_map('trim', explode('|', $line, 2));
|
||||
if ($code === '' || $label === '') {
|
||||
continue;
|
||||
}
|
||||
$buttons[] = ['language' => $code, 'text' => $label];
|
||||
}
|
||||
if (!$buttons) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$buttons_html = '';
|
||||
foreach ($buttons as $button) {
|
||||
$buttons_html .= '<li><a href="javascript:;" class="translate-button translate-ignore flex ac" data-language="'
|
||||
. esc_attr($button['language']) . '">' . esc_html($button['text']) . '</a></li>';
|
||||
}
|
||||
$is_mobile = wp_is_mobile();
|
||||
$dropdown = '<ul class="dropdown-menu hover-show-con">' . $buttons_html . '</ul>';
|
||||
$hover = !$is_mobile ? ' hover-show' : '';
|
||||
$toggle = !$is_mobile ? '' : ' data-toggle="dropdown"';
|
||||
return '<div class="dropdown language-dropdown ' . esc_attr($class) . $hover . '">'
|
||||
. '<a href="javascript:;" class="toggle-radius"' . $toggle . ' title="' . esc_attr__('切换语言', 'aksrt') . '">'
|
||||
. '<i class="fas fa-language" aria-hidden="true"></i></a>' . $dropdown . '</div>';
|
||||
}
|
||||
@@ -1,202 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* AKSRT i18n Switch — 多语言切换辅助函数
|
||||
*
|
||||
* 支持 Polylang、WPML 或 WordPress 内置语言切换。
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* 获取可用语言列表
|
||||
*
|
||||
* @return array<array{code: string, name: string, url: string, flag: string, active: bool}>
|
||||
*/
|
||||
function aksrt_get_available_languages(): array {
|
||||
$languages = [];
|
||||
|
||||
// Polylang
|
||||
if (function_exists('pll_the_languages')) {
|
||||
$pll_langs = pll_the_languages(['raw' => 1]);
|
||||
if (is_array($pll_langs)) {
|
||||
foreach ($pll_langs as $lang) {
|
||||
$languages[] = [
|
||||
'code' => $lang['slug'] ?? '',
|
||||
'name' => $lang['name'] ?? '',
|
||||
'url' => $lang['url'] ?? '#',
|
||||
'flag' => $lang['flag'] ?? '',
|
||||
'active' => !empty($lang['current_lang']),
|
||||
];
|
||||
}
|
||||
}
|
||||
return $languages;
|
||||
}
|
||||
|
||||
// WPML
|
||||
if (defined('ICL_LANGUAGE_CODE') && function_exists('icl_get_languages')) {
|
||||
$wpml_langs = icl_get_languages('skip_missing=0');
|
||||
if (is_array($wpml_langs)) {
|
||||
foreach ($wpml_langs as $lang) {
|
||||
$languages[] = [
|
||||
'code' => $lang['language_code'] ?? '',
|
||||
'name' => $lang['native_name'] ?? '',
|
||||
'url' => $lang['url'] ?? '#',
|
||||
'flag' => $lang['country_flag_url'] ?? '',
|
||||
'active' => !empty($lang['active']),
|
||||
];
|
||||
}
|
||||
}
|
||||
return $languages;
|
||||
}
|
||||
|
||||
// WordPress 内置:通过 filter 提供语言列表
|
||||
$filtered = apply_filters('aksrt_available_languages', []);
|
||||
if (is_array($filtered) && !empty($filtered)) {
|
||||
return $filtered;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否有多语言插件可用
|
||||
*/
|
||||
function aksrt_has_multilingual(): bool {
|
||||
if (function_exists('pll_the_languages')) return true;
|
||||
if (defined('ICL_LANGUAGE_CODE')) return true;
|
||||
if (!empty(apply_filters('aksrt_available_languages', []))) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前语言代码
|
||||
*/
|
||||
function aksrt_get_current_language(): string {
|
||||
if (function_exists('pll_current_language')) {
|
||||
return pll_current_language('slug');
|
||||
}
|
||||
if (defined('ICL_LANGUAGE_CODE')) {
|
||||
return ICL_LANGUAGE_CODE;
|
||||
}
|
||||
return get_locale();
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染语言切换器 HTML
|
||||
*
|
||||
* @param string $style dropdown | inline | flags
|
||||
* @return string
|
||||
*/
|
||||
function aksrt_render_language_switcher(string $style = 'dropdown'): string {
|
||||
$languages = aksrt_get_available_languages();
|
||||
if (count($languages) < 2) return '';
|
||||
|
||||
$current = aksrt_get_current_language();
|
||||
|
||||
if ($style === 'flags') {
|
||||
$html = '<div class="aksrt-lang-switch aksrt-lang-switch--flags">';
|
||||
foreach ($languages as $lang) {
|
||||
$active_class = $lang['active'] ? ' is-active' : '';
|
||||
$html .= '<a href="' . esc_url($lang['url']) . '" class="aksrt-lang-switch__flag' . esc_attr($active_class) . '" title="' . esc_attr($lang['name']) . '">';
|
||||
if ($lang['flag']) {
|
||||
$html .= '<img src="' . esc_url($lang['flag']) . '" alt="' . esc_attr($lang['code']) . '" width="20" height="14">';
|
||||
} else {
|
||||
$html .= '<span>' . esc_html(strtoupper($lang['code'])) . '</span>';
|
||||
}
|
||||
$html .= '</a>';
|
||||
}
|
||||
$html .= '</div>';
|
||||
return $html;
|
||||
}
|
||||
|
||||
// dropdown style
|
||||
$html = '<div class="aksrt-lang-switch">';
|
||||
$html .= '<button type="button" class="aksrt-lang-switch__toggle" aria-haspopup="true" aria-expanded="false">';
|
||||
|
||||
// 当前语言
|
||||
foreach ($languages as $lang) {
|
||||
if ($lang['active'] || $lang['code'] === $current) {
|
||||
if ($lang['flag']) {
|
||||
$html .= '<img src="' . esc_url($lang['flag']) . '" alt="" width="18" height="12"> ';
|
||||
}
|
||||
$html .= '<span>' . esc_html($lang['name']) . '</span>';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$html .= ' <i class="fas fa-chevron-down" style="font-size:10px;margin-left:4px;"></i>';
|
||||
$html .= '</button>';
|
||||
|
||||
$html .= '<ul class="aksrt-lang-switch__dropdown" role="menu">';
|
||||
foreach ($languages as $lang) {
|
||||
$active_class = $lang['active'] ? ' is-active' : '';
|
||||
$html .= '<li role="menuitem">';
|
||||
$html .= '<a href="' . esc_url($lang['url']) . '" class="aksrt-lang-switch__item' . esc_attr($active_class) . '">';
|
||||
if ($lang['flag']) {
|
||||
$html .= '<img src="' . esc_url($lang['flag']) . '" alt="" width="18" height="12"> ';
|
||||
}
|
||||
$html .= esc_html($lang['name']);
|
||||
$html .= '</a>';
|
||||
$html .= '</li>';
|
||||
}
|
||||
$html .= '</ul>';
|
||||
|
||||
$html .= '</div>';
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Header language dropdown (zibll zib_get_translate_language_btn).
|
||||
* Uses SPA i18n_available_langs when i18n enabled and position includes header.
|
||||
*
|
||||
* @param string $class Extra classes.
|
||||
*/
|
||||
function aksrt_get_translate_language_btn(string $class = ''): string {
|
||||
if (!class_exists('\AKSRT\Settings\Settings')) {
|
||||
return '';
|
||||
}
|
||||
$cfg = \AKSRT\Settings\Settings::get_i18n_config();
|
||||
if (empty($cfg['enabled'])) {
|
||||
return '';
|
||||
}
|
||||
$pos = (string) ($cfg['switcherPosition'] ?? 'footer');
|
||||
if (!in_array($pos, ['header', 'both'], true)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$raw = (string) ($cfg['availableLangs'] ?? '');
|
||||
$buttons = [];
|
||||
foreach (preg_split('/
|
||||
|␍|
|
||||
/', $raw) as $line) {
|
||||
$line = trim($line);
|
||||
if ($line === '' || strpos($line, '|') === false) {
|
||||
continue;
|
||||
}
|
||||
[$code, $label] = array_map('trim', explode('|', $line, 2));
|
||||
if ($code === '' || $label === '') {
|
||||
continue;
|
||||
}
|
||||
$buttons[] = ['language' => $code, 'text' => $label];
|
||||
}
|
||||
if (!$buttons) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$buttons_html = '';
|
||||
foreach ($buttons as $button) {
|
||||
$buttons_html .= '<li><a href="javascript:;" class="translate-button translate-ignore flex ac" data-language="'
|
||||
. esc_attr($button['language']) . '">' . esc_html($button['text']) . '</a></li>';
|
||||
}
|
||||
$is_mobile = wp_is_mobile();
|
||||
$dropdown = '<ul class="dropdown-menu hover-show-con">' . $buttons_html . '</ul>';
|
||||
$hover = !$is_mobile ? ' hover-show' : '';
|
||||
$toggle = !$is_mobile ? '' : ' data-toggle="dropdown"';
|
||||
return '<div class="dropdown language-dropdown ' . esc_attr($class) . $hover . '">'
|
||||
. '<a href="javascript:;" class="toggle-radius"' . $toggle . ' title="' . esc_attr__('切换语言', 'aksrt') . '">'
|
||||
. '<i class="fas fa-language" aria-hidden="true"></i></a>' . $dropdown . '</div>';
|
||||
}
|
||||
|
||||
@@ -100,9 +100,16 @@ if (!function_exists('aksrt_single_build_context')) :
|
||||
$show_pay_box = false;
|
||||
}
|
||||
|
||||
$show_aside = function_exists('aksrt_is_show_sidebar')
|
||||
? aksrt_is_show_sidebar()
|
||||
: false;
|
||||
$layout = function_exists('aksrt_get_post_show_layout')
|
||||
? aksrt_get_post_show_layout($post_id)
|
||||
: '';
|
||||
$has_article_widgets = is_active_sidebar('all_sidebar_top')
|
||||
|| is_active_sidebar('sidebar-article')
|
||||
|| is_active_sidebar('all_sidebar_bottom');
|
||||
$show_aside = !wp_is_mobile()
|
||||
&& get_post_type($post_id) === 'post'
|
||||
&& $layout !== 'no_sidebar'
|
||||
&& $has_article_widgets;
|
||||
|
||||
return [
|
||||
'post_id' => $post_id,
|
||||
|
||||
@@ -50,7 +50,7 @@ function aksrt_resolve_sidebar_id(): string {
|
||||
return 'home_sidebar';
|
||||
}
|
||||
|
||||
return 'sidebar-blog';
|
||||
return 'home_sidebar';
|
||||
}
|
||||
endif;
|
||||
|
||||
@@ -206,6 +206,27 @@ if (!function_exists('aksrt_get_site_layout')):
|
||||
* Zibll site-layout mode: 1=no sidebar, 2=right, 3=left.
|
||||
*/
|
||||
function aksrt_get_site_layout(): int {
|
||||
if (is_single()) {
|
||||
$post_id = (int) get_queried_object_id();
|
||||
$layout = aksrt_get_post_show_layout($post_id);
|
||||
$has_article_widgets = is_active_sidebar('all_sidebar_top')
|
||||
|| is_active_sidebar('sidebar-article')
|
||||
|| is_active_sidebar('all_sidebar_bottom');
|
||||
if (get_post_type($post_id) !== 'post' || wp_is_mobile() || $layout === 'no_sidebar' || !$has_article_widgets) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ($layout === 'sidebar_left') {
|
||||
return 3;
|
||||
}
|
||||
if ($layout === 'sidebar_right') {
|
||||
return 2;
|
||||
}
|
||||
|
||||
$cfg = aksrt_get_sidebar_config_safe();
|
||||
return ($cfg['position'] ?? 'right') === 'left' ? 3 : 2;
|
||||
}
|
||||
|
||||
if (!aksrt_is_show_sidebar()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -153,7 +153,6 @@ if (!function_exists('aksrt_homepage_has_sidebar')) {
|
||||
'all_sidebar_top',
|
||||
'home_sidebar',
|
||||
'all_sidebar_bottom',
|
||||
'sidebar-blog',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -175,8 +174,6 @@ if (!function_exists('aksrt_homepage_render_sidebar')) {
|
||||
|
||||
if (is_active_sidebar('home_sidebar')) {
|
||||
dynamic_sidebar('home_sidebar');
|
||||
} elseif (is_active_sidebar('sidebar-blog')) {
|
||||
dynamic_sidebar('sidebar-blog');
|
||||
}
|
||||
|
||||
if (is_active_sidebar('all_sidebar_bottom')) {
|
||||
|
||||
Reference in new issue
Block a user