*/ function aksrt_normalize_user_contacts($raw_contacts): array { $contacts = []; if (!is_array($raw_contacts)) { return $contacts; } foreach ($raw_contacts as $contact) { if (!is_array($contact)) { continue; } $url = isset($contact['url']) ? esc_url_raw(wp_unslash($contact['url'])) : ''; $icon = isset($contact['icon']) ? sanitize_key(wp_unslash($contact['icon'])) : ''; $icon = preg_replace('/^(fab|fas|far)[\s-]*/', '', $icon); $icon = preg_replace('/^fa[\s-]*/', '', (string) $icon); $icon = trim(preg_replace('/^[\s-]+/', '', (string) $icon), '- '); if (!$url || !$icon) { continue; } $contacts[] = [ 'name' => aksrt_icon_key_to_name($icon), 'url' => $url, 'icon' => $icon, ]; } return $contacts; } } if (!function_exists('aksrt_save_user_center_profile')) { /** * Save the user-center profile form for both AJAX and no-JS requests. * * @param int $user_id User being updated. * @param array $input Untrusted request data. * @return true|WP_Error */ function aksrt_save_user_center_profile(int $user_id, array $input) { if (!$user_id || $user_id !== get_current_user_id()) { return new WP_Error('aksrt_profile_forbidden', __('无权操作', 'aksrt')); } $errors = []; $update = ['ID' => $user_id]; if (isset($input['nickname'])) { $name = sanitize_text_field(wp_unslash($input['nickname'])); if (mb_strlen($name) < 1 || mb_strlen($name) > 50) { $errors[] = __('昵称长度需在 1-50 字之间', 'aksrt'); } else { $update['display_name'] = $name; } } if (isset($input['description'])) { $description = sanitize_textarea_field(wp_unslash($input['description'])); if (mb_strlen($description) > 500) { $errors[] = __('个人简介不能超过 500 字', 'aksrt'); } else { $update['description'] = $description; } } if ($errors) { return new WP_Error('aksrt_profile_invalid', implode('
', $errors)); } if (count($update) > 1) { $result = wp_update_user($update); if (is_wp_error($result)) { return $result; } } update_user_meta($user_id, 'aksrt_user_contacts', aksrt_normalize_user_contacts($input['contacts'] ?? [])); return true; } } if (!function_exists('aksrt_get_user_center_tab_url')) { /** * Build a canonical full-page user-center tab URL. */ function aksrt_get_user_center_tab_url(string $tab): string { $url = remove_query_arg([ 'tab', 'posts_page', 'comments_page', 'favorites_page', 'credits_page', 'post_status', 'saved', 'profile_error', ], aksrt_get_user_center_url()); return add_query_arg('tab', sanitize_key($tab), $url); } } if (!function_exists('aksrt_normalize_user_center_tabs')) { /** * Normalize the dashboard tab contract. */ function aksrt_normalize_user_center_tabs(array $tabs): array { $normalized = []; foreach ($tabs as $key => $tab) { $key = sanitize_key((string) $key); if (!$key || !is_array($tab)) { continue; } $normalized[$key] = [ 'label' => (string) ($tab['label'] ?? $key), 'icon' => sanitize_html_class((string) ($tab['icon'] ?? 'circle')), 'template' => isset($tab['template']) ? (string) $tab['template'] : '', 'context_callback' => $tab['context_callback'] ?? null, ]; } return $normalized; } } if (!function_exists('aksrt_capture_user_center_view')) { /** * Render an internal user-center view into an inert HTML payload. */ function aksrt_capture_user_center_view(string $view, array $context): string { $path = AKSRT_DIR . '/inc/helpers/user-center-renderers/profile-' . sanitize_file_name($view) . '.php'; if (!is_file($path)) { return ''; } ob_start(); $args = ['context' => $context]; require $path; return (string) ob_get_clean(); } } if (!function_exists('aksrt_capture_user_center_panel')) { /** * Capture a built-in or registered user-center panel body. */ function aksrt_capture_user_center_panel(string $tab_key, array $tab, array $context): string { $built_in_tabs = ['profile', 'posts', 'comments', 'favorites', 'credits', 'settings']; if (in_array($tab_key, $built_in_tabs, true)) { return aksrt_capture_user_center_view($tab_key, $context); } $panel_context = $context; if (is_callable($tab['context_callback'] ?? null)) { $custom_context = call_user_func($tab['context_callback'], $context, $tab_key); if (is_array($custom_context)) { $panel_context = array_merge($panel_context, $custom_context); } } $template = ltrim((string) ($tab['template'] ?? ''), '/'); if ($template === '') { return ''; } $template_path = locate_template([$template . '.php', $template], false, false); if (!$template_path) { return ''; } ob_start(); load_template($template_path, false, ['context' => $panel_context]); return (string) ob_get_clean(); } } if (!function_exists('aksrt_build_user_center_render_context')) { /** * Assemble all runtime data before template parts are entered. */ function aksrt_build_user_center_render_context(array $context): array { $active_tab = (string) ($context['active_tab'] ?? 'profile'); $context['header_html'] = aksrt_capture_user_center_view('header', $context); $context['sidebar_html'] = aksrt_capture_user_center_view('sidebar', $context); $context['mobile_tabs'] = []; foreach ((array) ($context['dashboard_tabs'] ?? []) as $tab_key => $tab) { $context['mobile_tabs'][] = [ 'key' => (string) $tab_key, 'label' => (string) ($tab['label'] ?? $tab_key), 'url' => aksrt_get_user_center_tab_url((string) $tab_key), 'is_active' => $active_tab === $tab_key, ]; } $context['panels'] = []; foreach ((array) ($context['dashboard_tabs'] ?? []) as $tab_key => $tab) { $context['panels'][$tab_key] = [ 'key' => (string) $tab_key, 'url' => aksrt_get_user_center_tab_url((string) $tab_key), 'is_active' => $active_tab === $tab_key, 'is_loaded' => $active_tab === $tab_key, 'html' => $active_tab === $tab_key ? aksrt_capture_user_center_panel((string) $tab_key, (array) $tab, $context) : '', ]; } $context['account_modals_html'] = 'settings' === $active_tab ? aksrt_capture_user_center_view('account-modals', $context) : ''; return $context; } } if (!function_exists('aksrt_get_user_center_render_context')) { /** * Resolve the current user's complete render context outside template files. */ function aksrt_get_user_center_render_context(WP_User $current_user): array { $user_id = (int) $current_user->ID; $user_center_settings = class_exists('AKSRT_Settings') ? AKSRT_Settings::get_user_center_settings() : [ 'tabs' => ['profile', 'posts', 'comments', 'favorites'], 'avatar_upload' => true, 'cover_upload' => true, 'social_links' => true, ]; $dashboard_tabs = class_exists('AKSRT_User_Center') ? AKSRT_User_Center::get_instance()->get_dashboard_tabs() : [ 'profile' => ['label' => __('个人信息', 'aksrt'), 'icon' => 'user'], 'posts' => ['label' => __('我的文章', 'aksrt'), 'icon' => 'file-alt'], 'comments' => ['label' => __('我的评论', 'aksrt'), 'icon' => 'comment'], 'favorites' => ['label' => __('我的收藏', 'aksrt'), 'icon' => 'star'], 'settings' => ['label' => __('账号设置', 'aksrt'), 'icon' => 'cog'], ]; $dashboard_tabs = aksrt_normalize_user_center_tabs($dashboard_tabs); $dashboard_tab_keys = array_keys($dashboard_tabs); $default_dashboard_tab = in_array('profile', $dashboard_tab_keys, true) ? 'profile' : ($dashboard_tab_keys[0] ?? 'profile'); $active_tab = isset($_GET['tab']) ? sanitize_key(wp_unslash($_GET['tab'])) : $default_dashboard_tab; if ('signin' === $active_tab) { $active_tab = 'credits'; } elseif ('invite' === $active_tab) { $active_tab = 'settings'; } if (!in_array($active_tab, $dashboard_tab_keys, true)) { $active_tab = $default_dashboard_tab; } $dashboard = function_exists('aksrt_get_user_dashboard_view') ? aksrt_get_user_dashboard_view($user_id, [ 'viewer_id' => $user_id, 'include_recent_posts' => false, 'include_governance' => false, ]) : []; $target_user = $current_user; $post_count = (int) ($dashboard['post_count'] ?? count_user_posts($user_id, 'post', true)); $comment_count = (int) ($dashboard['comment_count'] ?? get_comments(['user_id' => $user_id, 'count' => true, 'status' => 'approve'])); $favorite_post_ids = $dashboard['favorite_post_ids'] ?? []; $favorite_count = (int) ($dashboard['favorite_count'] ?? count($favorite_post_ids)); $user_contacts = $dashboard['user_contacts'] ?? get_user_meta($user_id, 'aksrt_user_contacts', true); $user_contacts = is_array($user_contacts) ? $user_contacts : []; $profile_signals = $dashboard['profile_signals'] ?? []; $user_background_url = (string) ($dashboard['background_url'] ?? get_user_meta($user_id, 'aksrt_background_image', true)); $allow_bg_upload = !empty($user_center_settings['cover_upload']) && class_exists('AKSRT_Settings') && aksrt_get_setting('allow_background_upload', false); $show_avatar_edit = !empty($user_center_settings['avatar_upload']); $avatar_settings_url = admin_url('profile.php'); $notification_unread_count = function_exists('aksrt_get_user_unread_notification_count') ? aksrt_get_user_unread_notification_count($user_id) : 0; $messages_url = function_exists('aksrt_get_messages_url') ? aksrt_get_messages_url() : aksrt_get_user_center_url('notif'); $per_page = 10; $posts_page = max(1, isset($_GET['posts_page']) ? (int) $_GET['posts_page'] : 1); $comments_page = max(1, isset($_GET['comments_page']) ? (int) $_GET['comments_page'] : 1); $favorites_page = max(1, isset($_GET['favorites_page']) ? (int) $_GET['favorites_page'] : 1); $post_status_filter = isset($_GET['post_status']) ? sanitize_key(wp_unslash($_GET['post_status'])) : 'publish'; if (!in_array($post_status_filter, ['all', 'publish', 'draft', 'pending', 'private'], true)) { $post_status_filter = 'publish'; } $user_status_counts = ['publish' => 0, 'draft' => 0, 'pending' => 0, 'private' => 0]; if ('posts' === $active_tab) { global $wpdb; $rows = $wpdb->get_results($wpdb->prepare( "SELECT post_status, COUNT(*) AS num FROM {$wpdb->posts} WHERE post_author = %d AND post_type = 'post' AND post_status IN ('publish','draft','pending','private') GROUP BY post_status", $user_id )); foreach ((array) $rows as $row) { $user_status_counts[$row->post_status] = (int) $row->num; } $post_count = (int) array_sum($user_status_counts); } $svg_empty_post = ''; $svg_empty_comment = ''; $svg_empty_favorite = ''; require_once AKSRT_DIR . '/templates/parts/user-shared-helpers.php'; $vip_settings = class_exists('AKSRT_Settings') ? AKSRT_Settings::get_payment_vip_settings() : []; $affiliate_settings = class_exists('AKSRT_Settings') ? AKSRT_Settings::get_payment_affiliate_settings() : []; $orders_url = function_exists('aksrt_get_orders_page_url') ? aksrt_get_orders_page_url() : ''; $show_vip = !empty($vip_settings['enabled']); $show_credit_tabs = (bool) ($dashboard['credits_enabled'] ?? false); $affiliate_enabled = $show_credit_tabs && !empty($affiliate_settings['enabled']); $currency_name = (string) ($dashboard['currency_name'] ?? __('积分', 'aksrt')); $tab_urls = []; foreach ($dashboard_tab_keys as $tab_key) { $tab_urls[$tab_key] = aksrt_get_user_center_tab_url($tab_key); } $render_context = [ 'user_center_settings' => $user_center_settings, 'dashboard_tabs' => $dashboard_tabs, 'dashboard_tab_keys' => $dashboard_tab_keys, 'default_dashboard_tab' => $default_dashboard_tab, 'active_tab' => $active_tab, 'dashboard' => $dashboard, 'target_user' => $target_user, 'user_id' => $user_id, 'post_count' => $post_count, 'comment_count' => $comment_count, 'favorite_post_ids' => $favorite_post_ids, 'favorite_count' => $favorite_count, 'user_contacts' => $user_contacts, 'profile_signals' => $profile_signals, 'user_background_url' => $user_background_url, 'allow_bg_upload' => $allow_bg_upload, 'show_avatar_edit' => $show_avatar_edit, 'avatar_settings_url' => $avatar_settings_url, 'notification_unread_count' => $notification_unread_count, 'messages_url' => $messages_url, 'per_page' => $per_page, 'posts_page' => $posts_page, 'comments_page' => $comments_page, 'favorites_page' => $favorites_page, 'post_status_filter' => $post_status_filter, 'user_status_counts' => $user_status_counts, 'svg_empty_post' => $svg_empty_post, 'svg_empty_comment' => $svg_empty_comment, 'svg_empty_favorite' => $svg_empty_favorite, 'orders_url' => $orders_url, 'show_vip' => $show_vip, 'show_credit_tabs' => $show_credit_tabs, 'affiliate_enabled' => $affiliate_enabled, 'currency_name' => $currency_name, 'credit_balance' => (float) ($dashboard['credit_balance'] ?? 0.0), 'credits_enabled' => $show_credit_tabs, 'vip_settings' => $vip_settings, 'show_invite_section' => $show_credit_tabs && class_exists('AKSRT_Settings') && AKSRT_Settings::invite_enabled() && AKSRT_Settings::invite_registration_enabled(), 'user_email' => (string) $current_user->user_email, ]; $render_context['runtime_config'] = [ 'ajaxUrl' => admin_url('admin-ajax.php'), 'publicNonce' => wp_create_nonce('aksrt_public_nonce'), 'settingsNonce' => wp_create_nonce('aksrt_account_settings'), 'userId' => $user_id, 'defaultTab' => $default_dashboard_tab, 'activeTab' => $active_tab, 'tabAliases' => ['signin' => 'credits', 'invite' => 'settings'], 'tabUrls' => $tab_urls, 'i18n' => [ 'contactUrlPlaceholder' => __('链接 URL', 'aksrt'), 'deleteLabel' => __('删除', 'aksrt'), 'saving' => __('保存中…', 'aksrt'), 'saveBtn' => __('保存修改', 'aksrt'), 'saved' => __('已保存', 'aksrt'), 'saveFailed' => __('保存失败', 'aksrt'), 'networkError' => __('网络错误,请重试', 'aksrt'), 'confirmUnfavorite' => __('确定取消收藏这篇文章?', 'aksrt'), 'unfavorited' => __('已取消收藏', 'aksrt'), 'operationFailed' => __('操作失败', 'aksrt'), 'confirmUnbind' => __('确定解除绑定?', 'aksrt'), 'unbinding' => __('解除中...', 'aksrt'), 'unbindSuccess' => __('解除成功', 'aksrt'), 'unbindFailed' => __('解除失败', 'aksrt'), 'unbind' => __('解除', 'aksrt'), ], ]; $render_context['cover_upload_config'] = $allow_bg_upload ? [ 'enabled' => true, 'restUrl' => rest_url('aksrt/v1/user/background'), 'restNonce' => wp_create_nonce('wp_rest'), 'buttonId' => 'aksrt-upload-bg-btn', 'inputId' => 'aksrt-bg-file-input', 'imageId' => 'aksrt-cover-img', 'fieldName' => 'background', 'maxSize' => 2097152, 'fallbackImage' => AKSRT_URI . '/assets/img/person_background.png', 'idleText' => __('编辑', 'aksrt'), 'loadingText' => __('上传中…', 'aksrt'), 'tooLargeText' => __('图片大小不能超过 2MB', 'aksrt'), 'successText' => __('背景图已更新', 'aksrt'), 'errorText' => __('上传失败', 'aksrt'), 'networkErrorText' => __('网络错误,请重试', 'aksrt'), ] : []; $render_context = aksrt_build_user_center_render_context($render_context); return $render_context; } }