refactor(widget): simplify author card settings

This commit is contained in:
2026-08-15 18:02:45 +08:00
parent caa5fca3c2
commit b7a3e433fa
+4 -18
View File
@@ -29,9 +29,6 @@ class WidgetAuthorCard extends \WP_Widget {
$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;
}
@@ -40,7 +37,6 @@ class WidgetAuthorCard extends \WP_Widget {
return;
}
$show_bg = !isset($instance['show_img_bg']) || !empty($instance['show_img_bg']);
$show_stats = !isset($instance['show_stats']) || !empty($instance['show_stats']);
$avatar = function_exists('aksrt_get_avatar_url')
@@ -70,12 +66,10 @@ class WidgetAuthorCard extends \WP_Widget {
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$card_cls = 'aksrt-author-card user-card author' . ($show_bg ? ' has-cover' : '');
$card_cls = 'aksrt-author-card user-card author has-cover';
echo '<div class="' . esc_attr($card_cls) . '">';
if ($show_bg) {
$bg = $cover !== '' ? ' style="background-image:url(' . esc_url($cover) . ')"' : '';
echo '<div class="aksrt-author-card__cover user-cover"' . $bg . '></div>';
}
$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';
@@ -109,14 +103,8 @@ class WidgetAuthorCard extends \WP_Widget {
public function form($instance): void {
?>
<p class="description"><?php esc_html_e('自动读取当前文章/帖子作者;也可固定用户 ID。', 'aksrt'); ?></p>
<p class="description"><?php esc_html_e('自动读取当前文章/帖子作者。', 'aksrt'); ?></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>
</p>
@@ -125,8 +113,6 @@ class WidgetAuthorCard extends \WP_Widget {
public function update($new_instance, $old_instance): array {
return [
'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,
];