(string) $instance['taxonomy'],
'number' => (int) $instance['count'],
'hide_empty' => true,
'orderby' => 'count',
'order' => 'DESC',
];
$ids = RendererHelpers::ids((string) $instance['term_ids']);
if ($ids) {
$args['include'] = $ids;
$args['orderby'] = 'include';
}
$terms = get_terms($args);
return is_wp_error($terms) ? [] : (array) $terms;
}
private static function term_cards(array $instance): string {
$terms = self::terms($instance);
if (!$terms) {
return RendererHelpers::empty_state(__('暂无分类', 'aksrt'));
}
$html = '
';
}
private static function term_aggregation(array $instance): string {
$terms = self::terms($instance);
if (!$terms) {
return RendererHelpers::empty_state(__('暂无聚合内容', 'aksrt'));
}
$taxonomy = (string) $instance['taxonomy'];
$term_ids = array_map(static fn($term): int => (int) $term->term_id, $terms);
$query = new \WP_Query([
'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => min(100, count($terms) * 6),
'ignore_sticky_posts' => true, 'no_found_rows' => true,
'tax_query' => [['taxonomy' => $taxonomy, 'field' => 'term_id', 'terms' => $term_ids]],
]);
$by_term = [];
foreach ($query->posts as $post) {
foreach (wp_get_post_terms((int) $post->ID, $taxonomy, ['fields' => 'ids']) as $term_id) {
if (in_array((int) $term_id, $term_ids, true) && count($by_term[$term_id] ?? []) < 4) {
$by_term[$term_id][] = $post;
}
}
}
$html = '';
foreach ($terms as $term) {
$link = get_term_link($term);
if (is_wp_error($link)) {
continue;
}
$html .= '
';
}
return $html . '
';
}
private static function posts(string $slug, array $instance, array $context): string {
$widget_id = (string) ($context['widget_id'] ?? '');
if (($instance['load_mode'] ?? 'sync') === 'ajax') {
return '';
}
$payload = PostRenderer::payload($instance, $slug, 1);
return '' . $payload['html'] . $payload['paginate_html'] . '
';
}
private static function tab_posts(array $instance, array $context): string {
$style = in_array(($instance['style'] ?? 'mini'), ['list', 'card', 'mini'], true) ? (string) $instance['style'] : 'mini';
$tabs = array_values((array) ($instance['tabs'] ?? []));
if (!$tabs) {
return RendererHelpers::empty_state(__('请配置文章栏目', 'aksrt'));
}
$buttons = '';
$key = sanitize_html_class((string) ($context['widget_id'] ?? 'preview')) ?: 'preview';
$panel_id = 'aksrt-tab-panel-' . $key;
$ajax_url = rest_url('aksrt/v1/widgets/tab_posts/items');
$first_tab_id = 'aksrt-tab-' . $key . '-0';
$placeholder_type = $style === 'card' ? 'card' : 'lists';
$placeholder = $style === 'mini'
? str_repeat('', max(1, (int) ($instance['count'] ?? 6)))
: aksrt_render_placeholder($placeholder_type, max(1, (int) ($instance['count'] ?? 6)));
$panels = '';
$visible_index = 0;
foreach ($tabs as $index => $tab) {
if (!is_array($tab) || trim((string) ($tab['title'] ?? '')) === '') continue;
$tab_id = 'aksrt-tab-' . $key . '-' . $visible_index;
$selected = $visible_index === 0;
$buttons .= '' . wp_kses_post((string) $tab['title']) . '';
if ($selected) {
$payload = PostRenderer::payload($instance, 'tab_posts', 1, (string) $index);
$body = $payload['html'] . $payload['paginate_html'] . '' . $placeholder . '
';
} else {
$body = '' . $placeholder . '
';
}
$panels .= '';
$visible_index++;
}
if ($buttons === '') return RendererHelpers::empty_state(__('请配置文章栏目', 'aksrt'));
return '';
}
private static function post_pay(array $instance): string {
$post = get_post();
if (!$post instanceof \WP_Post || !function_exists('aksrt_get_pay_meta')) {
return '';
}
$meta = aksrt_get_pay_meta($post);
if (($meta['pay_type'] ?? 'no') === 'no') {
return '';
}
return aksrt_get_paywall_html($post, [
'layout' => 'widget',
'theme' => (string) $instance['theme'],
]);
}
}