Files
2026-08-30 13:49:54 +08:00

129 lines
3.5 KiB
PHP

<?php
/**
* Shared user-card / author-card render helpers (legacy parity).
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
if (!function_exists('aksrt_user_card_query_posts')) :
/**
* Author posts for card strip.
*
* @param array<string,mixed> $args
* @return list<\WP_Post>
*/
function aksrt_user_card_query_posts(array $args): array {
$user_id = (int) ($args['user_id'] ?? 0);
$limit = max(0, min(20, (int) ($args['limit'] ?? 6)));
if ($user_id <= 0 || $limit <= 0) {
return [];
}
$orderby = sanitize_key((string) ($args['orderby'] ?? 'date'));
$post_type = sanitize_key((string) ($args['post_type'] ?? 'post'));
if (!post_type_exists($post_type)) {
$post_type = 'post';
}
$query_args = [
'post_status' => 'publish',
'post_type' => $post_type,
'author' => $user_id,
'posts_per_page' => $limit,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
];
$exclude = (int) ($args['exclude'] ?? 0);
if ($exclude <= 0) {
$exclude = (int) get_the_ID();
}
if ($exclude > 0) {
$query_args['post__not_in'] = [$exclude];
}
switch ($orderby) {
case 'views':
case 'view':
$query_args['meta_key'] = 'aksrt_post_views';
$query_args['orderby'] = 'meta_value_num';
$query_args['order'] = 'DESC';
break;
case 'like':
case 'likes':
$query_args['meta_key'] = '_aksrt_like_count';
$query_args['orderby'] = 'meta_value_num';
$query_args['order'] = 'DESC';
break;
case 'favorite':
case 'favorite_count':
$query_args['meta_key'] = '_aksrt_favorite_count';
$query_args['orderby'] = 'meta_value_num';
$query_args['order'] = 'DESC';
break;
case 'comment_count':
case 'comments':
$query_args['orderby'] = 'comment_count';
$query_args['order'] = 'DESC';
break;
case 'modified':
$query_args['orderby'] = 'modified';
$query_args['order'] = 'DESC';
break;
case 'rand':
case 'random':
$query_args['orderby'] = 'rand';
break;
default:
$query_args['orderby'] = 'date';
$query_args['order'] = 'DESC';
}
$q = new WP_Query($query_args);
$posts = $q->posts;
wp_reset_postdata();
return is_array($posts) ? array_values(array_filter($posts, static fn($p) => $p instanceof WP_Post)) : [];
}
endif;
if (!function_exists('aksrt_render_user_card_posts')) :
/**
* Render more-posts strip (card swiper-scroll or mini list).
*
* @param array<string,mixed> $args
*/
function aksrt_render_user_card_posts(array $args): string {
$style = sanitize_key((string) ($args['post_style'] ?? $args['style'] ?? 'mini'));
if (!in_array($style, ['card', 'mini'], true)) {
$style = 'mini';
}
$posts = aksrt_user_card_query_posts($args);
if (!$posts) {
return '';
}
ob_start();
if ($style === 'card') {
echo '<div class="swiper-container more-posts swiper-scroll"><div class="swiper-wrapper">';
foreach ($posts as $rp) {
echo '<div class="swiper-slide mr10">' .
aksrt_render_graphic_card_post($rp, ['class' => 'em09', 'cut' => 32]) .
'</div>';
}
echo '</div><div class="swiper-button-prev"></div><div class="swiper-button-next"></div></div>';
} else {
echo '<div class="more-posts-mini">';
foreach ($posts as $rp) {
echo '<div class="item"><a class="icon-circle text-ellipsis" href="' . esc_url(get_permalink($rp)) . '">' .
esc_html(get_the_title($rp)) .
'</a></div>';
}
echo '</div>';
}
return (string) ob_get_clean();
}
endif;