feat(widgets): paid posts AJAX + hot posts thumb persist
Wire /widgets/paid-posts load-more; fix hot posts show_thumb not saved in update().
This commit is contained in:
@@ -34,6 +34,11 @@ class WidgetAjax {
|
||||
'callback' => [$this, 'rest_tab_posts'],
|
||||
'permission_callback' => '__return_true',
|
||||
]);
|
||||
register_rest_route('aksrt/v1', '/widgets/paid-posts', [
|
||||
'methods' => 'GET',
|
||||
'callback' => [$this, 'rest_paid_posts'],
|
||||
'permission_callback' => '__return_true',
|
||||
]);
|
||||
}
|
||||
|
||||
public function rest_main_posts(\WP_REST_Request $request) {
|
||||
@@ -89,4 +94,26 @@ class WidgetAjax {
|
||||
'html' => $html,
|
||||
], 200);
|
||||
}
|
||||
|
||||
public function rest_paid_posts(\WP_REST_Request $request) {
|
||||
if (!class_exists('\AKSRT\Widgets\WidgetPaidPosts')) {
|
||||
return new \WP_REST_Response(['success' => false, 'message' => 'unavailable'], 500);
|
||||
}
|
||||
$instance = [
|
||||
'count' => max(1, min(20, (int) ($request->get_param('count') ?? 6))),
|
||||
'orderby' => sanitize_key((string) ($request->get_param('orderby') ?? 'date')),
|
||||
'paginate' => sanitize_key((string) ($request->get_param('paginate') ?? 'ajax')),
|
||||
];
|
||||
$page = max(1, (int) ($request->get_param('page') ?? $request->get_param('paged') ?? 1));
|
||||
$payload = \AKSRT\Widgets\WidgetPaidPosts::query_payload($instance, $page);
|
||||
return new \WP_REST_Response([
|
||||
'success' => true,
|
||||
'html' => (string) ($payload['html'] ?? ''),
|
||||
'paginate_html' => (string) ($payload['paginate_html'] ?? ''),
|
||||
'has_more' => !empty($payload['has_more']),
|
||||
'page' => (int) ($payload['page'] ?? $page),
|
||||
'max_pages' => (int) ($payload['max_pages'] ?? 1),
|
||||
'total' => (int) ($payload['total'] ?? 0),
|
||||
], 200);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,10 +149,11 @@ class HotPostsWidget extends \WP_Widget {
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
return [
|
||||
'title' => sanitize_text_field($new_instance['title'] ?? ''),
|
||||
'count' => absint($new_instance['count'] ?? 5),
|
||||
'order_by' => in_array($new_instance['order_by'] ?? '', ['views', 'comments', 'likes', 'favorites', 'downloads'], true) ? $new_instance['order_by'] : 'views',
|
||||
'days' => absint($new_instance['days'] ?? 30),
|
||||
'title' => sanitize_text_field($new_instance['title'] ?? ''),
|
||||
'count' => absint($new_instance['count'] ?? 5),
|
||||
'order_by' => in_array($new_instance['order_by'] ?? '', ['views', 'comments', 'likes', 'favorites', 'downloads'], true) ? $new_instance['order_by'] : 'views',
|
||||
'days' => absint($new_instance['days'] ?? 30),
|
||||
'show_thumb' => !empty($new_instance['show_thumb']) ? 1 : 0,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
/**
|
||||
* Paid posts list (zibll zib_widget_ui_posts_pay lite).
|
||||
* Paid posts list (zibll zib_widget_ui_posts_pay + ajax).
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
@@ -16,20 +16,20 @@ class WidgetPaidPosts extends \WP_Widget {
|
||||
'aksrt_paid_posts',
|
||||
esc_html__('AKSRT 付费内容列表', 'aksrt'),
|
||||
[
|
||||
'description' => esc_html__('展示开启付费/积分可见的文章。', 'aksrt'),
|
||||
'description' => esc_html__('展示开启付费/积分可见的文章(支持 AJAX)。', 'aksrt'),
|
||||
'classname' => 'widget_aksrt_paid_posts',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
public static function build_query_args(array $instance, int $paged = 1): array {
|
||||
$count = max(1, min(20, (int) ($instance['count'] ?? 6)));
|
||||
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'date'));
|
||||
|
||||
$query = [
|
||||
'post_type' => 'post',
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => $count,
|
||||
'paged' => max(1, $paged),
|
||||
'order' => 'DESC',
|
||||
'meta_query' => [
|
||||
'relation' => 'OR',
|
||||
@@ -53,7 +53,7 @@ class WidgetPaidPosts extends \WP_Widget {
|
||||
],
|
||||
];
|
||||
if ($orderby === 'views') {
|
||||
$query['meta_key'] = '_aksrt_views';
|
||||
$query['meta_key'] = 'aksrt_post_views';
|
||||
$query['orderby'] = 'meta_value_num';
|
||||
} elseif ($orderby === 'price') {
|
||||
$query['meta_key'] = '_aksrt_pay_price';
|
||||
@@ -61,26 +61,20 @@ class WidgetPaidPosts extends \WP_Widget {
|
||||
} else {
|
||||
$query['orderby'] = 'date';
|
||||
}
|
||||
return $query;
|
||||
}
|
||||
|
||||
$q = new \WP_Query($query);
|
||||
public static function render_items_html(\WP_Query $q): string {
|
||||
if (!$q->have_posts()) {
|
||||
wp_reset_postdata();
|
||||
return;
|
||||
return '';
|
||||
}
|
||||
|
||||
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
|
||||
if ($title !== '') {
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
echo '<ul class="aksrt-paid-posts">';
|
||||
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');
|
||||
echo '<li class="aksrt-paid-posts__item">';
|
||||
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>';
|
||||
}
|
||||
@@ -96,8 +90,72 @@ class WidgetPaidPosts extends \WP_Widget {
|
||||
}
|
||||
echo '</div></div></li>';
|
||||
}
|
||||
echo '</ul>';
|
||||
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>' : '';
|
||||
if ($html === '' && $page === 1) {
|
||||
$html = function_exists('aksrt_get_ajax_null')
|
||||
? aksrt_get_ajax_null(__('暂无付费内容', 'aksrt'))
|
||||
: '<p class="widget-empty ajax-item">' . esc_html__('暂无付费内容', 'aksrt') . '</p>';
|
||||
}
|
||||
$max_pages = max(1, (int) $q->max_num_pages);
|
||||
$total = (int) $q->found_posts;
|
||||
$has_more = $page < $max_pages;
|
||||
$paginate_html = '';
|
||||
if ($paginate === 'ajax' && $has_more) {
|
||||
$next = add_query_arg([
|
||||
'page' => $page + 1,
|
||||
'count' => $count,
|
||||
'orderby' => sanitize_key((string) ($instance['orderby'] ?? 'date')),
|
||||
'paginate' => 'ajax',
|
||||
], rest_url('aksrt/v1/widgets/paid-posts'));
|
||||
$paginate_html = function_exists('aksrt_get_ajax_next_paginate')
|
||||
? aksrt_get_ajax_next_paginate($total, $page, $count, $next)
|
||||
: '<div class="text-center theme-pagination ajax-pag"><div class="next-page ajax-next"><a href="' . esc_url($next) . '">' . esc_html__('加载更多', 'aksrt') . '</a></div></div>';
|
||||
} else {
|
||||
$paginate_html = '<div class="ajax-pag hide"><div class="next-page ajax-next"><a href="#"></a></div></div>';
|
||||
}
|
||||
return [
|
||||
'html' => $html,
|
||||
'paginate_html' => $paginate_html,
|
||||
'has_more' => $has_more,
|
||||
'page' => $page,
|
||||
'max_pages' => $max_pages,
|
||||
'total' => $total,
|
||||
];
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$paginate = sanitize_key((string) ($instance['paginate'] ?? 'ajax'));
|
||||
$instance['paginate'] = $paginate;
|
||||
$payload = self::query_payload($instance, 1);
|
||||
if ($payload['html'] === '' && empty($payload['total'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
|
||||
if ($title !== '') {
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
$rest = rest_url('aksrt/v1/widgets/paid-posts');
|
||||
echo '<div class="aksrt-paid-posts-wrap ajaxpager" data-aksrt-ajaxpager data-aksrt-ajax-url="' . esc_url($rest) . '">';
|
||||
echo $payload['html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
echo $payload['paginate_html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
echo '<div class="post_ajax_loader" data-aksrt-ajax-loader data-aksrt-ajax-loader-hide hidden></div>';
|
||||
echo '</div>';
|
||||
|
||||
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
@@ -123,6 +181,14 @@ class WidgetPaidPosts extends \WP_Widget {
|
||||
</select>
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label><?php esc_html_e('分页', 'aksrt'); ?>
|
||||
<select name="<?php echo esc_attr($this->get_field_name('paginate')); ?>">
|
||||
<option value="ajax" <?php selected(($instance['paginate'] ?? 'ajax'), 'ajax'); ?>><?php esc_html_e('AJAX', 'aksrt'); ?></option>
|
||||
<option value="none" <?php selected(($instance['paginate'] ?? ''), 'none'); ?>><?php esc_html_e('无', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</label>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
@@ -132,9 +198,10 @@ class WidgetPaidPosts extends \WP_Widget {
|
||||
$orderby = 'date';
|
||||
}
|
||||
return [
|
||||
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
|
||||
'count' => max(1, min(20, (int) ($new_instance['count'] ?? 6))),
|
||||
'orderby' => $orderby,
|
||||
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
|
||||
'count' => max(1, min(20, (int) ($new_instance['count'] ?? 6))),
|
||||
'orderby' => $orderby,
|
||||
'paginate' => sanitize_key((string) ($new_instance['paginate'] ?? 'ajax')) === 'none' ? 'none' : 'ajax',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user