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:
@@ -127,6 +127,11 @@ class TabPostsWidget extends \WP_Widget {
|
||||
$tabs = $this->normalize_tabs($instance['tabs'] ?? 'latest,comments,random');
|
||||
$tabs = array_values(array_filter($tabs, static fn($t) => !empty($t['enabled'])));
|
||||
if (!$tabs) {
|
||||
if (current_user_can('manage_options')) {
|
||||
echo function_exists('aksrt_get_widget_null')
|
||||
? aksrt_get_widget_null(__('请至少启用一个 Tab', 'aksrt')) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
: '<p class="widget-empty">' . esc_html__('请至少启用一个 Tab', 'aksrt') . '</p>';
|
||||
}
|
||||
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -90,6 +90,29 @@ class WidgetMainPosts extends \WP_Widget {
|
||||
|
||||
ob_start();
|
||||
// Outer wrapper only on page 1 full render handled by caller for ajax append items.
|
||||
$mode = $style === 'card' ? 'card' : ($style === 'mini' ? 'mini' : 'list');
|
||||
if (function_exists('aksrt_render_post')) {
|
||||
$html = '';
|
||||
$i = $start;
|
||||
while ($q->have_posts()) {
|
||||
$q->the_post();
|
||||
$i++;
|
||||
$html .= aksrt_render_post([
|
||||
'mode' => $mode,
|
||||
'context' => 'widget',
|
||||
'show_thumb' => $show_thumb,
|
||||
'show_meta' => $show_meta,
|
||||
'show_excerpt' => $mode === 'list',
|
||||
'show_number' => $show_number && $mode === 'mini',
|
||||
'number_offset' => $show_number ? $i : 0,
|
||||
'extra_class' => 'ajax-item',
|
||||
]);
|
||||
}
|
||||
wp_reset_postdata();
|
||||
return $html;
|
||||
}
|
||||
|
||||
$placeholder = get_template_directory_uri() . '/assets/img/placeholder.svg';
|
||||
$i = $start;
|
||||
while ($q->have_posts()) {
|
||||
$q->the_post();
|
||||
@@ -101,7 +124,7 @@ class WidgetMainPosts extends \WP_Widget {
|
||||
echo '<a class="aksrt-main-posts__card ajax-item" href="' . esc_url(get_permalink()) . '">';
|
||||
echo '<div class="aksrt-main-posts__thumb' . ($thumb ? '' : ' is-empty') . '">';
|
||||
if ($thumb) {
|
||||
echo '<img src="' . esc_url($thumb) . '" alt="" loading="lazy" />';
|
||||
echo '<img class="aksrt-lazy lazyload" data-src="' . esc_url($thumb) . '" src="' . esc_url($placeholder) . '" alt="" loading="lazy" />';
|
||||
}
|
||||
echo '</div>';
|
||||
echo '<div class="aksrt-main-posts__body">';
|
||||
@@ -122,7 +145,7 @@ class WidgetMainPosts extends \WP_Widget {
|
||||
if ($show_thumb) {
|
||||
echo '<span class="aksrt-main-posts__thumb mini' . ($thumb ? '' : ' is-empty') . '">';
|
||||
if ($thumb) {
|
||||
echo '<img src="' . esc_url($thumb) . '" alt="" loading="lazy" />';
|
||||
echo '<img class="aksrt-lazy lazyload" data-src="' . esc_url($thumb) . '" src="' . esc_url($placeholder) . '" alt="" loading="lazy" />';
|
||||
}
|
||||
echo '</span>';
|
||||
}
|
||||
|
||||
@@ -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'))
|
||||
|
||||
Reference in New Issue
Block a user