Files
wordpress-theme-aksrt/templates/pages/page-download-file.php
T
kate522 b447c41f13 refactor(admin): 清理付费 meta 死字段并补齐商品介绍渲染
延续上一轮 pay_count「只写不读」的排查,把其余写而不读的字段一次
性处理完。按字段性质分两类处理,不是一律删。

【删除】纯数据垃圾,无界面入口、无消费方:
- card_keys / purchase_limit / stock:只有 DomainModel 的默认值与
  sanitize,全仓零引用。契约测试原本还断言它们不得出现在 metabox 上。
- 随 card_keys 一并删除 sanitle_card_keys() 助手。
- video_scale_height「固定长宽比例」:参考主题里这是轮播组件 widget
  的参数,不属于付费视频字段,当初误搬到付费 metabox 上且从未被消费。
  连默认值、sanitize、metabox 保存项一起移除,并同步修正契约测试。

【补齐】有界面入口、管理员填了但不显示的四个字段:
pay_title / pay_doc / pay_details / pay_extra_hide 此前只有 metabox
输入框和 sanitize,前端一个都不读。按参考主题 pages/download.php
的结构接到单篇资源下载页:
- pay_title 有值则替代文章标题作商品标题(回退 get_the_title);
- pay_doc 商品简介、pay_details 更多详情渲染在标题区下方;
- pay_extra_hide 额外隐藏内容只在有下载权限时渲染在下载盒之后。
三处 HTML 输出均走 wp_kses_post,与 sanitize 侧的安全 HTML 口径一致。

同时把 pay_extra_hide 接进文章页付费盒
(aksrt_get_paid_box_html):该函数本身只在有付费权限时执行,天然
就是「付费后才可见」的位置,复用已有的 .pay-extra-hide 样式。

