feat(widgets): author card, forum tags, multi-tab topic lists
Add post-author card, forum tag term list, and multi-tab forum topic switcher widgets to close remaining zibll user/bbs widget gaps.
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
/**
|
||||
* Batch3e: author card, forum topic terms, forum tab topics.
|
||||
*/
|
||||
|
||||
.aksrt-author-card {
|
||||
border-radius: 12px;
|
||||
overflow: hidden;
|
||||
border: 1px solid var(--color-line, #e5e7eb);
|
||||
background: var(--surface-1, #fff);
|
||||
margin-bottom: 12px;
|
||||
text-align: center;
|
||||
}
|
||||
.aksrt-author-card__cover {
|
||||
height: 84px;
|
||||
background: linear-gradient(135deg, #93c5fd, #c4b5fd) center/cover no-repeat;
|
||||
}
|
||||
.aksrt-author-card__body { padding: 0 14px 16px; }
|
||||
.aksrt-author-card__avatar {
|
||||
display: inline-block;
|
||||
margin-top: -36px;
|
||||
border-radius: 50%;
|
||||
overflow: hidden;
|
||||
border: 3px solid var(--surface-1, #fff);
|
||||
width: 72px;
|
||||
height: 72px;
|
||||
}
|
||||
.aksrt-author-card__avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
|
||||
.aksrt-author-card__name {
|
||||
display: block;
|
||||
margin-top: 8px;
|
||||
font-weight: 700;
|
||||
font-size: 16px;
|
||||
color: inherit;
|
||||
text-decoration: none;
|
||||
}
|
||||
.aksrt-author-card__bio {
|
||||
margin-top: 6px;
|
||||
font-size: 12px;
|
||||
color: var(--muted-2-color, #888);
|
||||
line-height: 1.5;
|
||||
}
|
||||
.aksrt-author-card__stats {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
gap: 16px;
|
||||
margin-top: 12px;
|
||||
font-size: 12px;
|
||||
color: var(--muted-2-color, #888);
|
||||
}
|
||||
.aksrt-author-card__stats b {
|
||||
display: block;
|
||||
font-size: 16px;
|
||||
color: var(--color-ink, #111);
|
||||
}
|
||||
.aksrt-author-card__btn {
|
||||
display: inline-block;
|
||||
margin-top: 12px;
|
||||
padding: 6px 16px;
|
||||
border-radius: 999px;
|
||||
background: var(--color-primary, #3b82f6);
|
||||
color: #fff !important;
|
||||
text-decoration: none;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.aksrt-ftag-list { display: flex; flex-direction: column; gap: 6px; }
|
||||
.aksrt-ftag-list.is-card {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
gap: 8px;
|
||||
}
|
||||
.aksrt-ftag-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 8px 10px;
|
||||
border-radius: 8px;
|
||||
text-decoration: none;
|
||||
color: inherit;
|
||||
background: var(--surface-2, #f8fafc);
|
||||
font-size: 13px;
|
||||
}
|
||||
.aksrt-ftag-item__name { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
||||
.aksrt-ftag-item__count { font-size: 11px; color: var(--muted-2-color, #999); }
|
||||
|
||||
.aksrt-ftab { margin-bottom: 10px; }
|
||||
.aksrt-ftab__nav {
|
||||
list-style: none;
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 6px;
|
||||
margin: 0 0 10px;
|
||||
padding: 0;
|
||||
}
|
||||
.aksrt-ftab__btn {
|
||||
border: 0;
|
||||
background: var(--surface-2, #f3f4f6);
|
||||
border-radius: 999px;
|
||||
padding: 5px 12px;
|
||||
font-size: 12px;
|
||||
cursor: pointer;
|
||||
}
|
||||
.aksrt-ftab__btn.is-active {
|
||||
background: var(--color-primary, #3b82f6);
|
||||
color: #fff;
|
||||
}
|
||||
.aksrt-ftab__panel { display: none; }
|
||||
.aksrt-ftab__panel.is-active { display: block; }
|
||||
.aksrt-ftab__empty { font-size: 12px; color: var(--muted-2-color, #999); margin: 0; }
|
||||
@@ -6,3 +6,4 @@
|
||||
@import url("widget-batch3.css");
|
||||
@import url("widget-batch3c.css");
|
||||
@import url("widget-batch3d.css");
|
||||
@import url("widget-batch3e.css");
|
||||
|
||||
@@ -71,6 +71,7 @@ function aksrt_register_widgets() {
|
||||
register_widget('\AKSRT\Widgets\WidgetFeatureIntro');
|
||||
register_widget('\AKSRT\Widgets\WidgetTermCards');
|
||||
register_widget('\AKSRT\Widgets\WidgetPaidPosts');
|
||||
register_widget('\AKSRT\Widgets\WidgetAuthorCard');
|
||||
|
||||
|
||||
// Forum widgets
|
||||
@@ -95,6 +96,12 @@ function aksrt_register_widgets() {
|
||||
if (class_exists('\AKSRT\Widgets\WidgetForumTopicLists')) {
|
||||
register_widget('\AKSRT\Widgets\WidgetForumTopicLists');
|
||||
}
|
||||
if (class_exists('\AKSRT\Widgets\WidgetForumTopicTerms')) {
|
||||
register_widget('\AKSRT\Widgets\WidgetForumTopicTerms');
|
||||
}
|
||||
if (class_exists('\AKSRT\Widgets\WidgetForumTabTopics')) {
|
||||
register_widget('\AKSRT\Widgets\WidgetForumTabTopics');
|
||||
}
|
||||
|
||||
// Shop product widgets (§6.9)
|
||||
register_widget('\AKSRT\Widgets\WidgetProductList');
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
/**
|
||||
* Post author card (zibll widget_ui_avatar).
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
namespace AKSRT\Widgets;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
class WidgetAuthorCard extends \WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'aksrt_author_card',
|
||||
esc_html__('AKSRT 文章作者卡片', 'aksrt'),
|
||||
[
|
||||
'description' => esc_html__('在文章/帖子侧栏显示当前内容作者信息。', 'aksrt'),
|
||||
'classname' => 'widget_aksrt_author_card',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$author_id = 0;
|
||||
if (is_singular()) {
|
||||
$post = get_queried_object();
|
||||
if ($post instanceof \WP_Post) {
|
||||
$author_id = (int) $post->post_author;
|
||||
}
|
||||
}
|
||||
if ($author_id <= 0 && !empty($instance['user_id'])) {
|
||||
$author_id = (int) $instance['user_id'];
|
||||
}
|
||||
if ($author_id <= 0) {
|
||||
return;
|
||||
}
|
||||
$user = get_userdata($author_id);
|
||||
if (!$user) {
|
||||
return;
|
||||
}
|
||||
|
||||
$show_bg = !isset($instance['show_img_bg']) || !empty($instance['show_img_bg']);
|
||||
$show_stats = !isset($instance['show_stats']) || !empty($instance['show_stats']);
|
||||
|
||||
$avatar = function_exists('aksrt_get_avatar_url')
|
||||
? aksrt_get_avatar_url($author_id, ['size' => 120])
|
||||
: get_avatar_url($author_id, ['size' => 120]);
|
||||
$home = function_exists('aksrt_get_user_home_url')
|
||||
? aksrt_get_user_home_url($author_id)
|
||||
: get_author_posts_url($author_id);
|
||||
$bio = get_user_meta($author_id, 'description', true);
|
||||
if (!is_string($bio) || $bio === '') {
|
||||
$bio = (string) $user->description;
|
||||
}
|
||||
$cover = (string) get_user_meta($author_id, 'aksrt_cover', true);
|
||||
if ($cover === '') {
|
||||
$cover = (string) get_user_meta($author_id, 'custom_avatar_cover', true);
|
||||
}
|
||||
|
||||
$post_count = count_user_posts($author_id, 'post', true);
|
||||
$topic_count = post_type_exists('aksrt_forum_topic')
|
||||
? count_user_posts($author_id, 'aksrt_forum_topic', true)
|
||||
: 0;
|
||||
$followers = 0;
|
||||
if (class_exists('\AKSRT\User\Follow')) {
|
||||
$followers = \AKSRT\User\Follow::get_instance()->get_follower_count($author_id);
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
echo '<div class="aksrt-author-card' . ($show_bg ? ' has-cover' : '') . '">';
|
||||
if ($show_bg) {
|
||||
$bg = $cover !== '' ? ' style="background-image:url(' . esc_url($cover) . ')"' : '';
|
||||
echo '<div class="aksrt-author-card__cover"' . $bg . '></div>';
|
||||
}
|
||||
echo '<div class="aksrt-author-card__body">';
|
||||
echo '<a class="aksrt-author-card__avatar" href="' . esc_url($home) . '"><img src="' . esc_url($avatar) . '" alt="" width="72" height="72" loading="lazy" /></a>';
|
||||
echo '<a class="aksrt-author-card__name" href="' . esc_url($home) . '">' . esc_html($user->display_name) . '</a>';
|
||||
if ($bio !== '') {
|
||||
echo '<div class="aksrt-author-card__bio">' . esc_html(wp_trim_words($bio, 28)) . '</div>';
|
||||
}
|
||||
if ($show_stats) {
|
||||
echo '<div class="aksrt-author-card__stats">';
|
||||
echo '<span><b>' . esc_html((string) $post_count) . '</b>' . esc_html__('文章', 'aksrt') . '</span>';
|
||||
if ($topic_count > 0) {
|
||||
echo '<span><b>' . esc_html((string) $topic_count) . '</b>' . esc_html__('帖子', 'aksrt') . '</span>';
|
||||
}
|
||||
echo '<span><b>' . esc_html((string) $followers) . '</b>' . esc_html__('粉丝', 'aksrt') . '</span>';
|
||||
echo '</div>';
|
||||
}
|
||||
echo '<a class="aksrt-author-card__btn" href="' . esc_url($home) . '">' . esc_html__('查看主页', 'aksrt') . '</a>';
|
||||
echo '</div></div>';
|
||||
|
||||
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
?>
|
||||
<p class="description"><?php esc_html_e('自动读取当前文章/帖子作者;也可固定用户 ID。', 'aksrt'); ?></p>
|
||||
<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('固定用户 ID(可选)', 'aksrt'); ?>
|
||||
<input class="tiny-text" type="number" min="0" name="<?php echo esc_attr($this->get_field_name('user_id')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['user_id'] ?? 0))); ?>">
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_img_bg')); ?>" value="1" <?php checked(!isset($instance['show_img_bg']) || !empty($instance['show_img_bg'])); ?>> <?php esc_html_e('显示封面', 'aksrt'); ?></label><br>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_stats')); ?>" value="1" <?php checked(!isset($instance['show_stats']) || !empty($instance['show_stats'])); ?>> <?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'] ?? '')),
|
||||
'user_id' => max(0, (int) ($new_instance['user_id'] ?? 0)),
|
||||
'show_img_bg' => !empty($new_instance['show_img_bg']) ? 1 : 0,
|
||||
'show_stats' => !empty($new_instance['show_stats']) ? 1 : 0,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
<?php
|
||||
/**
|
||||
* Multi-tab forum topic lists (zibll zib_bbs_widget_ui_tab_posts lite).
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
namespace AKSRT\Widgets;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
class WidgetForumTabTopics extends \WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'aksrt_forum_tab_topics',
|
||||
esc_html__('论坛 - 多栏目帖子', 'aksrt'),
|
||||
[
|
||||
'description' => esc_html__('热门/最新/精华等栏目切换的帖子列表。', 'aksrt'),
|
||||
'classname' => 'widget_aksrt_forum_tab_topics',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
if (!post_type_exists('aksrt_forum_topic')) {
|
||||
return;
|
||||
}
|
||||
$tabs = $this->normalize_tabs($instance['tabs'] ?? []);
|
||||
if (!$tabs) {
|
||||
$tabs = [
|
||||
['title' => __('最新', 'aksrt'), 'orderby' => 'date'],
|
||||
['title' => __('热门', 'aksrt'), 'orderby' => 'views'],
|
||||
['title' => __('精华', 'aksrt'), 'orderby' => 'essence'],
|
||||
];
|
||||
}
|
||||
$count = max(1, min(20, (int) ($instance['count'] ?? 6)));
|
||||
$uid = 'aksrt-ftab-' . esc_attr($this->id);
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
echo '<div class="aksrt-ftab" id="' . $uid . '">';
|
||||
echo '<ul class="aksrt-ftab__nav" role="tablist">';
|
||||
foreach ($tabs as $i => $tab) {
|
||||
$cls = $i === 0 ? ' is-active' : '';
|
||||
echo '<li><button type="button" class="aksrt-ftab__btn' . esc_attr($cls) . '" data-index="' . (int) $i . '">' . esc_html($tab['title']) . '</button></li>';
|
||||
}
|
||||
echo '</ul>';
|
||||
|
||||
foreach ($tabs as $i => $tab) {
|
||||
$cls = $i === 0 ? ' is-active' : '';
|
||||
echo '<div class="aksrt-ftab__panel' . esc_attr($cls) . '" data-index="' . (int) $i . '">';
|
||||
$this->render_list($tab, $count);
|
||||
echo '</div>';
|
||||
}
|
||||
echo '</div>';
|
||||
|
||||
?>
|
||||
<script>
|
||||
(function(){
|
||||
var root=document.getElementById(<?php echo wp_json_encode($uid); ?>);
|
||||
if(!root||root._aksrtFtab)return; root._aksrtFtab=1;
|
||||
var btns=[].slice.call(root.querySelectorAll('.aksrt-ftab__btn'));
|
||||
var panels=[].slice.call(root.querySelectorAll('.aksrt-ftab__panel'));
|
||||
function go(i){
|
||||
btns.forEach(function(b,idx){b.classList.toggle('is-active',idx===i);});
|
||||
panels.forEach(function(p,idx){p.classList.toggle('is-active',idx===i);});
|
||||
}
|
||||
btns.forEach(function(b){ b.addEventListener('click', function(){ go(parseInt(b.getAttribute('data-index')||'0',10)); }); });
|
||||
})();
|
||||
</script>
|
||||
<?php
|
||||
|
||||
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{title:string,orderby:string} $tab
|
||||
*/
|
||||
private function render_list(array $tab, int $count): void {
|
||||
$query = [
|
||||
'post_type' => 'aksrt_forum_topic',
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => $count,
|
||||
'order' => 'DESC',
|
||||
];
|
||||
switch ($tab['orderby']) {
|
||||
case 'views':
|
||||
$query['meta_key'] = '_aksrt_views';
|
||||
$query['orderby'] = 'meta_value_num';
|
||||
break;
|
||||
case 'comments':
|
||||
$query['orderby'] = 'comment_count';
|
||||
break;
|
||||
case 'essence':
|
||||
$query['meta_query'] = [[
|
||||
'key' => '_aksrt_essence',
|
||||
'value' => '1',
|
||||
]];
|
||||
$query['orderby'] = 'date';
|
||||
break;
|
||||
default:
|
||||
$query['orderby'] = 'date';
|
||||
}
|
||||
$q = new \WP_Query($query);
|
||||
if (!$q->have_posts()) {
|
||||
echo '<p class="aksrt-ftab__empty">' . esc_html__('暂无内容', 'aksrt') . '</p>';
|
||||
wp_reset_postdata();
|
||||
return;
|
||||
}
|
||||
echo '<ul class="aksrt-ftopic-list is-mini">';
|
||||
while ($q->have_posts()) {
|
||||
$q->the_post();
|
||||
$views = (int) get_post_meta(get_the_ID(), '_aksrt_views', true);
|
||||
echo '<li class="aksrt-ftopic-list__item">';
|
||||
echo '<a class="aksrt-ftopic-list__link" href="' . esc_url(get_permalink()) . '">' . esc_html(get_the_title()) . '</a>';
|
||||
echo '<span class="aksrt-ftopic-list__views">' . esc_html((string) $views) . '</span>';
|
||||
echo '</li>';
|
||||
}
|
||||
echo '</ul>';
|
||||
wp_reset_postdata();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $raw
|
||||
* @return array<int, array{title:string,orderby:string}>
|
||||
*/
|
||||
private function normalize_tabs($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'] ?? ''));
|
||||
if ($title === '') {
|
||||
continue;
|
||||
}
|
||||
$orderby = sanitize_key((string) ($row['orderby'] ?? 'date'));
|
||||
if (!in_array($orderby, ['date', 'views', 'comments', 'essence'], true)) {
|
||||
$orderby = 'date';
|
||||
}
|
||||
$out[] = ['title' => $title, 'orderby' => $orderby];
|
||||
if (count($out) >= 6) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
$tabs = is_string($instance['tabs'] ?? null)
|
||||
? (string) $instance['tabs']
|
||||
: wp_json_encode($this->normalize_tabs($instance['tabs'] ?? []), 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('栏目 JSON', 'aksrt'); ?>
|
||||
<textarea class="widefat" rows="5" name="<?php echo esc_attr($this->get_field_name('tabs')); ?>" placeholder='[{"title":"最新","orderby":"date"},{"title":"热门","orderby":"views"},{"title":"精华","orderby":"essence"}]'><?php echo esc_textarea((string) $tabs); ?></textarea>
|
||||
</label>
|
||||
<small><?php esc_html_e('orderby: date|views|comments|essence', 'aksrt'); ?></small>
|
||||
</p>
|
||||
<p>
|
||||
<label><?php esc_html_e('每栏数量', 'aksrt'); ?>
|
||||
<input class="tiny-text" type="number" min="1" max="20" name="<?php echo esc_attr($this->get_field_name('count')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['count'] ?? 6))); ?>">
|
||||
</label>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
return [
|
||||
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
|
||||
'tabs' => $this->normalize_tabs($new_instance['tabs'] ?? []),
|
||||
'count' => max(1, min(20, (int) ($new_instance['count'] ?? 6))),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
/**
|
||||
* Forum topic tags / topic taxonomy list (zibll zib_bbs_widget_ui_topic_lists).
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
namespace AKSRT\Widgets;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
class WidgetForumTopicTerms extends \WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'aksrt_forum_topic_terms',
|
||||
esc_html__('论坛 - 话题标签列表', 'aksrt'),
|
||||
[
|
||||
'description' => esc_html__('展示论坛话题标签(aksrt_forum_tag)。', 'aksrt'),
|
||||
'classname' => 'widget_aksrt_forum_topic_terms',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$tax = taxonomy_exists('aksrt_forum_tag') ? 'aksrt_forum_tag' : (taxonomy_exists('aksrt_forum_topic_cat') ? 'aksrt_forum_topic_cat' : '');
|
||||
if ($tax === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$number = max(1, min(40, (int) ($instance['number'] ?? 12)));
|
||||
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'count'));
|
||||
$hide_empty = !isset($instance['hide_empty']) || !empty($instance['hide_empty']);
|
||||
$include = $this->parse_ids((string) ($instance['include'] ?? ''));
|
||||
$style = sanitize_key((string) ($instance['style'] ?? 'mini'));
|
||||
|
||||
$term_args = [
|
||||
'taxonomy' => $tax,
|
||||
'number' => $number,
|
||||
'hide_empty' => $hide_empty,
|
||||
];
|
||||
if ($include) {
|
||||
$term_args['include'] = $include;
|
||||
$term_args['orderby'] = 'include';
|
||||
} else {
|
||||
if ($orderby === 'name') {
|
||||
$term_args['orderby'] = 'name';
|
||||
$term_args['order'] = 'ASC';
|
||||
} elseif ($orderby === 'views') {
|
||||
$term_args['meta_key'] = '_aksrt_views';
|
||||
$term_args['orderby'] = 'meta_value_num';
|
||||
$term_args['order'] = 'DESC';
|
||||
} else {
|
||||
$term_args['orderby'] = 'count';
|
||||
$term_args['order'] = 'DESC';
|
||||
}
|
||||
}
|
||||
|
||||
$terms = get_terms($term_args);
|
||||
if (is_wp_error($terms) || !$terms) {
|
||||
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
|
||||
}
|
||||
|
||||
$list_cls = $style === 'card' ? 'aksrt-ftag-list is-card' : 'aksrt-ftag-list is-mini';
|
||||
echo '<div class="' . esc_attr($list_cls) . '">';
|
||||
foreach ($terms as $term) {
|
||||
$link = get_term_link($term);
|
||||
if (is_wp_error($link)) {
|
||||
continue;
|
||||
}
|
||||
$icon = (string) get_term_meta($term->term_id, '_aksrt_icon', true);
|
||||
echo '<a class="aksrt-ftag-item" href="' . esc_url($link) . '">';
|
||||
if ($icon !== '') {
|
||||
echo '<i class="' . esc_attr($icon) . '" aria-hidden="true"></i> ';
|
||||
}
|
||||
echo '<span class="aksrt-ftag-item__name">' . esc_html($term->name) . '</span>';
|
||||
echo '<span class="aksrt-ftag-item__count">' . esc_html((string) (int) $term->count) . '</span>';
|
||||
echo '</a>';
|
||||
}
|
||||
echo '</div>';
|
||||
|
||||
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
/** @return array<int,int> */
|
||||
private function parse_ids(string $raw): array {
|
||||
if ($raw === '') {
|
||||
return [];
|
||||
}
|
||||
$ids = [];
|
||||
foreach (preg_split('/[\s,]+/', $raw) ?: [] as $p) {
|
||||
$id = (int) $p;
|
||||
if ($id > 0) {
|
||||
$ids[] = $id;
|
||||
}
|
||||
}
|
||||
return array_values(array_unique($ids));
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
?>
|
||||
<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'] ?? __('话题', 'aksrt'))); ?>">
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label><?php esc_html_e('数量', 'aksrt'); ?>
|
||||
<input class="tiny-text" type="number" min="1" max="40" name="<?php echo esc_attr($this->get_field_name('number')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['number'] ?? 12))); ?>">
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label><?php esc_html_e('排序', 'aksrt'); ?>
|
||||
<select name="<?php echo esc_attr($this->get_field_name('orderby')); ?>">
|
||||
<option value="count" <?php selected(($instance['orderby'] ?? 'count'), 'count'); ?>><?php esc_html_e('帖子数', 'aksrt'); ?></option>
|
||||
<option value="name" <?php selected(($instance['orderby'] ?? ''), 'name'); ?>><?php esc_html_e('名称', 'aksrt'); ?></option>
|
||||
<option value="views" <?php selected(($instance['orderby'] ?? ''), 'views'); ?>><?php esc_html_e('浏览', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label><?php esc_html_e('样式', 'aksrt'); ?>
|
||||
<select name="<?php echo esc_attr($this->get_field_name('style')); ?>">
|
||||
<option value="mini" <?php selected(($instance['style'] ?? 'mini'), 'mini'); ?>><?php esc_html_e('列表', 'aksrt'); ?></option>
|
||||
<option value="card" <?php selected(($instance['style'] ?? ''), 'card'); ?>><?php esc_html_e('卡片', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label><?php esc_html_e('包含 term ID', 'aksrt'); ?>
|
||||
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('include')); ?>" value="<?php echo esc_attr((string) ($instance['include'] ?? '')); ?>">
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('hide_empty')); ?>" value="1" <?php checked(!isset($instance['hide_empty']) || !empty($instance['hide_empty'])); ?>> <?php esc_html_e('隐藏空标签', 'aksrt'); ?></label>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
$orderby = sanitize_key((string) ($new_instance['orderby'] ?? 'count'));
|
||||
if (!in_array($orderby, ['count', 'name', 'views'], true)) {
|
||||
$orderby = 'count';
|
||||
}
|
||||
return [
|
||||
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
|
||||
'number' => max(1, min(40, (int) ($new_instance['number'] ?? 12))),
|
||||
'orderby' => $orderby,
|
||||
'style' => ($new_instance['style'] ?? '') === 'card' ? 'card' : 'mini',
|
||||
'include' => sanitize_text_field((string) ($new_instance['include'] ?? '')),
|
||||
'hide_empty' => !empty($new_instance['hide_empty']) ? 1 : 0,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user