Files
wordpress-theme-aksrt/templates/parts/resource-download-box.php
T

287 lines
12 KiB
PHP

<?php
/**
* Unified paid-content download box.
*
* Downloadable content now follows the unified single.php mainline; resource-
* specific visuals are treated as content variants rather than a separate page
* system.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
$post_id = get_the_ID();
$meta = get_query_var('aksrt_resource_meta', null);
if (!is_array($meta)) {
$meta = function_exists('aksrt_get_download_box_meta') ? aksrt_get_download_box_meta($post_id) : null;
}
if (!is_array($meta)) return;
$downloads = is_array($meta['downloads']) ? $meta['downloads'] : [];
$pay_type = $meta['pay_type'] ?? 'download';
if ($pay_type !== 'download' || empty($downloads)) {
return;
}
$is_resource_context = function_exists('aksrt_is_resource_post') && aksrt_is_resource_post($post_id);
$context_variant = $is_resource_context ? 'resource' : 'content';
$subject_label = trim((string) ($meta['subject_label'] ?? ''));
if ($subject_label === '') {
$subject_label = $is_resource_context ? __('资源', 'aksrt') : __('内容', 'aksrt');
}
$price_label = function_exists('aksrt_get_pay_price_label')
? aksrt_get_pay_price_label($post_id)
: ['text' => '', 'free' => true];
// 下载策略检查
$gate = function_exists('aksrt_can_download') ? aksrt_can_download($post_id) : true;
$gate_blocked = is_wp_error($gate);
$gate_code = $gate_blocked ? $gate->get_error_code() : '';
$gate_data = $gate_blocked ? (array) $gate->get_error_data() : [];
$paid_settings = class_exists('AKSRT_Settings')
? AKSRT_Settings::get_paid_content_settings()
: ['download_credit' => true, 'download_captcha' => false];
$cost = function_exists('aksrt_get_download_cost') ? aksrt_get_download_cost($post_id) : (int) ceil((float) ($meta['price'] ?? 0));
$credit_active = !empty($paid_settings['download_credit']) && $cost > 0;
$captcha_active = !empty($paid_settings['download_captcha']) || !empty($meta['require_captcha']);
if ($captcha_active && function_exists('aksrt_is_captcha_enabled_for') && !aksrt_is_captcha_enabled_for('download')) {
$captcha_active = false;
}
$balance = (int) ($gate_data['balance'] ?? 0);
$login_url = (string) ($gate_data['login_url'] ?? wp_login_url(get_permalink($post_id)));
$is_need_login = $gate_blocked && $gate_code === 'aksrt_need_login';
$is_low_credit = $gate_blocked && $gate_code === 'aksrt_credits_insufficient';
// 缩略图
$thumbnail = get_the_post_thumbnail_url($post_id, 'medium') ?: '';
$has_thumbnail = $thumbnail !== '';
// 原价 & VIP 价
$original_price = (float) ($meta['original_price'] ?? 0);
$vip1_price = (float) ($meta['vip1_price'] ?? 0);
$vip2_price = (float) ($meta['vip2_price'] ?? 0);
$has_original = $original_price > 0 && $original_price > (float) ($meta['price'] ?? 0);
$has_vip = $vip1_price > 0 || $vip2_price > 0;
// 销量
$download_count = (int) ($meta['download_count'] ?? 0);
$sales_count = (int) ($meta['sales_count'] ?? 0);
$display_count = max($download_count, $sales_count);
// 付费类型徽章
$pay_type_badge = function_exists('aksrt_get_pay_type_badge') ? aksrt_get_pay_type_badge($pay_type) : '';
// 货币名称
$currency = class_exists('AKSRT_Settings') ? AKSRT_Settings::get_currency_name() : __('积分', 'aksrt');
?>
<section class="widget resource-download-box resource-download-box--v2<?php echo $gate_blocked ? ' is-blocked' : ''; ?> resource-download-box--<?php echo esc_attr($context_variant); ?>"
data-post-id="<?php echo esc_attr((string) $post_id); ?>"
data-price="<?php echo esc_attr((string) ($meta['price'] ?? 0)); ?>"
data-credit="<?php echo $credit_active ? '1' : '0'; ?>"
data-captcha="<?php echo $captcha_active ? '1' : '0'; ?>"
aria-label="<?php esc_attr_e('下载区', 'aksrt'); ?>">
<?php if ($display_count > 0) : ?>
<span class="resource-download-box__sales-badge">
<i class="fas fa-fire" aria-hidden="true"></i>
<?php
/* translators: %s: 下载/销量数 */
printf(esc_html__('已售 %s', 'aksrt'), number_format_i18n($display_count));
?>
</span>
<?php endif; ?>
<div class="resource-download-box__body">
<?php if ($has_thumbnail) : ?>
<div class="resource-download-box__thumb-col">
<img class="resource-download-box__thumb" src="<?php echo esc_url($thumbnail); ?>" alt="<?php echo esc_attr(get_the_title()); ?>"<?php echo function_exists('aksrt_lazy_loading') ? aksrt_lazy_loading('other') : ' loading="lazy"'; ?>>
<?php if ($pay_type_badge) : ?>
<div class="resource-download-box__pay-tag"><?php echo $pay_type_badge; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></div>
<?php endif; ?>
</div>
<?php endif; ?>
<div class="resource-download-box__content-col">
<header class="resource-download-box__header">
<div>
<p class="resource-download-box__eyebrow"><?php echo esc_html(sprintf(__('%s付费 / 下载能力', 'aksrt'), $subject_label)); ?></p>
<h2 class="resource-download-box__title">
<i class="fas fa-cloud-download-alt" aria-hidden="true"></i>
<?php esc_html_e('解锁下载', 'aksrt'); ?>
</h2>
</div>
<span class="resource-download-box__count">
<?php echo esc_html(sprintf(_n('%s 个下载源', '%s 个下载源', count($downloads), 'aksrt'), number_format_i18n(count($downloads)))); ?>
</span>
</header>
<div class="resource-download-box__pricing">
<span class="resource-download-box__price<?php echo !empty($price_label['free']) ? ' is-free' : ''; ?>">
<?php echo esc_html((string) ($price_label['text'] ?? '')); ?>
</span>
<?php if ($has_original) : ?>
<span class="resource-download-box__original-price">
<?php echo esc_html(aksrt_format_price($original_price) . ' ' . $currency); ?>
</span>
<?php endif; ?>
<?php echo $pay_type_badge && !$has_thumbnail ? $pay_type_badge : ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</div>
<?php if ($has_vip) : ?>
<div class="resource-download-box__vip-pricing">
<?php if ($vip1_price > 0) : ?>
<span class="resource-download-box__vip-badge resource-download-box__vip-badge--1">
<i class="fas fa-crown" aria-hidden="true"></i>
<?php
/* translators: %s: VIP1 价格 */
printf(esc_html__('VIP1 %s', 'aksrt'), aksrt_format_price($vip1_price));
?>
</span>
<?php endif; ?>
<?php if ($vip2_price > 0) : ?>
<span class="resource-download-box__vip-badge resource-download-box__vip-badge--2">
<i class="fas fa-gem" aria-hidden="true"></i>
<?php
/* translators: %s: VIP2 价格 */
printf(esc_html__('VIP2 %s', 'aksrt'), aksrt_format_price($vip2_price));
?>
</span>
<?php endif; ?>
</div>
<?php endif; ?>
<?php if (!empty($paid_settings['catalog_note'])) : ?>
<p class="resource-download-box__catalog-note"><?php echo esc_html((string) $paid_settings['catalog_note']); ?></p>
<?php endif; ?>
<ul class="resource-download-box__list">
<?php foreach ($downloads as $idx => $row) :
$label = isset($row['label']) ? (string) $row['label'] : '';
if ($label === '') {
$label = sprintf(__('下载源 %d', 'aksrt'), $idx + 1);
}
$type = isset($row['type']) ? (string) $row['type'] : 'direct';
$url = isset($row['url']) ? (string) $row['url'] : '';
$extract_code = isset($row['extract_code']) ? trim((string) $row['extract_code']) : '';
$has_extract = $extract_code !== '';
$service = function_exists('aksrt_detect_cloud_service') ? aksrt_detect_cloud_service($url) : ['key' => 'other', 'name' => $label, 'color' => '#6b7280'];
if (strpos($label, '下载源') === 0 && $service['key'] !== 'other') {
$label = $service['name'];
}
if ($is_need_login) {
$btn_tag = 'a';
$btn_text = __('登录后下载', 'aksrt');
$btn_icon = 'fa-sign-in-alt';
$btn_modifier = ' resource-download-box__btn--login';
} elseif ($is_low_credit) {
$btn_tag = 'button';
$btn_text = sprintf(__('解锁下载(%d 积分)', 'aksrt'), $cost);
$btn_icon = 'fa-coins';
$btn_modifier = ' resource-download-box__btn--credit-low';
} elseif ($credit_active && $cost > 0) {
$btn_tag = 'button';
$btn_text = sprintf(__('下载(%d 积分)', 'aksrt'), $cost);
$btn_icon = 'fa-download';
$btn_modifier = ' resource-download-box__btn--paid';
} else {
$btn_tag = 'button';
$btn_text = __('立即下载', 'aksrt');
$btn_icon = 'fa-download';
$btn_modifier = '';
}
?>
<li class="resource-download-box__item">
<?php if ($btn_tag === 'a') : ?>
<a
class="resource-download-box__btn btn-primary resource-download-box__btn--service-<?php echo esc_attr($service['key']); ?><?php echo esc_attr($btn_modifier); ?>"
href="<?php echo esc_url($login_url); ?>"
data-source-index="<?php echo esc_attr((string) $idx); ?>"
data-source-type="<?php echo esc_attr($type); ?>"
style="--service-color: <?php echo esc_attr($service['color']); ?>"
>
<i class="fas <?php echo esc_attr($btn_icon); ?>" aria-hidden="true"></i>
<span><?php echo esc_html($btn_text); ?></span>
<span class="resource-download-box__service-badge" style="background: <?php echo esc_attr($service['color']); ?>">
<?php echo esc_html($service['name']); ?>
</span>
</a>
<?php else : ?>
<button
type="button"
class="resource-download-box__btn btn-primary resource-download-box__btn--service-<?php echo esc_attr($service['key']); ?><?php echo esc_attr($btn_modifier); ?>"
data-action="<?php echo $is_low_credit ? 'aksrt-need-credits' : 'aksrt-download'; ?>"
data-source-index="<?php echo esc_attr((string) $idx); ?>"
data-source-type="<?php echo esc_attr($type); ?>"
data-has-extract="<?php echo $has_extract ? '1' : '0'; ?>"
data-balance="<?php echo esc_attr((string) $balance); ?>"
data-cost="<?php echo esc_attr((string) $cost); ?>"
style="--service-color: <?php echo esc_attr($service['color']); ?>"
>
<i class="fas <?php echo esc_attr($btn_icon); ?>" aria-hidden="true"></i>
<span><?php echo esc_html($btn_text); ?></span>
<?php if ($is_low_credit) : ?>
<small class="resource-download-box__btn-sub">
<?php printf(esc_html__('当前余额 %d', 'aksrt'), $balance); ?>
</small>
<?php else : ?>
<span class="resource-download-box__service-badge" style="background: <?php echo esc_attr($service['color']); ?>">
<?php echo esc_html($service['name']); ?>
</span>
<?php endif; ?>
</button>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<div class="resource-download-box__guarantees">
<span class="resource-download-box__guarantee">
<i class="fas fa-shield-alt" aria-hidden="true"></i>
<?php esc_html_e('安全检测', 'aksrt'); ?>
</span>
<span class="resource-download-box__guarantee">
<i class="fas fa-crown" aria-hidden="true"></i>
<?php esc_html_e('支持 VIP 权益', 'aksrt'); ?>
</span>
<span class="resource-download-box__guarantee">
<i class="fas fa-coins" aria-hidden="true"></i>
<?php esc_html_e('支持积分结算', 'aksrt'); ?>
</span>
</div>
</div>
</div>
<?php if ($is_need_login) : ?>
<p class="resource-download-box__hint resource-download-box__hint--login">
<i class="fas fa-info-circle" aria-hidden="true"></i>
<?php esc_html_e('登录后可解锁并下载该内容。', 'aksrt'); ?>
</p>
<?php elseif ($is_low_credit) : ?>
<p class="resource-download-box__hint resource-download-box__hint--credit">
<i class="fas fa-info-circle" aria-hidden="true"></i>
<?php printf(esc_html__('当前余额 %1$d 积分,需要 %2$d 积分。可通过签到、阅读、邀请好友等方式获取积分。', 'aksrt'), $balance, $cost); ?>
</p>
<?php endif; ?>
<?php if ($captcha_active && function_exists('aksrt_render_captcha')) : ?>
<div class="resource-download-box__captcha resource-download-box__captcha--hidden" data-aksrt-download-captcha>
<?php aksrt_render_captcha('download'); ?>
</div>
<?php endif; ?>
<footer class="resource-download-box__footer">
<p class="resource-download-box__notice-text">
<i class="fas fa-info-circle" aria-hidden="true"></i>
<?php esc_html_e('下载能力是统一付费内容的一部分,请确认内容适用性后再使用。', 'aksrt'); ?>
</p>
</footer>
</section>