fix(pay): unify download gate on cashier unlock not per-download credits

Route can_download/Download_State/permission card through Content_Visibility need_purchase; drop download_credit as a second toll; clarify admin labels for points purchase mode.
This commit is contained in:
2026-07-14 20:38:17 +08:00
parent 1039b26efd
commit 79f8c43970
5 changed files with 311 additions and 291 deletions
+11 -8
View File
@@ -2,7 +2,7 @@
<div class="aksrt-page-container">
<PageHeader
title="下载/付费内容中心"
desc="统一管理资源下载门槛、默认扣费、角色免扣和人工交付说明。运行中的下载 token 流程保持不变。"
desc="统一管理资源下载门槛、积分购买模式开关、角色免扣和人工交付说明。付费解锁走收银台订单,下载 token 不再按次扣费。"
>
<template #actions>
<n-space>
@@ -22,16 +22,16 @@
登录门槛:{{ form.download_login ? '已启用' : '已关闭' }}
</n-tag>
<n-tag round :type="form.download_credit ? 'warning' : 'default'">
积分扣费:{{ form.download_credit ? '已启用' : '已关闭' }}
积分购买模式:{{ form.download_credit ? '已启用' : '已关闭' }}
</n-tag>
<n-tag round :type="form.download_captcha ? 'warning' : 'default'">
下载验证码:{{ form.download_captcha ? '已启用' : '已关闭' }}
</n-tag>
<n-tag round type="info">
默认价格:{{ form.download_default_cost }} {{ currencyName }}
默认积分价:{{ form.download_default_cost }} {{ currencyName }}
</n-tag>
<n-tag round :type="form.payment_vip_free_download ? 'success' : 'default'">
角色免扣:{{ form.payment_vip_free_download ? '已启用' : '未启用' }}
角色白名单免费:{{ form.payment_vip_free_download ? '已启用' : '未启用' }}
</n-tag>
<n-tag round type="info">
Token TTL:10 分钟
@@ -48,18 +48,21 @@
<n-switch v-model:value="form.download_login" />
</n-form-item>
<n-form-item label="下载启用积分扣费">
<n-form-item label="允许积分购买模式">
<n-switch v-model:value="form.download_credit" />
<template #feedback>
<span class="aksrt-hint-text">开启后,文章 pay_modo=积分 可走收银台一次购买解锁;下载 token 不再按次扣积分。关闭时积分模式资源按免费/需登录策略处理。</span>
</template>
</n-form-item>
<n-form-item label="下载启用验证码">
<n-switch v-model:value="form.download_captcha" />
</n-form-item>
<n-form-item :label="`默认资源价格(${currencyName})`">
<n-form-item :label="`默认积分价(${currencyName})`">
<n-input-number v-model:value="form.download_default_cost" :min="0" :max="100000" style="width: 180px;" />
<template #feedback>
<span class="aksrt-hint-text">资源未单独设价时回退到这里。角色免扣命中时会直接跳过积分扣费。</span>
<span class="aksrt-hint-text">仅作运营参考/兼容字段;实际价格以文章 _aksrt_pay 的积分价/现金价为准。角色白名单命中时免费下载。</span>
</template>
</n-form-item>
</n-form>
@@ -84,7 +87,7 @@
<n-form-item label="免扣角色列表" v-if="form.payment_vip_free_download">
<n-input v-model:value="form.payment_vip_exempt_roles" placeholder="administrator,editor,vip" />
<template #feedback>
<span class="aksrt-hint-text">命中这些 WordPress role 的用户下载资源时将直接跳过积分扣费。</span>
<span class="aksrt-hint-text">命中这些 WordPress role 的用户可免费下载(无需购买)。</span>
</template>
</n-form-item>
+156 -111
View File
@@ -3,8 +3,7 @@
* AKSRT Download State
*
* Unified download state resolver for resource pages.
* Replaces scattered per-template download status checks
* with a single service that returns a normalized state object.
* Driven by AKSRT_Content_Visibility (cashier unlock), not per-download credits.
*
* @package AKSRT
*/
@@ -15,15 +14,15 @@ class AKSRT_Download_State {
private static $instance = null;
/**
* State constants
*/
const STATE_NOT_LOGGED_IN = 'not_logged_in';
const STATE_NO_PERMISSION = 'no_permission';
const STATE_INSUFFICIENT_CREDITS = 'insufficient_credits';
const STATE_OWNED = 'owned';
const STATE_AVAILABLE = 'available';
const STATE_PENDING_DELIVERY = 'pending_delivery';
const STATE_NOT_LOGGED_IN = 'not_logged_in';
const STATE_NO_PERMISSION = 'no_permission';
const STATE_NEED_PURCHASE = 'need_purchase';
const STATE_NEED_VIP = 'need_vip';
const STATE_OWNED = 'owned';
const STATE_AVAILABLE = 'available';
const STATE_PENDING_DELIVERY = 'pending_delivery';
/** @deprecated use STATE_NEED_PURCHASE — kept for old templates */
const STATE_INSUFFICIENT_CREDITS = 'need_purchase';
private function __construct() {}
@@ -38,66 +37,57 @@ class AKSRT_Download_State {
* Resolve the current user's download state for a given resource post.
*
* @param int $post_id Resource post ID.
* @return array {
* @type string $state One of the STATE_* constants.
* @type bool $can_download Whether the user can initiate download.
* @type string $message Human-readable status message.
* @type string $action_label Suggested button label.
* @type string $action_url Suggested button URL (if applicable).
* @type int $cost Download cost in credits.
* @type int $balance Current user credit balance.
* @type string $login_url Login URL (if not logged in).
* @return array{
* state:string,can_download:bool,message:string,action_label:string,
* action_url:string,cost:int,balance:int,login_url:string,pay_modo:string,
* amount:float,need_purchase:bool,need_vip:bool,has_paid_access:bool
* }
*/
public function resolve(int $post_id): array {
$meta = function_exists('aksrt_get_resource_meta') ? aksrt_get_resource_meta($post_id) : [];
$price = (float) ($meta['price'] ?? 0);
$downloads = is_array($meta['downloads'] ?? null) ? $meta['downloads'] : [];
$login_url = wp_login_url(get_permalink($post_id));
$is_logged_in = is_user_logged_in();
$user_id = $is_logged_in ? get_current_user_id() : 0;
$login_url = wp_login_url(get_permalink($post_id));
// No downloads available
if (empty($downloads)) {
$meta = function_exists('aksrt_get_download_box_meta')
? aksrt_get_download_box_meta($post_id)
: (function_exists('aksrt_get_resource_meta') ? aksrt_get_resource_meta($post_id) : []);
$downloads = is_array($meta['downloads'] ?? null) ? $meta['downloads'] : [];
if ($downloads === []) {
return $this->build_result(
self::STATE_NO_PERMISSION,
false,
__('该资源暂未提供下载链接。', 'aksrt'),
__('暂无下载', 'aksrt'),
'',
0, 0, $login_url
0,
0,
$login_url
);
}
// Not logged in
if (!$is_logged_in) {
$requires_login = false;
if (class_exists('AKSRT_Settings')) {
$paid = AKSRT_Settings::get_paid_content_settings();
$requires_login = !empty($paid['download_requires_login']);
}
if ($requires_login) {
return $this->build_result(
self::STATE_NOT_LOGGED_IN,
false,
__('登录后可下载此资源。', 'aksrt'),
__('登录下载', 'aksrt'),
$login_url,
0, 0, $login_url
);
}
}
$access = class_exists('AKSRT_Content_Visibility')
? AKSRT_Content_Visibility::get_post_access_state($post_id, $user_id)
: [];
$priced = function_exists('aksrt_get_user_pay_price')
? aksrt_get_user_pay_price($post_id, $user_id)
: null;
$pay_modo = is_array($priced)
? ((($priced['modo'] ?? '0') === 'points') ? 'points' : '0')
: '0';
$amount = is_array($priced) ? (float) ($priced['amount'] ?? 0) : 0.0;
$cost = function_exists('aksrt_resource_download_cost')
? (int) aksrt_resource_download_cost($post_id)
: 0;
$balance = ($user_id > 0 && class_exists('AKSRT_Credits'))
? AKSRT_Credits::balance($user_id)
: 0;
// Check gate (credits, permission, etc.)
$gate = function_exists('aksrt_can_download') ? aksrt_can_download($post_id) : true;
$gate = function_exists('aksrt_can_download') ? aksrt_can_download($post_id, $user_id) : true;
if (is_wp_error($gate)) {
$code = $gate->get_error_code();
$data = (array) $gate->get_error_data();
$balance = (int) ($data['balance'] ?? 0);
$cost = function_exists('aksrt_resource_download_cost')
? aksrt_resource_download_cost($post_id)
: (int) ceil($price);
$msg = $gate->get_error_message() ?: __('无下载权限。', 'aksrt');
if ($code === 'aksrt_need_login') {
return $this->build_result(
@@ -106,83 +96,132 @@ class AKSRT_Download_State {
__('登录后可下载此资源。', 'aksrt'),
__('登录下载', 'aksrt'),
(string) ($data['login_url'] ?? $login_url),
$cost, $balance, (string) ($data['login_url'] ?? $login_url)
$cost,
$balance,
(string) ($data['login_url'] ?? $login_url),
[
'pay_modo' => $pay_modo,
'amount' => $amount,
'need_purchase' => false,
'need_vip' => false,
'has_paid_access' => !empty($access['has_paid_access']),
]
);
}
if ($code === 'aksrt_credits_insufficient') {
if ($code === 'aksrt_need_vip') {
$vip_url = function_exists('aksrt_get_user_center_tab_url')
? aksrt_get_user_center_tab_url('credits')
: $login_url;
return $this->build_result(
self::STATE_INSUFFICIENT_CREDITS,
self::STATE_NEED_VIP,
false,
sprintf(
/* translators: 1: current balance, 2: required cost */
__('当前余额 %1$d 积分,需要 %2$d 积分。', 'aksrt'),
$balance,
$cost
),
sprintf(__('付费下载(%d 积分)', 'aksrt'), $cost),
$msg,
__('开通会员', 'aksrt'),
$vip_url,
$cost,
$balance,
$login_url,
[
'pay_modo' => $pay_modo,
'amount' => $amount,
'need_purchase' => false,
'need_vip' => true,
'has_paid_access' => false,
]
);
}
// need_purchase / legacy insufficient credits → cashier.
if (in_array($code, ['aksrt_need_purchase', 'aksrt_credits_insufficient'], true)) {
$label = $pay_modo === 'points'
? sprintf(__('购买(%s 积分)', 'aksrt'), (string) (int) ceil($amount > 0 ? $amount : $cost))
: __('去购买', 'aksrt');
return $this->build_result(
self::STATE_NEED_PURCHASE,
false,
$msg ?: __('请先购买后再下载。', 'aksrt'),
$label,
'',
$cost, $balance, $login_url
$cost,
$balance,
$login_url,
[
'pay_modo' => $pay_modo,
'amount' => $amount > 0 ? $amount : (float) $cost,
'need_purchase' => true,
'need_vip' => false,
'has_paid_access' => false,
]
);
}
// Other gate error — treat as no permission
return $this->build_result(
self::STATE_NO_PERMISSION,
false,
$gate->get_error_message() ?: __('无下载权限。', 'aksrt'),
$msg,
__('无权限', 'aksrt'),
'',
0, 0, $login_url
$cost,
$balance,
$login_url,
[
'pay_modo' => $pay_modo,
'amount' => $amount,
'need_purchase' => !empty($access['need_purchase']),
'need_vip' => !empty($access['need_vip']),
'has_paid_access' => !empty($access['has_paid_access']),
]
);
}
// Check if user already owns/downloads
$has_downloaded = false;
if ($is_logged_in) {
$downloaded = get_user_meta($user_id, '_aksrt_downloaded_resources', true);
$downloaded = is_array($downloaded) ? $downloaded : [];
$has_downloaded = in_array($post_id, $downloaded, true);
}
$cost = function_exists('aksrt_resource_download_cost')
? aksrt_resource_download_cost($post_id)
: (int) ceil($price);
$balance = 0;
if ($is_logged_in && class_exists('AKSRT_Credits')) {
$balance = AKSRT_Credits::balance($user_id);
}
if ($has_downloaded) {
// Paid / free unlock.
$has_paid = !empty($access['has_paid_access']);
if ($has_paid || $amount <= 0) {
$owned = $has_paid && $amount > 0;
return $this->build_result(
self::STATE_OWNED,
$owned ? self::STATE_OWNED : self::STATE_AVAILABLE,
true,
__('已下载,可再次下载。', 'aksrt'),
__('再次下载', 'aksrt'),
$owned
? __('已购买,可下载。', 'aksrt')
: __('此资源可免费下载。', 'aksrt'),
$owned ? __('立即下载', 'aksrt') : __('立即下载', 'aksrt'),
'',
$cost, $balance, $login_url
0,
$balance,
$login_url,
[
'pay_modo' => $pay_modo,
'amount' => 0.0,
'need_purchase' => false,
'need_vip' => false,
'has_paid_access' => true,
]
);
}
// Available
$label = $cost > 0
? sprintf(__('下载(%d 积分)', 'aksrt'), $cost)
: __('立即下载', 'aksrt');
// Gate true but still priced unpaid (should be rare).
return $this->build_result(
self::STATE_AVAILABLE,
true,
$cost > 0
? sprintf(__('下载此资源将消耗 %d 积分。', 'aksrt'), $cost)
: __('此资源可免费下载。', 'aksrt'),
$label,
self::STATE_NEED_PURCHASE,
false,
__('请先购买后再下载。', 'aksrt'),
__('去购买', 'aksrt'),
'',
$cost, $balance, $login_url
$cost,
$balance,
$login_url,
[
'pay_modo' => $pay_modo,
'amount' => $amount,
'need_purchase' => true,
'need_vip' => false,
'has_paid_access' => false,
]
);
}
/**
* Build a normalized result array.
* @param array<string,mixed> $extra
*/
private function build_result(
string $state,
@@ -192,17 +231,23 @@ class AKSRT_Download_State {
string $action_url,
int $cost,
int $balance,
string $login_url
string $login_url,
array $extra = []
): array {
return [
'state' => $state,
'can_download' => $can_download,
'message' => $message,
'action_label' => $action_label,
'action_url' => $action_url,
'cost' => $cost,
'balance' => $balance,
'login_url' => $login_url,
];
return array_merge([
'state' => $state,
'can_download' => $can_download,
'message' => $message,
'action_label' => $action_label,
'action_url' => $action_url,
'cost' => $cost,
'balance' => $balance,
'login_url' => $login_url,
'pay_modo' => '0',
'amount' => 0.0,
'need_purchase' => false,
'need_vip' => false,
'has_paid_access' => false,
], $extra);
}
}
+18 -16
View File
@@ -653,9 +653,8 @@ if (!function_exists('aksrt_can_download')) {
/**
* 下载授权门禁 —— 单一合同。
*
* 实际判定全部交给 AKSRT_Content_Visibility::can_download_resource()
* (由 inc/helpers/aksrt-resource.php 的同名 helper 在更早加载时定义)。
* 此处仅作为该 helper 不可用时的兜底,避免出现第二套下载真相。
* 实际判定交给 AKSRT_Content_Visibility::can_download_resource()。
* 不再使用 download_credit 做按次扣积分门禁。
*
* @param int|WP_Post|null $post
* @param int $user_id 0 表示当前用户
@@ -672,13 +671,13 @@ if (!function_exists('aksrt_can_download')) {
}
$meta = aksrt_get_pay_meta($p);
if ($meta['pay_type'] !== AKSRT_PAY_TYPE_DOWNLOAD) {
if (($meta['pay_type'] ?? '') !== AKSRT_PAY_TYPE_DOWNLOAD) {
return new WP_Error('aksrt_not_downloadable', __('该内容不是可下载资源。', 'aksrt'));
}
$paid_settings = class_exists('AKSRT_Settings')
? AKSRT_Settings::get_paid_content_settings()
: ['download_login' => true, 'download_credit' => true];
: ['download_login' => true];
$uid = $user_id > 0 ? $user_id : get_current_user_id();
@@ -687,18 +686,21 @@ if (!function_exists('aksrt_can_download')) {
return new WP_Error('aksrt_need_login', __('请先登录。', 'aksrt'), ['login_url' => $login_url]);
}
if ($uid > 0 && aksrt_has_paid_access($p, $uid)) return true;
if ($uid > 0 && function_exists('aksrt_is_vip_exempt') && aksrt_is_vip_exempt($uid)) return true;
if ($uid > 0 && function_exists('aksrt_has_paid_access') && aksrt_has_paid_access($p, $uid)) {
return true;
}
if ($uid > 0 && function_exists('aksrt_is_vip_exempt') && aksrt_is_vip_exempt($uid)) {
return true;
}
$cost = aksrt_get_download_cost($p);
if ($cost > 0 && !empty($paid_settings['download_credit'])) {
$balance = function_exists('aksrt_user_credits') ? aksrt_user_credits($uid) : 0;
if ($balance < $cost) {
return new WP_Error('aksrt_credits_insufficient', __('积分不足。', 'aksrt'), [
'balance' => $balance,
'cost' => $cost,
]);
}
// Fallback without Content_Visibility: require purchase when priced.
$cost = function_exists('aksrt_get_download_cost') ? aksrt_get_download_cost($p) : 0;
if ($cost > 0) {
return new WP_Error('aksrt_need_purchase', __('请先购买后再下载。', 'aksrt'), [
'status' => 402,
'cost' => $cost,
'post_id'=> (int) $p->ID,
]);
}
return true;
+20 -62
View File
@@ -383,9 +383,10 @@ if (!function_exists('aksrt_is_download_credit_exempt_user')) {
if (!function_exists('aksrt_can_download')) {
/**
* 是否可下载(P3 阶段:登录 + 积分校验已接入;P5 接入验证码)。
* 是否可下载。
*
* 返回 WP_Error 表示拒绝;返回 true 表示放行。
* 单一合同:委托 AKSRT_Content_Visibility(收银台订单解锁 / VIP / 登录 / 日限额)。
* 不再用 cms_features.download_credit 做「按次扣积分」门禁。
*
* @param int|WP_Post|null $post
* @param int $user_id 0 表示当前用户
@@ -397,68 +398,40 @@ if (!function_exists('aksrt_can_download')) {
}
$p = get_post($post);
if (!($p instanceof WP_Post) || !aksrt_is_resource_post($p)) {
if (!($p instanceof WP_Post)) {
return new WP_Error('aksrt_not_resource', __('该内容不是可下载资源。', 'aksrt'));
}
$meta = aksrt_get_resource_meta($p);
$uid = $user_id > 0 ? $user_id : get_current_user_id();
$paid_settings = class_exists('AKSRT_Settings')
? AKSRT_Settings::get_paid_content_settings()
: [
'download_login' => true,
'download_credit' => true,
];
: ['download_login' => true];
// 登录门槛:全局开关 OR 单条覆盖
$need_login = !empty($paid_settings['download_login']) || !empty($meta['require_login']);
$uid = $user_id > 0 ? $user_id : get_current_user_id();
if ($need_login && $uid <= 0) {
if (!empty($paid_settings['download_login']) && $uid <= 0) {
return new WP_Error('aksrt_need_login', __('请先登录后再下载。', 'aksrt'), [
'status' => 401,
'login_url' => wp_login_url(get_permalink($p)),
]);
}
// 积分门槛:仅在 cms_features.download_credit 开启 + 实际成本 > 0 + 登录态时才校验
$credit_enabled = !empty($paid_settings['download_credit']);
if ($credit_enabled) {
$cost = (int) aksrt_resource_download_cost($p);
if ($cost > 0) {
if ($uid <= 0) {
return new WP_Error('aksrt_need_login', __('请先登录后再下载。', 'aksrt'), [
'status' => 401,
'login_url' => wp_login_url(get_permalink($p)),
]);
}
$balance = class_exists('AKSRT_Credits') ? AKSRT_Credits::balance($uid) : 0;
if ($balance < $cost) {
return new WP_Error('aksrt_credits_insufficient', __('积分不足,无法下载。', 'aksrt'), [
'status' => 402,
'balance' => $balance,
'cost' => $cost,
]);
}
}
}
// P5 阶段:验证码校验(在请求时校验,不在 can_download 静态判定中拦截)
// Without Content_Visibility, only login gate remains.
return true;
}
}
if (!function_exists('aksrt_resource_download_cost')) {
/**
* 取实际需要扣除的积分(向上取整),未启用积分门槛时返回 0。
* 下载侧展示用「未解锁积分价」。
*
* 优先级:单条 _aksrt_res_price > 全局 download_default_cost。
* 现金 pay_modo:永远 0(走订单解锁,不按次扣积分)。
* 积分 pay_modo:未购时返回积分价(用于 UI);已购订单后 0。
* 实际扣费只在收银台创建 resource 订单时发生,下载 token 不再扣积分。
*/
function aksrt_resource_download_cost($post = null): int {
$p = get_post($post);
$uid = function_exists('get_current_user_id') ? (int) get_current_user_id() : 0;
// Paid order → no further credit deduct. Avoid has_paid_access() here (recursion via access state).
// Paid order → unlocked.
if ($p instanceof WP_Post && $uid > 0 && class_exists('AKSRT_Order')) {
if (AKSRT_Order::get_instance()->has_paid_access(
$uid,
@@ -468,13 +441,9 @@ if (!function_exists('aksrt_resource_download_cost')) {
return 0;
}
}
$paid_settings = class_exists('AKSRT_Settings')
? AKSRT_Settings::get_paid_content_settings()
: [
'download_credit' => true,
'default_cost' => 0,
];
if ($p instanceof WP_Post && $uid <= 0 && function_exists('aksrt_guest_has_paid_access') && aksrt_guest_has_paid_access((int) $p->ID)) {
return 0;
}
$priced = function_exists('aksrt_get_user_pay_price')
? aksrt_get_user_pay_price($post, $uid)
@@ -486,7 +455,7 @@ if (!function_exists('aksrt_resource_download_cost')) {
$pay_modo = (($pay['pay_modo'] ?? '0') === 'points') ? 'points' : '0';
}
// Cash mode always unlocks via resource order (cashier), never per-download credits.
// Cash mode: order unlock only.
if ($pay_modo !== 'points') {
return 0;
}
@@ -495,25 +464,14 @@ if (!function_exists('aksrt_resource_download_cost')) {
return 0;
}
// Points mode: one-time purchase via cashier. Unpaid amount is not deducted here —
// can_download returns need_purchase; after order, has_paid_access zeros cost above.
// Return display/gate cost only for free-tier (0) detection; unpaid still shows amount
// but rest_request must not deduct without paid order (see Content_Visibility).
if (is_array($priced)) {
$price = (float) ($priced['amount'] ?? 0);
if ($price > 0) {
return (int) ceil($price);
}
return 0;
return $price > 0 ? (int) ceil($price) : 0;
}
// Fallback without helper.
$meta = function_exists('aksrt_get_pay_meta') ? aksrt_get_pay_meta($post) : aksrt_get_resource_meta($post);
$meta = function_exists('aksrt_get_pay_meta') ? aksrt_get_pay_meta($post) : [];
$price = (float) ($meta['points_price'] ?? $meta['price'] ?? 0);
if ($price > 0) {
return (int) ceil($price);
}
return 0;
return $price > 0 ? (int) ceil($price) : 0;
}
}
@@ -2,8 +2,8 @@
/**
* Download Permission Card
*
* Shows download status, permission requirements, and action buttons.
* Uses AKSRT_Download_State for unified state resolution.
* Shows download status and actions from AKSRT_Download_State
* (Content_Visibility + cashier unlock — not per-download credits).
*
* @package AKSRT
*/
@@ -11,106 +11,118 @@
defined('ABSPATH') || exit;
$post_id = get_the_ID();
$is_logged_in = is_user_logged_in();
// Use unified state resolver if available
$state = [
'state' => 'available',
'can_download' => true,
'message' => '',
'action_label' => __('立即下载', 'aksrt'),
'action_url' => '',
'cost' => 0,
'balance' => 0,
'login_url' => wp_login_url(get_permalink($post_id)),
'pay_modo' => '0',
'amount' => 0.0,
'need_purchase' => false,
'need_vip' => false,
];
if (class_exists('AKSRT_Download_State')) {
$state = AKSRT_Download_State::get_instance()->resolve($post_id);
$status = $state['state'];
$can_download = $state['can_download'];
$status_text = $state['message'];
$action_label = $state['action_label'];
$action_url = $state['action_url'];
$download_cost = $state['cost'];
$user_credits = $state['balance'];
$login_url = $state['login_url'];
} else {
// Fallback: legacy logic
$download_settings = class_exists('AKSRT_Settings') ? AKSRT_Settings::get_cms_features() : [];
$requires_login = !empty($download_settings['download_requires_login']);
$requires_credits = !empty($download_settings['download_requires_credits']);
$download_cost = (int) ($download_settings['download_cost'] ?? 0);
$has_downloaded = false;
if ($is_logged_in) {
$downloaded = get_user_meta(get_current_user_id(), '_aksrt_downloaded_resources', true);
$downloaded = is_array($downloaded) ? $downloaded : [];
$has_downloaded = in_array($post_id, $downloaded, true);
}
$user_credits = 0;
if ($is_logged_in && class_exists('AKSRT_Credits')) {
$user_credits = AKSRT_Credits::balance(get_current_user_id());
}
$status = 'available';
$status_text = '';
$can_download = true;
$login_url = wp_login_url(get_permalink($post_id));
$action_label = __('立即下载', 'aksrt');
$action_url = '';
if (!$is_logged_in && $requires_login) {
$status = 'not_logged_in';
$status_text = __('请先登录后下载', 'aksrt');
$can_download = false;
$action_label = __('登录下载', 'aksrt');
$action_url = $login_url;
} elseif ($requires_credits && $download_cost > 0 && $user_credits < $download_cost && !$has_downloaded) {
$status = 'insufficient_credits';
$status_text = sprintf(__('需要 %d 积分,当前余额 %d', 'aksrt'), $download_cost, $user_credits);
$can_download = false;
$action_label = __('获取积分', 'aksrt');
$action_url = home_url('/user/credits');
} elseif ($has_downloaded) {
$status = 'owned';
$status_text = __('已下载,可再次下载', 'aksrt');
}
$state = array_merge($state, AKSRT_Download_State::get_instance()->resolve($post_id));
} elseif (function_exists('aksrt_can_download')) {
$gate = aksrt_can_download($post_id);
if (is_wp_error($gate)) {
$code = $gate->get_error_code();
$data = (array) $gate->get_error_data();
$state['can_download'] = false;
$state['message'] = $gate->get_error_message();
if ($code === 'aksrt_need_login') {
$state['state'] = 'not_logged_in';
$state['action_label'] = __('登录下载', 'aksrt');
$state['action_url'] = (string) ($data['login_url'] ?? $state['login_url']);
} elseif ($code === 'aksrt_need_vip') {
$state['state'] = 'need_vip';
$state['action_label'] = __('开通会员', 'aksrt');
$state['need_vip'] = true;
} else {
$state['state'] = 'need_purchase';
$state['action_label'] = __('去购买', 'aksrt');
$state['need_purchase'] = true;
}
}
}
// Map state to icon
$status = (string) ($state['state'] ?? 'available');
if ($status === 'insufficient_credits') {
$status = 'need_purchase';
}
$can_download = !empty($state['can_download']);
$status_text = (string) ($state['message'] ?? '');
$action_label = (string) ($state['action_label'] ?? __('立即下载', 'aksrt'));
$action_url = (string) ($state['action_url'] ?? '');
$login_url = (string) ($state['login_url'] ?? wp_login_url(get_permalink($post_id)));
$pay_modo = (string) ($state['pay_modo'] ?? '0');
$amount = (float) ($state['amount'] ?? 0);
$need_purchase = !empty($state['need_purchase']) || $status === 'need_purchase';
$need_vip = !empty($state['need_vip']) || $status === 'need_vip';
$state_icons = [
'available' => ['icon' => 'fa-check-circle', 'color' => 'success'],
'owned' => ['icon' => 'fa-download', 'color' => 'success'],
'not_logged_in' => ['icon' => 'fa-lock', 'color' => 'warning'],
'insufficient_credits' => ['icon' => 'fa-coins', 'color' => 'warning'],
'no_permission' => ['icon' => 'fa-ban', 'color' => 'warning'],
'pending_delivery' => ['icon' => 'fa-clock', 'color' => 'info'],
'available' => ['icon' => 'fa-check-circle', 'color' => 'success'],
'owned' => ['icon' => 'fa-download', 'color' => 'success'],
'not_logged_in' => ['icon' => 'fa-lock', 'color' => 'warning'],
'need_purchase' => ['icon' => 'fa-shopping-cart', 'color' => 'warning'],
'need_vip' => ['icon' => 'fa-crown', 'color' => 'warning'],
'no_permission' => ['icon' => 'fa-ban', 'color' => 'warning'],
'pending_delivery' => ['icon' => 'fa-clock', 'color' => 'info'],
];
$state_info = $state_icons[$status] ?? $state_icons['available'];
?>
<div class="aksrt-dl-perm-card aksrt-dl-perm-card--<?php echo esc_attr($status); ?>">
<div class="aksrt-dl-perm-card__status">
<i class="fas <?php echo esc_attr($state_info['icon']); ?> aksrt-dl-perm-card__icon aksrt-dl-perm-card__icon--<?php echo esc_attr($state_info['color']); ?>"></i>
<span><?php echo esc_html($status_text); ?></span>
</div>
<div class="download-permission-card download-permission-card--<?php echo esc_attr($status); ?>">
<div class="download-permission-card__status download-permission-card__status--<?php echo esc_attr($state_info['color']); ?>">
<i class="fas <?php echo esc_attr($state_info['icon']); ?>" aria-hidden="true"></i>
<span><?php echo esc_html($status_text !== '' ? $status_text : __('可下载', 'aksrt')); ?></span>
</div>
<?php if (isset($download_cost) && $download_cost > 0) : ?>
<div class="aksrt-dl-perm-card__cost">
<i class="fas fa-coins"></i>
<span><?php printf(esc_html__('下载消耗 %d 积分', 'aksrt'), $download_cost); ?></span>
<?php if ($is_logged_in) : ?>
<span class="aksrt-dl-perm-card__balance">(<?php printf(esc_html__('当前: %d', 'aksrt'), $user_credits); ?>)</span>
<?php endif; ?>
</div>
<?php endif; ?>
<?php if ($need_purchase && $amount > 0) : ?>
<div class="download-permission-card__cost">
<i class="fas fa-tag" aria-hidden="true"></i>
<span>
<?php
if ($pay_modo === 'points') {
printf(
/* translators: %d: points */
esc_html__('购买价格:%d 积分', 'aksrt'),
(int) ceil($amount)
);
} else {
printf(
/* translators: %s: price */
esc_html__('购买价格:¥%s', 'aksrt'),
esc_html(function_exists('aksrt_format_price') ? aksrt_format_price($amount) : (string) $amount)
);
}
?>
</span>
</div>
<?php endif; ?>
<div class="aksrt-dl-perm-card__actions">
<?php if ($can_download) : ?>
<a href="<?php echo esc_url(get_permalink($post_id)); ?>" class="aksrt-dl-perm-card__btn aksrt-dl-perm-card__btn--primary">
<i class="fas fa-download"></i>
<?php echo esc_html($action_label); ?>
</a>
<?php elseif ($action_url !== '') : ?>
<a href="<?php echo esc_url($action_url); ?>" class="aksrt-dl-perm-card__btn aksrt-dl-perm-card__btn--primary">
<i class="fas fa-arrow-right"></i>
<?php echo esc_html($action_label); ?>
</a>
<?php else : ?>
<span class="aksrt-dl-perm-card__btn aksrt-dl-perm-card__btn--outline aksrt-dl-perm-card__btn--disabled">
<i class="fas fa-info-circle"></i>
<?php echo esc_html($action_label); ?>
</span>
<?php endif; ?>
</div>
<div class="download-permission-card__actions">
<?php if ($can_download) : ?>
<span class="download-permission-card__hint"><?php esc_html_e('请在下方选择下载源', 'aksrt'); ?></span>
<?php elseif ($status === 'not_logged_in' && ($action_url !== '' || $login_url !== '')) : ?>
<a class="btn-primary btn-sm" href="<?php echo esc_url($action_url !== '' ? $action_url : $login_url); ?>">
<?php echo esc_html($action_label); ?>
</a>
<?php elseif ($need_vip) : ?>
<a class="btn-primary btn-sm" href="<?php echo esc_url($action_url !== '' ? $action_url : (function_exists('aksrt_get_user_center_tab_url') ? aksrt_get_user_center_tab_url('credits') : home_url('/'))); ?>">
<?php echo esc_html($action_label); ?>
</a>
<?php elseif ($need_purchase) : ?>
<button type="button" class="btn-primary btn-sm" data-aksrt-pay-content="<?php echo esc_attr((string) $post_id); ?>">
<?php echo esc_html($action_label); ?>
</button>
<?php else : ?>
<span class="download-permission-card__hint"><?php echo esc_html($status_text !== '' ? $status_text : __('暂无权限', 'aksrt')); ?></span>
<?php endif; ?>
</div>
</div>