验证:php -l 四个 PHP 文件无语法错误;
node --test tests/widget-system/*.test.cjs 449 项全部通过。
2026-10-06 20:17:58 +08:00

209 lines
9.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* Template Name: AKSRT - 单篇资源下载
* Template Post Type: page
*
* legacy pages/download.php equivalent.
* URL: {page}?post={post_id}
* Only for pay_type=download posts. Shows download buttons when allowed,
* or a no-permission state with a back link to the article.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
$post_id = isset($_GET['post']) ? absint($_GET['post']) : 0;
$post = $post_id > 0 ? get_post($post_id) : null;
if (!$post instanceof WP_Post || $post->post_status !== 'publish') {
get_header();
echo '<main class="container"><div class="content-wrap"><div class="content-layout">';
echo '<div class="alert jb-red em12" style="margin:2em;"><b>' . esc_html__('无效的下载请求', 'aksrt') . '</b></div>';
echo '</div></div></main>';
get_footer();
exit;
}
$pay_meta = function_exists('aksrt_get_pay_meta') ? aksrt_get_pay_meta($post) : ['pay_type' => 'no', 'downloads' => []];
$pay_type = (string) ($pay_meta['pay_type'] ?? 'no');
$is_download = $pay_type === 'download' || (function_exists('aksrt_is_downloadable_content') && aksrt_is_downloadable_content($post));
if (!$is_download) {
get_header();
echo '<main class="container"><div class="content-wrap"><div class="content-layout">';
echo '<div class="alert jb-red em12" style="margin:2em;"><b>' . esc_html__('该内容不是可下载资源', 'aksrt') . '</b></div>';
echo '<p><a class="but jb-yellow padding-lg" href="' . esc_url(get_permalink($post)) . '#posts-pay"><i class="fa fas fa-long-arrow-alt-left" aria-hidden="true"></i><span class="ml10">' . esc_html__('返回文章', 'aksrt') . '</span></a></p>';
echo '</div></div></main>';
get_footer();
exit;
}
$article_url = get_permalink($post) . '#posts-pay';
// 商品简介 / 更多详情 / 额外隐藏内容三个字段只在管理员填写时才有输出。
$pay_title = trim((string) ($pay_meta['pay_title'] ?? '')) !== '' ? (string) $pay_meta['pay_title'] : get_the_title($post);
$pay_doc = trim((string) ($pay_meta['pay_doc'] ?? ''));
$pay_details = trim((string) ($pay_meta['pay_details'] ?? ''));
$pay_extra_hide = trim((string) ($pay_meta['pay_extra_hide'] ?? ''));
$subtitle = '';
if (function_exists('get_the_subtitle')) {
// noop — AKSRT may not have legacy subtitle helper
}
$has_access = function_exists('aksrt_has_paid_access') ? aksrt_has_paid_access($post) : false;
$gate = function_exists('aksrt_can_download') ? aksrt_can_download($post) : true;
$can_dl = $gate === true;
$gate_code = is_wp_error($gate) ? $gate->get_error_code() : '';
$need_login = $gate_code === 'aksrt_need_login';
$need_purchase = $gate_code === 'aksrt_need_purchase';
$need_vip = $gate_code === 'aksrt_need_vip';
$priced = function_exists('aksrt_get_user_pay_price') ? aksrt_get_user_pay_price($post) : null;
$price = is_array($priced) ? (float) ($priced['amount'] ?? 0) : (float) ($pay_meta['price'] ?? 0);
$pay_modo = is_array($priced) ? ((($priced['modo'] ?? '0') === 'points') ? 'points' : '0') : ((($pay_meta['pay_modo'] ?? '0') === 'points') ? 'points' : '0');
$currency = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_currency_name() : __('积分', 'aksrt');
$cash_symbol = '¥';
if (class_exists('\AKSRT\Settings\Settings')) {
$paid_display = \AKSRT\Settings\Settings::get_paid_content_settings();
$cash_symbol = (string) ($paid_display['display']['currency_symbol'] ?? '¥');
}
$price_text = $price <= 0
? __('免费', 'aksrt')
: ($pay_modo === 'points'
? (function_exists('aksrt_format_price') ? aksrt_format_price($price) : (string) $price) . ' ' . $currency
: $cash_symbol . (function_exists('aksrt_format_price') ? aksrt_format_price($price) : (string) $price));
$login_url = wp_login_url(add_query_arg('post', $post_id, get_permalink()));
$vip_url = function_exists('aksrt_get_user_center_url') ? aksrt_get_user_center_url() : home_url('/');
$paid_name = '';
if ($has_access || $can_dl) {
if ($price <= 0) {
$paid_name = __('免费资源', 'aksrt');
} else {
$paid_name = __('已获得下载权限', 'aksrt');
}
}
get_header();
?>
<style>
.aksrt-single-download .but-download > .but,
.aksrt-single-download .but-download > a,
.aksrt-single-download .but-download > button,
.aksrt-single-download .resource-download-box__btn {
min-width: 200px;
padding: .5em;
margin-top: 10px;
}
.aksrt-single-download .pay-extra-hide {
background: var(--muted-border-color, #f0f0f0);
display: block;
margin: 10px 0;
padding: 20px;
color: var(--muted-color, #888);
border-radius: 4px;
}
</style>
<main class="container aksrt-single-download">
<div class="content-wrap">
<div class="content-layout">
<article class="article page-article main-bg theme-box box-body radius8 main-shadow">
<div class="pay-box">
<div class="article-header theme-box">
<div class="article-title">
<a href="<?php echo esc_url($article_url); ?>"><?php echo esc_html($pay_title); ?></a>
</div>
<?php if ($paid_name !== '') : ?>
<b class="badg jb-red mr6" style="font-size:12px;padding:2px 10px;line-height:1.4;">
<i class="fa fas fa-check mr6" aria-hidden="true"></i><?php echo esc_html($paid_name); ?>
</b>
<?php endif; ?>
</div>
<?php if ($pay_doc !== '') : ?>
<div><?php echo wp_kses_post($pay_doc); ?></div>
<?php endif; ?>
<?php if ($pay_details !== '') : ?>
<div class="muted-2-color em09" style="margin:2em 0;"><?php echo wp_kses_post($pay_details); ?></div>
<?php endif; ?>
<?php if ($can_dl) : ?>
<div style="margin-bottom:2em;" class="pay-box">
<?php
// Reuse single download box UI for the allowed state.
global $post;
$GLOBALS['post'] = $post;
setup_postdata($post);
$GLOBALS['aksrt_on_download_file_page'] = true;
get_template_part('templates/parts/resource-download-box');
unset($GLOBALS['aksrt_on_download_file_page']);
wp_reset_postdata();
?>
</div>
<?php if ($pay_extra_hide !== '') : ?>
<div class="pay-extra-hide"><?php echo wp_kses_post($pay_extra_hide); ?></div>
<?php endif; ?>
<?php elseif ($need_login) : ?>
<div class="alert jb-yellow em12" style="margin:2em;">
<b><?php echo ($price <= 0) ? esc_html__('免费资源,请登录后下载', 'aksrt') : esc_html__('请先登录后再下载', 'aksrt'); ?></b>
</div>
<div class="pay-extra-hide">
<?php
printf(
/* translators: %s: login url */
wp_kses_post(__('Hi!请先 <a href="%s">登录</a> 后再下载。', 'aksrt')),
esc_url($login_url)
);
?>
</div>
<a style="margin-bottom:2em;" href="<?php echo esc_url($login_url); ?>" class="but jb-red padding-lg">
<i class="fa fas fa-sign-in-alt" aria-hidden="true"></i>
<span class="ml10"><?php esc_html_e('前往登录', 'aksrt'); ?></span>
</a>
<?php elseif ($need_vip) : ?>
<div class="alert jb-yellow em12" style="margin:2em;">
<b><?php echo esc_html(is_wp_error($gate) ? $gate->get_error_message() : __('该资源仅限会员购买/下载。', 'aksrt')); ?></b>
</div>
<a style="margin-bottom:1em;" href="<?php echo esc_url($vip_url); ?>" class="but jb-vip padding-lg">
<i class="fa fas fa-certificate" aria-hidden="true"></i>
<span class="ml10"><?php esc_html_e('开通会员', 'aksrt'); ?></span>
</a>
<a style="margin-bottom:2em;" href="<?php echo esc_url($article_url); ?>" class="but jb-yellow padding-lg">
<i class="fa fas fa-long-arrow-alt-left" aria-hidden="true"></i>
<span class="ml10"><?php esc_html_e('返回文章', 'aksrt'); ?></span>
</a>
<?php elseif ($need_purchase) : ?>
<div class="alert jb-red em12" style="margin:2em;">
<b><?php esc_html_e('请先购买后再下载', 'aksrt'); ?></b>
</div>
<button
type="button"
class="but <?php echo $pay_modo === 'points' ? 'jb-yellow' : 'jb-red'; ?> padding-lg cashier-link"
data-action="aksrt-need-purchase"
data-aksrt-pay-content="<?php echo esc_attr((string) $post_id); ?>"
data-post-id="<?php echo esc_attr((string) $post_id); ?>"
style="margin-bottom:1em;"
>
<i class="fa fas fa-shopping-cart" aria-hidden="true"></i>
<span class="ml10"><?php printf(esc_html__('立即购买(%s)', 'aksrt'), $price_text); ?></span>
</button>
<a style="margin-bottom:2em;" href="<?php echo esc_url($article_url); ?>" class="but jb-yellow padding-lg">
<i class="fa fas fa-long-arrow-alt-left" aria-hidden="true"></i>
<span class="ml10"><?php esc_html_e('返回文章', 'aksrt'); ?></span>
</a>
<?php else : ?>
<div class="alert jb-red em12" style="margin:2em;">
<b><?php echo esc_html(is_wp_error($gate) ? $gate->get_error_message() : __('暂无下载权限', 'aksrt')); ?></b>
</div>
<a style="margin-bottom:2em;" href="<?php echo esc_url($article_url); ?>" class="but jb-yellow padding-lg">
<i class="fa fas fa-long-arrow-alt-left" aria-hidden="true"></i>
<span class="ml10"><?php esc_html_e('返回文章', 'aksrt'); ?></span>
</a>
<?php endif; ?>
</div>
</article>
</div>
</div>
</main>
<?php
get_footer();