refactor(widget): simplify search and author cards
This commit is contained in:
@@ -7,19 +7,15 @@
|
|||||||
|
|
||||||
namespace AKSRT\Widgets;
|
namespace AKSRT\Widgets;
|
||||||
|
|
||||||
use AKSRT\Widgets\Traits\CommonWidgetFields;
|
|
||||||
|
|
||||||
defined('ABSPATH') || exit;
|
defined('ABSPATH') || exit;
|
||||||
|
|
||||||
class WidgetAuthorCard extends \WP_Widget {
|
class WidgetAuthorCard extends \WP_Widget {
|
||||||
use CommonWidgetFields;
|
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
parent::__construct(
|
parent::__construct(
|
||||||
'aksrt_author_card',
|
'aksrt_author_card',
|
||||||
esc_html__('AKSRT 文章作者卡片', 'aksrt'),
|
esc_html__('AKSRT 文章作者卡片', 'aksrt'),
|
||||||
[
|
[
|
||||||
'description' => esc_html__('在文章/帖子侧栏显示当前内容作者信息与更多文章。', 'aksrt'),
|
'description' => esc_html__('在文章/帖子侧栏显示当前内容作者信息。', 'aksrt'),
|
||||||
'classname' => 'widget_aksrt_author_card',
|
'classname' => 'widget_aksrt_author_card',
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
@@ -46,25 +42,6 @@ class WidgetAuthorCard extends \WP_Widget {
|
|||||||
|
|
||||||
$show_bg = !isset($instance['show_img_bg']) || !empty($instance['show_img_bg']);
|
$show_bg = !isset($instance['show_img_bg']) || !empty($instance['show_img_bg']);
|
||||||
$show_stats = !isset($instance['show_stats']) || !empty($instance['show_stats']);
|
$show_stats = !isset($instance['show_stats']) || !empty($instance['show_stats']);
|
||||||
$show_posts = !isset($instance['show_posts']) || !empty($instance['show_posts']);
|
|
||||||
$post_style = sanitize_key((string) ($instance['post_style'] ?? 'mini'));
|
|
||||||
if (!in_array($post_style, ['card', 'mini'], true)) {
|
|
||||||
// Legacy show_img checkbox from zibll.
|
|
||||||
if (isset($instance['show_img'])) {
|
|
||||||
$post_style = !empty($instance['show_img']) ? 'card' : 'mini';
|
|
||||||
} else {
|
|
||||||
$post_style = 'mini';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$limit = max(0, min(12, (int) ($instance['limit'] ?? 6)));
|
|
||||||
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'date'));
|
|
||||||
// Zibll button palettes / button_2 type (shown only when viewer is the author).
|
|
||||||
$show_button = !isset($instance['show_button']) || !empty($instance['show_button']);
|
|
||||||
$btn1_text = (string) ($instance['button_1_text'] ?? __('发布文章', 'aksrt'));
|
|
||||||
$btn2_text = (string) ($instance['button_2_text'] ?? __('用户中心', 'aksrt'));
|
|
||||||
$btn1_class = WidgetUserCard::sanitize_palette((string) ($instance['button_1_class'] ?? 'jb-pink'), 'jb-pink');
|
|
||||||
$btn2_class = WidgetUserCard::sanitize_palette((string) ($instance['button_2_class'] ?? 'jb-blue'), 'jb-blue');
|
|
||||||
$btn2_type = (string) ($instance['button_2'] ?? 'center');
|
|
||||||
|
|
||||||
$avatar = function_exists('aksrt_get_avatar_url')
|
$avatar = function_exists('aksrt_get_avatar_url')
|
||||||
? aksrt_get_avatar_url($author_id, ['size' => 120])
|
? aksrt_get_avatar_url($author_id, ['size' => 120])
|
||||||
@@ -91,15 +68,10 @@ class WidgetAuthorCard extends \WP_Widget {
|
|||||||
? aksrt_get_user_name(['id' => $author_id, 'follow' => false, 'class' => 'flex ac jc'])
|
? aksrt_get_user_name(['id' => $author_id, 'follow' => false, 'class' => 'flex ac jc'])
|
||||||
: '<a href="' . esc_url($home) . '">' . esc_html($user->display_name) . '</a>';
|
: '<a href="' . esc_url($home) . '">' . esc_html($user->display_name) . '</a>';
|
||||||
|
|
||||||
$affix = function_exists('aksrt_widget_affix_attrs') ? aksrt_widget_affix_attrs($instance) : ['class' => '', 'attr' => ''];
|
|
||||||
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
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
|
|
||||||
}
|
|
||||||
|
|
||||||
$card_cls = 'aksrt-author-card user-card author' . ($show_bg ? ' has-cover' : '') . $affix['class'];
|
$card_cls = 'aksrt-author-card user-card author' . ($show_bg ? ' has-cover' : '');
|
||||||
echo '<div class="' . esc_attr($card_cls) . '"' . $affix['attr'] . '>';
|
echo '<div class="' . esc_attr($card_cls) . '">';
|
||||||
if ($show_bg) {
|
if ($show_bg) {
|
||||||
$bg = $cover !== '' ? ' style="background-image:url(' . esc_url($cover) . ')"' : '';
|
$bg = $cover !== '' ? ' style="background-image:url(' . esc_url($cover) . ')"' : '';
|
||||||
echo '<div class="aksrt-author-card__cover user-cover graphic"' . $bg . '></div>';
|
echo '<div class="aksrt-author-card__cover user-cover graphic"' . $bg . '></div>';
|
||||||
@@ -112,8 +84,7 @@ class WidgetAuthorCard extends \WP_Widget {
|
|||||||
if ($bio !== '') {
|
if ($bio !== '') {
|
||||||
echo '<div class="aksrt-author-card__bio user-desc muted-2-color em09">' . esc_html(wp_trim_words($bio, 28)) . '</div>';
|
echo '<div class="aksrt-author-card__bio user-desc muted-2-color em09">' . esc_html(wp_trim_words($bio, 28)) . '</div>';
|
||||||
}
|
}
|
||||||
$show_badge = !isset($instance['show_badge']) || !empty($instance['show_badge']);
|
if (class_exists('\AKSRT\User\Badges') && \AKSRT\User\Badges::public_display()) {
|
||||||
if ($show_badge && class_exists('\AKSRT\User\Badges') && \AKSRT\User\Badges::public_display()) {
|
|
||||||
$wear = \AKSRT\User\Badges::get_wear($author_id);
|
$wear = \AKSRT\User\Badges::get_wear($author_id);
|
||||||
if ($wear !== '') {
|
if ($wear !== '') {
|
||||||
$map = \AKSRT\User\Badges::get_dictionary_map();
|
$map = \AKSRT\User\Badges::get_dictionary_map();
|
||||||
@@ -145,48 +116,17 @@ class WidgetAuthorCard extends \WP_Widget {
|
|||||||
echo '<button type="button" class="aksrt-author-card__btn aksrt-author-card__follow but' . ($following ? ' is-following' : '') . '" data-aksrt-follow="' . (int) $author_id . '">' . esc_html($label) . '</button>';
|
echo '<button type="button" class="aksrt-author-card__btn aksrt-author-card__follow but' . ($following ? ' is-following' : '') . '" data-aksrt-follow="' . (int) $author_id . '">' . esc_html($label) . '</button>';
|
||||||
}
|
}
|
||||||
echo '<a class="aksrt-author-card__btn but" href="' . esc_url($home) . '">' . esc_html__('查看主页', 'aksrt') . '</a>';
|
echo '<a class="aksrt-author-card__btn but" href="' . esc_url($home) . '">' . esc_html__('查看主页', 'aksrt') . '</a>';
|
||||||
// Zibll: button_1 / button_2 only render when the logged-in viewer IS the author.
|
|
||||||
if ($show_button && $cuid > 0 && $cuid === $author_id) {
|
|
||||||
$center_url = function_exists('aksrt_get_user_center_url') ? aksrt_get_user_center_url() : $home;
|
|
||||||
$btn2_url = $btn2_type === 'home' ? $home : $center_url;
|
|
||||||
$compose_url = $home;
|
|
||||||
if (function_exists('aksrt_get_user_center_url')) {
|
|
||||||
$compose_url = aksrt_get_user_center_url('posts');
|
|
||||||
}
|
|
||||||
echo '<a class="aksrt-author-card__btn but ' . esc_attr($btn1_class) . '" href="' . esc_url($compose_url) . '">' . esc_html($btn1_text) . '</a>';
|
|
||||||
echo '<a class="aksrt-author-card__btn but ' . esc_attr($btn2_class) . '" href="' . esc_url($btn2_url) . '">' . esc_html($btn2_text) . '</a>';
|
|
||||||
}
|
|
||||||
echo '</div>';
|
echo '</div>';
|
||||||
echo '</div>'; // .user-content
|
echo '</div>'; // .user-content
|
||||||
|
|
||||||
if ($show_posts && $limit > 0 && function_exists('aksrt_render_user_card_posts')) {
|
|
||||||
echo aksrt_render_user_card_posts([ // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
||||||
'user_id' => $author_id,
|
|
||||||
'limit' => $limit,
|
|
||||||
'orderby' => $orderby,
|
|
||||||
'post_style' => $post_style,
|
|
||||||
'post_type' => 'post',
|
|
||||||
'show_empty' => true,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
echo '</div></div>';
|
echo '</div></div>';
|
||||||
|
|
||||||
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||||
}
|
}
|
||||||
|
|
||||||
public function form($instance): void {
|
public function form($instance): void {
|
||||||
$post_style = sanitize_key((string) ($instance['post_style'] ?? 'mini'));
|
|
||||||
if (!in_array($post_style, ['card', 'mini'], true)) {
|
|
||||||
$post_style = !empty($instance['show_img']) ? 'card' : 'mini';
|
|
||||||
}
|
|
||||||
?>
|
?>
|
||||||
<p class="description"><?php esc_html_e('自动读取当前文章/帖子作者;也可固定用户 ID。', 'aksrt'); ?></p>
|
<p class="description"><?php esc_html_e('自动读取当前文章/帖子作者;也可固定用户 ID。', 'aksrt'); ?></p>
|
||||||
<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'] ?? '')); ?>">
|
|
||||||
</label>
|
|
||||||
</p>
|
|
||||||
<p>
|
<p>
|
||||||
<label><?php esc_html_e('固定用户 ID(可选)', 'aksrt'); ?>
|
<label><?php esc_html_e('固定用户 ID(可选)', 'aksrt'); ?>
|
||||||
<input class="tiny-text" type="number" min="0" name="<?php echo esc_attr($this->get_field_name('user_id')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['user_id'] ?? 0))); ?>">
|
<input class="tiny-text" type="number" min="0" name="<?php echo esc_attr($this->get_field_name('user_id')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['user_id'] ?? 0))); ?>">
|
||||||
@@ -195,102 +135,17 @@ class WidgetAuthorCard extends \WP_Widget {
|
|||||||
<p>
|
<p>
|
||||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_img_bg')); ?>" value="1" <?php checked(!isset($instance['show_img_bg']) || !empty($instance['show_img_bg'])); ?>> <?php esc_html_e('显示封面', 'aksrt'); ?></label><br>
|
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_img_bg')); ?>" value="1" <?php checked(!isset($instance['show_img_bg']) || !empty($instance['show_img_bg'])); ?>> <?php esc_html_e('显示封面', 'aksrt'); ?></label><br>
|
||||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_stats')); ?>" value="1" <?php checked(!isset($instance['show_stats']) || !empty($instance['show_stats'])); ?>> <?php esc_html_e('显示统计', 'aksrt'); ?></label><br>
|
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_stats')); ?>" value="1" <?php checked(!isset($instance['show_stats']) || !empty($instance['show_stats'])); ?>> <?php esc_html_e('显示统计', 'aksrt'); ?></label><br>
|
||||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_follow')); ?>" value="1" <?php checked(!isset($instance['show_follow']) || !empty($instance['show_follow'])); ?>> <?php esc_html_e('显示关注按钮', 'aksrt'); ?></label><br>
|
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_follow')); ?>" value="1" <?php checked(!isset($instance['show_follow']) || !empty($instance['show_follow'])); ?>> <?php esc_html_e('显示关注按钮', 'aksrt'); ?></label>
|
||||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_badge')); ?>" value="1" <?php checked(!isset($instance['show_badge']) || !empty($instance['show_badge'])); ?>> <?php esc_html_e('显示佩戴徽章', 'aksrt'); ?></label><br>
|
|
||||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_posts')); ?>" value="1" <?php checked(!isset($instance['show_posts']) || !empty($instance['show_posts'])); ?>> <?php esc_html_e('显示更多文章', 'aksrt'); ?></label>
|
|
||||||
</p>
|
</p>
|
||||||
<p>
|
|
||||||
<label><?php esc_html_e('文章数量', 'aksrt'); ?>
|
|
||||||
<input class="tiny-text" type="number" min="0" max="12" name="<?php echo esc_attr($this->get_field_name('limit')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['limit'] ?? 6))); ?>">
|
|
||||||
</label>
|
|
||||||
</p>
|
|
||||||
<p>
|
|
||||||
<label><?php esc_html_e('文章样式', 'aksrt'); ?>
|
|
||||||
<select name="<?php echo esc_attr($this->get_field_name('post_style')); ?>">
|
|
||||||
<option value="mini" <?php selected($post_style, 'mini'); ?>><?php esc_html_e('迷你列表', 'aksrt'); ?></option>
|
|
||||||
<option value="card" <?php selected($post_style, 'card'); ?>><?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('orderby')); ?>">
|
|
||||||
<option value="date" <?php selected(($instance['orderby'] ?? 'date'), 'date'); ?>><?php esc_html_e('最新', 'aksrt'); ?></option>
|
|
||||||
<option value="modified" <?php selected(($instance['orderby'] ?? ''), 'modified'); ?>><?php esc_html_e('最近更新', 'aksrt'); ?></option>
|
|
||||||
<option value="views" <?php selected(($instance['orderby'] ?? ''), 'views'); ?>><?php esc_html_e('阅读量', 'aksrt'); ?></option>
|
|
||||||
<option value="like" <?php selected(($instance['orderby'] ?? ''), 'like'); ?>><?php esc_html_e('点赞', 'aksrt'); ?></option>
|
|
||||||
<option value="comment_count" <?php selected(($instance['orderby'] ?? ''), 'comment_count'); ?>><?php esc_html_e('评论数', 'aksrt'); ?></option>
|
|
||||||
<option value="favorite" <?php selected(($instance['orderby'] ?? ''), 'favorite'); ?>><?php esc_html_e('收藏', 'aksrt'); ?></option>
|
|
||||||
<option value="rand" <?php selected(($instance['orderby'] ?? ''), 'rand'); ?>><?php esc_html_e('随机', 'aksrt'); ?></option>
|
|
||||||
</select>
|
|
||||||
</label>
|
|
||||||
</p>
|
|
||||||
<hr style="margin:12px 0;">
|
|
||||||
<p style="font-weight:600;margin-bottom:8px;"><?php esc_html_e('按钮(仅当登录用户为作者时显示)', 'aksrt'); ?></p>
|
|
||||||
<p>
|
|
||||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_button')); ?>" value="1" <?php checked(!isset($instance['show_button']) || !empty($instance['show_button'])); ?>> <?php esc_html_e('显示按钮', 'aksrt'); ?></label>
|
|
||||||
</p>
|
|
||||||
<p>
|
|
||||||
<label><?php esc_html_e('按钮1文案', 'aksrt'); ?>
|
|
||||||
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('button_1_text')); ?>" value="<?php echo esc_attr((string) ($instance['button_1_text'] ?? __('发布文章', 'aksrt'))); ?>">
|
|
||||||
</label>
|
|
||||||
<label><?php esc_html_e('按钮1颜色', 'aksrt'); ?>
|
|
||||||
<select name="<?php echo esc_attr($this->get_field_name('button_1_class')); ?>">
|
|
||||||
<?php foreach (WidgetUserCard::palette_opts() as $k => $label) : ?>
|
|
||||||
<option value="<?php echo esc_attr($k); ?>" <?php selected(($instance['button_1_class'] ?? 'jb-pink'), $k); ?>><?php echo esc_html($label); ?></option>
|
|
||||||
<?php endforeach; ?>
|
|
||||||
</select>
|
|
||||||
</label>
|
|
||||||
</p>
|
|
||||||
<p>
|
|
||||||
<label><?php esc_html_e('按钮2类型', 'aksrt'); ?>
|
|
||||||
<select name="<?php echo esc_attr($this->get_field_name('button_2')); ?>">
|
|
||||||
<option value="home" <?php selected(($instance['button_2'] ?? 'center'), 'home'); ?>><?php esc_html_e('个人主页', 'aksrt'); ?></option>
|
|
||||||
<option value="center" <?php selected(($instance['button_2'] ?? 'center'), 'center'); ?>><?php esc_html_e('用户中心', 'aksrt'); ?></option>
|
|
||||||
</select>
|
|
||||||
</label>
|
|
||||||
<label><?php esc_html_e('按钮2文案', 'aksrt'); ?>
|
|
||||||
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('button_2_text')); ?>" value="<?php echo esc_attr((string) ($instance['button_2_text'] ?? __('用户中心', 'aksrt'))); ?>">
|
|
||||||
</label>
|
|
||||||
<label><?php esc_html_e('按钮2颜色', 'aksrt'); ?>
|
|
||||||
<select name="<?php echo esc_attr($this->get_field_name('button_2_class')); ?>">
|
|
||||||
<?php foreach (WidgetUserCard::palette_opts() as $k => $label) : ?>
|
|
||||||
<option value="<?php echo esc_attr($k); ?>" <?php selected(($instance['button_2_class'] ?? 'jb-blue'), $k); ?>><?php echo esc_html($label); ?></option>
|
|
||||||
<?php endforeach; ?>
|
|
||||||
</select>
|
|
||||||
</label>
|
|
||||||
</p>
|
|
||||||
<?php $this->render_common_form_section($instance); ?>
|
|
||||||
<?php
|
<?php
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update($new_instance, $old_instance): array {
|
public function update($new_instance, $old_instance): array {
|
||||||
$post_style = sanitize_key((string) ($new_instance['post_style'] ?? 'mini'));
|
return [
|
||||||
if (!in_array($post_style, ['card', 'mini'], true)) {
|
|
||||||
$post_style = 'mini';
|
|
||||||
}
|
|
||||||
$orderby = sanitize_key((string) ($new_instance['orderby'] ?? 'date'));
|
|
||||||
if (!in_array($orderby, ['date', 'views', 'comment_count', 'modified', 'like', 'favorite', 'rand'], true)) {
|
|
||||||
$orderby = 'date';
|
|
||||||
}
|
|
||||||
return $this->update_common_fields($new_instance, $old_instance, [
|
|
||||||
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
|
|
||||||
'user_id' => max(0, (int) ($new_instance['user_id'] ?? 0)),
|
'user_id' => max(0, (int) ($new_instance['user_id'] ?? 0)),
|
||||||
'show_img_bg' => !empty($new_instance['show_img_bg']) ? 1 : 0,
|
'show_img_bg' => !empty($new_instance['show_img_bg']) ? 1 : 0,
|
||||||
'show_stats' => !empty($new_instance['show_stats']) ? 1 : 0,
|
'show_stats' => !empty($new_instance['show_stats']) ? 1 : 0,
|
||||||
'show_follow' => !empty($new_instance['show_follow']) ? 1 : 0,
|
'show_follow' => !empty($new_instance['show_follow']) ? 1 : 0,
|
||||||
'show_badge' => !empty($new_instance['show_badge']) ? 1 : 0,
|
];
|
||||||
'show_posts' => !empty($new_instance['show_posts']) ? 1 : 0,
|
|
||||||
'show_button' => !empty($new_instance['show_button']) ? 1 : 0,
|
|
||||||
'limit' => max(0, min(12, (int) ($new_instance['limit'] ?? 6))),
|
|
||||||
'post_style' => $post_style,
|
|
||||||
'orderby' => $orderby,
|
|
||||||
'button_1' => 'post',
|
|
||||||
'button_1_text'=> sanitize_text_field((string) ($new_instance['button_1_text'] ?? '')),
|
|
||||||
'button_1_class'=> WidgetUserCard::sanitize_palette((string) ($new_instance['button_1_class'] ?? 'jb-pink'), 'jb-pink'),
|
|
||||||
'button_2' => in_array((string) ($new_instance['button_2'] ?? 'center'), ['home', 'center'], true) ? $new_instance['button_2'] : 'center',
|
|
||||||
'button_2_text'=> sanitize_text_field((string) ($new_instance['button_2_text'] ?? '')),
|
|
||||||
'button_2_class'=> WidgetUserCard::sanitize_palette((string) ($new_instance['button_2_class'] ?? 'jb-blue'), 'jb-blue'),
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user