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:
kate522 committed 2026-07-14 20:38:17 +08:00
1 parent 1039b26efd
commit 79f8c43970
5 files changed
+311 -291

No files matched your search

+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);
}
}