209 lines
7.1 KiB
PHP
209 lines
7.1 KiB
PHP
<?php
|
|
/**
|
|
* 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.
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
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';
|
|
|
|
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 {
|
|
* @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).
|
|
* }
|
|
*/
|
|
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'] : [];
|
|
|
|
$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)) {
|
|
return $this->build_result(
|
|
self::STATE_NO_PERMISSION,
|
|
false,
|
|
__('该资源暂未提供下载链接。', 'aksrt'),
|
|
__('暂无下载', 'aksrt'),
|
|
'',
|
|
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
|
|
);
|
|
}
|
|
}
|
|
|
|
// Check gate (credits, permission, etc.)
|
|
$gate = function_exists('aksrt_can_download') ? aksrt_can_download($post_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);
|
|
|
|
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)
|
|
);
|
|
}
|
|
|
|
if ($code === 'aksrt_credits_insufficient') {
|
|
return $this->build_result(
|
|
self::STATE_INSUFFICIENT_CREDITS,
|
|
false,
|
|
sprintf(
|
|
/* translators: 1: current balance, 2: required cost */
|
|
__('当前余额 %1$d 积分,需要 %2$d 积分。', 'aksrt'),
|
|
$balance,
|
|
$cost
|
|
),
|
|
sprintf(__('付费下载(%d 积分)', 'aksrt'), $cost),
|
|
'',
|
|
$cost, $balance, $login_url
|
|
);
|
|
}
|
|
|
|
// Other gate error — treat as no permission
|
|
return $this->build_result(
|
|
self::STATE_NO_PERMISSION,
|
|
false,
|
|
$gate->get_error_message() ?: __('无下载权限。', 'aksrt'),
|
|
__('无权限', 'aksrt'),
|
|
'',
|
|
0, 0, $login_url
|
|
);
|
|
}
|
|
|
|
// 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) {
|
|
return $this->build_result(
|
|
self::STATE_OWNED,
|
|
true,
|
|
__('已下载,可再次下载。', 'aksrt'),
|
|
__('再次下载', 'aksrt'),
|
|
'',
|
|
$cost, $balance, $login_url
|
|
);
|
|
}
|
|
|
|
// Available
|
|
$label = $cost > 0
|
|
? sprintf(__('下载(%d 积分)', 'aksrt'), $cost)
|
|
: __('立即下载', 'aksrt');
|
|
|
|
return $this->build_result(
|
|
self::STATE_AVAILABLE,
|
|
true,
|
|
$cost > 0
|
|
? sprintf(__('下载此资源将消耗 %d 积分。', 'aksrt'), $cost)
|
|
: __('此资源可免费下载。', 'aksrt'),
|
|
$label,
|
|
'',
|
|
$cost, $balance, $login_url
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Build a normalized result array.
|
|
*/
|
|
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 {
|
|
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,
|
|
];
|
|
}
|
|
}
|