Files
wordpress-theme-aksrt/inc/classes/widgets/class-aksrt-forum-widgets.php
T

252 lines
10 KiB
PHP

<?php
/**
* Forum-specific widgets.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
/**
* Forum Hot Topics Widget
*/
class AKSRT_Widget_Forum_Hot_Topics extends WP_Widget {
public function __construct() {
parent::__construct(
'aksrt_forum_hot_topics',
__('论坛 - 热门话题', 'aksrt'),
['description' => __('显示论坛热门话题列表。', 'aksrt')]
);
}
public function widget($args, $instance): void {
$title = !empty($instance['title']) ? $instance['title'] : __('热门话题', 'aksrt');
$count = !empty($instance['count']) ? max(1, min(20, (int) $instance['count'])) : 5;
$period = !empty($instance['period']) ? sanitize_key($instance['period']) : 'week';
echo $args['before_widget'];
echo $args['before_title'] . esc_html($title) . $args['after_title'];
$date_query = [];
if ($period === 'week') {
$date_query = [['after' => '7 days ago']];
} elseif ($period === 'month') {
$date_query = [['after' => '30 days ago']];
}
$query = new WP_Query([
'post_type' => 'aksrt_forum_topic',
'posts_per_page' => $count,
'post_status' => 'publish',
'meta_key' => '_aksrt_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'date_query' => $date_query,
]);
if ($query->have_posts()) {
echo '<ul class="aksrt-forum-widget__list">';
$i = 0;
while ($query->have_posts()) {
$query->the_post();
$i++;
$views = (int) get_post_meta(get_the_ID(), '_aksrt_views', true);
echo '<li class="aksrt-forum-widget__item">';
echo '<span class="aksrt-forum-widget__rank">' . $i . '</span>';
echo '<a href="' . esc_url(get_permalink()) . '" class="aksrt-forum-widget__link">';
echo esc_html(get_the_title());
echo '</a>';
echo '<span class="aksrt-forum-widget__views"><i class="fas fa-eye"></i> ' . $views . '</span>';
echo '</li>';
}
echo '</ul>';
wp_reset_postdata();
} else {
echo '<p class="aksrt-forum-widget__empty">' . esc_html__('暂无话题', 'aksrt') . '</p>';
}
echo $args['after_widget'];
}
public function form($instance): void {
$title = !empty($instance['title']) ? $instance['title'] : __('热门话题', 'aksrt');
$count = !empty($instance['count']) ? (int) $instance['count'] : 5;
$period = !empty($instance['period']) ? $instance['period'] : 'week';
?>
<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($count); ?>" min="1" max="20">
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id('period')); ?>"><?php esc_html_e('时间范围:', 'aksrt'); ?></label>
<select id="<?php echo esc_attr($this->get_field_id('period')); ?>" name="<?php echo esc_attr($this->get_field_name('period')); ?>">
<option value="all" <?php selected($period, 'all'); ?>><?php esc_html_e('全部', 'aksrt'); ?></option>
<option value="week" <?php selected($period, 'week'); ?>><?php esc_html_e('最近一周', 'aksrt'); ?></option>
<option value="month" <?php selected($period, 'month'); ?>><?php esc_html_e('最近一月', 'aksrt'); ?></option>
</select>
</p>
<?php
}
public function update($new_instance, $old_instance): array {
$instance = [];
$instance['title'] = sanitize_text_field($new_instance['title'] ?? '');
$instance['count'] = max(1, min(20, (int) ($new_instance['count'] ?? 5)));
$instance['period'] = in_array($new_instance['period'] ?? '', ['all', 'week', 'month'], true) ? $new_instance['period'] : 'week';
return $instance;
}
}
/**
* Forum Recent Topics Widget
*/
class AKSRT_Widget_Forum_Recent_Topics extends WP_Widget {
public function __construct() {
parent::__construct(
'aksrt_forum_recent_topics',
__('论坛 - 最新话题', 'aksrt'),
['description' => __('显示论坛最新话题列表。', 'aksrt')]
);
}
public function widget($args, $instance): void {
$title = !empty($instance['title']) ? $instance['title'] : __('最新话题', 'aksrt');
$count = !empty($instance['count']) ? max(1, min(20, (int) $instance['count'])) : 5;
echo $args['before_widget'];
echo $args['before_title'] . esc_html($title) . $args['after_title'];
$query = new WP_Query([
'post_type' => 'aksrt_forum_topic',
'posts_per_page' => $count,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
]);
if ($query->have_posts()) {
echo '<ul class="aksrt-forum-widget__list">';
while ($query->have_posts()) {
$query->the_post();
$author = get_the_author_meta('display_name');
echo '<li class="aksrt-forum-widget__item">';
echo '<a href="' . esc_url(get_permalink()) . '" class="aksrt-forum-widget__link">';
echo esc_html(get_the_title());
echo '</a>';
echo '<span class="aksrt-forum-widget__author">' . esc_html($author) . '</span>';
echo '</li>';
}
echo '</ul>';
wp_reset_postdata();
} else {
echo '<p class="aksrt-forum-widget__empty">' . esc_html__('暂无话题', 'aksrt') . '</p>';
}
echo $args['after_widget'];
}
public function form($instance): void {
$title = !empty($instance['title']) ? $instance['title'] : __('最新话题', 'aksrt');
$count = !empty($instance['count']) ? (int) $instance['count'] : 5;
?>
<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($count); ?>" min="1" max="20">
</p>
<?php
}
public function update($new_instance, $old_instance): array {
$instance = [];
$instance['title'] = sanitize_text_field($new_instance['title'] ?? '');
$instance['count'] = max(1, min(20, (int) ($new_instance['count'] ?? 5)));
return $instance;
}
}
/**
* Forum Board List Widget
*/
class AKSRT_Widget_Forum_Board_List extends WP_Widget {
public function __construct() {
parent::__construct(
'aksrt_forum_board_list',
__('论坛 - 版块列表', 'aksrt'),
['description' => __('显示论坛版块列表及话题数量。', 'aksrt')]
);
}
public function widget($args, $instance): void {
$title = !empty($instance['title']) ? $instance['title'] : __('版块列表', 'aksrt');
echo $args['before_widget'];
echo $args['before_title'] . esc_html($title) . $args['after_title'];
$boards = get_terms([
'taxonomy' => 'aksrt_forum_board',
'hide_empty' => true,
'orderby' => 'meta_value_num',
'meta_key' => '_aksrt_sort_order',
'order' => 'ASC',
]);
if (!is_wp_error($boards) && !empty($boards)) {
echo '<ul class="aksrt-forum-widget__boards">';
foreach ($boards as $board) {
$icon = get_term_meta($board->term_id, '_aksrt_icon', true);
echo '<li class="aksrt-forum-widget__board">';
echo '<a href="' . esc_url(get_term_link($board)) . '">';
if ($icon) {
echo '<i class="' . esc_attr($icon) . '"></i> ';
}
echo esc_html($board->name);
echo '<span class="aksrt-forum-widget__count">' . (int) $board->count . '</span>';
echo '</a>';
echo '</li>';
}
echo '</ul>';
} else {
echo '<p class="aksrt-forum-widget__empty">' . esc_html__('暂无版块', 'aksrt') . '</p>';
}
echo $args['after_widget'];
}
public function form($instance): void {
$title = !empty($instance['title']) ? $instance['title'] : __('版块列表', 'aksrt');
?>
<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>
<?php
}
public function update($new_instance, $old_instance): array {
$instance = [];
$instance['title'] = sanitize_text_field($new_instance['title'] ?? '');
return $instance;
}
}
/**
* Register forum widgets.
*/
function aksrt_register_forum_widgets(): void {
register_widget('AKSRT_Widget_Forum_Hot_Topics');
register_widget('AKSRT_Widget_Forum_Recent_Topics');
register_widget('AKSRT_Widget_Forum_Board_List');
}
add_action('widgets_init', 'aksrt_register_forum_widgets');