feat(widgets): user minicards + term graphic cards/aggregation depth
User lists render author-minicard rows; term cards/aggregation consume aksrt_graphic_card with style-1..4, meta CTA, and orderby-aware post rows.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
/**
|
||||
* Term aggregation cards (zibll zib_widget_ui_term_lists_card lite).
|
||||
* Term aggregation cards (zibll zib_widget_ui_term_lists_card → zib_term_aggregation).
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
@@ -16,7 +16,7 @@ class WidgetTermAggregation extends \WP_Widget {
|
||||
'aksrt_term_aggregation',
|
||||
esc_html__('AKSRT 分类聚合', 'aksrt'),
|
||||
[
|
||||
'description' => esc_html__('分类封面 + 最新文章列表聚合卡。', 'aksrt'),
|
||||
'description' => esc_html__('分类封面 + 描述 + 更多文章 CTA + 文章列表聚合卡。', 'aksrt'),
|
||||
'classname' => 'widget_aksrt_term_aggregation',
|
||||
]
|
||||
);
|
||||
@@ -37,15 +37,68 @@ class WidgetTermAggregation extends \WP_Widget {
|
||||
return array_values(array_unique($ids));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \WP_Term $term
|
||||
*/
|
||||
private static function term_cover_url(\WP_Term $term): string {
|
||||
if (class_exists(WidgetTermCards::class) && method_exists(WidgetTermCards::class, 'term_cover_url')) {
|
||||
return WidgetTermCards::term_cover_url($term);
|
||||
}
|
||||
$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) {
|
||||
$url = wp_get_attachment_image_url($img_id, 'medium');
|
||||
if ($url) {
|
||||
return (string) $url;
|
||||
}
|
||||
}
|
||||
$url = (string) get_term_meta($term->term_id, '_aksrt_cover_url', true);
|
||||
return $url !== '' ? $url : get_template_directory_uri() . '/assets/img/placeholder.svg';
|
||||
}
|
||||
|
||||
/**
|
||||
* Sum of post views under a term (best-effort).
|
||||
*/
|
||||
private static function term_views_sum(int $term_id, string $taxonomy): int {
|
||||
$q = new \WP_Query([
|
||||
'post_type' => 'post',
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => 50,
|
||||
'fields' => 'ids',
|
||||
'no_found_rows' => true,
|
||||
'update_post_meta_cache' => true,
|
||||
'update_post_term_cache' => false,
|
||||
'tax_query' => [[
|
||||
'taxonomy' => $taxonomy,
|
||||
'field' => 'term_id',
|
||||
'terms' => $term_id,
|
||||
]],
|
||||
]);
|
||||
$sum = 0;
|
||||
foreach ($q->posts as $pid) {
|
||||
$sum += function_exists('aksrt_get_post_views')
|
||||
? (int) aksrt_get_post_views((int) $pid)
|
||||
: (int) get_post_meta((int) $pid, 'aksrt_post_views', true);
|
||||
}
|
||||
return $sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $instance
|
||||
*/
|
||||
public static function render_html(array $instance): string {
|
||||
$taxonomy = sanitize_key((string) ($instance['taxonomy'] ?? 'category'));
|
||||
if (!taxonomy_exists($taxonomy)) {
|
||||
$taxonomy = 'category';
|
||||
}
|
||||
$term_ids = self::parse_ids_static((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)));
|
||||
$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)));
|
||||
$target_blank = !empty($instance['target_blank']);
|
||||
$tgt = $target_blank ? ' target="_blank" rel="noopener noreferrer"' : '';
|
||||
|
||||
if (!$term_ids) {
|
||||
$terms = get_terms([
|
||||
@@ -74,16 +127,38 @@ class WidgetTermAggregation extends \WP_Widget {
|
||||
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);
|
||||
|
||||
$cover = self::term_cover_url($term);
|
||||
$views = self::term_views_sum((int) $term->term_id, $taxonomy);
|
||||
$more_badge = '<badge class="img-badge px12"><i class="fa fa-fire" aria-hidden="true"></i> ' . esc_html((string) $views) . '</badge>';
|
||||
|
||||
$img_html = '';
|
||||
if (function_exists('aksrt_graphic_card')) {
|
||||
$img_html = aksrt_graphic_card([
|
||||
'type' => '',
|
||||
'class' => '',
|
||||
'img' => $cover,
|
||||
'alt' => $term->name,
|
||||
'link' => [
|
||||
'url' => $link,
|
||||
'target' => $target_blank ? '_blank' : '',
|
||||
],
|
||||
'lazy' => true,
|
||||
'height_scale' => 70,
|
||||
'mask_opacity' => 0,
|
||||
'more' => $more_badge,
|
||||
]);
|
||||
}
|
||||
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);
|
||||
|
||||
$description = trim((string) $term->description);
|
||||
if ($description === '' && current_user_can('manage_categories')) {
|
||||
$edit = get_edit_term_link((int) $term->term_id, $taxonomy);
|
||||
$description = esc_html__('请在后台为该分类添加描述。', 'aksrt');
|
||||
if ($edit) {
|
||||
$description .= ' <a href="' . esc_url($edit) . '">' . esc_html__('立即编辑', 'aksrt') . '</a>';
|
||||
}
|
||||
} else {
|
||||
$description = esc_html($description);
|
||||
}
|
||||
|
||||
$posts_q = [
|
||||
@@ -91,39 +166,75 @@ class WidgetTermAggregation extends \WP_Widget {
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => $count,
|
||||
'ignore_sticky_posts' => true,
|
||||
'no_found_rows' => true,
|
||||
'no_found_rows' => false,
|
||||
'tax_query' => [[
|
||||
'taxonomy' => $taxonomy,
|
||||
'field' => 'term_id',
|
||||
'terms' => $term_id,
|
||||
'terms' => (int) $term->term_id,
|
||||
]],
|
||||
];
|
||||
if ($orderby === 'views') {
|
||||
$posts_q['meta_key'] = 'aksrt_post_views';
|
||||
$posts_q['orderby'] = 'meta_value_num';
|
||||
$posts_q['order'] = 'DESC';
|
||||
$posts_q['orderby'] = 'meta_value_num';
|
||||
$posts_q['order'] = 'DESC';
|
||||
} elseif ($orderby === 'like') {
|
||||
$posts_q['meta_key'] = 'aksrt_post_likes';
|
||||
$posts_q['orderby'] = 'meta_value_num';
|
||||
$posts_q['order'] = 'DESC';
|
||||
} elseif ($orderby === 'comment_count') {
|
||||
$posts_q['orderby'] = 'comment_count';
|
||||
$posts_q['order'] = 'DESC';
|
||||
} elseif ($orderby === 'modified') {
|
||||
$posts_q['orderby'] = 'modified';
|
||||
$posts_q['order'] = 'DESC';
|
||||
$posts_q['order'] = 'DESC';
|
||||
} else {
|
||||
$posts_q['orderby'] = 'date';
|
||||
$posts_q['order'] = 'DESC';
|
||||
$posts_q['order'] = 'DESC';
|
||||
}
|
||||
$q = new \WP_Query($posts_q);
|
||||
$found = (int) $q->found_posts;
|
||||
|
||||
echo '<div class="zib-widget term-aggregation mb10 aksrt-term-agg__card">';
|
||||
echo '<div class="mb20 hover-zoom-img flex px12-sm">';
|
||||
echo '<div class="term-img flex0 em09-sm">' . $img_html . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
echo '<div class="term-title ml10 flex xx flex1 jsb">';
|
||||
echo '<a class="em14 key-color text-ellipsis"' . $tgt . ' href="' . esc_url($link) . '">' . esc_html($term->name) . '</a>';
|
||||
echo '<div class="text-ellipsis-2 muted-color">' . $description . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
echo '<div class="term-meta flex jsb ac">';
|
||||
echo '<span class="px12 muted-3-color text-ellipsis"><i class="fa fa-file-text-o fa-fw" aria-hidden="true"></i>'
|
||||
. esc_html(sprintf(
|
||||
/* translators: %s: article count */
|
||||
__('%s篇文章', 'aksrt'),
|
||||
(string) $found
|
||||
))
|
||||
. '</span>';
|
||||
echo '<a class="but p2-10 px12 c-blue hide-sm shrink0"' . $tgt . ' href="' . esc_url($link) . '"><i class="fa fa-angle-right" aria-hidden="true"></i>' . esc_html__('更多文章', 'aksrt') . '</a>';
|
||||
echo '</div></div></div>';
|
||||
|
||||
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>';
|
||||
$pid = get_the_ID();
|
||||
$title = get_the_title($pid);
|
||||
$meta = '';
|
||||
if ($orderby === 'views') {
|
||||
$v = function_exists('aksrt_get_post_views') ? (int) aksrt_get_post_views($pid) : (int) get_post_meta($pid, 'aksrt_post_views', true);
|
||||
$meta = '<i class="fa fa-eye mr3" aria-hidden="true"></i>' . esc_html((string) $v);
|
||||
} elseif ($orderby === 'like') {
|
||||
$v = function_exists('aksrt_get_post_likes') ? (int) aksrt_get_post_likes($pid) : (int) get_post_meta($pid, 'aksrt_post_likes', true);
|
||||
$meta = '<i class="fa fa-heart mr3" aria-hidden="true"></i>' . esc_html((string) $v);
|
||||
} elseif ($orderby === 'comment_count') {
|
||||
$meta = '<i class="fa fa-comment-o mr3" aria-hidden="true"></i>' . esc_html((string) get_comments_number($pid));
|
||||
} else {
|
||||
$ts = $orderby === 'modified' ? get_the_modified_time('U', $pid) : get_the_time('U', $pid);
|
||||
$ago = human_time_diff((int) $ts, current_time('timestamp'));
|
||||
$meta = '<i class="fa fa-clock-o mr3" aria-hidden="true"></i>' . esc_html($ago);
|
||||
}
|
||||
echo '<div class="mt10 flex jsb ac">';
|
||||
echo '<div class="text-ellipsis"><a class="icon-circle mln3"' . $tgt . ' href="' . esc_url(get_permalink($pid)) . '">' . esc_html($title) . '</a></div>';
|
||||
echo '<div class="em09 muted-3-color flex0 ml10">' . $meta . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
echo '</div>';
|
||||
}
|
||||
echo '</ul>';
|
||||
wp_reset_postdata();
|
||||
}
|
||||
echo '</div>';
|
||||
@@ -132,6 +243,10 @@ class WidgetTermAggregation extends \WP_Widget {
|
||||
return (string) ob_get_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $args
|
||||
* @param array<string,mixed> $instance
|
||||
*/
|
||||
public function widget($args, $instance): void {
|
||||
$html = self::render_html($instance);
|
||||
if ($html === '') {
|
||||
@@ -146,11 +261,9 @@ class WidgetTermAggregation extends \WP_Widget {
|
||||
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
/** @return array<int,int> */
|
||||
private function parse_ids(string $raw): array {
|
||||
return self::parse_ids_static($raw);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $instance
|
||||
*/
|
||||
public function form($instance): void {
|
||||
$taxs = get_taxonomies(['public' => true], 'objects');
|
||||
?>
|
||||
@@ -187,25 +300,36 @@ class WidgetTermAggregation extends \WP_Widget {
|
||||
<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>
|
||||
<option value="like" <?php selected(($instance['orderby'] ?? ''), 'like'); ?>><?php esc_html_e('点赞', 'aksrt'); ?></option>
|
||||
<option value="comment_count" <?php selected(($instance['orderby'] ?? ''), 'comment_count'); ?>><?php esc_html_e('评论', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('target_blank')); ?>" value="1" <?php checked(!empty($instance['target_blank'])); ?>> <?php esc_html_e('新窗口打开', 'aksrt'); ?></label>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $new_instance
|
||||
* @param array<string,mixed> $old_instance
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
public function update($new_instance, $old_instance): array {
|
||||
$orderby = sanitize_key((string) ($new_instance['orderby'] ?? 'date'));
|
||||
if (!in_array($orderby, ['date', 'modified', 'views'], true)) {
|
||||
if (!in_array($orderby, ['date', 'modified', 'views', 'like', 'comment_count'], 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))),
|
||||
'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))),
|
||||
'target_blank' => !empty($new_instance['target_blank']) ? 1 : 0,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user