Extend form/query/REST number pager support to category block and forum recent; product tab form exposes number mode already supported by list.
104 lines
4.8 KiB
PHP
104 lines
4.8 KiB
PHP
<?php
|
|
/**
|
|
* Forum recent topics — thin wrapper over hot-topics payload with date order.
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
namespace AKSRT\Widgets;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class WidgetForumRecentTopics extends \WP_Widget {
|
|
|
|
public function __construct() {
|
|
parent::__construct(
|
|
'aksrt_forum_recent_topics',
|
|
__('论坛 - 最新话题', 'aksrt'),
|
|
[
|
|
'description' => __('显示论坛最新话题列表(共享迷你行渲染)。', 'aksrt'),
|
|
'classname' => 'widget_aksrt_forum_recent_topics',
|
|
]
|
|
);
|
|
}
|
|
|
|
public function widget($args, $instance): void {
|
|
if (!post_type_exists('aksrt_forum_topic')) {
|
|
return;
|
|
}
|
|
$title = !empty($instance['title']) ? $instance['title'] : __('最新话题', 'aksrt');
|
|
$instance['orderby'] = 'date';
|
|
$instance['period'] = 'all';
|
|
$instance['style'] = sanitize_key((string) ($instance['style'] ?? 'mini'));
|
|
if (!in_array($instance['style'], ['mini', 'rank'], true)) {
|
|
$instance['style'] = 'mini';
|
|
}
|
|
$instance['paginate'] = sanitize_key((string) ($instance['paginate'] ?? 'ajax'));
|
|
$instance['plate'] = max(0, (int) ($instance['plate'] ?? 0));
|
|
$payload = WidgetForumHotTopics::query_payload($instance, 1);
|
|
|
|
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
$rest = rest_url('aksrt/v1/widgets/forum-topics');
|
|
echo '<div class="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
|
|
}
|
|
|
|
public function form($instance): void {
|
|
$title = !empty($instance['title']) ? $instance['title'] : __('最新话题', 'aksrt');
|
|
$count = !empty($instance['count']) ? (int) $instance['count'] : 5;
|
|
$plate = (int) ($instance['plate'] ?? 0);
|
|
$style = !empty($instance['style']) ? $instance['style'] : 'mini';
|
|
?>
|
|
<p>
|
|
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('标题:', 'aksrt'); ?></label>
|
|
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" value="<?php echo esc_attr($title); ?>">
|
|
</p>
|
|
<p>
|
|
<label for="<?php echo esc_attr($this->get_field_id('count')); ?>"><?php esc_html_e('数量:', 'aksrt'); ?></label>
|
|
<input class="tiny-text" id="<?php echo esc_attr($this->get_field_id('count')); ?>" name="<?php echo esc_attr($this->get_field_name('count')); ?>" type="number" value="<?php echo esc_attr((string) $count); ?>" min="1" max="20">
|
|
</p>
|
|
<p>
|
|
<label><?php esc_html_e('版块 ID(可选)', 'aksrt'); ?>
|
|
<input class="tiny-text" type="number" min="0" name="<?php echo esc_attr($this->get_field_name('plate')); ?>" value="<?php echo esc_attr((string) $plate); ?>">
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label><?php esc_html_e('样式', 'aksrt'); ?>
|
|
<select name="<?php echo esc_attr($this->get_field_name('style')); ?>">
|
|
<option value="mini" <?php selected($style, 'mini'); ?>><?php esc_html_e('迷你信息行', 'aksrt'); ?></option>
|
|
<option value="rank" <?php selected($style, 'rank'); ?>><?php esc_html_e('排行榜', 'aksrt'); ?></option>
|
|
</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="number" <?php selected(($instance['paginate'] ?? ''), 'number'); ?>><?php esc_html_e('数字分页', 'aksrt'); ?></option>
|
|
<option value="none" <?php selected(($instance['paginate'] ?? ''), 'none'); ?>><?php esc_html_e('无', 'aksrt'); ?></option>
|
|
</select>
|
|
</label>
|
|
</p>
|
|
<?php
|
|
}
|
|
|
|
public function update($new_instance, $old_instance): array {
|
|
$style = sanitize_key((string) ($new_instance['style'] ?? 'mini'));
|
|
if (!in_array($style, ['mini', 'rank'], true)) {
|
|
$style = 'mini';
|
|
}
|
|
return [
|
|
'title' => sanitize_text_field($new_instance['title'] ?? ''),
|
|
'count' => max(1, min(20, (int) ($new_instance['count'] ?? 5))),
|
|
'plate' => max(0, (int) ($new_instance['plate'] ?? 0)),
|
|
'style' => $style,
|
|
'paginate' => in_array(sanitize_key((string) ($new_instance['paginate'] ?? 'ajax')), ['none', 'ajax', 'number'], true) ? sanitize_key((string) ($new_instance['paginate'] ?? 'ajax')) : 'ajax',
|
|
];
|
|
}
|
|
}
|