Files
wordpress-theme-aksrt/templates/pages/page-download.php
T

221 lines
11 KiB
PHP

<?php
/**
* Template Name: AKSRT - Download Center
* Template Post Type: page
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
get_header();
$paged = max(1, (int) get_query_var('paged'));
$per_page = 12;
$type_filter = isset($_GET['resource_type']) ? sanitize_key((string) $_GET['resource_type']) : '';
$resource_type_labels = function_exists('aksrt_get_pay_types') ? aksrt_get_pay_types() : [];
unset($resource_type_labels[AKSRT_Domain_Model::PAY_TYPE_READING]);
if ($type_filter !== '' && !isset($resource_type_labels[$type_filter])) {
$type_filter = '';
}
$query_args = [
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $per_page,
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC',
'ignore_sticky_posts' => true,
'meta_query' => [
[
'key' => AKSRT_Domain_Model::RESOURCE_MARKER_KEY,
'value' => '1',
],
],
];
if ($type_filter !== '') {
$query_args['meta_query'][] = [
'key' => AKSRT_Domain_Model::PAY_TYPE_KEY,
'value' => $type_filter,
];
}
$resource_query = new WP_Query($query_args);
$is_logged_in = is_user_logged_in();
$user_credits = 0;
$user_download_count = 0;
if ($is_logged_in) {
$uid = get_current_user_id();
if (class_exists('AKSRT_Credits')) {
$user_credits = AKSRT_Credits::balance($uid);
}
$dl_meta = get_user_meta($uid, '_aksrt_downloaded_resources', true);
$user_download_count = is_array($dl_meta) ? count($dl_meta) : 0;
}
$currency_name = class_exists('AKSRT_Settings') ? AKSRT_Settings::get_currency_name() : __('积分', 'aksrt');
$dl_settings = class_exists('AKSRT_Settings') ? AKSRT_Settings::get_cms_features() : [];
$dl_requires_login = !empty($dl_settings['download_requires_login']);
?>
<main id="main-content" class="site-main container" tabindex="-1">
<?php get_template_part('templates/parts/download/download-hero'); ?>
<div class="aksrt-download-center container">
<div class="content-wrap">
<div class="content-layout">
<div class="content-layout__primary">
<div class="aksrt-dl-status-bar">
<?php if ($is_logged_in) : ?>
<div class="aksrt-dl-status-bar__item">
<i class="fas fa-coins" aria-hidden="true"></i>
<span class="aksrt-dl-status-bar__label"><?php echo esc_html($currency_name); ?></span>
<span class="aksrt-dl-status-bar__value"><?php echo esc_html(number_format_i18n($user_credits)); ?></span>
</div>
<div class="aksrt-dl-status-bar__item">
<i class="fas fa-download" aria-hidden="true"></i>
<span class="aksrt-dl-status-bar__label"><?php esc_html_e('已下载', 'aksrt'); ?></span>
<span class="aksrt-dl-status-bar__value"><?php echo esc_html(number_format_i18n($user_download_count)); ?></span>
</div>
<div class="aksrt-dl-status-bar__item aksrt-dl-status-bar__item--hint">
<i class="fas fa-info-circle" aria-hidden="true"></i>
<span><?php esc_html_e('点击资源卡片查看详情和下载状态', 'aksrt'); ?></span>
</div>
<?php else : ?>
<div class="aksrt-dl-status-bar__item aksrt-dl-status-bar__item--hint">
<i class="fas fa-lock" aria-hidden="true"></i>
<span>
<?php if ($dl_requires_login) : ?>
<?php esc_html_e('登录后可查看下载状态和积分余额', 'aksrt'); ?>
<?php else : ?>
<?php esc_html_e('登录后可管理下载记录和积分余额', 'aksrt'); ?>
<?php endif; ?>
</span>
</div>
<div class="aksrt-dl-status-bar__actions">
<a href="<?php echo esc_url(aksrt_get_login_url()); ?>" class="btn-outline btn-sm"><?php esc_html_e('登录', 'aksrt'); ?></a>
<?php if (get_option('users_can_register')) : ?>
<a href="<?php echo esc_url(aksrt_get_register_url()); ?>" class="btn-primary btn-sm"><?php esc_html_e('注册', 'aksrt'); ?></a>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
<div class="aksrt-download-center__layout">
<div class="aksrt-download-center__main">
<?php if ($resource_query->have_posts()) : ?>
<div class="aksrt-download-center__grid">
<?php while ($resource_query->have_posts()) : $resource_query->the_post(); ?>
<article class="aksrt-download-card">
<?php if (has_post_thumbnail()) : ?>
<div class="aksrt-download-card__thumb">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('medium'); ?>
</a>
</div>
<?php endif; ?>
<div class="aksrt-download-card__body">
<h3 class="aksrt-download-card__title">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h3>
<p class="aksrt-download-card__excerpt"><?php echo esc_html(wp_trim_words(get_the_excerpt(), 20)); ?></p>
<div class="aksrt-download-card__meta">
<span class="aksrt-download-card__date">
<i class="fas fa-clock"></i>
<?php echo esc_html(get_the_date()); ?>
</span>
<?php
$dl_count = (int) get_post_meta(get_the_ID(), AKSRT_Domain_Model::RESOURCE_DOWNLOAD_COUNT_KEY, true);
if ($dl_count > 0) :
?>
<span class="aksrt-download-card__downloads">
<i class="fas fa-download"></i>
<?php echo esc_html(number_format_i18n($dl_count)); ?>
</span>
<?php endif; ?>
</div>
<?php
$card_meta = function_exists('aksrt_get_resource_meta') ? aksrt_get_resource_meta(get_the_ID()) : [];
$card_price = (float) ($card_meta['price'] ?? 0);
$price_label = function_exists('aksrt_resource_price_label')
? aksrt_resource_price_label($card_price)
: ['text' => __('免费', 'aksrt'), 'free' => true];
?>
<div class="aksrt-download-card__footer">
<span class="aksrt-download-card__price<?php echo $price_label['free'] ? ' is-free' : ' is-paid'; ?>">
<?php echo esc_html($price_label['text']); ?>
</span>
<a href="<?php the_permalink(); ?>" class="aksrt-download-card__action">
<?php esc_html_e('查看详情', 'aksrt'); ?>
<i class="fas fa-arrow-right" aria-hidden="true"></i>
</a>
</div>
</div>
</article>
<?php endwhile; ?>
</div>
<?php
if (function_exists('aksrt_render_pagination')) {
aksrt_render_pagination($resource_query);
}
wp_reset_postdata();
?>
<?php else : ?>
<div class="aksrt-download-center__empty">
<i class="fas fa-folder-open"></i>
<p><?php esc_html_e('暂无可下载资源。', 'aksrt'); ?></p>
</div>
<?php endif; ?>
</div>
<aside class="aksrt-download-center__sidebar sidebar">
<?php get_template_part('templates/parts/download/download-permission-card'); ?>
<div class="aksrt-dl-sidebar-card">
<h3 class="aksrt-dl-sidebar-card__title">
<i class="fas fa-folder"></i>
<?php esc_html_e('资源类型', 'aksrt'); ?>
</h3>
<?php if (!empty($resource_type_labels)) : ?>
<ul class="aksrt-dl-sidebar-card__list">
<li class="<?php echo $type_filter === '' ? 'is-active' : ''; ?>">
<a href="<?php echo esc_url(get_permalink()); ?>">
<?php esc_html_e('全部资源', 'aksrt'); ?>
</a>
</li>
<?php foreach ($resource_type_labels as $type_key => $type_label) : ?>
<li class="<?php echo $type_filter === $type_key ? 'is-active' : ''; ?>">
<a href="<?php echo esc_url(add_query_arg('resource_type', $type_key, get_permalink())); ?>">
<?php echo esc_html($type_label); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
<div class="aksrt-dl-sidebar-card">
<h3 class="aksrt-dl-sidebar-card__title">
<i class="fas fa-lightbulb"></i>
<?php esc_html_e('下载须知', 'aksrt'); ?>
</h3>
<div class="aksrt-dl-sidebar-card__tips">
<p><i class="fas fa-shield-alt" aria-hidden="true"></i> <?php esc_html_e('所有资源均经过安全检测', 'aksrt'); ?></p>
<p><i class="fas fa-clock" aria-hidden="true"></i> <?php esc_html_e('下载链接 10 分钟内有效', 'aksrt'); ?></p>
<p><i class="fas fa-sync-alt" aria-hidden="true"></i> <?php esc_html_e('已下载资源可再次下载', 'aksrt'); ?></p>
</div>
</div>
</div>
</div>
</div>
</main>
<?php
get_footer();