Files
wordpress-theme-aksrt/inc/helpers/aksrt-user-dashboard.php
T
kate522 8e0f9d989d feat(user-center): add cash wallet balance separate from credits
Introduce AKSRT_Balance (user_meta aksrt_balance + balance_log) for
Zibll-like pay_balance wallet. User center assets panel shows wallet
and points; balance gateway/card-pass/refunds credit cash when enabled.
2026-07-16 13:44:37 +08:00

115 lines
4.9 KiB
PHP

<?php
/**
* User Dashboard Data Helper
*
* Aggregates user data for author pages and public user homepages.
* Shared between author.php and page-user-home.php to avoid duplicate queries.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
/**
* Get aggregated dashboard view data for a user.
*
* @param int $user_id Target user ID.
* @param array $args Optional. Supported keys:
* - viewer_id: Current viewer ID.
* - include_recent_posts: Whether to query recent posts.
* - include_governance: Whether to resolve notice/report data.
* @return array {
* @type int $post_count Published post count.
* @type int $comment_count Approved comment count.
* @type int $favorite_count Favorited post count.
* @type array $favorite_post_ids Array of favorited post IDs.
* @type array $user_contacts Social/contact links from user meta.
* @type array $profile_signals Identity chips (verified, level, badges).
* @type array $profile_notice Ban/restriction notice, if any.
* @type string $background_url Cover image URL.
* @type float $credit_balance Credit balance (own profile only).
* @type string $currency_name Currency/credits display name.
* @type bool $credits_enabled Whether credits system is active.
* @type bool $is_own_profile Whether viewer is the user themselves.
* }
*/
function aksrt_get_user_dashboard_view(int $user_id, array $args = []): array {
$viewer_id = (int) ($args['viewer_id'] ?? get_current_user_id());
$include_recent_posts = !array_key_exists('include_recent_posts', $args) || (bool) $args['include_recent_posts'];
$include_governance = !array_key_exists('include_governance', $args) || (bool) $args['include_governance'];
$is_own_profile = is_user_logged_in() && $viewer_id === $user_id;
$post_count = (int) count_user_posts($user_id, 'post', true);
$comment_count = (int) get_comments([
'user_id' => $user_id,
'count' => true,
'status' => 'approve',
]);
$favorite_post_ids = function_exists('aksrt_get_user_interaction_posts')
? aksrt_get_user_interaction_posts('_aksrt_favorite_posts', $user_id)
: [];
$favorite_count = count($favorite_post_ids);
$recent_posts = $include_recent_posts
? get_posts([
'author' => $user_id,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC',
'no_found_rows' => true,
])
: [];
$user_contacts = get_user_meta($user_id, 'aksrt_user_contacts', true) ?: [];
if (!is_array($user_contacts)) {
$user_contacts = [];
}
$user_center_settings = class_exists('AKSRT_Settings')
? AKSRT_Settings::get_user_center_settings()
: ['social_links' => true];
$background_url = get_user_meta($user_id, 'aksrt_background_image', true) ?: '';
$credits_enabled = class_exists('AKSRT_Settings') && AKSRT_Settings::credits_enabled();
$credit_balance = $credits_enabled && $is_own_profile && function_exists('aksrt_user_credits')
? (float) aksrt_user_credits($user_id)
: 0.0;
$currency_name = $credits_enabled && class_exists('AKSRT_Settings')
? AKSRT_Settings::get_currency_name()
: __('积分', 'aksrt');
// Cash wallet (Zibll pay_balance) — separate from points.
$balance_enabled = $is_own_profile && class_exists('AKSRT_Balance') && AKSRT_Balance::enabled();
$wallet_balance = $balance_enabled ? AKSRT_Balance::balance($user_id) : 0.0;
$user_home_manager = $include_governance && class_exists('AKSRT_User_Home')
? AKSRT_User_Home::get_instance()
: null;
$profile_signals = $user_home_manager ? $user_home_manager->get_profile_signals($user_id) : [];
$profile_notice = $user_home_manager ? $user_home_manager->get_profile_notice($user_id) : [];
$show_report = $user_home_manager ? $user_home_manager->show_report_entry() : false;
return [
'post_count' => $post_count,
'comment_count' => $comment_count,
'favorite_count' => $favorite_count,
'favorite_post_ids'=> $favorite_post_ids,
'recent_posts' => $recent_posts,
'user_contacts' => $user_contacts,
'social_links_on' => !empty($user_center_settings['social_links']),
'profile_signals' => $profile_signals,
'profile_notice' => $profile_notice,
'show_report' => $show_report,
'background_url' => $background_url,
'credit_balance' => $credit_balance,
'currency_name' => $currency_name,
'credits_enabled' => $credits_enabled,
'balance_enabled' => $balance_enabled,
'wallet_balance' => $wallet_balance,
'is_own_profile' => $is_own_profile,
];
}