Files
wordpress-theme-aksrt/src/Widgets/WidgetAuthorCard.php
T

121 lines
5.1 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) {
return;
}
$user = get_userdata($author_id);
if (!$user) {
return;
}
$show_stats = !isset($instance['show_stats']) || !empty($instance['show_stats']);
$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_background_image', true);
if ($cover === '') {
$cover = get_theme_file_uri('assets/img/person_background.png');
}
$post_count = count_user_posts($author_id, 'post', true);
$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
$card_cls = 'aksrt-author-card user-card author has-cover';
echo '<div class="' . esc_attr($card_cls) . '">';
$bg = ' style="background-image:url(' . esc_url($cover) . ')"';
echo '<div class="aksrt-author-card__cover user-cover"' . $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>';
}
if ($show_stats) {
echo '<div class="aksrt-author-card__stats">';
echo '<span><b>' . esc_html((string) $post_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
echo '</div></div>';
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
public function form($instance): void {
?>
<p class="description"><?php esc_html_e('自动读取当前文章/帖子作者。', 'aksrt'); ?></p>
<p>
<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>
</p>
<?php
}
public function update($new_instance, $old_instance): array {
return [
'show_stats' => !empty($new_instance['show_stats']) ? 1 : 0,
'show_follow' => !empty($new_instance['show_follow']) ? 1 : 0,
];
}
}