esc_html__('以列表形式展示文章分类', 'aksrt'), 'customize_selective_refresh' => true, 'classname' => 'widget_aksrt_categories', ] ); } /** @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)); } /** * @param array $instance */ public static function render_html(array $instance): string { $count = max(1, min(50, (int) ($instance['count'] ?? 8))); $orderby = sanitize_key((string) ($instance['orderby'] ?? 'count')); if (!in_array($orderby, ['count', 'name', 'id', 'slug'], true)) { $orderby = 'count'; } $order = strtoupper((string) ($instance['order'] ?? 'DESC')) === 'ASC' ? 'ASC' : 'DESC'; $hide_empty = !isset($instance['hide_empty']) || !empty($instance['hide_empty']); $hierarchical = !empty($instance['hierarchical']); $parent = max(0, (int) ($instance['parent'] ?? 0)); $show_count = !isset($instance['show_count']) || !empty($instance['show_count']); $show_desc = !empty($instance['show_desc']); $show_thumb = !empty($instance['show_thumb']); $exclude = self::parse_ids_static((string) ($instance['exclude'] ?? '')); $include = self::parse_ids_static((string) ($instance['include'] ?? '')); $term_args = [ 'taxonomy' => 'category', 'number' => $hierarchical ? 0 : $count, 'orderby' => $orderby, 'order' => $order, 'hide_empty' => $hide_empty, 'hierarchical' => $hierarchical, ]; if ($parent > 0) { $term_args['parent'] = $parent; } elseif ($hierarchical) { $term_args['parent'] = 0; } if ($include) { $term_args['include'] = $include; $term_args['orderby'] = 'include'; unset($term_args['number']); } if ($exclude) { $term_args['exclude'] = $exclude; } $cats = get_categories($term_args); if (!$cats) { return function_exists('aksrt_get_ajax_null') ? aksrt_get_ajax_null(__('暂无分类', 'aksrt')) : '

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

'; } ob_start(); $cls = 'widget-list aksrt-cat-list ajax-item' . ($hierarchical ? ' is-tree' : '') . ($show_thumb ? ' has-thumb' : ''); echo ''; return (string) ob_get_clean(); } /** * @param \WP_Term $cat */ private static function render_cat_item($cat, bool $show_count, bool $show_desc, bool $show_thumb, int $depth, int $limit, int &$n, bool $hide_empty, string $orderby, string $order): string { if ($n > 0 && $limit > 0 && $n >= $limit && $depth >= 0) { return ''; } $html = '
  • '; $html .= ''; if ($show_thumb) { $thumb = ''; $img_id = (int) get_term_meta($cat->term_id, 'thumbnail_id', true); if (!$img_id) { $img_id = (int) get_term_meta($cat->term_id, '_aksrt_cover', true); } if ($img_id) { $thumb = (string) (wp_get_attachment_image_url($img_id, 'thumbnail') ?: ''); } if ($thumb === '') { $thumb = (string) get_term_meta($cat->term_id, '_aksrt_cover_url', true); } if ($thumb !== '') { $ph = get_template_directory_uri() . '/assets/img/placeholder.svg'; $html .= ''; } else { $html .= ''; } } $html .= ''; $html .= '' . esc_html($cat->name) . ''; if ($show_desc && $cat->description !== '') { $desc = wp_trim_words(wp_strip_all_tags((string) $cat->description), 12, '…'); $html .= '' . esc_html($desc) . ''; } $html .= ''; if ($show_count) { $html .= '' . (int) $cat->count . ''; } $html .= ''; if ($depth >= 0) { $children = get_categories([ 'taxonomy' => 'category', 'parent' => (int) $cat->term_id, 'hide_empty' => $hide_empty, 'orderby' => $orderby, 'order' => $order, ]); if ($children) { $html .= '
      '; foreach ($children as $child) { $n++; if ($limit > 0 && $n > $limit) { break; } $html .= self::render_cat_item($child, $show_count, $show_desc, $show_thumb, $depth + 1, $limit, $n, $hide_empty, $orderby, $order); } $html .= '
    '; } } $html .= '
  • '; return $html; } /** * @param array $args * @param array $instance */ public function widget($args, $instance): void { 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 } $affix = function_exists('aksrt_widget_affix_attrs') ? aksrt_widget_affix_attrs($instance) : ['class' => '', 'attr' => '']; if ($affix['class'] !== '') { echo '
    '; } echo self::render_html($instance); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped if ($affix['class'] !== '') { echo '
    '; } echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /** * @param array $instance */ public function form($instance): void { ?>





    render_common_form_section($instance); ?> $new_instance * @param array $old_instance * @return array */ public function update($new_instance, $old_instance): array { $orderby = sanitize_key((string) ($new_instance['orderby'] ?? 'count')); if (!in_array($orderby, ['count', 'name', 'id', 'slug'], true)) { $orderby = 'count'; } return $this->update_common_fields($new_instance, $old_instance, [ 'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')), 'count' => max(1, min(50, (int) ($new_instance['count'] ?? 8))), 'orderby' => $orderby, 'order' => strtoupper((string) ($new_instance['order'] ?? 'DESC')) === 'ASC' ? 'ASC' : 'DESC', 'parent' => max(0, (int) ($new_instance['parent'] ?? 0)), 'include' => sanitize_text_field((string) ($new_instance['include'] ?? '')), 'exclude' => sanitize_text_field((string) ($new_instance['exclude'] ?? '')), 'show_count' => !empty($new_instance['show_count']) ? 1 : 0, 'show_desc' => !empty($new_instance['show_desc']) ? 1 : 0, 'show_thumb' => !empty($new_instance['show_thumb']) ? 1 : 0, 'hide_empty' => !empty($new_instance['hide_empty']) ? 1 : 0, 'hierarchical' => !empty($new_instance['hierarchical']) ? 1 : 0, ]); } }