feat(topics): restore topic taxonomy workflow

This commit is contained in:
kate522 committed 2026-11-10 22:31:11 +08:00
1 parent f3dd1ec3b1
commit a254cbd3e9
59 files changed
+771 -192

No files matched your search

+32 -16
View File
@@ -40,13 +40,16 @@ endif;
if (!function_exists('jio_widget_term_cover_url')) :
/**
* Category cover image URL, resolved through the theme's canonical chain:
* Term cover image URL, resolved through the theme's canonical chain:
* explicit term cover → latest thumbnailed post inside the term → optional
* default placeholder. The reference hides term cards without a real cover
* while the aggregation card always falls back to a default image, so the
* last step is opt-in via $default_fallback.
* default placeholder. The last step is opt-in via $default_fallback.
*/
function jio_widget_term_cover_url(int $term_id, bool $default_fallback = false): string {
function jio_widget_term_cover_url(int $term_id, bool $default_fallback = false, string $taxonomy = 'category'): string {
$taxonomy = $taxonomy === 'topics' ? 'topics' : 'category';
$cover = class_exists('Jio\\Core\\TermMeta') ? \Jio\Core\TermMeta::get_meta_value($term_id, 'cover_url', '') : '';
if (is_string($cover) && $cover !== '') {
return esc_url_raw($cover);
}
$cover_id = (int) get_term_meta($term_id, 'jio_category_cover_id', true);
if ($cover_id > 0) {
$url = wp_get_attachment_url($cover_id);
@@ -54,21 +57,29 @@ if (!function_exists('jio_widget_term_cover_url')) :
return (string) $url;
}
}
$posts = get_posts([
'category__in' => [$term_id],
$term_query = $taxonomy === 'topics'
? ['tax_query' => [['taxonomy' => 'topics', 'field' => 'term_id', 'terms' => [$term_id]]]]
: ['category__in' => [$term_id]];
$posts = get_posts(array_merge($term_query, [
'posts_per_page' => 1,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => [['key' => '_thumbnail_id', 'compare' => 'EXISTS']],
'fields' => 'ids',
]);
]));
if (!empty($posts)) {
$url = get_the_post_thumbnail_url((int) $posts[0], 'medium_large');
if ($url) {
return (string) $url;
}
}
if ($default_fallback && $taxonomy === 'topics' && class_exists('Jio\\Settings\\Settings')) {
$settings = \Jio\Settings\Settings::get_topics_page_settings();
if (!empty($settings['default_cover'])) {
return esc_url_raw((string) $settings['default_cover']);
}
}
return $default_fallback ? get_template_directory_uri() . '/assets/img/placeholder.svg' : '';
}
endif;
@@ -190,20 +201,22 @@ endif;
if (!function_exists('jio_widget_term_aggregation')) :
/**
* Category aggregation card: cover + description + latest posts list.
* Term aggregation card: cover + description + latest posts list.
*
* @param array<string,mixed> $args {term_id, target_blank, orderby, order, count}.
* @param array<string,mixed> $args {term_id, taxonomy, target_blank, orderby, order, count}.
*/
function jio_widget_term_aggregation(array $args = []): string {
$args = wp_parse_args($args, [
'term_id' => 0,
'taxonomy' => 'category',
'target_blank' => false,
'orderby' => 'modified',
'order' => 'DESC',
'count' => 4,
]);
$term_id = (int) $args['term_id'];
$term = $term_id > 0 ? get_term($term_id, 'category') : null;
$taxonomy = $args['taxonomy'] === 'topics' ? 'topics' : 'category';
$term = $term_id > 0 ? get_term($term_id, $taxonomy) : null;
if (!$term instanceof WP_Term || is_wp_error($term)) {
return '';
}
@@ -214,11 +227,11 @@ if (!function_exists('jio_widget_term_aggregation')) :
}
$href = esc_url((string) $href);
$img = jio_widget_term_cover_url((int) $term->term_id, true);
$img = jio_widget_term_cover_url((int) $term->term_id, true, $taxonomy);
$img_html = jio_graphic_card([
'class' => '',
'img' => $img,
'alt' => __('分类', 'jio') . '-' . $term->name,
'alt' => ($taxonomy === 'topics' ? __('专题', 'jio') : __('分类', 'jio')) . '-' . $term->name,
'link' => ['url' => $href, 'target' => !empty($args['target_blank']) ? '_blank' : ''],
'height_scale' => 0,
'mask_opacity' => 0,
@@ -229,13 +242,16 @@ if (!function_exists('jio_widget_term_aggregation')) :
$name = '<a class="em14 key-color text-ellipsis"' . $target . ' href="' . $href . '">' . esc_html($term->name) . '</a>';
$description = wp_kses_post((string) $term->description);
if ($description === '' && current_user_can('manage_categories')) {
$description = esc_html__('请在WordPress后台-文章-文章分类中添加描述!', 'jio')
. '<a href="' . esc_url(get_edit_term_link($term_id, 'category')) . '">' . esc_html__('立即编辑', 'jio') . '</a>';
$description = $taxonomy === 'topics'
? esc_html__('请在WordPress后台-文章-专题中添加描述!', 'jio')
: esc_html__('请在WordPress后台-文章-文章分类中添加描述!', 'jio');
$description .= '<a href="' . esc_url(get_edit_term_link($term_id, $taxonomy)) . '">' . esc_html__('立即编辑', 'jio') . '</a>';
}
$description = '<div class="text-ellipsis-2 muted-color">' . $description . '</div>';
$query_args = [
'cat' => (string) $term->term_id,
'topics' => $taxonomy === 'topics' ? (string) $term->term_id : '',
'cat' => $taxonomy === 'category' ? (string) $term->term_id : '',
'orderby' => (string) $args['orderby'],
'order' => strtoupper((string) $args['order']) === 'ASC' ? 'ASC' : 'DESC',
'count' => max(1, (int) $args['count']),