Merge pull request 'Worktree ux depth batch11' (#229) from worktree-ux-depth-batch11 into main

Reviewed-on: CYL/wordpress-theme-aksrt#229
This commit is contained in:
2026-07-21 08:27:42 +08:00
4 changed files with 787 additions and 131 deletions
+307 -7
View File
@@ -453,19 +453,67 @@
display: flex;
align-items: center;
gap: 10px;
padding: 10px 14px;
padding: 8px 14px;
border-radius: 10px;
background: #eff6ff;
color: #1d4ed8;
margin-bottom: 8px;
overflow: hidden;
}
.aksrt-scroll-notice.is-radius { border-radius: 999px; }
.aksrt-scroll-notice.is-radius,
.aksrt-scroll-notice.radius { border-radius: 999px; }
.aksrt-scroll-notice.color-c-green { background: #ecfdf5; color: #047857; }
.aksrt-scroll-notice.color-c-yellow { background: #fffbeb; color: #b45309; }
.aksrt-scroll-notice.color-c-red { background: #fef2f2; color: #b91c1c; }
.aksrt-scroll-notice.color-c-purple { background: #f5f3ff; color: #6d28d9; }
.aksrt-scroll-notice.color-c-blue-2 { background: #dbeafe; color: #1e3a8a; }
.aksrt-scroll-notice.color-c-green-2 { background: #d1fae5; color: #065f46; }
.aksrt-scroll-notice.color-c-yellow-2 { background: #ffedd5; color: #9a3412; }
.aksrt-scroll-notice.color-c-red-2 { background: #fee2e2; color: #991b1b; }
.aksrt-scroll-notice.color-c-purple-2 { background: #ede9fe; color: #5b21b6; }
.aksrt-scroll-notice.color-b-theme { background: var(--color-primary, #3b82f6); color: #fff; }
.aksrt-scroll-notice.color-b-red { background: #ef4444; color: #fff; }
.aksrt-scroll-notice.color-b-yellow { background: #f59e0b; color: #fff; }
.aksrt-scroll-notice.color-b-blue { background: #3b82f6; color: #fff; }
.aksrt-scroll-notice.color-b-green { background: #10b981; color: #fff; }
.aksrt-scroll-notice.color-b-purple { background: #8b5cf6; color: #fff; }
.aksrt-scroll-notice.text-center { justify-content: center; text-align: center; }
.aksrt-scroll-notice.text-right { justify-content: flex-end; text-align: right; }
.aksrt-scroll-notice.is-affix[data-affix="true"] {
position: sticky;
top: calc(var(--nav-height, 60px) + 12px);
z-index: 5;
}
.aksrt-scroll-notice__icon { flex-shrink: 0; }
.aksrt-scroll-notice .aksrt-notice-swiper,
.aksrt-scroll-notice .new-swiper {
flex: 1;
min-width: 0;
height: 1.5em;
margin: 0;
}
.aksrt-scroll-notice .swiper-slide {
height: 1.5em !important;
display: flex;
align-items: center;
}
.aksrt-scroll-notice .swiper-slide a,
.aksrt-scroll-notice .swiper-slide .text-ellipsis {
display: block;
width: 100%;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
color: inherit;
text-decoration: none;
font-size: 13px;
line-height: 1.5;
}
.aksrt-scroll-notice .bulletin-icon {
display: inline-flex;
width: 1.1em;
margin-right: 6px;
}
.aksrt-scroll-notice__viewport { position: relative; flex: 1; min-width: 0; height: 1.4em; }
.aksrt-scroll-notice__item {
position: absolute;
@@ -476,17 +524,14 @@
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
line-height: 1.4em;
font-size: 13px;
line-height: 1.4;
}
.aksrt-scroll-notice__item.is-active {
opacity: 1;
transform: translateY(0);
}
.aksrt-scroll-notice__item a {
color: inherit;
text-decoration: none;
}
.aksrt-scroll-notice__item a { color: inherit; text-decoration: none; }
.aksrt-scroll-notice__item i { margin-right: 6px; opacity: .85; }
.hot-post-item { display: flex; align-items: center; gap: 10px; }
@@ -1003,3 +1048,258 @@
color: var(--muted-2-color, #999);
text-align: center;
}
/* ---- batch11: search widget type/cat + FAQ structure ---- */
.aksrt-search-widget__form { margin: 0; }
.aksrt-search-widget__types {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin-bottom: 10px;
}
.aksrt-search-widget__type {
border: 1px solid var(--color-line, #e5e7eb);
background: var(--surface-2, #f8fafc);
color: inherit;
border-radius: 999px;
padding: 4px 12px;
font-size: 12px;
font-weight: 600;
cursor: pointer;
line-height: 1.4;
}
.aksrt-search-widget__type.is-active {
background: var(--color-primary, #3b82f6);
border-color: var(--color-primary, #3b82f6);
color: #fff;
}
.aksrt-search-widget__line {
display: flex;
align-items: stretch;
gap: 0;
border: 1px solid var(--color-line, #e5e7eb);
border-radius: 10px;
overflow: hidden;
background: var(--surface-1, #fff);
}
.aksrt-search-widget__cat {
flex: 0 0 auto;
max-width: 38%;
border-right: 1px solid var(--color-line, #e5e7eb);
background: var(--surface-2, #f8fafc);
}
.aksrt-search-widget__cat-select {
width: 100%;
height: 42px;
border: 0;
background: transparent;
padding: 0 10px;
font-size: 13px;
color: inherit;
outline: none;
cursor: pointer;
}
.aksrt-search-widget__field {
position: relative;
flex: 1;
min-width: 0;
display: flex;
align-items: center;
}
.aksrt-search-widget__field .widget-search-box__input,
.aksrt-search-widget .widget-search-box__input {
width: 100%;
height: 42px;
border: 0;
background: transparent;
padding: 0 44px 0 12px;
font-size: 14px;
color: inherit;
outline: none;
}
.aksrt-search-widget .widget-search-box__btn {
position: absolute;
right: 4px;
top: 50%;
transform: translateY(-50%);
width: 34px;
height: 34px;
border: 0;
border-radius: 8px;
background: var(--color-primary, #3b82f6);
color: #fff;
cursor: pointer;
display: inline-flex;
align-items: center;
justify-content: center;
}
.aksrt-search-widget .widget-search-box__btn:hover { filter: brightness(1.05); }
/* FAQ structure parity */
.aksrt-faq.produck-wid-faq-block { margin: 0; }
.aksrt-faq__head { margin-bottom: 14px; }
.aksrt-faq__search-wrap {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 10px;
border-radius: 12px;
background: var(--surface-2, #f3f4f6);
border: 1px solid var(--color-line, #e5e7eb);
}
.aksrt-faq__search-icon { color: var(--muted-2-color, #999); }
.aksrt-faq__search-input {
flex: 1;
min-width: 0;
border: 0;
background: transparent;
outline: none;
font-size: 14px;
color: inherit;
height: 34px;
}
.aksrt-faq__search-btn {
border: 0;
border-radius: 8px;
background: var(--color-primary, #3b82f6);
color: #fff;
padding: 0 14px;
height: 34px;
font-weight: 600;
cursor: pointer;
font-size: 13px;
}
.aksrt-faq__main {
display: grid;
grid-template-columns: minmax(0, 1.4fr) minmax(220px, .9fr);
gap: 16px;
align-items: start;
}
.aksrt-faq__main.is-full { grid-template-columns: 1fr; }
.aksrt-faq__list { display: flex; flex-direction: column; gap: 10px; }
.aksrt-faq__item {
border: 1px solid var(--color-line, #e5e7eb);
border-radius: 12px;
background: var(--surface-1, #fff);
padding: 0;
overflow: hidden;
}
.aksrt-faq__summary {
list-style: none;
display: flex;
align-items: center;
gap: 12px;
padding: 12px 14px;
cursor: pointer;
}
.aksrt-faq__summary::-webkit-details-marker { display: none; }
.aksrt-faq__badge {
flex-shrink: 0;
width: 34px;
height: 34px;
border-radius: 10px;
display: inline-flex;
align-items: center;
justify-content: center;
font-size: 12px;
font-weight: 700;
color: #fff;
background: #8b5cf6;
}
.aksrt-faq__badge.tone-pink { background: #ec4899; }
.aksrt-faq__badge.tone-green { background: #10b981; }
.aksrt-faq__badge.tone-orange { background: #f59e0b; }
.aksrt-faq__badge.tone-blue { background: #3b82f6; }
.aksrt-faq__badge.tone-purple { background: #8b5cf6; }
.aksrt-faq__qblock { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.aksrt-faq__q { font-weight: 700; font-size: 14px; line-height: 1.35; }
.aksrt-faq__preview {
font-size: 12px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.aksrt-faq__chevron {
color: var(--muted-2-color, #999);
transition: transform .2s ease;
flex-shrink: 0;
}
.aksrt-faq__item[open] .aksrt-faq__chevron { transform: rotate(180deg); }
.aksrt-faq__a {
padding: 0 14px 14px 60px;
font-size: 13px;
color: var(--color-ink-3, #555);
line-height: 1.65;
}
.aksrt-faq__aside { display: flex; flex-direction: column; gap: 12px; }
.aksrt-faq__doc {
border: 1px solid var(--color-line, #e5e7eb);
border-radius: 12px;
padding: 14px;
background: var(--surface-1, #fff);
}
.aksrt-faq__doc-head { display: flex; gap: 10px; align-items: flex-start; margin-bottom: 10px; }
.aksrt-faq__doc-ico {
width: 36px; height: 36px; border-radius: 10px;
display: inline-flex; align-items: center; justify-content: center;
background: #eff6ff; color: #2563eb; flex-shrink: 0;
}
.aksrt-faq__doc-title { margin: 0; font-size: 15px; }
.aksrt-faq__doc-desc { margin: 4px 0 0; font-size: 12px; }
.aksrt-faq__doc-list { list-style: none; margin: 0; padding: 0; }
.aksrt-faq__doc-li + .aksrt-faq__doc-li { margin-top: 8px; }
.aksrt-faq__doc-a {
display: flex;
align-items: center;
gap: 10px;
padding: 8px;
border-radius: 10px;
background: var(--surface-2, #f8fafc);
text-decoration: none;
color: inherit;
}
.aksrt-faq__doc-li-ico {
width: 28px; height: 28px; border-radius: 8px;
display: inline-flex; align-items: center; justify-content: center;
background: #fff; color: var(--color-primary, #3b82f6); flex-shrink: 0;
}
.aksrt-faq__doc-li-mid { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.aksrt-faq__doc-li-title { font-size: 13px; font-weight: 600; }
.aksrt-faq__doc-li-desc { font-size: 11px; }
.aksrt-faq__doc-go {
font-size: 12px;
color: var(--color-primary, #3b82f6);
white-space: nowrap;
flex-shrink: 0;
}
.aksrt-faq__help {
border-radius: 12px;
padding: 14px;
background: linear-gradient(135deg, #3b82f6, #8b5cf6);
color: #fff;
}
.aksrt-faq__help-top { display: flex; gap: 10px; align-items: flex-start; }
.aksrt-faq__help-ico {
width: 36px; height: 36px; border-radius: 10px;
display: inline-flex; align-items: center; justify-content: center;
background: rgba(255,255,255,.18); flex-shrink: 0;
}
.aksrt-faq__help-title { margin: 0; font-size: 15px; }
.aksrt-faq__help-sub { margin: 4px 0 0; font-size: 12px; opacity: .92; }
.aksrt-faq__help-btn {
display: inline-flex;
align-items: center;
gap: 6px;
margin-top: 12px;
padding: 8px 14px;
border-radius: 999px;
background: #fff;
color: #1d4ed8 !important;
text-decoration: none;
font-weight: 700;
font-size: 13px;
}
@media (max-width: 900px) {
.aksrt-faq__main { grid-template-columns: 1fr; }
.aksrt-faq__a { padding-left: 14px; }
}
+78 -47
View File
@@ -1,6 +1,6 @@
<?php
/**
* Scrolling notice bar (zibll widget_ui_notice lite).
* Scrolling notice bar (zibll widget_ui_notice / zib_notice → vertical new-swiper).
*
* @package AKSRT
*/
@@ -13,7 +13,8 @@ class NoticeWidget extends \WP_Widget {
public function __construct() {
parent::__construct('aksrt_notice', esc_html__('AKSRT 滚动公告', 'aksrt'), [
'description' => esc_html__('多条公告滚动显示。', 'aksrt'),
'description' => esc_html__('多条公告垂直滚动(共享 Swiper 栈)。', 'aksrt'),
'classname' => 'widget_aksrt_notice',
]);
}
@@ -23,10 +24,12 @@ class NoticeWidget extends \WP_Widget {
return;
}
$color = sanitize_html_class((string) ($instance['color'] ?? 'c-blue'));
$blank = !empty($instance['blank']);
$radius = !empty($instance['radius']);
$color = sanitize_html_class((string) ($instance['color'] ?? 'c-blue'));
$blank = !empty($instance['blank']);
$radius = !empty($instance['radius']);
$interval = max(2, min(15, (int) ($instance['interval'] ?? 4)));
$align = sanitize_html_class((string) ($instance['alignment'] ?? ''));
$in_affix = !empty($instance['in_affix']);
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
@@ -34,48 +37,47 @@ class NoticeWidget extends \WP_Widget {
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
$uid = 'aksrt-notice-' . esc_attr($this->id);
$class = 'aksrt-scroll-notice color-' . $color . ($radius ? ' is-radius' : '');
echo '<div class="' . esc_attr($class) . '" id="' . $uid . '" data-interval="' . esc_attr((string) ($interval * 1000)) . '">';
echo '<div class="aksrt-scroll-notice__icon"><i class="fas fa-bullhorn" aria-hidden="true"></i></div>';
echo '<div class="aksrt-scroll-notice__viewport">';
foreach ($items as $i => $item) {
$active = $i === 0 ? ' is-active' : '';
$wrap_cls = 'aksrt-scroll-notice swiper-bulletin color-' . $color;
if ($radius) {
$wrap_cls .= ' is-radius radius';
}
if ($align !== '') {
$wrap_cls .= ' ' . $align;
}
if ($in_affix) {
$wrap_cls .= ' is-affix';
}
$attr = $in_affix ? ' data-affix="true"' : '';
echo '<div class="' . esc_attr($wrap_cls) . '"' . $attr . '>';
echo '<div class="aksrt-scroll-notice__icon bulletin-icon"><i class="fas fa-bullhorn" aria-hidden="true"></i></div>';
// Prefer shared vertical new-swiper (zib_notice parity) via slider.js Type C.
echo '<div class="new-swiper aksrt-notice-swiper" data-direction="vertical" data-loop data-autoplay data-interval="' . esc_attr((string) ($interval * 1000)) . '">';
echo '<div class="swiper-wrapper">';
foreach ($items as $item) {
$inner = '';
if ($item['icon'] !== '') {
$inner .= '<i class="' . esc_attr($item['icon']) . '" aria-hidden="true"></i> ';
$icon = $item['icon'];
if (strpos($icon, 'fa-') === 0 && strpos($icon, ' ') === false) {
$icon = 'fa ' . $icon;
}
$inner .= '<div class="relative bulletin-icon mr6"><i class="abs-center ' . esc_attr($icon) . '" aria-hidden="true"></i></div>';
}
$inner .= esc_html($item['title']);
echo '<div class="aksrt-scroll-notice__item' . esc_attr($active) . '">';
echo '<div class="swiper-slide notice-slide">';
if ($item['href'] !== '') {
$tgt = $blank ? ' target="_blank" rel="noopener noreferrer"' : '';
echo '<a href="' . esc_url($item['href']) . '"' . $tgt . '>' . $inner . '</a>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<a class="text-ellipsis"' .
($blank ? ' target="_blank" rel="noopener noreferrer"' : '') .
' href="' . esc_url($item['href']) . '">' . $inner . '</a>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} else {
echo '<span>' . $inner . '</span>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<span class="text-ellipsis">' . $inner . '</span>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
echo '</div>';
}
echo '</div></div>';
if (count($items) > 1) {
?>
<script>
(function(){
var root=document.getElementById(<?php echo wp_json_encode($uid); ?>);
if(!root||root._aksrtNotice)return; root._aksrtNotice=1;
var items=[].slice.call(root.querySelectorAll('.aksrt-scroll-notice__item'));
var i=0, ms=parseInt(root.getAttribute('data-interval')||'4000',10)||4000;
setInterval(function(){
if(items.length<2)return;
items[i].classList.remove('is-active');
i=(i+1)%items.length;
items[i].classList.add('is-active');
}, ms);
})();
</script>
<?php
}
echo '</div>';
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
@@ -86,7 +88,6 @@ class NoticeWidget extends \WP_Widget {
private function normalize_items(array $instance): array {
$raw = $instance['items'] ?? $instance['img_ids'] ?? null;
if ($raw === null && !empty($instance['content'])) {
// Legacy single content.
return [[
'title' => wp_strip_all_tags((string) $instance['content']),
'icon' => sanitize_text_field((string) ($instance['icon'] ?? 'fas fa-bullhorn')),
@@ -130,6 +131,24 @@ class NoticeWidget extends \WP_Widget {
];
}
$json = wp_json_encode($items, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$colors = [
'c-blue' => __('透明蓝', 'aksrt'),
'c-green' => __('透明绿', 'aksrt'),
'c-yellow' => __('透明黄', 'aksrt'),
'c-red' => __('透明粉红', 'aksrt'),
'c-purple' => __('透明紫', 'aksrt'),
'c-blue-2' => __('透明深蓝', 'aksrt'),
'c-green-2' => __('透明墨绿', 'aksrt'),
'c-yellow-2' => __('透明橘黄', 'aksrt'),
'c-red-2' => __('透明红', 'aksrt'),
'c-purple-2' => __('透明深紫', 'aksrt'),
'b-theme' => __('主题色', 'aksrt'),
'b-red' => __('红色', 'aksrt'),
'b-yellow' => __('黄色', 'aksrt'),
'b-blue' => __('蓝色', 'aksrt'),
'b-green' => __('绿色', 'aksrt'),
'b-purple' => __('紫色', 'aksrt'),
];
?>
<p>
<label><?php esc_html_e('标题(可选)', 'aksrt'); ?>
@@ -147,29 +166,41 @@ class NoticeWidget extends \WP_Widget {
</label>
</p>
<p>
<label><?php esc_html_e('主题色 class', 'aksrt'); ?>
<select name="<?php echo esc_attr($this->get_field_name('color')); ?>">
<?php foreach (['c-blue' => '蓝', 'c-green' => '绿', 'c-yellow' => '黄', 'c-red' => '红', 'c-purple' => '紫'] as $k => $label) : ?>
<label><?php esc_html_e('主题色彩', 'aksrt'); ?>
<select class="widefat" name="<?php echo esc_attr($this->get_field_name('color')); ?>">
<?php foreach ($colors as $k => $label) : ?>
<option value="<?php echo esc_attr($k); ?>" <?php selected(($instance['color'] ?? 'c-blue'), $k); ?>><?php echo esc_html($label); ?></option>
<?php endforeach; ?>
</select>
</label>
</p>
<p>
<label><?php esc_html_e('对齐', 'aksrt'); ?>
<select name="<?php echo esc_attr($this->get_field_name('alignment')); ?>">
<option value="" <?php selected(($instance['alignment'] ?? ''), ''); ?>><?php esc_html_e('靠左', 'aksrt'); ?></option>
<option value="text-center" <?php selected(($instance['alignment'] ?? ''), 'text-center'); ?>><?php esc_html_e('居中', 'aksrt'); ?></option>
<option value="text-right" <?php selected(($instance['alignment'] ?? ''), 'text-right'); ?>><?php esc_html_e('靠右', 'aksrt'); ?></option>
</select>
</label>
</p>
<p>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('blank')); ?>" value="1" <?php checked(!empty($instance['blank'])); ?>> <?php esc_html_e('链接新窗口', 'aksrt'); ?></label><br>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('radius')); ?>" value="1" <?php checked(!empty($instance['radius'])); ?>> <?php esc_html_e('两端圆形', 'aksrt'); ?></label>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('radius')); ?>" value="1" <?php checked(!empty($instance['radius'])); ?>> <?php esc_html_e('两端圆形', 'aksrt'); ?></label><br>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('in_affix')); ?>" value="1" <?php checked(!empty($instance['in_affix'])); ?>> <?php esc_html_e('侧栏随动', 'aksrt'); ?></label>
</p>
<?php
}
public function update($new_instance, $old_instance): array {
return [
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'items' => $this->normalize_items(['items' => $new_instance['items'] ?? []]),
'interval' => max(2, min(15, (int) ($new_instance['interval'] ?? 4))),
'color' => sanitize_html_class((string) ($new_instance['color'] ?? 'c-blue')),
'blank' => !empty($new_instance['blank']) ? 1 : 0,
'radius' => !empty($new_instance['radius']) ? 1 : 0,
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'items' => $this->normalize_items(['items' => $new_instance['items'] ?? []]),
'interval' => max(2, min(15, (int) ($new_instance['interval'] ?? 4))),
'color' => sanitize_html_class((string) ($new_instance['color'] ?? 'c-blue')),
'alignment' => sanitize_html_class((string) ($new_instance['alignment'] ?? '')),
'blank' => !empty($new_instance['blank']) ? 1 : 0,
'radius' => !empty($new_instance['radius']) ? 1 : 0,
'in_affix' => !empty($new_instance['in_affix']) ? 1 : 0,
];
}
}
+189 -29
View File
@@ -1,6 +1,6 @@
<?php
/**
* Search box with hot keywords (zibll widget_ui_search lite).
* Search box with hot keywords + type/cat scope (zibll widget_ui_search / zib_get_search_box).
*
* @package AKSRT
*/
@@ -13,7 +13,8 @@ class SearchWidget extends \WP_Widget {
public function __construct() {
parent::__construct('aksrt_search', esc_html__('AKSRT 搜索', 'aksrt'), [
'description' => esc_html__('搜索框,可显示热门关键词。', 'aksrt'),
'description' => esc_html__('搜索框:类型切换、分类范围、热门关键词与搜索历史。', 'aksrt'),
'classname' => 'widget_aksrt_search',
]);
}
@@ -25,16 +26,76 @@ class SearchWidget extends \WP_Widget {
}
$placeholder = sanitize_text_field((string) ($instance['placeholder'] ?? __('开启精彩搜索', 'aksrt')));
$show_kw = !empty($instance['show_keywords']);
if ($placeholder === '' && function_exists('aksrt_get_search_placeholder')) {
$placeholder = aksrt_get_search_placeholder();
}
$show_kw = !empty($instance['show_keywords']);
$show_history = !empty($instance['show_history']);
$kw_title = sanitize_text_field((string) ($instance['keywords_title'] ?? __('热门搜索', 'aksrt')));
$more_but = sanitize_text_field((string) ($instance['more_but'] ?? ''));
$more_url = esc_url_raw(trim((string) ($instance['more_but_url'] ?? '')));
$show_types = !empty($instance['show_type']) || !isset($instance['show_type']);
$show_cat = !empty($instance['show_input_cat']);
$kw_title = sanitize_text_field((string) ($instance['keywords_title'] ?? __('热门搜索', 'aksrt')));
$more_but = sanitize_text_field((string) ($instance['more_but'] ?? ''));
$more_url = esc_url_raw(trim((string) ($instance['more_but_url'] ?? '')));
$state = function_exists('aksrt_get_search_form_state')
? aksrt_get_search_form_state()
: [
'type' => 'post',
'types' => ['post' => __('文章', 'aksrt')],
'category_id' => 0,
'category_enabled' => false,
'categories' => [],
];
$type_options = is_array($state['types'] ?? null) ? $state['types'] : ['post' => __('文章', 'aksrt')];
$in_type = sanitize_key((string) ($instance['in_type'] ?? $state['type'] ?? 'post'));
if (!isset($type_options[$in_type])) {
$in_type = array_key_first($type_options) ?: 'post';
}
// Category scope.
$in_cat = max(0, (int) ($instance['in_cat'] ?? 0));
if ($in_cat <= 0) {
$in_cat = (int) ($state['category_id'] ?? 0);
}
$cat_enabled = $show_cat && ($state['category_enabled'] || !empty($instance['show_input_cat']));
$categories = [];
if ($cat_enabled) {
$more_raw = trim((string) ($instance['more_cats'] ?? ''));
if ($more_raw !== '') {
$ids = array_values(array_filter(array_map('intval', preg_split('/[,,\s]+/', $more_raw) ?: [])));
if ($ids) {
$got = get_categories([
'taxonomy' => 'category',
'include' => $ids,
'hide_empty' => false,
'orderby' => 'include',
]);
$categories = is_array($got) ? $got : [];
}
}
if (!$categories) {
$categories = is_array($state['categories'] ?? null) ? $state['categories'] : [];
}
if (!$categories && function_exists('aksrt_get_search_categories')) {
$categories = aksrt_get_search_categories();
}
}
// Only show type UI when more than one type is configured, unless forced.
if ($show_types && count($type_options) < 2 && empty($instance['force_show_type'])) {
// Still keep hidden type field for default.
$show_types_ui = false;
} else {
$show_types_ui = $show_types && count($type_options) >= 1;
}
$keywords = [];
if ($show_kw) {
if (class_exists('\AKSRT\Settings\Settings') && method_exists('\AKSRT\Settings\Settings', 'hot_search_keywords')) {
$keywords = \AKSRT\Settings\Settings::hot_search_keywords();
} elseif (function_exists('aksrt_get_search_hot_keywords')) {
$keywords = aksrt_get_search_hot_keywords();
} elseif (function_exists('aksrt_get_setting')) {
$raw = (string) aksrt_get_setting('hot_search_keywords', '');
$keywords = array_values(array_filter(array_map('trim', preg_split('/[\r\n,,]+/', $raw) ?: [])));
@@ -43,40 +104,111 @@ class SearchWidget extends \WP_Widget {
if ($custom !== '') {
$keywords = array_values(array_filter(array_map('trim', preg_split('/[\r\n,,]+/', $custom) ?: [])));
}
$keywords = array_slice($keywords, 0, 12);
$keywords = array_slice(is_array($keywords) ? $keywords : [], 0, 12);
}
$uid = 'aksrt-sw-' . esc_attr($this->id);
?>
<div class="widget-search-box aksrt-search-widget">
<form role="search" method="get" action="<?php echo esc_url(home_url('/')); ?>" data-aksrt-search-form>
<div class="widget-search-box__inner">
<input type="search" name="s" class="widget-search-box__input" placeholder="<?php echo esc_attr($placeholder); ?>" value="<?php echo esc_attr(get_search_query()); ?>" autocomplete="off">
<input type="hidden" name="aksrt_search_type" value="post">
<button type="submit" class="widget-search-box__btn" aria-label="<?php esc_attr_e('搜索', 'aksrt'); ?>">
<i class="fas fa-search"></i>
</button>
</div>
</form>
<?php if ($show_history) : ?>
<div class="aksrt-search-widget__history" data-aksrt-search-history>
<div class="aksrt-search-widget__kw-head">
<div class="aksrt-search-widget__kw-title"><?php esc_html_e('搜索历史', 'aksrt'); ?></div>
<button type="button" class="aksrt-search-widget__history-clear" data-aksrt-search-history-clear hidden><?php esc_html_e('清空', 'aksrt'); ?></button>
</div>
<div class="aksrt-search-widget__history-list" data-aksrt-search-history-list></div>
<div class="widget-search-box aksrt-search-widget search-input" id="<?php echo $uid; ?>">
<form
role="search"
method="get"
class="aksrt-search-widget__form search-form"
action="<?php echo esc_url(home_url('/')); ?>"
data-aksrt-search-form="widget"
data-default-search-type="<?php echo esc_attr($in_type); ?>"
data-default-category="<?php echo esc_attr((string) $in_cat); ?>"
>
<?php if ($show_types_ui) : ?>
<div class="aksrt-search-widget__types" role="tablist" aria-label="<?php esc_attr_e('搜索类型', 'aksrt'); ?>">
<?php foreach ($type_options as $type_key => $type_label) : ?>
<button
type="button"
class="aksrt-search-widget__type<?php echo $in_type === $type_key ? ' is-active' : ''; ?>"
data-search-type="<?php echo esc_attr((string) $type_key); ?>"
><?php echo esc_html((string) $type_label); ?></button>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if ($keywords) : ?>
<div class="aksrt-search-widget__line line-form">
<?php if ($cat_enabled && !empty($categories)) : ?>
<div class="aksrt-search-widget__cat option-dropdown">
<label class="screen-reader-text" for="<?php echo $uid; ?>-cat"><?php esc_html_e('分类', 'aksrt'); ?></label>
<select
id="<?php echo $uid; ?>-cat"
class="aksrt-search-widget__cat-select"
data-search-category-select
<?php disabled($in_type !== 'post'); ?>
>
<option value="0"><?php esc_html_e('全部分类', 'aksrt'); ?></option>
<?php foreach ($categories as $term) :
if (!$term instanceof \WP_Term) {
continue;
}
?>
<option value="<?php echo esc_attr((string) $term->term_id); ?>" <?php selected($in_cat, (int) $term->term_id); ?>>
<?php echo esc_html($term->name); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<?php endif; ?>
<div class="aksrt-search-widget__field search-input-text">
<label class="screen-reader-text" for="<?php echo $uid; ?>-s"><?php esc_html_e('搜索关键词', 'aksrt'); ?></label>
<input
id="<?php echo $uid; ?>-s"
type="search"
name="s"
class="widget-search-box__input line-form-input"
placeholder="<?php echo esc_attr($placeholder); ?>"
value="<?php echo esc_attr(get_search_query()); ?>"
autocomplete="off"
>
<button type="submit" class="widget-search-box__btn" aria-label="<?php esc_attr_e('搜索', 'aksrt'); ?>">
<i class="fas fa-search" aria-hidden="true"></i>
</button>
</div>
</div>
<input type="hidden" name="aksrt_search_type" value="<?php echo esc_attr($in_type); ?>">
<?php if ($cat_enabled) : ?>
<input type="hidden" name="aksrt_category" value="<?php echo esc_attr($in_type === 'post' && $in_cat > 0 ? (string) $in_cat : ''); ?>">
<?php endif; ?>
</form>
<?php if ($show_history) : ?>
<div class="aksrt-search-widget__history" data-aksrt-search-history>
<div class="aksrt-search-widget__kw-head">
<div class="aksrt-search-widget__kw-title"><?php esc_html_e('搜索历史', 'aksrt'); ?></div>
<button type="button" class="aksrt-search-widget__history-clear" data-aksrt-search-history-clear hidden><?php esc_html_e('清空', 'aksrt'); ?></button>
</div>
<div class="aksrt-search-widget__history-list" data-aksrt-search-history-list></div>
</div>
<?php endif; ?>
<?php if ($keywords) : ?>
<div class="aksrt-search-widget__keywords">
<?php if ($kw_title !== '') : ?>
<div class="aksrt-search-widget__kw-title"><?php echo esc_html($kw_title); ?></div>
<?php endif; ?>
<div class="aksrt-search-widget__kw-list">
<?php foreach ($keywords as $kw) : ?>
<a class="aksrt-search-widget__kw" href="<?php echo esc_url(add_query_arg('s', $kw, home_url('/'))); ?>"><?php echo esc_html($kw); ?></a>
<?php foreach ($keywords as $kw) :
$kw = is_array($kw) ? (string) ($kw['keyword'] ?? $kw['text'] ?? '') : (string) $kw;
if ($kw === '') {
continue;
}
$url = function_exists('aksrt_build_search_url')
? aksrt_build_search_url($kw, ['aksrt_search_type' => $in_type])
: add_query_arg(['s' => $kw, 'aksrt_search_type' => $in_type], home_url('/'));
?>
<a class="aksrt-search-widget__kw" href="<?php echo esc_url($url); ?>"><?php echo esc_html($kw); ?></a>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
<?php if ($more_but !== '' && $more_url !== '') : ?>
<div class="aksrt-search-widget__more">
<a href="<?php echo esc_url($more_url); ?>"><?php echo esc_html($more_but); ?></a>
@@ -88,6 +220,10 @@ class SearchWidget extends \WP_Widget {
}
public function form($instance): void {
$type_options = function_exists('aksrt_get_search_type_options')
? aksrt_get_search_type_options()
: ['post' => __('文章', 'aksrt')];
$in_type = sanitize_key((string) ($instance['in_type'] ?? 'post'));
?>
<p>
<label><?php esc_html_e('标题(可选)', 'aksrt'); ?>
@@ -100,10 +236,29 @@ class SearchWidget extends \WP_Widget {
</label>
</p>
<p>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_keywords')); ?>" value="1" <?php checked(!empty($instance['show_keywords'])); ?>> <?php esc_html_e('显示热门关键词', 'aksrt'); ?></label>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_type')); ?>" value="1" <?php checked(!empty($instance['show_type']) || !isset($instance['show_type'])); ?>> <?php esc_html_e('显示类型切换(≥2 种时)', 'aksrt'); ?></label><br>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_input_cat')); ?>" value="1" <?php checked(!empty($instance['show_input_cat'])); ?>> <?php esc_html_e('显示分类筛选', 'aksrt'); ?></label><br>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_keywords')); ?>" value="1" <?php checked(!empty($instance['show_keywords'])); ?>> <?php esc_html_e('显示热门关键词', 'aksrt'); ?></label><br>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_history')); ?>" value="1" <?php checked(!empty($instance['show_history'])); ?>> <?php esc_html_e('显示搜索历史', 'aksrt'); ?></label>
</p>
<p>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_history')); ?>" value="1" <?php checked(!empty($instance['show_history'])); ?>> <?php esc_html_e('显示搜索历史', 'aksrt'); ?></label>
<label><?php esc_html_e('默认类型', 'aksrt'); ?>
<select class="widefat" name="<?php echo esc_attr($this->get_field_name('in_type')); ?>">
<?php foreach ($type_options as $k => $label) : ?>
<option value="<?php echo esc_attr($k); ?>" <?php selected($in_type, $k); ?>><?php echo esc_html($label); ?></option>
<?php endforeach; ?>
</select>
</label>
</p>
<p>
<label><?php esc_html_e('默认分类 ID(0=全部)', 'aksrt'); ?>
<input class="tiny-text" type="number" min="0" name="<?php echo esc_attr($this->get_field_name('in_cat')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['in_cat'] ?? 0))); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('可选分类 ID(逗号;空=后台/全部分类)', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('more_cats')); ?>" value="<?php echo esc_attr((string) ($instance['more_cats'] ?? '')); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('关键词标题', 'aksrt'); ?>
@@ -134,6 +289,11 @@ class SearchWidget extends \WP_Widget {
'placeholder' => sanitize_text_field((string) ($new_instance['placeholder'] ?? '')),
'show_keywords' => !empty($new_instance['show_keywords']) ? 1 : 0,
'show_history' => !empty($new_instance['show_history']) ? 1 : 0,
'show_type' => !empty($new_instance['show_type']) ? 1 : 0,
'show_input_cat' => !empty($new_instance['show_input_cat']) ? 1 : 0,
'in_type' => sanitize_key((string) ($new_instance['in_type'] ?? 'post')),
'in_cat' => max(0, (int) ($new_instance['in_cat'] ?? 0)),
'more_cats' => sanitize_text_field((string) ($new_instance['more_cats'] ?? '')),
'keywords_title' => sanitize_text_field((string) ($new_instance['keywords_title'] ?? '')),
'keywords' => sanitize_textarea_field((string) ($new_instance['keywords'] ?? '')),
'more_but' => sanitize_text_field((string) ($new_instance['more_but'] ?? '')),
+213 -48
View File
@@ -1,6 +1,6 @@
<?php
/**
* FAQ accordion (zibll zib_widget_faq lite).
* FAQ accordion (zibll zib_widget_faq / produck-wid-faq structure parity).
*
* @package AKSRT
*/
@@ -11,12 +11,14 @@ defined('ABSPATH') || exit;
class WidgetFaq extends \WP_Widget {
private const BADGE_TONES = ['purple', 'pink', 'green', 'orange', 'blue'];
public function __construct() {
parent::__construct(
'aksrt_faq',
esc_html__('AKSRT 常见问题', 'aksrt'),
[
'description' => esc_html__('可折叠 FAQ 列表,支持搜索框与侧栏文档卡。', 'aksrt'),
'description' => esc_html__('FAQ 摘要卡 + 文档链接列表 + 帮助 CTA。', 'aksrt'),
'classname' => 'widget_aksrt_faq',
]
);
@@ -25,6 +27,12 @@ class WidgetFaq extends \WP_Widget {
public function widget($args, $instance): void {
$faqs = $this->normalize_faqs($instance['faqs'] ?? []);
if (!$faqs) {
if (current_user_can('manage_options')) {
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<div class="c-red muted-box mb20"><b>' . esc_html__('常见问题解答:', 'aksrt') . '</b>' .
esc_html__('请添加至少一条问题', 'aksrt') . '</div>';
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
return;
}
@@ -35,23 +43,60 @@ class WidgetFaq extends \WP_Widget {
}
$uid = 'aksrt-faq-' . esc_attr($this->id);
echo '<div class="aksrt-faq" id="' . $uid . '">';
echo '<section class="aksrt-faq produck-wid-faq-block" id="' . $uid . '">';
if (!empty($instance['search_s'])) {
$ph = (string) ($instance['search_placeholder'] ?? __('搜索问题…', 'aksrt'));
echo '<form class="aksrt-faq__search" method="get" action="' . esc_url(home_url('/')) . '" role="search">';
echo '<input type="search" name="s" class="aksrt-faq__search-input" placeholder="' . esc_attr($ph) . '" />';
echo '<button type="submit" class="aksrt-faq__search-btn">' . esc_html((string) ($instance['search_btn_text'] ?? __('搜索', 'aksrt'))) . '</button>';
echo '</form>';
$btn = (string) ($instance['search_btn_text'] ?? __('搜索', 'aksrt'));
echo '<header class="aksrt-faq__head produck-faq-head">';
echo '<form class="aksrt-faq__search search-form" method="get" action="' . esc_url(home_url('/')) . '" role="search">';
echo '<div class="aksrt-faq__search-wrap produck-faq-search">';
echo '<span class="aksrt-faq__search-icon" aria-hidden="true"><i class="fas fa-search"></i></span>';
echo '<input type="search" name="s" class="aksrt-faq__search-input produck-faq-search-input" placeholder="' . esc_attr($ph) . '" autocomplete="off" />';
echo '<button type="submit" class="aksrt-faq__search-btn produck-faq-search-btn"><span>' . esc_html($btn) . '</span></button>';
echo '</div></form></header>';
}
echo '<div class="aksrt-faq__list">';
$doc_title = trim((string) ($instance['doc_title'] ?? ''));
$doc_desc = trim((string) ($instance['doc_desc'] ?? ''));
$doc_icon = sanitize_text_field((string) ($instance['doc_icon'] ?? 'fas fa-book'));
$doc_links = $this->normalize_doc_links($instance['doc_links'] ?? []);
$help_title = trim((string) ($instance['help_title'] ?? ''));
$help_sub = trim((string) ($instance['help_subtitle'] ?? ''));
$help_icon = sanitize_text_field((string) ($instance['help_icon'] ?? 'fas fa-graduation-cap'));
$help_url = esc_url_raw(trim((string) ($instance['help_btn_url'] ?? '')));
$help_btn = trim((string) ($instance['help_btn_text'] ?? ''));
$has_doc = ($doc_title !== '' || $doc_desc !== '' || $doc_links);
$has_help = ($help_title !== '' || $help_sub !== '' || ($help_btn !== '' && $help_url !== ''));
$main_cls = 'aksrt-faq__main produck-faq-main';
if (!$has_doc && !$has_help) {
$main_cls .= ' is-full';
}
echo '<div class="' . esc_attr($main_cls) . '">';
echo '<div class="aksrt-faq__list produck-faq-list-col">';
$n = 0;
foreach ($faqs as $f) {
$n++;
$num = str_pad((string) $n, 2, '0', STR_PAD_LEFT);
echo '<details class="aksrt-faq__item">';
echo '<summary class="aksrt-faq__q"><span class="aksrt-faq__num">' . esc_html($num) . '</span><span class="aksrt-faq__qtext">' . esc_html($f['question']) . '</span></summary>';
$num = str_pad((string) $n, 2, '0', STR_PAD_LEFT);
$tone = self::BADGE_TONES[($n - 1) % count(self::BADGE_TONES)];
$preview = $f['answer'] !== '' ? wp_strip_all_tags($f['answer']) : '';
if ($preview !== '') {
$preview = wp_trim_words($preview, 28);
}
echo '<details class="aksrt-faq__item produck-faq-summary">';
echo '<summary class="aksrt-faq__summary">';
echo '<span class="aksrt-faq__badge produck-faq-badge tone-' . esc_attr($tone) . '">' . esc_html($num) . '</span>';
echo '<span class="aksrt-faq__qblock">';
echo '<span class="aksrt-faq__q produck-faq-q">' . esc_html($f['question']) . '</span>';
if ($preview !== '') {
echo '<span class="aksrt-faq__preview muted-2-color">' . esc_html($preview) . '</span>';
}
echo '</span>';
echo '<i class="fas fa-angle-down aksrt-faq__chevron" aria-hidden="true"></i>';
echo '</summary>';
if ($f['answer'] !== '') {
echo '<div class="aksrt-faq__a">' . wp_kses_post(wpautop($f['answer'])) . '</div>';
}
@@ -59,41 +104,78 @@ class WidgetFaq extends \WP_Widget {
}
echo '</div>';
$doc_title = trim((string) ($instance['doc_title'] ?? ''));
$doc_desc = trim((string) ($instance['doc_desc'] ?? ''));
$help_title = trim((string) ($instance['help_title'] ?? ''));
$help_url = esc_url_raw(trim((string) ($instance['help_btn_url'] ?? '')));
$help_btn = trim((string) ($instance['help_btn_text'] ?? ''));
if ($doc_title !== '' || $help_title !== '') {
echo '<div class="aksrt-faq__aside">';
if ($doc_title !== '') {
echo '<div class="aksrt-faq__doc"><strong>' . esc_html($doc_title) . '</strong>';
if ($has_doc || $has_help) {
echo '<aside class="aksrt-faq__aside produck-faq-aside">';
if ($has_doc) {
echo '<div class="aksrt-faq__doc produck-faq-doc-card zib-widget">';
echo '<div class="aksrt-faq__doc-head">';
if ($doc_icon !== '') {
echo '<span class="aksrt-faq__doc-ico"><i class="' . esc_attr($doc_icon) . '" aria-hidden="true"></i></span>';
}
echo '<div>';
if ($doc_title !== '') {
echo '<h3 class="aksrt-faq__doc-title">' . esc_html($doc_title) . '</h3>';
}
if ($doc_desc !== '') {
echo '<p>' . esc_html($doc_desc) . '</p>';
echo '<p class="aksrt-faq__doc-desc muted-2-color">' . nl2br(esc_html($doc_desc)) . '</p>';
}
echo '</div></div>';
if ($doc_links) {
echo '<ul class="aksrt-faq__doc-list produck-faq-doc-list">';
foreach ($doc_links as $dl) {
$durl = $dl['url'] !== '' ? $dl['url'] : '#';
echo '<li class="aksrt-faq__doc-li">';
echo '<a class="aksrt-faq__doc-a" href="' . esc_url($durl) . '">';
if ($dl['icon'] !== '') {
echo '<span class="aksrt-faq__doc-li-ico"><i class="' . esc_attr($dl['icon']) . '" aria-hidden="true"></i></span>';
}
echo '<span class="aksrt-faq__doc-li-mid">';
echo '<span class="aksrt-faq__doc-li-title">' . esc_html($dl['title']);
if ($dl['tag'] !== '') {
echo ' <span class="badg badg-sm c-red ml6">' . esc_html($dl['tag']) . '</span>';
}
echo '</span>';
if ($dl['desc'] !== '') {
echo '<span class="aksrt-faq__doc-li-desc muted-2-color">' . esc_html($dl['desc']) . '</span>';
}
echo '</span>';
$go = $dl['btn_text'] !== '' ? $dl['btn_text'] : __('查看教程', 'aksrt');
echo '<span class="aksrt-faq__doc-go">' . esc_html($go) . ' <i class="fas fa-angle-right" aria-hidden="true"></i></span>';
echo '</a></li>';
}
echo '</ul>';
}
echo '</div>';
}
if ($help_title !== '') {
echo '<div class="aksrt-faq__help"><strong>' . esc_html($help_title) . '</strong>';
$sub = trim((string) ($instance['help_subtitle'] ?? ''));
if ($sub !== '') {
echo '<p>' . esc_html($sub) . '</p>';
if ($has_help) {
echo '<div class="aksrt-faq__help produck-faq-help-card">';
echo '<div class="aksrt-faq__help-top">';
if ($help_icon !== '') {
echo '<span class="aksrt-faq__help-ico"><i class="' . esc_attr($help_icon) . '" aria-hidden="true"></i></span>';
}
echo '<div class="aksrt-faq__help-text">';
if ($help_title !== '') {
echo '<h3 class="aksrt-faq__help-title">' . esc_html($help_title) . '</h3>';
}
if ($help_sub !== '') {
echo '<p class="aksrt-faq__help-sub">' . nl2br(esc_html($help_sub)) . '</p>';
}
echo '</div></div>';
if ($help_btn !== '' && $help_url !== '') {
echo '<a class="aksrt-faq__help-btn" href="' . esc_url($help_url) . '">' . esc_html($help_btn) . '</a>';
echo '<a class="aksrt-faq__help-btn" href="' . esc_url($help_url) . '">' . esc_html($help_btn) . ' <i class="fas fa-arrow-right" aria-hidden="true"></i></a>';
}
echo '</div>';
}
echo '</div>';
echo '</aside>';
}
echo '</div>';
echo '</div></section>';
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* @param mixed $raw
* @return array<int, array{question:string,answer:string}>
* @return array<int, array{question:string,answer:string,category:string}>
*/
private function normalize_faqs($raw): array {
if (is_string($raw)) {
@@ -112,11 +194,10 @@ class WidgetFaq extends \WP_Widget {
if ($q === '') {
continue;
}
$a = (string) ($row['answer'] ?? $row['a'] ?? '');
// Allow basic HTML in answers after kses on output.
$out[] = [
'question' => $q,
'answer' => wp_kses_post($a),
'answer' => wp_kses_post((string) ($row['answer'] ?? $row['a'] ?? '')),
'category' => sanitize_key((string) ($row['category'] ?? 'all')),
];
if (count($out) >= 30) {
break;
@@ -125,39 +206,120 @@ class WidgetFaq extends \WP_Widget {
return $out;
}
/**
* @param mixed $raw
* @return array<int, array{title:string,url:string,desc:string,icon:string,tag:string,btn_text:string}>
*/
private function normalize_doc_links($raw): array {
if (is_string($raw)) {
$decoded = json_decode($raw, true);
$raw = is_array($decoded) ? $decoded : [];
}
if (!is_array($raw)) {
return [];
}
$out = [];
foreach ($raw as $row) {
if (!is_array($row)) {
continue;
}
$title = sanitize_text_field((string) ($row['title'] ?? ''));
$url = esc_url_raw(trim((string) ($row['url'] ?? $row['link'] ?? '')));
if ($title === '' && $url === '') {
continue;
}
$out[] = [
'title' => $title,
'url' => $url,
'desc' => sanitize_text_field((string) ($row['desc'] ?? '')),
'icon' => sanitize_text_field((string) ($row['icon'] ?? '')),
'tag' => sanitize_text_field((string) ($row['tag'] ?? '')),
'btn_text' => sanitize_text_field((string) ($row['btn_text'] ?? '')),
];
if (count($out) >= 12) {
break;
}
}
return $out;
}
public function form($instance): void {
$faqs = is_string($instance['faqs'] ?? null)
? (string) $instance['faqs']
: wp_json_encode($this->normalize_faqs($instance['faqs'] ?? []), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$faqs = $this->normalize_faqs($instance['faqs'] ?? []);
if (!$faqs) {
$faqs = [
['question' => '', 'answer' => '', 'category' => 'all'],
['question' => '', 'answer' => '', 'category' => 'all'],
];
}
$doc_links = $this->normalize_doc_links($instance['doc_links'] ?? []);
if (!$doc_links) {
$doc_links = [
['title' => '', 'url' => '', 'desc' => '', 'icon' => 'fas fa-file-alt', 'tag' => '', 'btn_text' => ''],
];
}
?>
<p>
<label><?php esc_html_e('标题', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr((string) ($instance['title'] ?? '')); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('FAQ JSON', 'aksrt'); ?>
<textarea class="widefat" rows="8" name="<?php echo esc_attr($this->get_field_name('faqs')); ?>" placeholder='[{"question":"如何付费?","answer":"进入用户中心…"}]'><?php echo esc_textarea((string) $faqs); ?></textarea>
</label>
</p>
<p>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('search_s')); ?>" value="1" <?php checked(!empty($instance['search_s'])); ?>> <?php esc_html_e('显示搜索框', 'aksrt'); ?></label>
</p>
<p>
<label><?php esc_html_e('文档卡标题', 'aksrt'); ?>
<label><?php esc_html_e('搜索占位', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('search_placeholder')); ?>" value="<?php echo esc_attr((string) ($instance['search_placeholder'] ?? __('搜索问题…', 'aksrt'))); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('搜索按钮', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('search_btn_text')); ?>" value="<?php echo esc_attr((string) ($instance['search_btn_text'] ?? __('搜索', 'aksrt'))); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('FAQ JSON', 'aksrt'); ?>
<textarea class="widefat" rows="6" name="<?php echo esc_attr($this->get_field_name('faqs')); ?>" placeholder='[{"question":"…","answer":"…"}]'><?php echo esc_textarea((string) wp_json_encode($faqs, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); ?></textarea>
</label>
</p>
<hr>
<p><strong><?php esc_html_e('文档卡', 'aksrt'); ?></strong></p>
<p>
<label><?php esc_html_e('文档标题', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('doc_title')); ?>" value="<?php echo esc_attr((string) ($instance['doc_title'] ?? '')); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('文档卡描述', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('doc_desc')); ?>" value="<?php echo esc_attr((string) ($instance['doc_desc'] ?? '')); ?>">
<label><?php esc_html_e('文档描述', 'aksrt'); ?>
<textarea class="widefat" rows="2" name="<?php echo esc_attr($this->get_field_name('doc_desc')); ?>"><?php echo esc_textarea((string) ($instance['doc_desc'] ?? '')); ?></textarea>
</label>
</p>
<p>
<label><?php esc_html_e('帮助卡标题', 'aksrt'); ?>
<label><?php esc_html_e('文档图标 class', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('doc_icon')); ?>" value="<?php echo esc_attr((string) ($instance['doc_icon'] ?? 'fas fa-book')); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('文档链接 JSON', 'aksrt'); ?>
<textarea class="widefat" rows="5" name="<?php echo esc_attr($this->get_field_name('doc_links')); ?>" placeholder='[{"title":"教程","url":"#","desc":"","icon":"fas fa-file","tag":"热门","btn_text":"查看教程"}]'><?php echo esc_textarea((string) wp_json_encode($doc_links, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); ?></textarea>
</label>
</p>
<hr>
<p><strong><?php esc_html_e('帮助卡', 'aksrt'); ?></strong></p>
<p>
<label><?php esc_html_e('帮助标题', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('help_title')); ?>" value="<?php echo esc_attr((string) ($instance['help_title'] ?? '')); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('帮助副标题', 'aksrt'); ?>
<textarea class="widefat" rows="2" name="<?php echo esc_attr($this->get_field_name('help_subtitle')); ?>"><?php echo esc_textarea((string) ($instance['help_subtitle'] ?? '')); ?></textarea>
</label>
</p>
<p>
<label><?php esc_html_e('帮助图标 class', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('help_icon')); ?>" value="<?php echo esc_attr((string) ($instance['help_icon'] ?? 'fas fa-graduation-cap')); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('帮助按钮文案', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('help_btn_text')); ?>" value="<?php echo esc_attr((string) ($instance['help_btn_text'] ?? '')); ?>">
@@ -174,14 +336,17 @@ class WidgetFaq extends \WP_Widget {
public function update($new_instance, $old_instance): array {
return [
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'faqs' => $this->normalize_faqs($new_instance['faqs'] ?? []),
'search_s' => !empty($new_instance['search_s']) ? 1 : 0,
'search_placeholder' => sanitize_text_field((string) ($new_instance['search_placeholder'] ?? '')),
'search_btn_text' => sanitize_text_field((string) ($new_instance['search_btn_text'] ?? '')),
'faqs' => $this->normalize_faqs($new_instance['faqs'] ?? []),
'doc_title' => sanitize_text_field((string) ($new_instance['doc_title'] ?? '')),
'doc_desc' => sanitize_text_field((string) ($new_instance['doc_desc'] ?? '')),
'doc_desc' => sanitize_textarea_field((string) ($new_instance['doc_desc'] ?? '')),
'doc_icon' => sanitize_text_field((string) ($new_instance['doc_icon'] ?? '')),
'doc_links' => $this->normalize_doc_links($new_instance['doc_links'] ?? []),
'help_title' => sanitize_text_field((string) ($new_instance['help_title'] ?? '')),
'help_subtitle' => sanitize_text_field((string) ($new_instance['help_subtitle'] ?? '')),
'help_subtitle' => sanitize_textarea_field((string) ($new_instance['help_subtitle'] ?? '')),
'help_icon' => sanitize_text_field((string) ($new_instance['help_icon'] ?? '')),
'help_btn_text' => sanitize_text_field((string) ($new_instance['help_btn_text'] ?? '')),
'help_btn_url' => esc_url_raw(trim((string) ($new_instance['help_btn_url'] ?? ''))),
];