Files
wordpress-theme-aksrt/inc/helpers/aksrt-header-userbox.php
T

192 lines
6.8 KiB
PHP

<?php
/**
* Header user-box density helpers (zibll zib_header_user_box subset).
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
if (!function_exists('aksrt_header_user_checkin_btn')) :
/**
* Check-in CTA for header dropdown (uses REST /aksrt/v1/signin).
*/
function aksrt_header_user_checkin_btn(int $user_id = 0): string {
$user_id = $user_id > 0 ? $user_id : get_current_user_id();
if ($user_id <= 0 || !class_exists('\AKSRT\Credits\Signin')) {
return '';
}
$cfg = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_signin_reward_settings()
: ['enabled' => false];
if (empty($cfg['enabled'])) {
return '';
}
// Lightweight today status via private API if available.
$signed = false;
try {
$signin = \AKSRT\Credits\Signin::get_instance();
// Prefer public method if present.
if (method_exists($signin, 'is_signed_today')) {
$signed = (bool) $signin->is_signed_today($user_id);
} else {
// Fallback: direct meta/table probe via rest_today logic is heavy; use option day key.
$day = wp_date('Y-m-d');
$signed = (bool) get_user_meta($user_id, 'aksrt_signin_' . $day, true);
}
} catch (\Throwable $e) {
$signed = false;
}
$details = '';
if (function_exists('aksrt_get_remote_modal_link')) {
$details = aksrt_get_remote_modal_link([
'tag' => 'a',
'class' => 'aksrt-header-checkin-details muted-2-color em09 mt6 block text-center',
'text' => __('签到详情', 'aksrt'),
'query_arg' => ['action' => 'aksrt_checkin_details_modal'],
'data_class' => 'modal-mini full-sm',
'height' => 420,
'mobile_bottom' => true,
]);
}
if ($signed) {
return '<button type="button" class="but block c-yellow aksrt-header-checkin is-done" disabled>'
. '<i class="fas fa-calendar-check" aria-hidden="true"></i> '
. esc_html__('今日已签到', 'aksrt')
. '</button>'
. $details;
}
return '<button type="button" class="but block c-yellow aksrt-header-checkin" data-aksrt-checkin="1">'
. '<i class="fas fa-calendar-check" aria-hidden="true"></i> '
. esc_html__('签到领取今日奖励', 'aksrt')
. '</button>'
. $details;
}
endif;
if (!function_exists('aksrt_header_user_wallet_mini')) :
/**
* Mini wallet strip: credits + cash balance.
*/
function aksrt_header_user_wallet_mini(int $user_id = 0): string {
$user_id = $user_id > 0 ? $user_id : get_current_user_id();
if ($user_id <= 0) {
return '';
}
$credits = function_exists('aksrt_user_credits') ? (int) aksrt_user_credits($user_id) : 0;
$currency = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_currency_name()
: __('积分', 'aksrt');
$wallet = 0.0;
if (class_exists('\AKSRT\Credits\Balance')) {
$wallet = (float) \AKSRT\Credits\Balance::balance($user_id);
}
$center = function_exists('aksrt_get_user_center_url') ? aksrt_get_user_center_url('credits') : '#';
$html = '<a class="aksrt-header-wallet flex ac jsb" href="' . esc_url($center) . '">';
$html .= '<span class="aksrt-header-wallet__item"><i class="fas fa-coins" aria-hidden="true"></i> '
. esc_html($currency) . ' <b>' . esc_html(number_format_i18n($credits)) . '</b></span>';
$html .= '<span class="aksrt-header-wallet__item"><i class="fas fa-wallet" aria-hidden="true"></i> '
. esc_html__('余额', 'aksrt') . ' <b>' . esc_html(number_format_i18n($wallet, 2)) . '</b></span>';
$html .= '</a>';
return $html;
}
endif;
if (!function_exists('aksrt_header_user_badges_row')) :
/**
* Identity / medal chips under profile.
*/
function aksrt_header_user_badges_row(int $user_id = 0): string {
$user_id = $user_id > 0 ? $user_id : get_current_user_id();
if ($user_id <= 0 || !function_exists('aksrt_render_user_identity_chips')) {
return '';
}
$chips = aksrt_render_user_identity_chips($user_id, 'card');
if ($chips === '') {
return '';
}
return '<div class="em09 author-tag mb10 mt6 flex jc aksrt-header-badges">' . $chips . '</div>';
}
endif;
if (!function_exists('aksrt_header_payvip_bar')) :
/**
* VIP status / open CTA bar (zibll zib_get_header_payvip_icon parity).
*/
function aksrt_header_payvip_bar(int $user_id = 0): string {
$user_id = $user_id > 0 ? $user_id : get_current_user_id();
if ($user_id <= 0) {
return '';
}
$vip = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_payment_vip_settings()
: ['enabled' => false, 'levels' => []];
if (empty($vip['enabled'])) {
return '';
}
$level = function_exists('aksrt_get_user_vip_level') ? (int) aksrt_get_user_vip_level($user_id) : 0;
if ($level > 0) {
$levels = is_array($vip['levels'] ?? null) ? $vip['levels'] : [];
$name = (string) ($levels[$level]['name'] ?? ($level === 2 ? __('二级会员', 'aksrt') : __('一级会员', 'aksrt')));
$exp_label = '';
if (function_exists('aksrt_get_vip_info')) {
$info = aksrt_get_vip_info($user_id);
$exp_label = (string) ($info['expires_label'] ?? '');
}
$html = '<div class="mt10 em09 aksrt-header-payvip">';
$html .= '<span class="radius4 payvip-icon btn-block text-center vipbg-v2 level-' . (int) $level . '">';
$html .= '<i class="fas fa-gem" aria-hidden="true"></i>';
$html .= '<span>' . esc_html($name) . '</span>';
if ($exp_label !== '') {
$html .= '<span class="ml10 badg jb-yellow vip-expdate-tag"><i class="fas fa-clock" aria-hidden="true"></i>'
. esc_html($exp_label) . '</span>';
}
$html .= '</span></div>';
return $html;
}
return '<div class="mt10 em09 aksrt-header-payvip">'
. '<a class="pay-vip but radius4 payvip-icon btn-block navbar-cta" href="javascript:;" vip-level="1">'
. '<i class="fas fa-gem" aria-hidden="true"></i>'
. esc_html__('开通会员 尊享会员权益', 'aksrt')
. '</a></div>';
}
endif;
if (!function_exists('aksrt_render_signout_modal')) :
/**
* Confirm logout modal markup (once per page).
*/
function aksrt_render_signout_modal(): void {
static $done = false;
if ($done || !is_user_logged_in()) {
return;
}
$done = true;
$logout = wp_logout_url(home_url('/'));
?>
<div class="modal fade" id="modal_signout" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-mini" role="document">
<div class="modal-content">
<div class="modal-body text-center" style="padding:24px;">
<div class="em2x mb10"><i class="fa fa-sign-out" aria-hidden="true"></i></div>
<div class="mb15"><?php esc_html_e('确认退出当前账号?', 'aksrt'); ?></div>
<div class="but-average flex jc" style="gap:10px;">
<button type="button" class="but" data-dismiss="modal"><?php esc_html_e('取消', 'aksrt'); ?></button>
<a class="but c-red" href="<?php echo esc_url($logout); ?>"><?php esc_html_e('确认退出', 'aksrt'); ?></a>
</div>
</div>
</div>
</div>
</div>
<?php
}
endif;