1:1 zibll user widget guest shell + checkin/actions, main post list styles, term aggregation cards, and ranking by check-in days.
198 lines
7.2 KiB
PHP
198 lines
7.2 KiB
PHP
<?php
|
|
/**
|
|
* Term aggregation cards (zibll zib_widget_ui_term_lists_card lite).
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
namespace AKSRT\Widgets;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class WidgetTermAggregation extends \WP_Widget {
|
|
|
|
public function __construct() {
|
|
parent::__construct(
|
|
'aksrt_term_aggregation',
|
|
esc_html__('AKSRT 分类聚合', 'aksrt'),
|
|
[
|
|
'description' => esc_html__('分类封面 + 最新文章列表聚合卡。', 'aksrt'),
|
|
'classname' => 'widget_aksrt_term_aggregation',
|
|
]
|
|
);
|
|
}
|
|
|
|
public function widget($args, $instance): void {
|
|
$taxonomy = sanitize_key((string) ($instance['taxonomy'] ?? 'category'));
|
|
if (!taxonomy_exists($taxonomy)) {
|
|
$taxonomy = 'category';
|
|
}
|
|
$term_ids = $this->parse_ids((string) ($instance['term_ids'] ?? ''));
|
|
$count = max(1, min(12, (int) ($instance['count'] ?? 4)));
|
|
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'date'));
|
|
$columns = max(1, min(3, (int) ($instance['columns'] ?? 1)));
|
|
|
|
if (!$term_ids) {
|
|
// Fallback: top terms by count.
|
|
$terms = get_terms([
|
|
'taxonomy' => $taxonomy,
|
|
'number' => max(1, min(4, (int) ($instance['auto_terms'] ?? 2))),
|
|
'orderby' => 'count',
|
|
'order' => 'DESC',
|
|
'hide_empty' => true,
|
|
]);
|
|
if (is_wp_error($terms) || !$terms) {
|
|
return;
|
|
}
|
|
$term_ids = array_map(static fn($t) => (int) $t->term_id, $terms);
|
|
}
|
|
|
|
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-term-agg" style="--aksrt-ta-cols:' . esc_attr((string) $columns) . '">';
|
|
foreach ($term_ids as $term_id) {
|
|
$term = get_term($term_id, $taxonomy);
|
|
if (!$term || is_wp_error($term)) {
|
|
continue;
|
|
}
|
|
$link = get_term_link($term);
|
|
if (is_wp_error($link)) {
|
|
continue;
|
|
}
|
|
$cover = '';
|
|
$img_id = (int) get_term_meta($term_id, 'thumbnail_id', true);
|
|
if (!$img_id) {
|
|
$img_id = (int) get_term_meta($term_id, '_aksrt_cover', true);
|
|
}
|
|
if ($img_id) {
|
|
$cover = wp_get_attachment_image_url($img_id, 'medium') ?: '';
|
|
}
|
|
if ($cover === '') {
|
|
$cover = (string) get_term_meta($term_id, '_aksrt_cover_url', true);
|
|
}
|
|
|
|
$posts_q = [
|
|
'post_type' => 'post',
|
|
'post_status' => 'publish',
|
|
'posts_per_page' => $count,
|
|
'ignore_sticky_posts' => true,
|
|
'no_found_rows' => true,
|
|
'tax_query' => [[
|
|
'taxonomy' => $taxonomy,
|
|
'field' => 'term_id',
|
|
'terms' => $term_id,
|
|
]],
|
|
];
|
|
if ($orderby === 'views') {
|
|
$posts_q['meta_key'] = 'aksrt_post_views';
|
|
$posts_q['orderby'] = 'meta_value_num';
|
|
$posts_q['order'] = 'DESC';
|
|
} elseif ($orderby === 'modified') {
|
|
$posts_q['orderby'] = 'modified';
|
|
$posts_q['order'] = 'DESC';
|
|
} else {
|
|
$posts_q['orderby'] = 'date';
|
|
$posts_q['order'] = 'DESC';
|
|
}
|
|
$q = new \WP_Query($posts_q);
|
|
|
|
echo '<div class="aksrt-term-agg__card">';
|
|
echo '<a class="aksrt-term-agg__head" href="' . esc_url($link) . '">';
|
|
echo '<div class="aksrt-term-agg__cover' . ($cover ? '' : ' is-empty') . '"' . ($cover ? ' style="background-image:url(' . esc_url($cover) . ')"' : '') . '></div>';
|
|
echo '<div class="aksrt-term-agg__name">' . esc_html($term->name) . '</div>';
|
|
echo '<div class="aksrt-term-agg__count">' . esc_html(sprintf(_n('%d 篇', '%d 篇', (int) $term->count, 'aksrt'), (int) $term->count)) . '</div>';
|
|
echo '</a>';
|
|
if ($q->have_posts()) {
|
|
echo '<ul class="aksrt-term-agg__posts">';
|
|
while ($q->have_posts()) {
|
|
$q->the_post();
|
|
echo '<li><a href="' . esc_url(get_permalink()) . '">' . esc_html(get_the_title()) . '</a></li>';
|
|
}
|
|
echo '</ul>';
|
|
wp_reset_postdata();
|
|
}
|
|
echo '</div>';
|
|
}
|
|
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); ?></option>
|
|
<?php endforeach; ?>
|
|
</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('term_ids')); ?>" value="<?php echo esc_attr((string) ($instance['term_ids'] ?? '')); ?>">
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label><?php esc_html_e('每分类文章数', 'aksrt'); ?>
|
|
<input class="tiny-text" type="number" min="1" max="12" name="<?php echo esc_attr($this->get_field_name('count')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['count'] ?? 4))); ?>">
|
|
</label>
|
|
<label><?php esc_html_e('列数', 'aksrt'); ?>
|
|
<input class="tiny-text" type="number" min="1" max="3" name="<?php echo esc_attr($this->get_field_name('columns')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['columns'] ?? 1))); ?>">
|
|
</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="modified" <?php selected(($instance['orderby'] ?? ''), 'modified'); ?>><?php esc_html_e('更新', 'aksrt'); ?></option>
|
|
<option value="views" <?php selected(($instance['orderby'] ?? ''), 'views'); ?>><?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', 'modified', 'views'], true)) {
|
|
$orderby = 'date';
|
|
}
|
|
return [
|
|
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
|
|
'taxonomy' => sanitize_key((string) ($new_instance['taxonomy'] ?? 'category')),
|
|
'term_ids' => sanitize_text_field((string) ($new_instance['term_ids'] ?? '')),
|
|
'count' => max(1, min(12, (int) ($new_instance['count'] ?? 4))),
|
|
'columns' => max(1, min(3, (int) ($new_instance['columns'] ?? 1))),
|
|
'orderby' => $orderby,
|
|
'auto_terms' => max(1, min(4, (int) ($new_instance['auto_terms'] ?? 2))),
|
|
];
|
|
}
|
|
}
|