better-staridc-MNBT
1<?php
2/**
3 * 前台:我的主机 Shortcode + 前端 AJAX。
4 *
5 * @package MnbtWp
6 */
7
8namespace MnbtWp;
9
10use MnbtWp\Services\Provision;
11use MnbtWp\Woo\Woo;
12
13if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15}
16
17/**
18 * 前台控制器。
19 */
20class Front {
21
22 /**
23 * 构造:注册 Shortcode 与 AJAX。
24 */
25 public function __construct() {
26 add_shortcode( 'mnbt_my_hosts', array( $this, 'shortcode_my_hosts' ) );
27 add_action( 'wp_ajax_mnbtwp_host_action', array( $this, 'ajax_host_action' ) );
28 add_action( 'wp_ajax_nopriv_mnbtwp_host_action', array( $this, 'ajax_denied' ) );
29 add_action( 'wp_ajax_mnbtwp_create_renew', array( $this, 'ajax_create_renew' ) );
30 add_action( 'wp_ajax_nopriv_mnbtwp_create_renew', array( $this, 'ajax_denied' ) );
31 }
32
33 /**
34 * 未登录 AJAX 拒绝。
35 */
36 public function ajax_denied() {
37 wp_send_json_error( __( '请先登录', 'wp-seller-plugin' ) );
38 }
39
40 /**
41 * [mnbt_my_hosts] Shortcode:列出当前用户主机。
42 *
43 * @param array $atts Shortcode 属性。
44 * @return string
45 */
46 public function shortcode_my_hosts( $atts ) {
47 if ( ! is_user_logged_in() ) {
48 return '<p>' . esc_html__( '请先登录后查看您的主机。', 'wp-seller-plugin' ) . '</p>';
49 }
50
51 global $wpdb;
52 $user_id = get_current_user_id();
53 $hosts = $wpdb->get_results(
54 $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mnbt_hosts WHERE user_id=%d ORDER BY id DESC", $user_id ),
55 ARRAY_A
56 );
57
58 $provider_map = array();
59 foreach ( $hosts as $h ) {
60 if ( ! isset( $provider_map[ $h['provider_id'] ] ) ) {
61 $provider_map[ $h['provider_id'] ] = Helpers::get_provider( $h['provider_id'] );
62 }
63 }
64
65 ob_start();
66 include MNBTWP_DIR . 'includes/front/views/my-hosts.php';
67 return ob_get_clean();
68 }
69
70 /**
71 * 主机操作:start / stop / change_pass / delete。
72 */
73 public function ajax_host_action() {
74 check_ajax_referer( 'mnbtwp_front', 'nonce' );
75 if ( ! is_user_logged_in() ) {
76 wp_send_json_error( __( '请先登录', 'wp-seller-plugin' ) );
77 }
78
79 $action = sanitize_key( $_POST['host_action'] ?? '' );
80 $host_id = (int) ( $_POST['host_id'] ?? 0 );
81 $host = Helpers::get_host( $host_id );
82
83 if ( ! $host || (int) $host['user_id'] !== get_current_user_id() ) {
84 wp_send_json_error( __( '无权操作', 'wp-seller-plugin' ) );
85 }
86 $provider = Helpers::get_provider( $host['provider_id'] );
87 if ( ! $provider ) {
88 wp_send_json_error( __( '节点不可用', 'wp-seller-plugin' ) );
89 }
90
91 try {
92 $client = Helpers::client_for_provider( $provider );
93 switch ( $action ) {
94 case 'start':
95 $client->startSite( $host['username'] );
96 break;
97 case 'stop':
98 $client->stopSite( $host['username'] );
99 break;
100 case 'change_pass':
101 $password = sanitize_text_field( $_POST['password'] ?? '' );
102 if ( mb_strlen( $password ) < 6 ) {
103 wp_send_json_error( __( '密码不能少于 6 位', 'wp-seller-plugin' ) );
104 }
105 $client->changePassword( $host['username'], $password );
106 global $wpdb;
107 $wpdb->update(
108 $wpdb->prefix . 'mnbt_hosts',
109 array( 'password_enc' => Crypto::encrypt( $password ), 'updated_at' => current_time( 'mysql' ) ),
110 array( 'id' => (int) $host['id'] )
111 );
112 break;
113 case 'delete':
114 $client->deleteHost( $host['username'] );
115 global $wpdb;
116 $wpdb->delete( $wpdb->prefix . 'mnbt_hosts', array( 'id' => (int) $host['id'] ), array( '%d' ) );
117 break;
118 default:
119 wp_send_json_error( __( '未知操作', 'wp-seller-plugin' ) );
120 }
121 wp_send_json_success( __( '操作成功', 'wp-seller-plugin' ) );
122 } catch ( \Exception $e ) {
123 wp_send_json_error( $e->getMessage() );
124 }
125 }
126
127 /**
128 * 创建续费订单并返回结账支付地址。
129 */
130 public function ajax_create_renew() {
131 check_ajax_referer( 'mnbtwp_front', 'nonce' );
132 if ( ! is_user_logged_in() ) {
133 wp_send_json_error( __( '请先登录', 'wp-seller-plugin' ) );
134 }
135 $host_id = (int) ( $_POST['host_id'] ?? 0 );
136 $months = max( 1, (int) ( $_POST['months'] ?? 1 ) );
137 $host = Helpers::get_host( $host_id );
138 if ( ! $host || (int) $host['user_id'] !== get_current_user_id() ) {
139 wp_send_json_error( __( '无权操作', 'wp-seller-plugin' ) );
140 }
141 if ( ! class_exists( 'WooCommerce' ) ) {
142 wp_send_json_error( __( '未启用 WooCommerce', 'wp-seller-plugin' ) );
143 }
144 try {
145 $order = Woo::create_renew_order( $host_id, $months );
146 wp_send_json_success(
147 array(
148 'checkout_url' => $order->get_checkout_payment_url(),
149 )
150 );
151 } catch ( \Exception $e ) {
152 wp_send_json_error( $e->getMessage() );
153 }
154 }
155}