Files
wordpress-theme-aksrt/src/Widgets/WidgetPostPayBox.php
T

187 lines
8.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
/**
* Compact paid-content purchase card for the article sidebar.
*
* @package AKSRT
*/
namespace AKSRT\Widgets;
defined('ABSPATH') || exit;
class WidgetPostPayBox extends \WP_Widget {
public function __construct() {
parent::__construct(
'aksrt_post_pay_box',
esc_html__('AKSRT 付费购买', 'aksrt'),
[
'description' => esc_html__('仅在文章页 - 侧边栏展示当前文章的紧凑购买卡。', 'aksrt'),
'classname' => 'widget_aksrt_post_pay_box',
]
);
}
/**
* @param array<string,mixed> $instance
*/
public static function render_html(array $instance = []): string {
if (!is_single() || get_post_type() !== 'post') {
return '';
}
$post = get_queried_object();
if (!($post instanceof \WP_Post) || !function_exists('aksrt_get_pay_meta') || !function_exists('aksrt_is_paid_post') || !aksrt_is_paid_post($post)) {
return '';
}
$meta = aksrt_get_pay_meta($post);
$pay_type = (string) ($meta['pay_type'] ?? 'no');
if ($pay_type === '' || $pay_type === 'no') {
return '';
}
$theme = sanitize_html_class((string) ($instance['theme'] ?? 'jb-red'));
$allowed_themes = ['jb-red', 'jb-yellow', 'jb-blue', 'jb-green', 'jb-purple', 'jb-vip1', 'jb-vip2'];
if (!in_array($theme, $allowed_themes, true)) {
$theme = 'jb-red';
}
$price = function_exists('aksrt_get_user_pay_price')
? aksrt_get_user_pay_price($post)
: [
'amount' => (float) ($meta['price'] ?? 0),
'base' => (float) ($meta['price'] ?? 0),
'original' => (float) ($meta['original_price'] ?? 0),
'modo' => '0',
'currency' => '¥',
];
$amount = max(0.0, (float) ($price['amount'] ?? 0));
$base = max(0.0, (float) ($price['base'] ?? $amount));
$original = max(0.0, (float) ($price['original'] ?? 0));
$pay_modo = (($price['modo'] ?? '0') === 'points') ? 'points' : '0';
$currency = (string) ($price['currency'] ?? ($pay_modo === 'points' ? __('积分', 'aksrt') : '¥'));
$format = static function (float $value) use ($pay_modo, $currency): string {
$number = function_exists('aksrt_format_price') ? aksrt_format_price($value) : number_format($value, 2, '.', '');
return $pay_modo === 'points' ? $number . ' ' . $currency : $currency . $number;
};
$has_access = function_exists('aksrt_has_paid_access') && aksrt_has_paid_access($post);
$is_logged_in = is_user_logged_in();
$login_url = function_exists('aksrt_get_login_url') ? aksrt_get_login_url() : wp_login_url(get_permalink($post));
$demo = trim((string) ($meta['demo_url'] ?? ''));
$sales = 0;
if (function_exists('aksrt_get_download_box_meta')) {
$box = aksrt_get_download_box_meta($post);
$sales = max(0, (int) ($box['sales_count'] ?? 0));
}
$type_labels = [
'reading' => __('付费阅读', 'aksrt'),
'download' => __('付费下载', 'aksrt'),
'gallery' => __('付费图库', 'aksrt'),
'video' => __('付费视频', 'aksrt'),
'vip' => __('会员专属', 'aksrt'),
];
$type_label = $type_labels[$pay_type] ?? __('付费内容', 'aksrt');
$action_labels = [
'reading' => __('查看内容', 'aksrt'),
'download' => __('前往下载', 'aksrt'),
'gallery' => __('查看图片', 'aksrt'),
'video' => __('查看视频', 'aksrt'),
'vip' => __('查看内容', 'aksrt'),
];
$action_label = $action_labels[$pay_type] ?? __('查看内容', 'aksrt');
$show_original = $original > $amount && $original > 0;
$show_member_discount = !$has_access && $amount > 0 && $base > $amount;
ob_start();
?>
<section class="aksrt-post-pay-card <?php echo esc_attr($theme); ?><?php echo $has_access ? ' is-unlocked' : ''; ?>" aria-label="<?php esc_attr_e('付费购买', 'aksrt'); ?>">
<header class="aksrt-post-pay-card__header">
<span class="aksrt-post-pay-card__type"><?php echo esc_html($type_label); ?></span>
<?php if ($sales > 0) : ?>
<span class="aksrt-post-pay-card__sales"><?php echo esc_html(sprintf(__('已售 %s', 'aksrt'), number_format_i18n($sales))); ?></span>
<?php endif; ?>
</header>
<div class="aksrt-post-pay-card__price-row">
<?php if ($has_access) : ?>
<strong class="aksrt-post-pay-card__status"><i class="fa fa-check-circle" aria-hidden="true"></i><?php esc_html_e('已解锁', 'aksrt'); ?></strong>
<?php elseif ($amount > 0) : ?>
<strong class="aksrt-post-pay-card__price"><?php echo esc_html($format($amount)); ?></strong>
<?php if ($show_original) : ?><del><?php echo esc_html($format($original)); ?></del><?php endif; ?>
<?php else : ?>
<strong class="aksrt-post-pay-card__price"><?php esc_html_e('免费', 'aksrt'); ?></strong>
<?php endif; ?>
</div>
<?php if ($show_member_discount) : ?>
<p class="aksrt-post-pay-card__hint"><?php echo esc_html(sprintf(__('已应用会员优惠,原价 %s', 'aksrt'), $format($base))); ?></p>
<?php endif; ?>
<div class="aksrt-post-pay-card__actions">
<?php if ($has_access) : ?>
<a class="but btn-block <?php echo esc_attr($theme); ?>" href="#posts-pay"><i class="fa fa-arrow-down" aria-hidden="true"></i><?php echo esc_html($action_label); ?></a>
<?php elseif (!$is_logged_in) : ?>
<a class="but btn-block jb-blue" href="<?php echo esc_url($login_url); ?>"><i class="fa fa-sign-in" aria-hidden="true"></i><?php esc_html_e('登录后购买', 'aksrt'); ?></a>
<?php if ($amount > 0 && $pay_modo !== 'points' && function_exists('aksrt_allow_guest_content_pay') && aksrt_allow_guest_content_pay($post)) : ?>
<button type="button" class="but btn-block <?php echo esc_attr($theme); ?> cashier-link" data-aksrt-pay-content="<?php echo esc_attr((string) $post->ID); ?>"><i class="fa fa-shopping-cart" aria-hidden="true"></i><?php echo esc_html(sprintf(__('游客购买(%s)', 'aksrt'), $format($amount))); ?></button>
<?php endif; ?>
<?php else : ?>
<button type="button" class="but btn-block <?php echo esc_attr($theme); ?> cashier-link" data-aksrt-pay-content="<?php echo esc_attr((string) $post->ID); ?>"><i class="fa fa-shopping-cart" aria-hidden="true"></i><?php echo esc_html(sprintf(__('立即购买(%s)', 'aksrt'), $format($amount))); ?></button>
<?php endif; ?>
<?php if ($demo !== '') : ?>
<a class="but hollow btn-block" href="<?php echo esc_url($demo); ?>" target="_blank" rel="noopener noreferrer"><i class="fa fa-external-link" aria-hidden="true"></i><?php esc_html_e('演示地址', 'aksrt'); ?></a>
<?php endif; ?>
</div>
</section>
<?php
return (string) ob_get_clean();
}
public function widget($args, $instance): void {
$html = self::render_html($instance);
if ($html === '') {
return;
}
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
public function form($instance): void {
$theme = sanitize_html_class((string) ($instance['theme'] ?? 'jb-red'));
$options = [
'jb-red' => __('红', 'aksrt'),
'jb-yellow' => __('橙', 'aksrt'),
'jb-blue' => __('蓝', 'aksrt'),
'jb-green' => __('绿', 'aksrt'),
'jb-purple' => __('紫', 'aksrt'),
'jb-vip1' => __('会员金', 'aksrt'),
'jb-vip2' => __('会员灰', 'aksrt'),
];
?>
<p class="description"><?php esc_html_e('仅在文章页 - 侧边栏显示付费文章的紧凑购买卡。', 'aksrt'); ?></p>
<p>
<label><?php esc_html_e('色彩主题', 'aksrt'); ?>
<select class="widefat" name="<?php echo esc_attr($this->get_field_name('theme')); ?>">
<?php foreach ($options as $value => $label) : ?>
<option value="<?php echo esc_attr($value); ?>" <?php selected($theme, $value); ?>><?php echo esc_html($label); ?></option>
<?php endforeach; ?>
</select>
</label>
</p>
<?php
}
public function update($new_instance, $old_instance): array {
$theme = sanitize_html_class((string) ($new_instance['theme'] ?? 'jb-red'));
$allowed = ['jb-red', 'jb-yellow', 'jb-blue', 'jb-green', 'jb-purple', 'jb-vip1', 'jb-vip2'];
if (!in_array($theme, $allowed, true)) {
$theme = 'jb-red';
}
return ['theme' => $theme];
}
}