feat(widgets): pricing/FAQ/stats + forum topic list and bbs sidebars
Add zibll produck widgets (pricing plans, FAQ, stats), forum topic lists, register bbs_* sidebar zones, and wire plate/topic/home/compose templates to render those widget areas.
This commit is contained in:
1 parent
81bf3193a4
commit
d8682ce1fe
11 files changed
+1036
-1
No files matched your search
@@ -0,0 +1,189 @@
|
||||
<?php
|
||||
/**
|
||||
* FAQ accordion (zibll zib_widget_faq lite).
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
namespace AKSRT\Widgets;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
class WidgetFaq extends \WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'aksrt_faq',
|
||||
esc_html__('AKSRT 常见问题', 'aksrt'),
|
||||
[
|
||||
'description' => esc_html__('可折叠 FAQ 列表,支持搜索框与侧栏文档卡。', 'aksrt'),
|
||||
'classname' => 'widget_aksrt_faq',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$faqs = $this->normalize_faqs($instance['faqs'] ?? []);
|
||||
if (!$faqs) {
|
||||
return;
|
||||
}
|
||||
|
||||
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
|
||||
if ($title !== '') {
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
$uid = 'aksrt-faq-' . esc_attr($this->id);
|
||||
echo '<div class="aksrt-faq" 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>';
|
||||
}
|
||||
|
||||
echo '<div class="aksrt-faq__list">';
|
||||
$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>';
|
||||
if ($f['answer'] !== '') {
|
||||
echo '<div class="aksrt-faq__a">' . wp_kses_post(wpautop($f['answer'])) . '</div>';
|
||||
}
|
||||
echo '</details>';
|
||||
}
|
||||
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 ($doc_desc !== '') {
|
||||
echo '<p>' . esc_html($doc_desc) . '</p>';
|
||||
}
|
||||
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 ($help_btn !== '' && $help_url !== '') {
|
||||
echo '<a class="aksrt-faq__help-btn" href="' . esc_url($help_url) . '">' . esc_html($help_btn) . '</a>';
|
||||
}
|
||||
echo '</div>';
|
||||
}
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
echo '</div>';
|
||||
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $raw
|
||||
* @return array<int, array{question:string,answer:string}>
|
||||
*/
|
||||
private function normalize_faqs($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;
|
||||
}
|
||||
$q = sanitize_text_field((string) ($row['question'] ?? $row['q'] ?? ''));
|
||||
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),
|
||||
];
|
||||
if (count($out) >= 30) {
|
||||
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);
|
||||
?>
|
||||
<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'); ?>
|
||||
<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>
|
||||
</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'); ?>
|
||||
<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'] ?? '')); ?>">
|
||||
</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_url')); ?>" value="<?php echo esc_attr((string) ($instance['help_btn_url'] ?? '')); ?>">
|
||||
</label>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
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'] ?? '')),
|
||||
'doc_title' => sanitize_text_field((string) ($new_instance['doc_title'] ?? '')),
|
||||
'doc_desc' => sanitize_text_field((string) ($new_instance['doc_desc'] ?? '')),
|
||||
'help_title' => sanitize_text_field((string) ($new_instance['help_title'] ?? '')),
|
||||
'help_subtitle' => sanitize_text_field((string) ($new_instance['help_subtitle'] ?? '')),
|
||||
'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'] ?? ''))),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user