feat(widgets): main/paid/tab posts shared renderer depth

Main and paid lists prefer aksrt_render_post mini/card/list contracts;
tab posts show admin empty guidance when no enabled tabs.
This commit is contained in:
2026-07-21 10:20:07 +08:00
parent 842ff488ce
commit 9e9f3d00e9
3 changed files with 64 additions and 19 deletions
+34 -17
View File
@@ -68,42 +68,59 @@ class WidgetPaidPosts extends \WP_Widget {
if (!$q->have_posts()) {
return '';
}
$placeholder = get_template_directory_uri() . '/assets/img/placeholder.svg';
ob_start();
while ($q->have_posts()) {
$q->the_post();
$price = (float) get_post_meta(get_the_ID(), '_aksrt_pay_price', true);
$credits = (int) get_post_meta(get_the_ID(), '_aksrt_credits_cost', true);
$thumb = get_the_post_thumbnail_url(get_the_ID(), 'thumbnail');
$pid = get_the_ID();
$price = (float) get_post_meta($pid, '_aksrt_pay_price', true);
$credits = (int) get_post_meta($pid, '_aksrt_credits_cost', true);
$thumb = get_the_post_thumbnail_url($pid, 'thumbnail');
if ($price > 0) {
$badge = '¥' . number_format($price, 2, '.', '');
} elseif ($credits > 0) {
$badge = sprintf(__('%d 积分', 'aksrt'), $credits);
} else {
$badge = __('付费', 'aksrt');
}
if (function_exists('aksrt_render_post')) {
$item = aksrt_render_post([
'mode' => 'mini',
'context' => 'widget',
'show_thumb' => true,
'show_meta' => true,
'show_excerpt' => false,
'extra_class' => 'ajax-item aksrt-paid-posts__mini',
]);
if ($item !== '') {
echo '<div class="aksrt-paid-posts__item is-mini ajax-item">';
echo $item; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<span class="aksrt-paid-posts__price badge">' . esc_html($badge) . '</span>';
echo '</div>';
continue;
}
}
echo '<li class="aksrt-paid-posts__item ajax-item">';
if ($thumb) {
echo '<a class="aksrt-paid-posts__thumb" href="' . esc_url(get_permalink()) . '"><img src="' . esc_url($thumb) . '" alt="" loading="lazy" /></a>';
echo '<a class="aksrt-paid-posts__thumb" href="' . esc_url(get_permalink()) . '"><img class="aksrt-lazy lazyload" data-src="' . esc_url($thumb) . '" src="' . esc_url($placeholder) . '" alt="" loading="lazy" /></a>';
}
echo '<div class="aksrt-paid-posts__body">';
echo '<a class="aksrt-paid-posts__title" href="' . esc_url(get_permalink()) . '">' . esc_html(get_the_title()) . '</a>';
echo '<div class="aksrt-paid-posts__meta">';
if ($price > 0) {
echo '<span class="aksrt-paid-posts__price">¥' . esc_html(number_format($price, 2, '.', '')) . '</span>';
} elseif ($credits > 0) {
echo '<span class="aksrt-paid-posts__price">' . esc_html(sprintf(__('%d 积分', 'aksrt'), $credits)) . '</span>';
} else {
echo '<span class="aksrt-paid-posts__price">' . esc_html__('付费', 'aksrt') . '</span>';
}
echo '</div></div></li>';
echo '<div class="aksrt-paid-posts__meta"><span class="aksrt-paid-posts__price">' . esc_html($badge) . '</span></div>';
echo '</div></li>';
}
wp_reset_postdata();
return (string) ob_get_clean();
}
/**
* @return array{html:string,paginate_html:string,has_more:bool,page:int,max_pages:int,total:int}
*/
public static function query_payload(array $instance, int $page = 1): array {
$page = max(1, $page);
$paginate = sanitize_key((string) ($instance['paginate'] ?? 'ajax'));
$count = max(1, min(20, (int) ($instance['count'] ?? 6)));
$q = new \WP_Query(self::build_query_args($instance, $page));
$items = self::render_items_html($q);
$html = $items !== '' ? '<ul class="aksrt-paid-posts">' . $items . '</ul>' : '';
$wrap_tag = (strpos($items, 'posts-mini') !== false || strpos($items, 'aksrt-paid-posts__mini') !== false) ? 'div' : 'ul';
$html = $items !== '' ? '<' . $wrap_tag . ' class="aksrt-paid-posts">' . $items . '</' . $wrap_tag . '>' : '';
if ($html === '' && $page === 1) {
$html = function_exists('aksrt_get_ajax_null')
? aksrt_get_ajax_null(__('暂无付费内容', 'aksrt'))