__('未登录显示登录入口;登录后显示当前用户卡片、签到、快捷按钮与更多文章。', 'aksrt'), 'classname' => 'widget_aksrt_user_card', ] ); } public function widget($args, $instance): void { $show_cover = !isset($instance['show_img_bg']) || !empty($instance['show_img_bg']); $show_stats = !empty($instance['show_stats']) || !isset($instance['show_stats']); $show_actions = !empty($instance['show_button']) || !isset($instance['show_button']); $show_checkin = !empty($instance['show_checkin']) || !isset($instance['show_checkin']); // zibll user widget forces show_posts=false; author widget enables it. // AKSRT keeps optional show_posts for current-user card (default off). $show_posts = !empty($instance['show_posts']); $affix = function_exists('aksrt_widget_affix_attrs') ? aksrt_widget_affix_attrs($instance) : ['class' => '', 'attr' => '']; $loged_title = (string) ($instance['loged_title'] ?? __('Hi!请登录', 'aksrt')); $btn1_text = (string) ($instance['button_1_text'] ?? __('发布文章', 'aksrt')); $btn2_text = (string) ($instance['button_2_text'] ?? __('用户中心', 'aksrt')); // Zibll button color palettes (button_1_class / button_2_class). $btn1_class = sanitize_html_class((string) ($instance['button_1_class'] ?? 'jb-pink')) ?: 'jb-pink'; $btn2_class = sanitize_html_class((string) ($instance['button_2_class'] ?? 'jb-blue')) ?: 'jb-blue'; // Zibll button types: button_1 = 'post' (compose); button_2 ∈ {home,center}. $btn2_type = (string) ($instance['button_2'] ?? 'center'); $post_style = sanitize_key((string) ($instance['post_style'] ?? 'mini')); if (!in_array($post_style, ['card', 'mini'], true)) { $post_style = 'mini'; } $limit = max(0, min(12, (int) ($instance['limit'] ?? 6))); $orderby = sanitize_key((string) ($instance['orderby'] ?? 'date')); // Fixed user id wins; else current user. $user_id = !empty($instance['user_id']) ? (int) $instance['user_id'] : 0; if ($user_id <= 0 && is_user_logged_in()) { $user_id = get_current_user_id(); } 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 } if ($affix['class'] !== '') { echo '
'; } if ($user_id <= 0) { $this->render_guest($show_cover, $loged_title, $this->get_guest_cover_url($instance)); if ($affix['class'] !== '') { echo '
'; } echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped return; } $user = get_userdata($user_id); if (!$user) { if ($affix['class'] !== '') { echo ''; } echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped return; } $is_self = is_user_logged_in() && (int) get_current_user_id() === $user_id; $avatar_url = function_exists('aksrt_get_avatar_url') ? aksrt_get_avatar_url($user_id, ['size' => 144]) : get_avatar_url($user_id, ['size' => 144]); $profile_url = function_exists('aksrt_get_user_home_url') ? aksrt_get_user_home_url($user_id) : get_author_posts_url($user_id); $center_url = function_exists('aksrt_get_user_center_url') ? aksrt_get_user_center_url() : $profile_url; // Zibll button_2 type: home → profile, center → user center. $btn2_url = $btn2_type === 'home' ? $profile_url : $center_url; $compose_url = home_url('/'); if (function_exists('get_pages')) { $pages = get_pages(['meta_key' => '_wp_page_template', 'meta_value' => 'templates/pages/page-submit.php', 'number' => 1]); if (!empty($pages[0])) { $compose_url = get_permalink($pages[0]); } } if ($compose_url === home_url('/') && function_exists('aksrt_get_user_center_url')) { $compose_url = aksrt_get_user_center_url('posts'); } $bio = get_user_meta($user_id, 'description', true); if (!is_string($bio) || $bio === '') { $bio = (string) $user->description; } $cover = (string) get_user_meta($user_id, 'aksrt_background_image', true); if ($cover === '') { $cover = $this->get_default_cover_url(); } $post_count = count_user_posts($user_id, 'post', true); $follower_count = 0; $following_count = 0; if (class_exists('\AKSRT\User\Follow')) { $follower_count = \AKSRT\User\Follow::get_instance()->get_follower_count($user_id); $following_count = \AKSRT\User\Follow::get_instance()->get_following_count($user_id); } $name_html = function_exists('aksrt_get_user_name') ? aksrt_get_user_name([ 'id' => $user_id, 'vip' => true, 'follow' => !$is_self, 'class' => 'aksrt-user-card__name-row', ]) : '' . esc_html($user->display_name) . ''; $checkin_btn = ''; if ($show_checkin && $is_self && function_exists('aksrt_header_user_checkin_btn')) { $checkin_btn = aksrt_header_user_checkin_btn($user_id, true); } $checkin_html = $checkin_btn !== '' ? '
' . $checkin_btn . '
' : ''; echo '
'; if ($show_cover) { echo '
' . $checkin_html . '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } elseif ($checkin_btn !== '') { echo '
' . $checkin_btn . '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } echo '
'; echo '
'; echo '
'; $ph = get_template_directory_uri() . '/assets/img/placeholder.svg'; echo '' . esc_attr($user->display_name) . ''; echo '
'; echo '
'; echo '

