250 lines
11 KiB
PHP
250 lines
11 KiB
PHP
<?php
|
|
/**
|
|
* 我的订阅前端页面模板
|
|
*
|
|
* @package Yoone_Subscriptions
|
|
* @version 1.0.0
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit; // 防止直接访问
|
|
}
|
|
|
|
$user_id = get_current_user_id();
|
|
|
|
if (!$user_id) {
|
|
wc_print_notice(__('请先登录查看您的订阅', 'yoone-subscriptions'), 'error');
|
|
return;
|
|
}
|
|
|
|
global $wpdb;
|
|
|
|
// 获取用户的订阅
|
|
$subscriptions = $wpdb->get_results($wpdb->prepare(
|
|
"SELECT s.*, p.post_title as product_name
|
|
FROM {$wpdb->prefix}yoone_subscriptions s
|
|
LEFT JOIN {$wpdb->posts} p ON s.product_id = p.ID
|
|
WHERE s.user_id = %d
|
|
ORDER BY s.created_at DESC",
|
|
$user_id
|
|
));
|
|
|
|
?>
|
|
|
|
<div class="yoone-my-subscriptions">
|
|
<h2><?php _e('我的订阅', 'yoone-subscriptions'); ?></h2>
|
|
|
|
<?php if (empty($subscriptions)): ?>
|
|
|
|
<div class="woocommerce-message woocommerce-message--info woocommerce-Message woocommerce-Message--info woocommerce-info">
|
|
<p><?php _e('您还没有任何订阅。', 'yoone-subscriptions'); ?></p>
|
|
<p>
|
|
<a href="<?php echo wc_get_page_permalink('shop'); ?>" class="button">
|
|
<?php _e('浏览产品', 'yoone-subscriptions'); ?>
|
|
</a>
|
|
</p>
|
|
</div>
|
|
|
|
<?php else: ?>
|
|
|
|
<table class="woocommerce-orders-table woocommerce-MyAccount-orders shop_table shop_table_responsive my_account_orders account-orders-table">
|
|
<thead>
|
|
<tr>
|
|
<th class="woocommerce-orders-table__header woocommerce-orders-table__header-subscription-id">
|
|
<span class="nobr"><?php _e('订阅', 'yoone-subscriptions'); ?></span>
|
|
</th>
|
|
<th class="woocommerce-orders-table__header woocommerce-orders-table__header-subscription-status">
|
|
<span class="nobr"><?php _e('状态', 'yoone-subscriptions'); ?></span>
|
|
</th>
|
|
<th class="woocommerce-orders-table__header woocommerce-orders-table__header-subscription-next-payment">
|
|
<span class="nobr"><?php _e('下次付款', 'yoone-subscriptions'); ?></span>
|
|
</th>
|
|
<th class="woocommerce-orders-table__header woocommerce-orders-table__header-subscription-total">
|
|
<span class="nobr"><?php _e('总计', 'yoone-subscriptions'); ?></span>
|
|
</th>
|
|
<th class="woocommerce-orders-table__header woocommerce-orders-table__header-subscription-actions">
|
|
<span class="nobr"><?php _e('操作', 'yoone-subscriptions'); ?></span>
|
|
</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($subscriptions as $subscription): ?>
|
|
<tr class="woocommerce-orders-table__row woocommerce-orders-table__row--status-<?php echo esc_attr($subscription->status); ?> order">
|
|
<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-id" data-title="<?php _e('订阅', 'yoone-subscriptions'); ?>">
|
|
<a href="<?php echo esc_url(wc_get_account_endpoint_url('yoone-subscriptions') . '/' . $subscription->id); ?>">
|
|
#<?php echo $subscription->id; ?>
|
|
</a>
|
|
<br>
|
|
<small><?php echo esc_html($subscription->product_name ?: __('未知产品', 'yoone-subscriptions')); ?></small>
|
|
</td>
|
|
<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-status" data-title="<?php _e('状态', 'yoone-subscriptions'); ?>">
|
|
<span class="subscription-status status-<?php echo esc_attr($subscription->status); ?>">
|
|
<?php echo Yoone_Frontend::get_subscription_status_label($subscription->status); ?>
|
|
</span>
|
|
</td>
|
|
<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-next-payment" data-title="<?php _e('下次付款', 'yoone-subscriptions'); ?>">
|
|
<?php if ($subscription->next_payment_date && $subscription->status === 'active'): ?>
|
|
<time datetime="<?php echo esc_attr($subscription->next_payment_date); ?>">
|
|
<?php echo date_i18n(wc_date_format(), strtotime($subscription->next_payment_date)); ?>
|
|
</time>
|
|
<?php else: ?>
|
|
<span class="na">–</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-total" data-title="<?php _e('总计', 'yoone-subscriptions'); ?>">
|
|
<span class="woocommerce-Price-amount amount">
|
|
<?php echo Yoone_Helper::format_price($subscription->subscription_price); ?>
|
|
</span>
|
|
<small class="woocommerce-Price-currencySymbol">
|
|
/ <?php echo Yoone_Helper::format_billing_period($subscription->billing_period, $subscription->billing_interval); ?>
|
|
</small>
|
|
</td>
|
|
<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-actions" data-title="<?php _e('操作', 'yoone-subscriptions'); ?>">
|
|
<a href="<?php echo esc_url(wc_get_account_endpoint_url('yoone-subscriptions') . '/' . $subscription->id); ?>" class="woocommerce-button button view">
|
|
<?php _e('查看', 'yoone-subscriptions'); ?>
|
|
</a>
|
|
|
|
<?php if ($subscription->status === 'active'): ?>
|
|
<a href="<?php echo esc_url(wp_nonce_url(add_query_arg(array('action' => 'pause', 'subscription_id' => $subscription->id)), 'pause_subscription_' . $subscription->id)); ?>"
|
|
class="woocommerce-button button pause"
|
|
onclick="return confirm('<?php _e('确定要暂停这个订阅吗?', 'yoone-subscriptions'); ?>')">
|
|
<?php _e('暂停', 'yoone-subscriptions'); ?>
|
|
</a>
|
|
<?php elseif ($subscription->status === 'paused'): ?>
|
|
<a href="<?php echo esc_url(wp_nonce_url(add_query_arg(array('action' => 'resume', 'subscription_id' => $subscription->id)), 'resume_subscription_' . $subscription->id)); ?>"
|
|
class="woocommerce-button button resume">
|
|
<?php _e('恢复', 'yoone-subscriptions'); ?>
|
|
</a>
|
|
<?php endif; ?>
|
|
|
|
<?php if (in_array($subscription->status, array('active', 'paused'))): ?>
|
|
<a href="<?php echo esc_url(wp_nonce_url(add_query_arg(array('action' => 'cancel', 'subscription_id' => $subscription->id)), 'cancel_subscription_' . $subscription->id)); ?>"
|
|
class="woocommerce-button button cancel"
|
|
onclick="return confirm('<?php _e('确定要取消这个订阅吗?此操作不可撤销。', 'yoone-subscriptions'); ?>')">
|
|
<?php _e('取消', 'yoone-subscriptions'); ?>
|
|
</a>
|
|
<?php endif; ?>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<style>
|
|
.yoone-my-subscriptions .subscription-status {
|
|
padding: 3px 8px;
|
|
border-radius: 3px;
|
|
font-size: 11px;
|
|
font-weight: bold;
|
|
text-transform: uppercase;
|
|
color: white;
|
|
}
|
|
|
|
.yoone-my-subscriptions .status-active { background: #46b450; }
|
|
.yoone-my-subscriptions .status-paused { background: #ffb900; }
|
|
.yoone-my-subscriptions .status-cancelled { background: #dc3232; }
|
|
.yoone-my-subscriptions .status-expired { background: #666; }
|
|
|
|
.yoone-my-subscriptions .woocommerce-button {
|
|
margin-right: 5px;
|
|
margin-bottom: 5px;
|
|
}
|
|
|
|
.yoone-my-subscriptions .woocommerce-button.pause {
|
|
background-color: #ffb900;
|
|
border-color: #ffb900;
|
|
}
|
|
|
|
.yoone-my-subscriptions .woocommerce-button.resume {
|
|
background-color: #46b450;
|
|
border-color: #46b450;
|
|
}
|
|
|
|
.yoone-my-subscriptions .woocommerce-button.cancel {
|
|
background-color: #dc3232;
|
|
border-color: #dc3232;
|
|
}
|
|
|
|
@media (max-width: 768px) {
|
|
.yoone-my-subscriptions .woocommerce-orders-table__cell {
|
|
display: block;
|
|
text-align: right;
|
|
border: none;
|
|
border-bottom: 1px solid #e1e1e1;
|
|
padding: 0.5em 0;
|
|
}
|
|
|
|
.yoone-my-subscriptions .woocommerce-orders-table__cell:before {
|
|
content: attr(data-title) ": ";
|
|
font-weight: bold;
|
|
float: left;
|
|
}
|
|
|
|
.yoone-my-subscriptions .woocommerce-orders-table__cell-subscription-actions {
|
|
text-align: left;
|
|
}
|
|
|
|
.yoone-my-subscriptions .woocommerce-orders-table__cell-subscription-actions:before {
|
|
display: none;
|
|
}
|
|
}
|
|
</style>
|
|
|
|
<?php
|
|
// 处理订阅操作
|
|
if (isset($_GET['action']) && isset($_GET['subscription_id'])) {
|
|
$action = sanitize_text_field($_GET['action']);
|
|
$subscription_id = intval($_GET['subscription_id']);
|
|
|
|
// 验证订阅属于当前用户
|
|
$subscription = $wpdb->get_row($wpdb->prepare(
|
|
"SELECT * FROM {$wpdb->prefix}yoone_subscriptions WHERE id = %d AND user_id = %d",
|
|
$subscription_id,
|
|
$user_id
|
|
));
|
|
|
|
if ($subscription) {
|
|
switch ($action) {
|
|
case 'pause':
|
|
if (wp_verify_nonce($_GET['_wpnonce'], 'pause_subscription_' . $subscription_id)) {
|
|
$wpdb->update(
|
|
$wpdb->prefix . 'yoone_subscriptions',
|
|
array('status' => 'paused', 'updated_at' => current_time('mysql')),
|
|
array('id' => $subscription_id)
|
|
);
|
|
wc_add_notice(__('订阅已暂停', 'yoone-subscriptions'), 'success');
|
|
}
|
|
break;
|
|
|
|
case 'resume':
|
|
if (wp_verify_nonce($_GET['_wpnonce'], 'resume_subscription_' . $subscription_id)) {
|
|
$wpdb->update(
|
|
$wpdb->prefix . 'yoone_subscriptions',
|
|
array('status' => 'active', 'updated_at' => current_time('mysql')),
|
|
array('id' => $subscription_id)
|
|
);
|
|
wc_add_notice(__('订阅已恢复', 'yoone-subscriptions'), 'success');
|
|
}
|
|
break;
|
|
|
|
case 'cancel':
|
|
if (wp_verify_nonce($_GET['_wpnonce'], 'cancel_subscription_' . $subscription_id)) {
|
|
$wpdb->update(
|
|
$wpdb->prefix . 'yoone_subscriptions',
|
|
array('status' => 'cancelled', 'end_date' => current_time('mysql'), 'updated_at' => current_time('mysql')),
|
|
array('id' => $subscription_id)
|
|
);
|
|
wc_add_notice(__('订阅已取消', 'yoone-subscriptions'), 'success');
|
|
}
|
|
break;
|
|
}
|
|
|
|
// 重定向以避免重复提交
|
|
wp_redirect(wc_get_account_endpoint_url('yoone-subscriptions'));
|
|
exit;
|
|
}
|
|
}
|
|
?>
|