esc_html__('以卡片展示分类或标签。', 'aksrt'), 'classname' => 'widget_aksrt_term_cards', ] ); } /** @return array */ public static function parse_ids_static(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 static function render_html(array $instance): string { $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 = self::parse_ids_static((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 function_exists('aksrt_get_ajax_null') ? aksrt_get_ajax_null(__('暂无分类', 'aksrt')) : '

' . esc_html__('暂无分类', 'aksrt') . '

'; } $cols = max(1, min(4, (int) ($instance['columns'] ?? 2))); ob_start(); echo '
'; 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 ''; if ($thumb !== '') { echo '
'; } else { echo '
'; } echo '
'; echo '
' . esc_html($term->name) . '
'; echo '
' . esc_html(sprintf(_n('%d 篇', '%d 篇', (int) $term->count, 'aksrt'), (int) $term->count)) . '
'; echo '
'; } echo '
'; return (string) ob_get_clean(); } public function widget($args, $instance): void { $html = self::render_html($instance); if ($html === '') { 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 $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /** @return array */ private function parse_ids(string $raw): array { return self::parse_ids_static($raw); } public function form($instance): void { $taxs = get_taxonomies(['public' => true], 'objects'); ?>

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, ]; } }