Files
wordpress-theme-aksrt/src/AKSRT/Widgets/WidgetAuthorCard.php
T
kate522 d248727870 feat(widgets): reviews/pricing/announce empty + identity lazy avatars
Admin null states for reviews/pricing/announcement; lazyload avatars and
covers on about/author/user cards and review slides.
2026-07-21 11:17:01 +08:00

237 lines
11 KiB
PHP

<?php
/**
* Post author card (zibll widget_ui_avatar + zib_get_user_card_box depth).
*
* @package AKSRT
*/
namespace AKSRT\Widgets;
defined('ABSPATH') || exit;
class WidgetAuthorCard extends \WP_Widget {
public function __construct() {
parent::__construct(
'aksrt_author_card',
esc_html__('AKSRT 文章作者卡片', 'aksrt'),
[
'description' => esc_html__('在文章/帖子侧栏显示当前内容作者信息与更多文章。', 'aksrt'),
'classname' => 'widget_aksrt_author_card',
]
);
}
public function widget($args, $instance): void {
$author_id = 0;
if (is_singular()) {
$post = get_queried_object();
if ($post instanceof \WP_Post) {
$author_id = (int) $post->post_author;
}
}
if ($author_id <= 0 && !empty($instance['user_id'])) {
$author_id = (int) $instance['user_id'];
}
if ($author_id <= 0) {
return;
}
$user = get_userdata($author_id);
if (!$user) {
return;
}
$show_bg = !isset($instance['show_img_bg']) || !empty($instance['show_img_bg']);
$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'));
$avatar = function_exists('aksrt_get_avatar_url')
? aksrt_get_avatar_url($author_id, ['size' => 120])
: get_avatar_url($author_id, ['size' => 120]);
$home = function_exists('aksrt_get_user_home_url')
? aksrt_get_user_home_url($author_id)
: get_author_posts_url($author_id);
$bio = get_user_meta($author_id, 'description', true);
if (!is_string($bio) || $bio === '') {
$bio = (string) $user->description;
}
$cover = (string) get_user_meta($author_id, 'aksrt_cover', true);
if ($cover === '') {
$cover = (string) get_user_meta($author_id, 'custom_avatar_cover', true);
}
$post_count = count_user_posts($author_id, 'post', true);
$topic_count = post_type_exists('aksrt_forum_topic')
? count_user_posts($author_id, 'aksrt_forum_topic', true)
: 0;
$followers = 0;
if (class_exists('\AKSRT\User\Follow')) {
$followers = \AKSRT\User\Follow::get_instance()->get_follower_count($author_id);
}
$name_html = function_exists('aksrt_get_user_name')
? aksrt_get_user_name(['id' => $author_id, 'follow' => false, 'class' => 'flex ac jc'])
: '<a href="' . esc_url($home) . '">' . esc_html($user->display_name) . '</a>';
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 '<div class="aksrt-author-card user-card author zib-widget' . ($show_bg ? ' has-cover' : '') . '">';
if ($show_bg) {
$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__body card-content">';
echo '<div class="user-content">';
$ph = get_template_directory_uri() . '/assets/img/placeholder.svg';
echo '<a class="aksrt-author-card__avatar user-avatar" href="' . esc_url($home) . '"><img class="avatar aksrt-lazy lazyload" data-src="' . esc_url($avatar) . '" src="' . esc_url($ph) . '" alt="" width="72" height="72" loading="lazy" /></a>';
echo '<div class="aksrt-author-card__name user-name">' . $name_html . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
if ($bio !== '') {
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 ($show_badge && class_exists('\AKSRT\User\Badges') && \AKSRT\User\Badges::public_display()) {
$wear = \AKSRT\User\Badges::get_wear($author_id);
if ($wear === '') {
$wear = sanitize_key((string) get_user_meta($author_id, 'wear_medal', true));
}
if ($wear !== '') {
$map = \AKSRT\User\Badges::get_dictionary_map();
if (isset($map[$wear])) {
$badge = $map[$wear];
echo '<div class="aksrt-author-card__badge author-tag tone-' . esc_attr((string) ($badge['tone'] ?? 'neutral')) . '">';
if (!empty($badge['icon_url'])) {
echo '<img src="' . esc_url((string) $badge['icon_url']) . '" alt="" width="16" height="16" loading="lazy" /> ';
} else {
echo '<i class="fas fa-medal" aria-hidden="true"></i> ';
}
echo esc_html((string) $badge['label']);
echo '</div>';
}
}
}
if ($show_stats) {
echo '<div class="aksrt-author-card__stats">';
echo '<span><b>' . esc_html((string) $post_count) . '</b>' . esc_html__('文章', 'aksrt') . '</span>';
if ($topic_count > 0) {
echo '<span><b>' . esc_html((string) $topic_count) . '</b>' . esc_html__('帖子', 'aksrt') . '</span>';
}
echo '<span><b>' . esc_html((string) $followers) . '</b>' . esc_html__('粉丝', 'aksrt') . '</span>';
echo '</div>';
}
$cuid = get_current_user_id();
$show_follow = !isset($instance['show_follow']) || !empty($instance['show_follow']);
echo '<div class="aksrt-author-card__actions user-btns">';
if ($show_follow && $cuid > 0 && $cuid !== $author_id && class_exists('\AKSRT\User\Follow')) {
$following = \AKSRT\User\Follow::get_instance()->is_following($cuid, $author_id);
$label = $following ? __('已关注', 'aksrt') : __('关注', 'aksrt');
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 '</div>';
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 $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
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>
<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>
<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))); ?>">
</label>
</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_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_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>
<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="views" <?php selected(($instance['orderby'] ?? ''), 'views'); ?>><?php esc_html_e('阅读量', 'aksrt'); ?></option>
<option value="comment_count" <?php selected(($instance['orderby'] ?? ''), 'comment_count'); ?>><?php esc_html_e('评论数', 'aksrt'); ?></option>
<option value="rand" <?php selected(($instance['orderby'] ?? ''), 'rand'); ?>><?php esc_html_e('随机', 'aksrt'); ?></option>
</select>
</label>
</p>
<?php
}
public function update($new_instance, $old_instance): array {
$post_style = sanitize_key((string) ($new_instance['post_style'] ?? 'mini'));
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', 'rand'], true)) {
$orderby = 'date';
}
return [
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'user_id' => max(0, (int) ($new_instance['user_id'] ?? 0)),
'show_img_bg' => !empty($new_instance['show_img_bg']) ? 1 : 0,
'show_stats' => !empty($new_instance['show_stats']) ? 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,
'limit' => max(0, min(12, (int) ($new_instance['limit'] ?? 6))),
'post_style' => $post_style,
'orderby' => $orderby,
];
}
}