From 8a23a3d5264d00d459901646e29de66fe6effab4 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 15 Aug 2026 16:18:07 +0800 Subject: [PATCH] feat(widget): add custom user card button links --- src/Widgets/WidgetUserCard.php | 129 +++++++++++---------------------- 1 file changed, 43 insertions(+), 86 deletions(-) diff --git a/src/Widgets/WidgetUserCard.php b/src/Widgets/WidgetUserCard.php index c60891e0..3cc48fb4 100644 --- a/src/Widgets/WidgetUserCard.php +++ b/src/Widgets/WidgetUserCard.php @@ -28,24 +28,15 @@ class WidgetUserCard extends \WP_Widget { $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')); + $btn1_url = (string) ($instance['button_1_url'] ?? ''); + $btn2_url = (string) ($instance['button_2_url'] ?? ''); // 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; @@ -79,6 +70,8 @@ class WidgetUserCard extends \WP_Widget { echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped return; } + $btn1_url = esc_url($btn1_url !== '' ? $btn1_url : $this->get_default_compose_url()); + $btn2_url = esc_url($btn2_url !== '' ? $btn2_url : $this->get_default_center_url()); $is_self = is_user_logged_in() && (int) get_current_user_id() === $user_id; $avatar_url = function_exists('aksrt_get_avatar_url') @@ -87,21 +80,6 @@ class WidgetUserCard extends \WP_Widget { $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; @@ -161,24 +139,13 @@ class WidgetUserCard extends \WP_Widget { } if ($show_actions && $is_self) { echo '
'; - echo '' . esc_html($btn1_text) . ''; - echo '' . esc_html($btn2_text) . ''; + echo '' . esc_html($btn1_text) . ''; + echo '' . esc_html($btn2_text) . ''; echo '
'; } elseif ($show_actions) { echo '
' . esc_html__('查看主页', 'aksrt') . '
'; } - 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'] !== '') { @@ -224,10 +191,6 @@ class WidgetUserCard extends \WP_Widget { } 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'; - } ?>

-

- -

-

- -

-

- +

get_default_compose_url(); } - $orderby = sanitize_key((string) ($new_instance['orderby'] ?? 'date')); - if (!in_array($orderby, ['date', 'views', 'comment_count', 'modified', 'rand'], true)) { - $orderby = 'date'; + if ($button_2_url === '') { + $button_2_url = $this->get_default_center_url(); } + return [ 'title' => 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_url' => $button_1_url, '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_url' => $button_2_url, '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_center_url(): string { + return function_exists('aksrt_get_user_center_url') ? aksrt_get_user_center_url() : home_url('/'); + } + + private function get_default_compose_url(): string { + $compose_url = home_url('/'); + $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'); + } + + return $compose_url; + } + private function get_default_cover_url(): string { return get_theme_file_uri(self::DEFAULT_COVER_FILE); }