esc_html__('横向滚动的文章卡片/迷你列表(共享 swiper-scroll)。', 'aksrt'), 'classname' => 'widget_aksrt_oneline_posts', ] ); } /** * @param array $instance * @return array */ public static function build_query_args(array $instance): array { $count = max(1, min(20, (int) ($instance['count'] ?? 8))); $orderby = sanitize_key((string) ($instance['orderby'] ?? 'date')); $cat = (int) ($instance['cat'] ?? 0); $limit_day = max(0, (int) ($instance['limit_day'] ?? 0)); $query = [ 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => $count, 'ignore_sticky_posts' => true, 'no_found_rows' => true, ]; if ($cat > 0) { $query['cat'] = $cat; } if ($limit_day > 0) { $query['date_query'] = [['after' => $limit_day . ' days ago']]; } switch ($orderby) { case 'views': $query['meta_key'] = 'aksrt_post_views'; $query['orderby'] = 'meta_value_num'; $query['order'] = 'DESC'; break; case 'comments': $query['orderby'] = 'comment_count'; $query['order'] = 'DESC'; break; case 'rand': $query['orderby'] = 'rand'; break; default: $query['orderby'] = 'date'; $query['order'] = 'DESC'; } $topics_raw = trim((string) ($instance['topics'] ?? '')); if ($topics_raw !== '' && taxonomy_exists('topics')) { $topic_ids = array_values(array_filter(array_map('intval', preg_split('/[,,\s]+/', $topics_raw) ?: []))); if ($topic_ids) { $query['tax_query'] = [[ 'taxonomy' => 'topics', 'field' => 'term_id', 'terms' => $topic_ids, ]]; } } $pay_type = sanitize_key((string) ($instance['pay_type'] ?? '')); if ($pay_type !== '' && $pay_type !== '0' && $pay_type !== 'no') { if ($pay_type === 'any' || $pay_type === 'paid') { $query['meta_query'] = [ 'relation' => 'OR', ['key' => '_aksrt_pay_price', 'value' => 0, 'compare' => '>', 'type' => 'NUMERIC'], ['key' => '_aksrt_pay_type', 'value' => ['reading', 'download', 'gallery', 'video', 'vip'], 'compare' => 'IN'], ['key' => '_aksrt_visibility', 'value' => ['pay', 'credits'], 'compare' => 'IN'], ]; } else { $query['meta_query'] = [['key' => '_aksrt_pay_type', 'value' => $pay_type]]; } } return $query; } /** * @param array $instance */ public static function render_html(array $instance): string { $style = sanitize_key((string) ($instance['style'] ?? 'card')); if (!in_array($style, ['card', 'mini', 'list'], true)) { $style = 'card'; } $q = new \WP_Query(self::build_query_args($instance)); if (!$q->have_posts()) { wp_reset_postdata(); return function_exists('aksrt_get_ajax_null') ? aksrt_get_ajax_null(__('暂无文章', 'aksrt')) : '

' . esc_html__('暂无文章', 'aksrt') . '

'; } $mode = $style === 'mini' ? 'mini' : ($style === 'list' ? 'list' : 'card'); $cfg = [ 'mode' => $mode, 'context' => 'widget', 'show_thumb' => true, 'show_meta' => $style !== 'mini', 'show_excerpt' => false, 'show_author' => false, 'show_comments' => false, 'show_likes' => false, 'show_tags' => false, 'show_badges' => $style === 'card', 'skip_wrapper' => true, 'extra_class' => 'posts-item', ]; $slides = ''; if (function_exists('aksrt_render_post')) { while ($q->have_posts()) { $q->the_post(); $item = aksrt_render_post($cfg); if ($item === '') { continue; } // Ensure freeMode width calc has a posts-item root when renderer wraps differently. if (strpos($item, 'posts-item') === false && strpos($item, 'posts-mini') === false) { $item = '
' . $item . '
'; } $slides .= '
' . $item . '
'; } wp_reset_postdata(); } else { // Fallback: bare thumb+title (should rarely hit). while ($q->have_posts()) { $q->the_post(); $thumb = get_the_post_thumbnail_url(get_the_ID(), $style === 'card' ? 'medium' : 'thumbnail'); $slides .= ''; } wp_reset_postdata(); } if ($slides === '') { return function_exists('aksrt_get_ajax_null') ? aksrt_get_ajax_null(__('暂无文章', 'aksrt')) : '

' . esc_html__('暂无文章', 'aksrt') . '

'; } // zibll: .widget-main-post.style-{card|mini|list} + .swiper-container.swiper-scroll data-slideClass="posts-item". ob_start(); echo '
'; echo '
'; echo '
'; echo $slides; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo '
'; echo '
'; echo '
'; echo '
'; return (string) ob_get_clean(); } /** * @param array $args * @param array $instance */ 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 } $rest = rest_url('aksrt/v1/widgets/oneline-posts'); echo '
'; echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo '
'; echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /** * @param array $instance */ public function form($instance): void { ?>

$new_instance * @param array $old_instance * @return array */ public function update($new_instance, $old_instance): array { $style = sanitize_key((string) ($new_instance['style'] ?? 'card')); if (!in_array($style, ['card', 'mini', 'list'], true)) { $style = 'card'; } $orderby = sanitize_key((string) ($new_instance['orderby'] ?? 'date')); if (!in_array($orderby, ['date', 'views', 'comments', 'rand'], true)) { $orderby = 'date'; } return [ 'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')), 'count' => max(1, min(20, (int) ($new_instance['count'] ?? 8))), 'style' => $style, 'orderby' => $orderby, 'cat' => max(0, (int) ($new_instance['cat'] ?? 0)), 'limit_day' => max(0, (int) ($new_instance['limit_day'] ?? 0)), 'topics' => sanitize_text_field((string) ($new_instance['topics'] ?? '')), 'pay_type' => sanitize_key((string) ($new_instance['pay_type'] ?? '')), ]; } }