refactor: flatten src/AKSRT → src (drop theme-identifier dir)
Move all domain folders from src/AKSRT/{Domain} to src/{Domain}; remap
Composer PSR-4 "AKSRT\\": "src/"; update audit script + README + ADR
paths; remove dead tools/build-psr4-map.php (scanned deleted inc/classes).
This commit is contained in:
@@ -0,0 +1,250 @@
|
||||
<?php
|
||||
/**
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
namespace AKSRT\Content;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
class DownloadState {
|
||||
|
||||
private static $instance = null;
|
||||
|
||||
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() {}
|
||||
|
||||
public static function get_instance(): self {
|
||||
if (null === self::$instance) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the current user's download state for a given resource post.
|
||||
*
|
||||
* @param int $post_id Resource post ID.
|
||||
* @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 {
|
||||
$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;
|
||||
|
||||
$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
|
||||
);
|
||||
}
|
||||
|
||||
$access = class_exists('AKSRT\\Content\\Visibility')
|
||||
? 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\Credits'))
|
||||
? \AKSRT\Credits\Credits::balance($user_id)
|
||||
: 0;
|
||||
|
||||
$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();
|
||||
$msg = $gate->get_error_message() ?: __('无下载权限。', 'aksrt');
|
||||
|
||||
if ($code === 'aksrt_need_login') {
|
||||
return $this->build_result(
|
||||
self::STATE_NOT_LOGGED_IN,
|
||||
false,
|
||||
__('登录后可下载此资源。', 'aksrt'),
|
||||
__('登录下载', 'aksrt'),
|
||||
(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_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_NEED_VIP,
|
||||
false,
|
||||
$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,
|
||||
[
|
||||
'pay_modo' => $pay_modo,
|
||||
'amount' => $amount > 0 ? $amount : (float) $cost,
|
||||
'need_purchase' => true,
|
||||
'need_vip' => false,
|
||||
'has_paid_access' => false,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
return $this->build_result(
|
||||
self::STATE_NO_PERMISSION,
|
||||
false,
|
||||
$msg,
|
||||
__('无权限', 'aksrt'),
|
||||
'',
|
||||
$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']),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
// Paid / free unlock.
|
||||
$has_paid = !empty($access['has_paid_access']);
|
||||
if ($has_paid || $amount <= 0) {
|
||||
$owned = $has_paid && $amount > 0;
|
||||
return $this->build_result(
|
||||
$owned ? self::STATE_OWNED : self::STATE_AVAILABLE,
|
||||
true,
|
||||
$owned
|
||||
? __('已购买,可下载。', 'aksrt')
|
||||
: __('此资源可免费下载。', 'aksrt'),
|
||||
$owned ? __('立即下载', 'aksrt') : __('立即下载', 'aksrt'),
|
||||
'',
|
||||
0,
|
||||
$balance,
|
||||
$login_url,
|
||||
[
|
||||
'pay_modo' => $pay_modo,
|
||||
'amount' => 0.0,
|
||||
'need_purchase' => false,
|
||||
'need_vip' => false,
|
||||
'has_paid_access' => true,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
// Gate true but still priced unpaid (should be rare).
|
||||
return $this->build_result(
|
||||
self::STATE_NEED_PURCHASE,
|
||||
false,
|
||||
__('请先购买后再下载。', 'aksrt'),
|
||||
__('去购买', 'aksrt'),
|
||||
'',
|
||||
$cost,
|
||||
$balance,
|
||||
$login_url,
|
||||
[
|
||||
'pay_modo' => $pay_modo,
|
||||
'amount' => $amount,
|
||||
'need_purchase' => true,
|
||||
'need_vip' => false,
|
||||
'has_paid_access' => false,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $extra
|
||||
*/
|
||||
private function build_result(
|
||||
string $state,
|
||||
bool $can_download,
|
||||
string $message,
|
||||
string $action_label,
|
||||
string $action_url,
|
||||
int $cost,
|
||||
int $balance,
|
||||
string $login_url,
|
||||
array $extra = []
|
||||
): array {
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user