153 lines
4.8 KiB
PHP
153 lines
4.8 KiB
PHP
<?php
|
|
/**
|
|
* Shared user-card / author-card render helpers (zibll zib_get_user_card_box 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);
|
|
$show_empty = !empty($args['show_empty']);
|
|
|
|
if (!$posts) {
|
|
if (!$show_empty) {
|
|
return '';
|
|
}
|
|
return '<div class="user-card-posts mt10 muted-2-color em09 text-center">' .
|
|
esc_html__('暂无更多文章', 'aksrt') .
|
|
'</div>';
|
|
}
|
|
|
|
ob_start();
|
|
echo '<div class="user-card-posts mt10">';
|
|
if ($style === 'card') {
|
|
echo '<div class="swiper-container more-posts swiper-scroll"><div class="swiper-wrapper">';
|
|
foreach ($posts as $rp) {
|
|
$thumb = get_the_post_thumbnail_url($rp, 'medium');
|
|
$title = get_the_title($rp);
|
|
$time = function_exists('aksrt_time_ago')
|
|
? aksrt_time_ago((string) $rp->post_date)
|
|
: human_time_diff((int) get_the_time('U', $rp), (int) current_time('timestamp')) . __('前', 'aksrt');
|
|
$views = function_exists('aksrt_get_post_views') ? (int) aksrt_get_post_views($rp->ID) : 0;
|
|
echo '<div class="swiper-slide mr10">';
|
|
echo '<a class="aksrt-more-post-card" href="' . esc_url(get_permalink($rp)) . '">';
|
|
if ($thumb) {
|
|
echo '<span class="aksrt-more-post-card__thumb"><img' . aksrt_get_lazy_attr('lazy_posts_thumb', (string) $thumb, 'fit-cover') . ' alt="" decoding="async"></span>';
|
|
}
|
|
echo '<span class="aksrt-more-post-card__body">';
|
|
echo '<span class="aksrt-more-post-card__title text-ellipsis">' . esc_html($title) . '</span>';
|
|
echo '<span class="aksrt-more-post-card__meta muted-2-color em09">';
|
|
echo '<item>' . esc_html($time) . '</item>';
|
|
if ($views > 0) {
|
|
echo '<item class="pull-right"><i class="fa fa-eye" aria-hidden="true"></i> ' . esc_html((string) $views) . '</item>';
|
|
}
|
|
echo '</span></span></a></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>';
|
|
}
|
|
echo '</div>';
|
|
return (string) ob_get_clean();
|
|
}
|
|
endif;
|