feat(widgets): deepen buttons, video player, recent comments

Add go_link/hollow/radius/icon slots for buttons; loop/poster/hide
controls for video player; exclude post/keywords on recent comments.
This commit is contained in:
2026-07-19 01:17:41 +08:00
parent e2decd8d71
commit aabc1cd142
4 changed files with 455 additions and 277 deletions
+106 -70
View File
@@ -1,5 +1,7 @@
<?php
/**
* Recent comments mini list (zibll widget_ui_new_comment lite).
*
* @package AKSRT
*/
@@ -9,81 +11,115 @@ defined('ABSPATH') || exit;
class RecentCommentsWidget extends \WP_Widget {
public function __construct() {
parent::__construct('aksrt_recent_comments', esc_html__('AKSRT 最新评论', 'aksrt'), [
'description' => esc_html__('显示最新评论列表', 'aksrt'),
]);
}
public function __construct() {
parent::__construct('aksrt_recent_comments', esc_html__('AKSRT 最新评论', 'aksrt'), [
'description' => esc_html__('显示最新评论列表', 'aksrt'),
]);
}
public function widget($args, $instance): void {
echo $args['before_widget'];
$title = apply_filters('widget_title', $instance['title'] ?? __('最新评论', 'aksrt'), $instance, $this->id_base);
$count = max(1, min(20, (int) ($instance['count'] ?? 5)));
$show_avatar = !empty($instance['show_avatar']);
public function widget($args, $instance): void {
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$title = apply_filters('widget_title', (string) ($instance['title'] ?? __('最新评论', 'aksrt')), $instance, $this->id_base);
$count = max(1, min(20, (int) ($instance['count'] ?? $instance['limit'] ?? 5)));
$show_avatar = !isset($instance['show_avatar']) || !empty($instance['show_avatar']);
$outpost = max(0, (int) ($instance['outpost'] ?? 0));
$outer = sanitize_text_field((string) ($instance['outer'] ?? ''));
if ($title) echo $args['before_title'] . esc_html($title) . $args['after_title'];
if ($title !== '') {
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
$comments = get_comments([
'number' => $count,
'status' => 'approve',
'post_status' => 'publish',
]);
$query = [
'number' => $count,
'status' => 'approve',
'post_status' => 'publish',
];
if ($outpost > 0) {
$query['post__not_in'] = [$outpost];
}
if ($outer !== '') {
// Exclude comments containing outer keywords (comma separated).
// Implemented post-filter below.
}
$comments = get_comments($query);
if ($outer !== '' && $comments) {
$keys = array_filter(array_map('trim', preg_split('/[,,]/', $outer) ?: []));
if ($keys) {
$comments = array_values(array_filter($comments, static function ($c) use ($keys) {
$body = (string) $c->comment_content;
foreach ($keys as $k) {
if ($k !== '' && str_contains($body, $k)) {
return false;
}
}
return true;
}));
}
}
if ($comments) : ?>
<div class="widget-recent-comments">
<?php foreach ($comments as $comment) :
$post = get_post($comment->comment_post_ID);
if (!$post) continue;
?>
<div class="widget-recent-comments__item">
<?php if ($show_avatar) : ?>
<div class="widget-recent-comments__avatar">
<?php echo get_avatar($comment, 32); ?>
</div>
<?php endif; ?>
<div class="widget-recent-comments__body">
<div class="widget-recent-comments__meta">
<strong class="widget-recent-comments__author"><?php echo esc_html($comment->comment_author); ?></strong>
<span class="widget-recent-comments__time"><?php echo esc_html(human_time_diff(strtotime($comment->comment_date), current_time('timestamp'))); ?></span>
</div>
<p class="widget-recent-comments__text">
<a href="<?php echo esc_url(get_comment_link($comment)); ?>"><?php echo esc_html(wp_trim_words($comment->comment_content, 15, '...')); ?></a>
</p>
<span class="widget-recent-comments__post">
<?php esc_html_e('于', 'aksrt'); ?> <a href="<?php echo esc_url(get_permalink($post->ID)); ?>"><?php echo esc_html(get_the_title($post->ID)); ?></a>
</span>
</div>
</div>
<?php endforeach; ?>
</div>
<?php else : ?>
<?php echo function_exists('aksrt_get_widget_null') ? aksrt_get_widget_null(__('暂无评论', 'aksrt')) : '<p class="widget-empty">' . esc_html__('暂无评论', 'aksrt') . '</p>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php endif;
if ($comments) {
echo '<div class="widget-recent-comments comment-mini-lists">';
foreach ($comments as $comment) {
$post = get_post($comment->comment_post_ID);
if (!$post) {
continue;
}
echo '<div class="widget-recent-comments__item">';
if ($show_avatar) {
echo '<div class="widget-recent-comments__avatar">' . get_avatar($comment, 32) . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
echo '<div class="widget-recent-comments__body">';
echo '<div class="widget-recent-comments__meta">';
echo '<strong class="widget-recent-comments__author">' . esc_html($comment->comment_author) . '</strong>';
echo '<span class="widget-recent-comments__time">' . esc_html(human_time_diff(strtotime($comment->comment_date), current_time('timestamp'))) . '</span>';
echo '</div>';
echo '<p class="widget-recent-comments__text"><a href="' . esc_url(get_comment_link($comment)) . '">' . esc_html(wp_trim_words($comment->comment_content, 15, '...')) . '</a></p>';
echo '<div class="widget-recent-comments__post muted-2-color em09"><a href="' . esc_url(get_permalink($post)) . '">' . esc_html(get_the_title($post)) . '</a></div>';
echo '</div></div>';
}
echo '</div>';
} else {
echo '<p class="widget-empty">' . esc_html__('暂无评论', 'aksrt') . '</p>';
}
echo $args['after_widget'];
}
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
public function form($instance): void {
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e('标题:', 'aksrt'); ?></label>
<input class="widefat" type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr($instance['title'] ?? __('最新评论', 'aksrt')); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id('count'); ?>"><?php esc_html_e('显示数量:', 'aksrt'); ?></label>
<input class="tiny-text" type="number" min="1" max="20" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" value="<?php echo (int) ($instance['count'] ?? 5); ?>">
</p>
<p>
<label><input type="checkbox" name="<?php echo $this->get_field_name('show_avatar'); ?>" value="1" <?php checked(!empty($instance['show_avatar'])); ?>> <?php esc_html_e('显示头像', 'aksrt'); ?></label>
</p>
<?php
}
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'] ?? 5))); ?>">
</label>
</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('outpost')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['outpost'] ?? 0))); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('排除关键词(逗号)', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('outer')); ?>" value="<?php echo esc_attr((string) ($instance['outer'] ?? '')); ?>">
</label>
</p>
<p>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_avatar')); ?>" value="1" <?php checked(!isset($instance['show_avatar']) || !empty($instance['show_avatar'])); ?>> <?php esc_html_e('显示头像', 'aksrt'); ?></label>
</p>
<?php
}
public function update($new_instance, $old_instance): array {
return [
'title' => sanitize_text_field($new_instance['title'] ?? ''),
'count' => max(1, min(20, (int) ($new_instance['count'] ?? 5))),
'show_avatar' => !empty($new_instance['show_avatar']),
];
}
public function update($new_instance, $old_instance): array {
return [
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'count' => max(1, min(20, (int) ($new_instance['count'] ?? 5))),
'outpost' => max(0, (int) ($new_instance['outpost'] ?? 0)),
'outer' => sanitize_text_field((string) ($new_instance['outer'] ?? '')),
'show_avatar' => !empty($new_instance['show_avatar']) ? 1 : 0,
];
}
}