feat(widgets): feature intro, term cards, paid posts lists

Close more zibll produck/posts widget gaps: media-tab feature intro,
taxonomy term cards, and paid/credits content list with CSS.
This commit is contained in:
2026-07-19 00:29:37 +08:00
parent 47d202bb96
commit 9356faf13d
6 changed files with 700 additions and 0 deletions
+163
View File
@@ -0,0 +1,163 @@
/**
* Batch3d: feature intro, term cards, paid posts.
*/
.aksrt-fi {
display: grid;
grid-template-columns: 1fr 1.1fr;
gap: 28px;
align-items: center;
margin-bottom: 20px;
}
.aksrt-fi.media-left { grid-template-columns: 1.1fr 1fr; }
.aksrt-fi.media-left .aksrt-fi__text { order: 2; }
.aksrt-fi.media-left .aksrt-fi__media { order: 1; }
@media (max-width: 991.98px) {
.aksrt-fi,
.aksrt-fi.media-left {
grid-template-columns: 1fr;
}
.aksrt-fi.media-left .aksrt-fi__text,
.aksrt-fi.media-left .aksrt-fi__media { order: initial; }
}
.aksrt-fi__title {
font-size: 28px;
line-height: 1.25;
margin: 0 0 12px;
}
.aksrt-fi__desc {
font-size: 14px;
color: var(--muted-2-color, #666);
line-height: 1.7;
margin-bottom: 14px;
}
.aksrt-fi__stat {
display: flex;
align-items: baseline;
gap: 6px;
margin-bottom: 14px;
}
.aksrt-fi__stat-num {
font-size: 36px;
font-weight: 800;
color: var(--color-primary, #3b82f6);
line-height: 1;
}
.aksrt-fi__stat-plus { font-size: 22px; font-weight: 700; color: var(--color-primary, #3b82f6); }
.aksrt-fi__stat-label { font-size: 13px; color: var(--muted-2-color, #888); margin-left: 4px; }
.aksrt-fi__tabs { display: flex; flex-wrap: wrap; gap: 8px; }
.aksrt-fi__tab {
border: 1px solid var(--color-line, #e5e7eb);
background: var(--surface-1, #fff);
border-radius: 999px;
padding: 6px 14px;
font-size: 13px;
cursor: pointer;
}
.aksrt-fi__tab.is-active {
background: var(--color-primary, #3b82f6);
border-color: transparent;
color: #fff;
}
.aksrt-fi__panel { display: none; }
.aksrt-fi__panel.is-active { display: block; }
.aksrt-fi__browser {
border-radius: 12px;
overflow: hidden;
border: 1px solid var(--color-line, #e5e7eb);
background: var(--surface-2, #f3f4f6);
box-shadow: 0 12px 32px rgba(0,0,0,.08);
}
.aksrt-fi__browser.is-plain { box-shadow: none; }
.aksrt-fi__browser-bar {
height: 28px;
background: #eef1f5;
display: flex;
align-items: center;
gap: 6px;
padding: 0 12px;
}
.aksrt-fi__browser-bar span {
width: 8px;
height: 8px;
border-radius: 50%;
background: #d0d5dd;
}
.aksrt-fi__browser-bar span:nth-child(1) { background: #ff5f57; }
.aksrt-fi__browser-bar span:nth-child(2) { background: #febc2e; }
.aksrt-fi__browser-bar span:nth-child(3) { background: #28c840; }
.aksrt-fi__browser-body {
position: relative;
width: 100%;
height: 0;
background: #111;
}
.aksrt-fi__img,
.aksrt-fi__video {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
object-fit: cover;
display: block;
}
.aksrt-term-cards {
display: grid;
grid-template-columns: repeat(var(--aksrt-tc-cols, 2), minmax(0, 1fr));
gap: 12px;
margin-bottom: 12px;
}
.aksrt-term-card {
display: block;
border-radius: 12px;
overflow: hidden;
border: 1px solid var(--color-line, #e5e7eb);
text-decoration: none;
color: inherit;
background: var(--surface-1, #fff);
}
.aksrt-term-card__cover {
height: 88px;
background: var(--surface-2, #f3f4f6) center/cover no-repeat;
}
.aksrt-term-card__cover.is-empty {
background: linear-gradient(135deg, #dbeafe, #eff6ff);
}
.aksrt-term-card__body { padding: 10px 12px 12px; }
.aksrt-term-card__name { font-weight: 700; font-size: 14px; }
.aksrt-term-card__count { font-size: 12px; color: var(--muted-2-color, #888); margin-top: 2px; }
.aksrt-paid-posts { list-style: none; margin: 0; padding: 0; }
.aksrt-paid-posts__item {
display: flex;
gap: 10px;
padding: 8px 0;
border-bottom: 1px dashed var(--color-line, #e5e7eb);
}
.aksrt-paid-posts__item:last-child { border-bottom: 0; }
.aksrt-paid-posts__thumb {
width: 56px;
height: 56px;
border-radius: 8px;
overflow: hidden;
flex-shrink: 0;
background: var(--surface-2, #eee);
}
.aksrt-paid-posts__thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.aksrt-paid-posts__body { min-width: 0; flex: 1; }
.aksrt-paid-posts__title {
display: block;
color: inherit;
text-decoration: none;
font-size: 13px;
font-weight: 600;
line-height: 1.4;
}
.aksrt-paid-posts__price {
display: inline-block;
margin-top: 4px;
font-size: 12px;
color: #e11d48;
font-weight: 700;
}
+1
View File
@@ -5,3 +5,4 @@
@import url("widget-slider.css");
@import url("widget-batch3.css");
@import url("widget-batch3c.css");
@import url("widget-batch3d.css");
+3
View File
@@ -68,6 +68,9 @@ function aksrt_register_widgets() {
register_widget('\AKSRT\Widgets\WidgetStats');
register_widget('\AKSRT\Widgets\WidgetReviews');
register_widget('\AKSRT\Widgets\WidgetHighlights');
register_widget('\AKSRT\Widgets\WidgetFeatureIntro');
register_widget('\AKSRT\Widgets\WidgetTermCards');
register_widget('\AKSRT\Widgets\WidgetPaidPosts');
// Forum widgets
+227
View File
@@ -0,0 +1,227 @@
<?php
/**
* Feature intro with media tabs (zibll zib_widget_feature_intro lite).
*
* @package AKSRT
*/
namespace AKSRT\Widgets;
defined('ABSPATH') || exit;
class WidgetFeatureIntro extends \WP_Widget {
public function __construct() {
parent::__construct(
'aksrt_feature_intro',
esc_html__('AKSRT 功能介绍', 'aksrt'),
[
'description' => esc_html__('左文右图/视频的 TAB 功能介绍模块。', 'aksrt'),
'classname' => 'widget_aksrt_feature_intro',
]
);
}
public function widget($args, $instance): void {
$slides = $this->normalize_slides($instance['slides'] ?? []);
if (!$slides) {
return;
}
$heading = trim((string) ($instance['heading'] ?? ''));
$desc = trim((string) ($instance['description'] ?? ''));
$media_pos = (($instance['media_position'] ?? 'right') === 'left') ? 'left' : 'right';
$chrome = !empty($instance['browser_chrome']) || !isset($instance['browser_chrome']);
$ratio = max(30, min(100, (int) ($instance['media_ratio'] ?? 66)));
$show_stat = !empty($instance['show_stat']);
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$uid = 'aksrt-fi-' . esc_attr($this->id);
echo '<div class="aksrt-fi media-' . esc_attr($media_pos) . '" id="' . $uid . '">';
echo '<div class="aksrt-fi__text">';
if ($heading !== '') {
echo '<h2 class="aksrt-fi__title">' . esc_html($heading) . '</h2>';
}
if ($desc !== '') {
echo '<div class="aksrt-fi__desc">' . wp_kses_post(wpautop($desc)) . '</div>';
}
if ($show_stat) {
$sn = sanitize_text_field((string) ($instance['stat_number'] ?? ''));
$ss = sanitize_text_field((string) ($instance['stat_suffix'] ?? '+'));
$sl = sanitize_text_field((string) ($instance['stat_label'] ?? ''));
if ($sn !== '') {
echo '<div class="aksrt-fi__stat"><span class="aksrt-fi__stat-num">' . esc_html($sn) . '</span>';
if ($ss !== '') {
echo '<span class="aksrt-fi__stat-plus">' . esc_html($ss) . '</span>';
}
if ($sl !== '') {
echo '<span class="aksrt-fi__stat-label">' . esc_html($sl) . '</span>';
}
echo '</div>';
}
}
if (count($slides) > 1) {
echo '<div class="aksrt-fi__tabs" role="tablist">';
foreach ($slides as $i => $s) {
$cls = $i === 0 ? ' is-active' : '';
$label = $s['tab_label'] !== '' ? $s['tab_label'] : sprintf(__('面板 %d', 'aksrt'), $i + 1);
echo '<button type="button" class="aksrt-fi__tab' . esc_attr($cls) . '" data-index="' . (int) $i . '" role="tab">' . esc_html($label) . '</button>';
}
echo '</div>';
}
echo '</div>';
echo '<div class="aksrt-fi__media">';
foreach ($slides as $i => $s) {
$cls = $i === 0 ? ' is-active' : '';
echo '<div class="aksrt-fi__panel' . esc_attr($cls) . '" data-index="' . (int) $i . '">';
$box_cls = $chrome ? 'aksrt-fi__browser' : 'aksrt-fi__browser is-plain';
echo '<div class="' . esc_attr($box_cls) . '">';
if ($chrome) {
echo '<div class="aksrt-fi__browser-bar" aria-hidden="true"><span></span><span></span><span></span></div>';
}
echo '<div class="aksrt-fi__browser-body" style="padding-bottom:' . esc_attr((string) $ratio) . '%">';
if ($s['media_type'] === 'video' && $s['video_url'] !== '') {
$poster = $s['video_poster'] !== '' ? ' poster="' . esc_url($s['video_poster']) . '"' : '';
echo '<video class="aksrt-fi__video" src="' . esc_url($s['video_url']) . '"' . $poster . ' autoplay muted loop playsinline></video>';
} elseif ($s['image'] !== '') {
echo '<img class="aksrt-fi__img" src="' . esc_url($s['image']) . '" alt="' . esc_attr($s['tab_label']) . '" loading="lazy" />';
}
echo '</div></div></div>';
}
echo '</div></div>';
if (count($slides) > 1) {
?>
<script>
(function(){
var root=document.getElementById(<?php echo wp_json_encode($uid); ?>);
if(!root||root._aksrtFi)return; root._aksrtFi=1;
var tabs=[].slice.call(root.querySelectorAll('.aksrt-fi__tab'));
var panels=[].slice.call(root.querySelectorAll('.aksrt-fi__panel'));
function go(i){
tabs.forEach(function(t,idx){t.classList.toggle('is-active',idx===i);});
panels.forEach(function(p,idx){p.classList.toggle('is-active',idx===i);});
}
tabs.forEach(function(t){ t.addEventListener('click', function(){ go(parseInt(t.getAttribute('data-index')||'0',10)); }); });
})();
</script>
<?php
}
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* @param mixed $raw
* @return array<int, array<string,string>>
*/
private function normalize_slides($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;
}
$type = sanitize_key((string) ($row['media_type'] ?? 'image'));
if ($type !== 'video') {
$type = 'image';
}
$image = esc_url_raw(trim((string) ($row['image'] ?? '')));
$video = esc_url_raw(trim((string) ($row['video_url'] ?? '')));
if ($type === 'video' && $video === '') {
continue;
}
if ($type === 'image' && $image === '') {
continue;
}
$out[] = [
'tab_label' => sanitize_text_field((string) ($row['tab_label'] ?? '')),
'media_type' => $type,
'image' => $image,
'video_url' => $video,
'video_poster' => esc_url_raw(trim((string) ($row['video_poster'] ?? ''))),
];
if (count($out) >= 8) {
break;
}
}
return $out;
}
public function form($instance): void {
$json = is_string($instance['slides'] ?? null)
? (string) $instance['slides']
: wp_json_encode($this->normalize_slides($instance['slides'] ?? []), 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('heading')); ?>" value="<?php echo esc_attr((string) ($instance['heading'] ?? '')); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('描述', 'aksrt'); ?>
<textarea class="widefat" rows="3" name="<?php echo esc_attr($this->get_field_name('description')); ?>"><?php echo esc_textarea((string) ($instance['description'] ?? '')); ?></textarea>
</label>
</p>
<p>
<label><?php esc_html_e('媒体 TAB JSON', 'aksrt'); ?>
<textarea class="widefat" rows="6" name="<?php echo esc_attr($this->get_field_name('slides')); ?>" placeholder='[{"tab_label":"面板1","media_type":"image","image":"https://..."}]'><?php echo esc_textarea((string) $json); ?></textarea>
</label>
</p>
<p>
<label><?php esc_html_e('媒体位置', 'aksrt'); ?>
<select name="<?php echo esc_attr($this->get_field_name('media_position')); ?>">
<option value="right" <?php selected(($instance['media_position'] ?? 'right'), 'right'); ?>><?php esc_html_e('右侧', 'aksrt'); ?></option>
<option value="left" <?php selected(($instance['media_position'] ?? ''), 'left'); ?>><?php esc_html_e('左侧', 'aksrt'); ?></option>
</select>
</label>
</p>
<p>
<label><?php esc_html_e('媒体高度占比 %', 'aksrt'); ?>
<input class="tiny-text" type="number" min="30" max="100" name="<?php echo esc_attr($this->get_field_name('media_ratio')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['media_ratio'] ?? 66))); ?>">
</label>
</p>
<p>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('browser_chrome')); ?>" value="1" <?php checked(!empty($instance['browser_chrome']) || !isset($instance['browser_chrome'])); ?>> <?php esc_html_e('浏览器外框', 'aksrt'); ?></label><br>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_stat')); ?>" value="1" <?php checked(!empty($instance['show_stat'])); ?>> <?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('stat_number')); ?>" value="<?php echo esc_attr((string) ($instance['stat_number'] ?? '')); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('统计后缀', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('stat_suffix')); ?>" value="<?php echo esc_attr((string) ($instance['stat_suffix'] ?? '+')); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('统计标签', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('stat_label')); ?>" value="<?php echo esc_attr((string) ($instance['stat_label'] ?? '')); ?>">
</label>
</p>
<?php
}
public function update($new_instance, $old_instance): array {
return [
'heading' => sanitize_text_field((string) ($new_instance['heading'] ?? '')),
'description' => wp_kses_post((string) ($new_instance['description'] ?? '')),
'slides' => $this->normalize_slides($new_instance['slides'] ?? []),
'media_position' => ($new_instance['media_position'] ?? '') === 'left' ? 'left' : 'right',
'media_ratio' => max(30, min(100, (int) ($new_instance['media_ratio'] ?? 66))),
'browser_chrome' => !empty($new_instance['browser_chrome']) ? 1 : 0,
'show_stat' => !empty($new_instance['show_stat']) ? 1 : 0,
'stat_number' => sanitize_text_field((string) ($new_instance['stat_number'] ?? '')),
'stat_suffix' => sanitize_text_field((string) ($new_instance['stat_suffix'] ?? '')),
'stat_label' => sanitize_text_field((string) ($new_instance['stat_label'] ?? '')),
];
}
}
+140
View File
@@ -0,0 +1,140 @@
<?php
/**
* Paid posts list (zibll zib_widget_ui_posts_pay lite).
*
* @package AKSRT
*/
namespace AKSRT\Widgets;
defined('ABSPATH') || exit;
class WidgetPaidPosts extends \WP_Widget {
public function __construct() {
parent::__construct(
'aksrt_paid_posts',
esc_html__('AKSRT 付费内容列表', 'aksrt'),
[
'description' => esc_html__('展示开启付费/积分可见的文章。', 'aksrt'),
'classname' => 'widget_aksrt_paid_posts',
]
);
}
public function widget($args, $instance): void {
$count = max(1, min(20, (int) ($instance['count'] ?? 6)));
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'date'));
$query = [
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $count,
'order' => 'DESC',
'meta_query' => [
'relation' => 'OR',
[
'key' => '_aksrt_pay_price',
'value' => 0,
'compare' => '>',
'type' => 'NUMERIC',
],
[
'key' => '_aksrt_credits_cost',
'value' => 0,
'compare' => '>',
'type' => 'NUMERIC',
],
[
'key' => '_aksrt_visibility',
'value' => ['pay', 'credits'],
'compare' => 'IN',
],
],
];
if ($orderby === 'views') {
$query['meta_key'] = '_aksrt_views';
$query['orderby'] = 'meta_value_num';
} elseif ($orderby === 'price') {
$query['meta_key'] = '_aksrt_pay_price';
$query['orderby'] = 'meta_value_num';
} else {
$query['orderby'] = 'date';
}
$q = new \WP_Query($query);
if (!$q->have_posts()) {
wp_reset_postdata();
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
}
echo '<ul class="aksrt-paid-posts">';
while ($q->have_posts()) {
$q->the_post();
$price = (float) get_post_meta(get_the_ID(), '_aksrt_pay_price', true);
$credits = (int) get_post_meta(get_the_ID(), '_aksrt_credits_cost', true);
$thumb = get_the_post_thumbnail_url(get_the_ID(), 'thumbnail');
echo '<li class="aksrt-paid-posts__item">';
if ($thumb) {
echo '<a class="aksrt-paid-posts__thumb" href="' . esc_url(get_permalink()) . '"><img src="' . esc_url($thumb) . '" alt="" loading="lazy" /></a>';
}
echo '<div class="aksrt-paid-posts__body">';
echo '<a class="aksrt-paid-posts__title" href="' . esc_url(get_permalink()) . '">' . esc_html(get_the_title()) . '</a>';
echo '<div class="aksrt-paid-posts__meta">';
if ($price > 0) {
echo '<span class="aksrt-paid-posts__price">¥' . esc_html(number_format($price, 2, '.', '')) . '</span>';
} elseif ($credits > 0) {
echo '<span class="aksrt-paid-posts__price">' . esc_html(sprintf(__('%d 积分', 'aksrt'), $credits)) . '</span>';
} else {
echo '<span class="aksrt-paid-posts__price">' . esc_html__('付费', 'aksrt') . '</span>';
}
echo '</div></div></li>';
}
echo '</ul>';
wp_reset_postdata();
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
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="20" name="<?php echo esc_attr($this->get_field_name('count')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['count'] ?? 6))); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('排序', 'aksrt'); ?>
<select name="<?php echo esc_attr($this->get_field_name('orderby')); ?>">
<option value="date" <?php selected(($instance['orderby'] ?? 'date'), 'date'); ?>><?php esc_html_e('时间', 'aksrt'); ?></option>
<option value="views" <?php selected(($instance['orderby'] ?? ''), 'views'); ?>><?php esc_html_e('浏览', 'aksrt'); ?></option>
<option value="price" <?php selected(($instance['orderby'] ?? ''), 'price'); ?>><?php esc_html_e('价格', 'aksrt'); ?></option>
</select>
</label>
</p>
<?php
}
public function update($new_instance, $old_instance): array {
$orderby = sanitize_key((string) ($new_instance['orderby'] ?? 'date'));
if (!in_array($orderby, ['date', 'views', 'price'], true)) {
$orderby = 'date';
}
return [
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'count' => max(1, min(20, (int) ($new_instance['count'] ?? 6))),
'orderby' => $orderby,
];
}
}
+166
View File
@@ -0,0 +1,166 @@
<?php
/**
* Taxonomy term cards (zibll zib_widget_ui_term_card lite).
*
* @package AKSRT
*/
namespace AKSRT\Widgets;
defined('ABSPATH') || exit;
class WidgetTermCards extends \WP_Widget {
public function __construct() {
parent::__construct(
'aksrt_term_cards',
esc_html__('AKSRT 分类/标签卡片', 'aksrt'),
[
'description' => esc_html__('以卡片展示分类或标签。', 'aksrt'),
'classname' => 'widget_aksrt_term_cards',
]
);
}
public function widget($args, $instance): void {
$taxonomy = sanitize_key((string) ($instance['taxonomy'] ?? 'category'));
if (!taxonomy_exists($taxonomy)) {
$taxonomy = 'category';
}
$number = max(1, min(30, (int) ($instance['number'] ?? 8)));
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'count'));
$include = $this->parse_ids((string) ($instance['include'] ?? ''));
$hide_empty = !isset($instance['hide_empty']) || !empty($instance['hide_empty']);
$term_args = [
'taxonomy' => $taxonomy,
'number' => $number,
'hide_empty' => $hide_empty,
];
if ($include) {
$term_args['include'] = $include;
$term_args['orderby'] = 'include';
} else {
$term_args['orderby'] = in_array($orderby, ['name', 'count', 'id'], true) ? $orderby : 'count';
$term_args['order'] = $orderby === 'name' ? 'ASC' : '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
}
$cols = max(1, min(4, (int) ($instance['columns'] ?? 2)));
echo '<div class="aksrt-term-cards" style="--aksrt-tc-cols:' . esc_attr((string) $cols) . '">';
foreach ($terms as $term) {
$link = get_term_link($term);
if (is_wp_error($link)) {
continue;
}
$thumb = '';
if (function_exists('get_term_meta')) {
$img_id = (int) get_term_meta($term->term_id, 'thumbnail_id', true);
if (!$img_id) {
$img_id = (int) get_term_meta($term->term_id, '_aksrt_cover', true);
}
if ($img_id) {
$thumb = wp_get_attachment_image_url($img_id, 'medium') ?: '';
}
if ($thumb === '') {
$thumb = (string) get_term_meta($term->term_id, '_aksrt_cover_url', true);
}
}
echo '<a class="aksrt-term-card" href="' . esc_url($link) . '">';
if ($thumb !== '') {
echo '<div class="aksrt-term-card__cover" style="background-image:url(' . esc_url($thumb) . ')"></div>';
} else {
echo '<div class="aksrt-term-card__cover is-empty"></div>';
}
echo '<div class="aksrt-term-card__body">';
echo '<div class="aksrt-term-card__name">' . esc_html($term->name) . '</div>';
echo '<div class="aksrt-term-card__count">' . esc_html(sprintf(_n('%d 篇', '%d 篇', (int) $term->count, 'aksrt'), (int) $term->count)) . '</div>';
echo '</div></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 {
$taxs = get_taxonomies(['public' => true], 'objects');
?>
<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('分类法', 'aksrt'); ?>
<select class="widefat" name="<?php echo esc_attr($this->get_field_name('taxonomy')); ?>">
<?php foreach ($taxs as $tax) : ?>
<option value="<?php echo esc_attr($tax->name); ?>" <?php selected(($instance['taxonomy'] ?? 'category'), $tax->name); ?>><?php echo esc_html($tax->labels->name . ' (' . $tax->name . ')'); ?></option>
<?php endforeach; ?>
</select>
</label>
</p>
<p>
<label><?php esc_html_e('数量', 'aksrt'); ?>
<input class="tiny-text" type="number" min="1" max="30" name="<?php echo esc_attr($this->get_field_name('number')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['number'] ?? 8))); ?>">
</label>
<label><?php esc_html_e('列数', 'aksrt'); ?>
<input class="tiny-text" type="number" min="1" max="4" name="<?php echo esc_attr($this->get_field_name('columns')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['columns'] ?? 2))); ?>">
</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="id" <?php selected(($instance['orderby'] ?? ''), 'id'); ?>><?php esc_html_e('ID', '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 {
return [
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'taxonomy' => sanitize_key((string) ($new_instance['taxonomy'] ?? 'category')),
'number' => max(1, min(30, (int) ($new_instance['number'] ?? 8))),
'columns' => max(1, min(4, (int) ($new_instance['columns'] ?? 2))),
'orderby' => in_array(($new_instance['orderby'] ?? ''), ['count', 'name', 'id'], true) ? $new_instance['orderby'] : 'count',
'include' => sanitize_text_field((string) ($new_instance['include'] ?? '')),
'hide_empty' => !empty($new_instance['hide_empty']) ? 1 : 0,
];
}
}