*/ public static function get_post_access_state($post = null, int $user_id = 0): array { $resource_post = get_post($post); $uid = $user_id > 0 ? $user_id : get_current_user_id(); $pay_meta = function_exists('aksrt_get_pay_meta') ? aksrt_get_pay_meta($resource_post) : AKSRT_Domain_Model::default_pay_meta(); $pay_type = AKSRT_Domain_Model::normalize_pay_type((string) ($pay_meta['pay_type'] ?? AKSRT_Domain_Model::PAY_TYPE_NO)); $price = (float) ($pay_meta['price'] ?? 0); $paid_settings = class_exists('AKSRT_Settings') ? AKSRT_Settings::get_paid_content_settings() : [ 'download_login' => true, 'download_credit' => true, 'download_captcha' => false, ]; $state = [ 'post_id' => $resource_post instanceof WP_Post ? (int) $resource_post->ID : 0, 'pay_type' => $pay_type, 'price' => $price, 'need_login' => false, 'need_purchase' => false, 'need_vip' => false, 'require_captcha' => false, 'has_paid_access' => false, 'can_view' => true, 'can_download' => true, 'download_cost' => 0, ]; if (!($resource_post instanceof WP_Post)) { $state['can_view'] = false; $state['can_download'] = false; return $state; } if ($pay_type === AKSRT_Domain_Model::PAY_TYPE_NO) { return $state; } $has_free_access = $price <= 0 && $pay_type !== AKSRT_Domain_Model::PAY_TYPE_VIP; $has_paid_order = false; if ($uid > 0 && class_exists('AKSRT_Order')) { $has_paid_order = AKSRT_Order::get_instance()->has_paid_access( $uid, AKSRT_Domain_Model::ORDER_PRODUCT_RESOURCE, (int) $resource_post->ID ); } $has_vip_access = false; if ($uid > 0) { if (function_exists('aksrt_is_vip_exempt') && aksrt_is_vip_exempt($uid)) { $has_vip_access = true; } elseif (function_exists('aksrt_is_vip') && $pay_type === AKSRT_Domain_Model::PAY_TYPE_VIP) { $has_vip_access = aksrt_is_vip($uid); } } if ($pay_type === AKSRT_Domain_Model::PAY_TYPE_DOWNLOAD) { $download_cost = function_exists('aksrt_resource_download_cost') ? (int) aksrt_resource_download_cost($resource_post) : (int) ceil($price); $state['download_cost'] = max(0, $download_cost); $state['need_login'] = !empty($paid_settings['download_login']) || !empty($pay_meta['require_login']); $state['require_captcha'] = !empty($paid_settings['download_captcha']) || !empty($pay_meta['require_captcha']); $state['need_purchase'] = !$has_vip_access && $state['download_cost'] > 0; $state['has_paid_access'] = $has_vip_access || $has_paid_order || !$state['need_purchase']; $state['can_view'] = true; $state['can_download'] = !$state['need_login'] || $uid > 0; return $state; } if ($pay_type === AKSRT_Domain_Model::PAY_TYPE_VIP) { $state['need_vip'] = true; $state['has_paid_access'] = $has_vip_access; $state['can_view'] = $has_vip_access; $state['can_download'] = false; return $state; } $state['need_login'] = !empty($pay_meta['require_login']); $state['need_purchase'] = !$has_free_access; $state['has_paid_access'] = $has_free_access || $has_paid_order || $has_vip_access; $state['can_view'] = $state['has_paid_access'] && (!$state['need_login'] || $uid > 0); $state['can_download'] = false; return $state; } public static function can_view_paid_content($post = null, int $user_id = 0): bool { $state = self::get_post_access_state($post, $user_id); return !empty($state['can_view']); } /** * @param int|WP_Post|null $post * @return true|WP_Error */ public static function can_download_resource($post = null, int $user_id = 0) { $resource_post = get_post($post); if (!($resource_post instanceof WP_Post) || !function_exists('aksrt_is_resource_post') || !aksrt_is_resource_post($resource_post)) { return new WP_Error('aksrt_not_resource', __('该内容不是可下载资源。', 'aksrt')); } $state = self::get_post_access_state($resource_post, $user_id); $uid = $user_id > 0 ? $user_id : get_current_user_id(); if (($state['pay_type'] ?? '') !== AKSRT_Domain_Model::PAY_TYPE_DOWNLOAD) { return new WP_Error('aksrt_not_downloadable', __('该内容不是可下载资源。', 'aksrt')); } if (!empty($state['need_login']) && $uid <= 0) { return new WP_Error('aksrt_need_login', __('请先登录后再下载。', 'aksrt'), [ 'status' => 401, 'login_url' => wp_login_url(get_permalink($resource_post)), ]); } $cost = (int) ($state['download_cost'] ?? 0); $paid_settings = class_exists('AKSRT_Settings') ? AKSRT_Settings::get_paid_content_settings() : ['download_credit' => true]; if (!empty($paid_settings['download_credit']) && $cost > 0) { $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, ]); } } return true; } public function render_hide_shortcode(array $atts, string $content = ''): string { $atts = shortcode_atts([ 'mode' => 'comment', 'message' => '', 'pid' => get_the_ID(), ], $atts, 'aksrt_hide'); $mode = sanitize_key((string) $atts['mode']); $pid = (int) $atts['pid']; $message = (string) $atts['message']; if ($pid <= 0) { return ''; } if ($this->is_unlocked($mode, $pid)) { return '