Close more zibll produck/posts widget gaps: media-tab feature intro, taxonomy term cards, and paid/credits content list with CSS.
141 lines
4.8 KiB
PHP
141 lines
4.8 KiB
PHP
<?php
|
|
/**
|
|
* Paid posts list (zibll zib_widget_ui_posts_pay lite).
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
namespace AKSRT\Widgets;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class WidgetPaidPosts extends \WP_Widget {
|
|
|
|
public function __construct() {
|
|
parent::__construct(
|
|
'aksrt_paid_posts',
|
|
esc_html__('AKSRT 付费内容列表', 'aksrt'),
|
|
[
|
|
'description' => esc_html__('展示开启付费/积分可见的文章。', 'aksrt'),
|
|
'classname' => 'widget_aksrt_paid_posts',
|
|
]
|
|
);
|
|
}
|
|
|
|
public function widget($args, $instance): void {
|
|
$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,
|
|
'order' => 'DESC',
|
|
'meta_query' => [
|
|
'relation' => 'OR',
|
|
[
|
|
'key' => '_aksrt_pay_price',
|
|
'value' => 0,
|
|
'compare' => '>',
|
|
'type' => 'NUMERIC',
|
|
],
|
|
[
|
|
'key' => '_aksrt_credits_cost',
|
|
'value' => 0,
|
|
'compare' => '>',
|
|
'type' => 'NUMERIC',
|
|
],
|
|
[
|
|
'key' => '_aksrt_visibility',
|
|
'value' => ['pay', 'credits'],
|
|
'compare' => 'IN',
|
|
],
|
|
],
|
|
];
|
|
if ($orderby === 'views') {
|
|
$query['meta_key'] = '_aksrt_views';
|
|
$query['orderby'] = 'meta_value_num';
|
|
} elseif ($orderby === 'price') {
|
|
$query['meta_key'] = '_aksrt_pay_price';
|
|
$query['orderby'] = 'meta_value_num';
|
|
} else {
|
|
$query['orderby'] = 'date';
|
|
}
|
|
|
|
$q = new \WP_Query($query);
|
|
if (!$q->have_posts()) {
|
|
wp_reset_postdata();
|
|
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">';
|
|
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">';
|
|
if ($thumb) {
|
|
echo '<a class="aksrt-paid-posts__thumb" href="' . esc_url(get_permalink()) . '"><img src="' . esc_url($thumb) . '" 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 '</ul>';
|
|
wp_reset_postdata();
|
|
|
|
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
}
|
|
|
|
public function form($instance): void {
|
|
?>
|
|
<p>
|
|
<label><?php esc_html_e('标题', 'aksrt'); ?>
|
|
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr((string) ($instance['title'] ?? __('付费精选', 'aksrt'))); ?>">
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label><?php esc_html_e('数量', 'aksrt'); ?>
|
|
<input class="tiny-text" type="number" min="1" max="20" name="<?php echo esc_attr($this->get_field_name('count')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['count'] ?? 6))); ?>">
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label><?php esc_html_e('排序', 'aksrt'); ?>
|
|
<select name="<?php echo esc_attr($this->get_field_name('orderby')); ?>">
|
|
<option value="date" <?php selected(($instance['orderby'] ?? 'date'), 'date'); ?>><?php esc_html_e('时间', 'aksrt'); ?></option>
|
|
<option value="views" <?php selected(($instance['orderby'] ?? ''), 'views'); ?>><?php esc_html_e('浏览', 'aksrt'); ?></option>
|
|
<option value="price" <?php selected(($instance['orderby'] ?? ''), 'price'); ?>><?php esc_html_e('价格', 'aksrt'); ?></option>
|
|
</select>
|
|
</label>
|
|
</p>
|
|
<?php
|
|
}
|
|
|
|
public function update($new_instance, $old_instance): array {
|
|
$orderby = sanitize_key((string) ($new_instance['orderby'] ?? 'date'));
|
|
if (!in_array($orderby, ['date', 'views', 'price'], true)) {
|
|
$orderby = 'date';
|
|
}
|
|
return [
|
|
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
|
|
'count' => max(1, min(20, (int) ($new_instance['count'] ?? 6))),
|
|
'orderby' => $orderby,
|
|
];
|
|
}
|
|
}
|