build_result( self::STATE_NO_PERMISSION, false, __('该资源暂未提供下载链接。', 'aksrt'), __('暂无下载', 'aksrt'), '', 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; $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 $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); } }