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:
1 parent
842ff488ce
commit
9e9f3d00e9
3 files changed
+64
-19
No files matched your search
@@ -127,6 +127,11 @@ class TabPostsWidget extends \WP_Widget {
|
|||||||
$tabs = $this->normalize_tabs($instance['tabs'] ?? 'latest,comments,random');
|
$tabs = $this->normalize_tabs($instance['tabs'] ?? 'latest,comments,random');
|
||||||
$tabs = array_values(array_filter($tabs, static fn($t) => !empty($t['enabled'])));
|
$tabs = array_values(array_filter($tabs, static fn($t) => !empty($t['enabled'])));
|
||||||
if (!$tabs) {
|
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
|
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -90,6 +90,29 @@ class WidgetMainPosts extends \WP_Widget {
|
|||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
// Outer wrapper only on page 1 full render handled by caller for ajax append items.
|
// 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;
|
$i = $start;
|
||||||
while ($q->have_posts()) {
|
while ($q->have_posts()) {
|
||||||
$q->the_post();
|
$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 '<a class="aksrt-main-posts__card ajax-item" href="' . esc_url(get_permalink()) . '">';
|
||||||
echo '<div class="aksrt-main-posts__thumb' . ($thumb ? '' : ' is-empty') . '">';
|
echo '<div class="aksrt-main-posts__thumb' . ($thumb ? '' : ' is-empty') . '">';
|
||||||
if ($thumb) {
|
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>';
|
||||||
echo '<div class="aksrt-main-posts__body">';
|
echo '<div class="aksrt-main-posts__body">';
|
||||||
@@ -122,7 +145,7 @@ class WidgetMainPosts extends \WP_Widget {
|
|||||||
if ($show_thumb) {
|
if ($show_thumb) {
|
||||||
echo '<span class="aksrt-main-posts__thumb mini' . ($thumb ? '' : ' is-empty') . '">';
|
echo '<span class="aksrt-main-posts__thumb mini' . ($thumb ? '' : ' is-empty') . '">';
|
||||||
if ($thumb) {
|
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>';
|
echo '</span>';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,42 +68,59 @@ class WidgetPaidPosts extends \WP_Widget {
|
|||||||
if (!$q->have_posts()) {
|
if (!$q->have_posts()) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
$placeholder = get_template_directory_uri() . '/assets/img/placeholder.svg';
|
||||||
ob_start();
|
ob_start();
|
||||||
while ($q->have_posts()) {
|
while ($q->have_posts()) {
|
||||||
$q->the_post();
|
$q->the_post();
|
||||||
$price = (float) get_post_meta(get_the_ID(), '_aksrt_pay_price', true);
|
$pid = get_the_ID();
|
||||||
$credits = (int) get_post_meta(get_the_ID(), '_aksrt_credits_cost', true);
|
$price = (float) get_post_meta($pid, '_aksrt_pay_price', true);
|
||||||
$thumb = get_the_post_thumbnail_url(get_the_ID(), 'thumbnail');
|
$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">';
|
echo '<li class="aksrt-paid-posts__item ajax-item">';
|
||||||
if ($thumb) {
|
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 '<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 '<a class="aksrt-paid-posts__title" href="' . esc_url(get_permalink()) . '">' . esc_html(get_the_title()) . '</a>';
|
||||||
echo '<div class="aksrt-paid-posts__meta">';
|
echo '<div class="aksrt-paid-posts__meta"><span class="aksrt-paid-posts__price">' . esc_html($badge) . '</span></div>';
|
||||||
if ($price > 0) {
|
echo '</div></li>';
|
||||||
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>';
|
|
||||||
}
|
}
|
||||||
wp_reset_postdata();
|
wp_reset_postdata();
|
||||||
return (string) ob_get_clean();
|
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 {
|
public static function query_payload(array $instance, int $page = 1): array {
|
||||||
$page = max(1, $page);
|
$page = max(1, $page);
|
||||||
$paginate = sanitize_key((string) ($instance['paginate'] ?? 'ajax'));
|
$paginate = sanitize_key((string) ($instance['paginate'] ?? 'ajax'));
|
||||||
$count = max(1, min(20, (int) ($instance['count'] ?? 6)));
|
$count = max(1, min(20, (int) ($instance['count'] ?? 6)));
|
||||||
$q = new \WP_Query(self::build_query_args($instance, $page));
|
$q = new \WP_Query(self::build_query_args($instance, $page));
|
||||||
$items = self::render_items_html($q);
|
$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) {
|
if ($html === '' && $page === 1) {
|
||||||
$html = function_exists('aksrt_get_ajax_null')
|
$html = function_exists('aksrt_get_ajax_null')
|
||||||
? aksrt_get_ajax_null(__('暂无付费内容', 'aksrt'))
|
? aksrt_get_ajax_null(__('暂无付费内容', 'aksrt'))
|
||||||
|
|||||||
Reference in new issue
Block a user