better-staridc-MNBT
1<?php
2/**
3 * 我的主机视图([mnbt_my_hosts])。
4 *
5 * @package MnbtWp
6 */
7
8if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10}
11
12$mnbtwp_nonce = wp_create_nonce( 'mnbtwp_front' );
13?>
14<div class="mnbtwp-hosts">
15 <?php if ( empty( $hosts ) ) : ?>
16 <p><?php esc_html_e( '您还没有购买任何主机。', 'wp-seller-plugin' ); ?></p>
17 <?php else : ?>
18 <?php foreach ( $hosts as $mnbtwp_host ) : ?>
19 <?php
20 $mnbtwp_provider = isset( $provider_map[ $mnbtwp_host['provider_id'] ] ) ? $provider_map[ $mnbtwp_host['provider_id'] ] : null;
21 $mnbtwp_quota = Helpers::parse_quota( $mnbtwp_host['quota_json'] );
22 $mnbtwp_status = $mnbtwp_host['status'];
23 $mnbtwp_label = Helpers::host_status_label( $mnbtwp_status );
24 $mnbtwp_console = $mnbtwp_provider ? Helpers::control_url( $mnbtwp_provider ) : '';
25 ?>
26 <div class="mnbtwp-host-card" style="border:1px solid #ddd;border-radius:8px;padding:16px;margin-bottom:16px;background:#fff;">
27 <div style="display:flex;justify-content:space-between;align-items:center;flex-wrap:wrap;gap:8px;">
28 <div>
29 <strong style="font-size:16px;"><?php echo esc_html( $mnbtwp_host['site_domain'] ? $mnbtwp_host['site_domain'] : $mnbtwp_host['username'] ); ?></strong>
30 <span style="margin-left:8px;padding:2px 8px;border-radius:10px;font-size:12px;
31 <?php echo in_array( $mnbtwp_status, array( 'active', 'provisioning' ), true ) ? 'background:#e6f7e6;color:#1a7f37;' : 'background:#fdeaea;color:#c62828;'; ?>">
32 <?php echo esc_html( $mnbtwp_label ); ?>
33 </span>
34 </div>
35 <div>
36 <?php if ( $mnbtwp_console ) : ?>
37 <a class="button" href="<?php echo esc_url( $mnbtwp_console ); ?>" target="_blank" rel="noopener">
38 <?php esc_html_e( '打开控制台', 'wp-seller-plugin' ); ?>
39 </a>
40 <?php endif; ?>
41 </div>
42 </div>
43
44 <table style="width:100%;margin-top:12px;border-collapse:collapse;font-size:13px;">
45 <tr>
46 <td style="padding:4px 0;color:#666;width:90px;"><?php esc_html_e( '主机账号', 'wp-seller-plugin' ); ?></td>
47 <td><code><?php echo esc_html( $mnbtwp_host['username'] ); ?></code></td>
48 <td style="padding:4px 0;color:#666;width:90px;"><?php esc_html_e( '到期时间', 'wp-seller-plugin' ); ?></td>
49 <td>
50 <?php
51 echo esc_html(
52 $mnbtwp_host['expire_date'] && '0000-00-00' !== $mnbtwp_host['expire_date']
53 ? $mnbtwp_host['expire_date']
54 : __( '长期', 'wp-seller-plugin' )
55 );
56 ?>
57 </td>
58 </tr>
59 </table>
60
61 <?php foreach ( array( 'web' => __( '网站空间', 'wp-seller-plugin' ), 'sql' => __( '数据库', 'wp-seller-plugin' ), 'flow' => __( '流量', 'wp-seller-plugin' ) ) as $mnbtwp_key => $mnbtwp_name ) : ?>
62 <?php
63 $mnbtwp_max = (int) ( $mnbtwp_quota[ $mnbtwp_key ]['max'] ?? 0 );
64 $mnbtwp_used = (int) ( $mnbtwp_quota[ $mnbtwp_key ]['used'] ?? 0 );
65 if ( $mnbtwp_max <= 0 ) {
66 continue;
67 }
68 $mnbtwp_pct = min( 100, (int) round( $mnbtwp_used / $mnbtwp_max * 100 ) );
69 ?>
70 <div style="margin-top:8px;">
71 <div style="display:flex;justify-content:space-between;font-size:12px;color:#666;">
72 <span><?php echo esc_html( $mnbtwp_name ); ?></span>
73 <span><?php echo (int) $mnbtwp_used; ?> / <?php echo (int) $mnbtwp_max; ?> MB</span>
74 </div>
75 <div style="background:#eee;border-radius:4px;height:8px;margin-top:4px;">
76 <div style="width:<?php echo (int) $mnbtwp_pct; ?>%;height:8px;border-radius:4px;background:<?php echo $mnbtwp_pct >= 90 ? '#d63638' : '#2271b1'; ?>;"></div>
77 </div>
78 </div>
79 <?php endforeach; ?>
80
81 <div style="margin-top:14px;display:flex;gap:8px;flex-wrap:wrap;">
82 <button type="button" class="button" onclick="mnbtwpAction(<?php echo (int) $mnbtwp_host['id']; ?>,'start')"><?php esc_html_e( '启动', 'wp-seller-plugin' ); ?></button>
83 <button type="button" class="button" onclick="mnbtwpAction(<?php echo (int) $mnbtwp_host['id']; ?>,'stop')"><?php esc_html_e( '停止', 'wp-seller-plugin' ); ?></button>
84 <button type="button" class="button" onclick="mnbtwpChangePass(<?php echo (int) $mnbtwp_host['id']; ?>)"><?php esc_html_e( '重置密码', 'wp-seller-plugin' ); ?></button>
85 <button type="button" class="button" onclick="mnbtwpRenew(<?php echo (int) $mnbtwp_host['id']; ?>)"><?php esc_html_e( '续费', 'wp-seller-plugin' ); ?></button>
86 <button type="button" class="button" style="color:#c62828;" onclick="mnbtwpDelete(<?php echo (int) $mnbtwp_host['id']; ?>)"><?php esc_html_e( '删除', 'wp-seller-plugin' ); ?></button>
87 </div>
88 </div>
89 <?php endforeach; ?>
90 <?php endif; ?>
91</div>
92
93<style>
94.mnbtwp-host-card .button { margin: 0; }
95</style>
96
97<script type="text/javascript">
98 var mnbtwpFrontNonce = '<?php echo esc_js( $mnbtwp_nonce ); ?>';
99
100 function mnbtwpAjax(data, done) {
101 data.nonce = mnbtwpFrontNonce;
102 jQuery.post('<?php echo esc_js( admin_url( 'admin-ajax.php' ) ); ?>', data, function (r) {
103 if (r && r.success) {
104 done(r);
105 } else {
106 alert((r && r.data) ? r.data : '<?php echo esc_js( __( '操作失败', 'wp-seller-plugin' ) ); ?>');
107 }
108 }).fail(function () {
109 alert('<?php echo esc_js( __( '网络错误', 'wp-seller-plugin' ) ); ?>');
110 });
111 }
112
113 function mnbtwpAction(hostId, action) {
114 if (action === 'delete') return;
115 mnbtwpAjax({ action: 'mnbtwp_host_action', host_action: action, host_id: hostId }, function () {
116 location.reload();
117 });
118 }
119
120 function mnbtwpChangePass(hostId) {
121 var pw = prompt('<?php echo esc_js( __( '请输入新密码(至少 6 位)', 'wp-seller-plugin' ) ); ?>');
122 if (!pw || pw.length < 6) return;
123 mnbtwpAjax({ action: 'mnbtwp_host_action', host_action: 'change_pass', host_id: hostId, password: pw }, function () {
124 alert('<?php echo esc_js( __( '密码已重置', 'wp-seller-plugin' ) ); ?>');
125 });
126 }
127
128 function mnbtwpDelete(hostId) {
129 if (!confirm('<?php echo esc_js( __( '删除后站点、数据库、FTP 将被清除且不可恢复,确定删除?', 'wp-seller-plugin' ) ); ?>')) return;
130 mnbtwpAjax({ action: 'mnbtwp_host_action', host_action: 'delete', host_id: hostId }, function () {
131 location.reload();
132 });
133 }
134
135 function mnbtwpRenew(hostId) {
136 var months = prompt('<?php echo esc_js( __( '请输入续费月数(如 1 / 3 / 6 / 12)', 'wp-seller-plugin' ) ); ?>', '1');
137 if (!months || parseInt(months, 10) < 1) return;
138 mnbtwpAjax({ action: 'mnbtwp_create_renew', host_id: hostId, months: parseInt(months, 10) }, function (r) {
139 if (r.data && r.data.checkout_url) {
140 window.location.href = r.data.checkout_url;
141 }
142 });
143 }
144</script>