' . $name_html . '

'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped if ($bio !== '') { echo '

' . esc_html(wp_trim_words($bio, 24)) . '

'; } if ($show_stats) { echo '
'; echo '
' . esc_html(number_format_i18n($post_count)) . '' . esc_html__('文章', 'aksrt') . '
'; echo '
' . esc_html(number_format_i18n($follower_count)) . '' . esc_html__('粉丝', 'aksrt') . '
'; echo '
' . esc_html(number_format_i18n($following_count)) . '' . esc_html__('关注', 'aksrt') . '
'; echo '
'; } if ($show_actions && $is_self) { echo ''; } elseif ($show_actions) { echo ''; } 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' => $user_id, 'limit' => $limit, 'orderby' => $orderby, 'post_style' => $post_style, 'post_type' => 'post', 'show_empty' => false, ]); } echo '
'; if ($affix['class'] !== '') { echo ''; } echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } private function render_guest(bool $show_cover, string $loged_title, string $cover): void { $login = function_exists('aksrt_get_login_url') ? aksrt_get_login_url() : wp_login_url(); $register = function_exists('aksrt_get_register_url') ? aksrt_get_register_url() : wp_registration_url(); $default_avatar = function_exists('get_avatar_url') ? get_avatar_url(0, ['size' => 144, 'force_default' => true]) : includes_url('images/blank.gif'); $oauth_html = ''; if (class_exists('\AKSRT\Auth\OAuth')) { $oauth = \AKSRT\Auth\OAuth::get_instance(); if (method_exists($oauth, 'render_social_buttons')) { $oauth_html = (string) $oauth->render_social_buttons(__('社交账号登录', 'aksrt')); } elseif (method_exists($oauth, 'get_login_buttons')) { $oauth_html = (string) $oauth->get_login_buttons(); } } echo '
'; if ($show_cover) { echo '
'; } echo '
'; echo '
'; echo '
' . esc_html($loged_title) . '
'; echo ''; if ($oauth_html !== '') { echo ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } echo '
'; } public function form($instance): void { $post_style = sanitize_key((string) ($instance['post_style'] ?? 'mini')); if (!in_array($post_style, ['card', 'mini'], true)) { $post_style = 'mini'; } ?>





sanitize_text_field((string) ($new_instance['title'] ?? '')), 'user_id' => max(0, (int) ($new_instance['user_id'] ?? 0)), 'loged_title' => sanitize_text_field((string) ($new_instance['loged_title'] ?? '')), 'guest_cover' => esc_url_raw(trim((string) ($new_instance['guest_cover'] ?? ''))), 'button_1' => 'post', 'button_1_text' => sanitize_text_field((string) ($new_instance['button_1_text'] ?? '')), 'button_1_class'=> self::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'=> self::sanitize_palette((string) ($new_instance['button_2_class'] ?? 'jb-blue'), 'jb-blue'), 'show_img_bg' => !empty($new_instance['show_img_bg']) ? 1 : 0, 'show_stats' => !empty($new_instance['show_stats']) ? 1 : 0, 'show_button' => !empty($new_instance['show_button']) ? 1 : 0, 'show_checkin' => !empty($new_instance['show_checkin']) ? 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, ]; } private function get_default_cover_url(): string { return get_theme_file_uri(self::DEFAULT_COVER_FILE); } private function get_guest_cover_url(array $instance): string { $cover = esc_url_raw(trim((string) ($instance['guest_cover'] ?? ''))); return $cover !== '' ? $cover : $this->get_default_cover_url(); } /** Zibll CFS_Module::zib_palette() gradient/solid keys (c/b/jb families). */ public static function palette_opts(): array { return [ 'jb-red' => __('红', 'aksrt'), 'jb-yellow' => __('橙', 'aksrt'), 'jb-pink' => __('粉', 'aksrt'), 'jb-cyan' => __('青', 'aksrt'), 'jb-blue' => __('蓝', 'aksrt'), 'jb-green' => __('绿', 'aksrt'), 'jb-purple' => __('紫', 'aksrt'), 'jb-vip1' => __('会员金', 'aksrt'), 'jb-vip2' => __('会员灰', 'aksrt'), 'b-red' => __('红(纯色)', 'aksrt'), 'b-yellow' => __('橙(纯色)', 'aksrt'), 'b-cyan' => __('青(纯色)', 'aksrt'), 'b-blue' => __('蓝(纯色)', 'aksrt'), 'b-green' => __('绿(纯色)', 'aksrt'), 'b-purple' => __('紫(纯色)', 'aksrt'), 'b-black' => __('黑(纯色)', 'aksrt'), ]; } public static function sanitize_palette(string $value, string $fallback): string { $value = sanitize_html_class($value); return array_key_exists($value, self::palette_opts()) ? $value : $fallback; } }