Files
wordpress-theme-aksrt/src/WidgetSystem/PostQuery.php
T
kate522 b639d57c5b feat(widgets): replicate 12 reference widgets for full Zibll parity
- rewrite schemas for posts, tab_posts, oneline_posts, hot_posts,
  term_cards, term_aggregation, media_cards, combo_info, partners,
  recent_comments, posts_toc and slider_media_combo to mirror the
  modern reference modules field by field (options, defaults,
  dependencies, min/max/step, subheadings)
- port reference renderers: hot ranking with TOP badges, category
  aggregation card, graphic cover cards, partners CSS marquee,
  combo info cards with auto band palette, recent comment list
- rename 分类与标签聚合卡片 to 分类聚合卡片 and restrict it to
  categories (no tags/topics branch in this project)
- drop topics filter fields and the topics query branch; align
  orderby value domain with supported post meta
- add searchable sortable terms admin field type and spinner unit
  suffix rendering
- unify widget pagination and lazy loading on the REST client with
  the runtime-owned ias reveal trigger; remove the legacy
  post_ajax_trigger dual path
- align section-navs TOC with the reference contract (wznav ids,
  3-heading minimum, zib-widget box)
2026-09-05 11:28:00 +08:00

119 lines
5.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace AKSRT\WidgetSystem;
defined('ABSPATH') || exit;
/**
* Builds post queries exclusively from normalized, saved widget settings.
*/
final class PostQuery {
public static function build(array $instance, int $page = 1, array $segment = []): \WP_Query {
return new \WP_Query(self::arguments($instance, $page, $segment));
}
/** @return array<string,mixed> */
public static function arguments(array $instance, int $page = 1, array $segment = []): array {
$config = array_merge($instance, $segment);
$count = max(1, min(50, (int) ($config['count'] ?? 8)));
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $count,
'paged' => max(1, $page),
'ignore_sticky_posts' => true,
'no_found_rows' => false,
];
if (array_key_exists('paginate', $config) && in_array((string) $config['paginate'], ['', 'none'], true)) {
$args['no_found_rows'] = true;
$args['paged'] = 1;
}
// The modern post widgets use Zibll's signed `cat` syntax. Legacy
// category_ids remains available only to the unrelated generic posts
// widgets; a tab segment must never fall back to it.
$is_reference_segment = array_key_exists('cat', $segment);
$cat_raw = array_key_exists('cat', $config) ? (string) $config['cat'] : ($is_reference_segment ? '' : (string) ($config['category_ids'] ?? ''));
$categories = self::signed_ids($cat_raw);
if (!empty($config['category_id'])) $categories = [(int) $config['category_id']];
$cat_in = array_values(array_filter($categories, static fn(int $id): bool => $id > 0));
$cat_not = array_values(array_map('abs', array_filter($categories, static fn(int $id): bool => $id < 0)));
if ($cat_in) $args['category__in'] = $cat_in;
if ($cat_not) $args['category__not_in'] = $cat_not;
$tags = $is_reference_segment ? [] : RendererHelpers::ids((string) ($config['tag_ids'] ?? ''));
if ($tags) {
$args['tag__in'] = $tags;
}
$days = max(0, (int) ($config['limit_day'] ?? 0));
if ($days > 0) {
$now = function_exists('current_time') ? current_time('timestamp') : time();
$format = static function (int $timestamp): string {
return function_exists('wp_date') ? wp_date('Y-m-d H:i:s', $timestamp) : date('Y-m-d H:i:s', $timestamp);
};
$args['date_query'] = [['after' => $format($now - ($days * DAY_IN_SECONDS)), 'before' => $format($now), 'inclusive' => true]];
}
self::apply_order($args, (string) ($config['orderby'] ?? 'date'), (string) ($config['order'] ?? 'DESC'));
$pay = $is_reference_segment ? ($config['zibpay_type'] ?? []) : ($config['pay_type'] ?? '');
self::apply_pay_type($args, $pay);
return $args;
}
/** @return list<int> */
private static function signed_ids(string $raw): array {
$tokens = preg_split('/[,,\s]+/', trim($raw), -1, PREG_SPLIT_NO_EMPTY) ?: [];
$out = [];
foreach ($tokens as $token) if (preg_match('/^-?\d+$/', (string) $token)) $out[] = (int) $token;
return array_values(array_unique(array_filter($out, static fn(int $id): bool => $id !== 0)));
}
public static function segment(array $instance, string $segment): array {
$tabs = array_values((array) ($instance['tabs'] ?? []));
if (!$tabs) {
return [];
}
$index = ctype_digit($segment) ? (int) $segment : 0;
return isset($tabs[$index]) && is_array($tabs[$index]) ? $tabs[$index] : $tabs[0];
}
private static function apply_order(array &$args, string $orderby, string $order): void {
$args['order'] = strtoupper($order) === 'ASC' ? 'ASC' : 'DESC';
$meta = [
'views' => class_exists('AKSRT\\Core\\DomainModel') ? \AKSRT\Core\DomainModel::POST_VIEW_COUNT_KEY : 'aksrt_post_views',
'like' => '_aksrt_like_count',
'favorite' => '_aksrt_favorite_count',
];
if (isset($meta[$orderby])) {
$args['meta_key'] = $meta[$orderby];
$args['orderby'] = 'meta_value_num';
return;
}
if ($orderby === 'comment_count') {
$args['orderby'] = 'comment_count';
} elseif (in_array($orderby, ['date', 'modified', 'rand'], true)) {
$args['orderby'] = $orderby;
} else {
$args['orderby'] = 'date';
}
}
private static function apply_pay_type(array &$args, $pay_type): void {
$values = is_array($pay_type) ? array_map('strval', $pay_type) : array_filter(array_map('trim', preg_split('/[,,\s]+/', (string) $pay_type) ?: []));
$map = ['1' => 'reading', '2' => 'download', '5' => 'gallery', '6' => 'video', 'reading' => 'reading', 'download' => 'download', 'gallery' => 'gallery', 'video' => 'video', 'vip' => 'vip'];
$values = array_values(array_unique(array_filter(array_map(static fn($value): string => $map[(string) $value] ?? '', $values))));
if (!$values) return;
$type_key = class_exists('AKSRT\\Core\\DomainModel') ? \AKSRT\Core\DomainModel::PAY_TYPE_KEY : '_aksrt_pay_type';
$meta_key = class_exists('AKSRT\\Core\\DomainModel') ? \AKSRT\Core\DomainModel::PAY_META_KEY : '_aksrt_pay';
$pay_query = [
'relation' => 'OR',
['key' => $type_key, 'value' => $values, 'compare' => 'IN'],
];
// Some AKSRT installations only have the structured pay array. LIKE
// accepts one scalar per clause, so emit a clause for each allowed type.
foreach ($values as $value) {
$pay_query[] = ['key' => $meta_key, 'value' => '"' . $value . '"', 'compare' => 'LIKE'];
}
$args['meta_query'] = [$pay_query];
}
}