仰望星辰工作室

better-staridc-MNBT

better-staridc-MNBT/ wp_seller_plugin/includes/class-helpers.php 3.6 KB · 133 行 原始文件
Z zfhsh first commit 1 天前
1<?php
2/**
3 * 通用工具:节点/主机查询、Client 构造、用户名生成、状态文案。
4 *
5 * @package MnbtWp
6 */
7
8namespace MnbtWp;
9
10use MnbtWp\Mnbt\Client;
11
12if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14}
15
16/**
17 * 工具类。
18 */
19class Helpers {
20
21 /**
22 * 按 ID 取节点配置行。
23 *
24 * @param int $id 节点 ID。
25 * @return array|null
26 */
27 public static function get_provider( $id ) {
28 global $wpdb;
29 $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mnbt_providers WHERE id=%d", (int) $id ), ARRAY_A );
30 return $row ? $row : null;
31 }
32
33 /**
34 * 根据节点行构造 Client(解密密钥)。
35 *
36 * @param array $provider 节点行。
37 * @return Client
38 */
39 public static function client_for_provider( array $provider ) {
40 return new Client(
41 array(
42 'api_url' => $provider['api_url'],
43 'btdh' => $provider['btdh'],
44 'mn_key' => Crypto::decrypt( $provider['mn_key_enc'] ),
45 'mn_keye' => Crypto::decrypt( $provider['mn_keye_enc'] ),
46 'mn_vs' => (int) $provider['mn_vs'],
47 'provider_id' => (int) $provider['id'],
48 )
49 );
50 }
51
52 /**
53 * 按 ID 取主机行。
54 *
55 * @param int $id 主机 ID。
56 * @return array|null
57 */
58 public static function get_host( $id ) {
59 global $wpdb;
60 $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mnbt_hosts WHERE id=%d", (int) $id ), ARRAY_A );
61 return $row ? $row : null;
62 }
63
64 /**
65 * 主机状态文案。
66 *
67 * @param string $status 状态码。
68 * @return string
69 */
70 public static function host_status_label( $status ) {
71 $labels = array(
72 'pending' => __( '待开通', 'wp-seller-plugin' ),
73 'provisioning' => __( '开通中', 'wp-seller-plugin' ),
74 'active' => __( '运行中', 'wp-seller-plugin' ),
75 'suspended' => __( '已暂停', 'wp-seller-plugin' ),
76 'expired' => __( '已到期', 'wp-seller-plugin' ),
77 'failed' => __( '开通失败', 'wp-seller-plugin' ),
78 );
79 return isset( $labels[ $status ] ) ? $labels[ $status ] : $status;
80 }
81
82 /**
83 * 生成主机用户名(≥6 位,避免与 MNBT 现有用户冲突)。
84 *
85 * @param int $user_id WP 用户 ID。
86 * @return string
87 */
88 public static function generate_username( $user_id ) {
89 $suffix = substr( str_replace( array( '0', '.' ), '', uniqid( '', true ) ), 0, 4 );
90 return 'wp' . (int) $user_id . $suffix;
91 }
92
93 /**
94 * 主机控制台地址(MNBT 用户登录页)。
95 *
96 * @param array $provider 节点行。
97 * @return string
98 */
99 public static function control_url( array $provider ) {
100 return untrailingslashit( $provider['api_url'] ) . '/user/login.php';
101 }
102
103 /**
104 * 从 quota_json 读取配额(含用量)。
105 *
106 * @param string $quota_json 主机 quota_json。
107 * @return array ['web'=>['max','used'],'sql'=>...,'flow'=>...]
108 */
109 public static function parse_quota( $quota_json ) {
110 $data = json_decode( (string) $quota_json, true );
111 if ( ! is_array( $data ) ) {
112 $data = array();
113 }
114 return array(
115 'web' => isset( $data['web'] ) && is_array( $data['web'] ) ? $data['web'] : array( 'max' => 0, 'used' => 0 ),
116 'sql' => isset( $data['sql'] ) && is_array( $data['sql'] ) ? $data['sql'] : array( 'max' => 0, 'used' => 0 ),
117 'flow' => isset( $data['flow'] ) && is_array( $data['flow'] ) ? $data['flow'] : array( 'max' => 0, 'used' => 0 ),
118 );
119 }
120
121 /**
122 * 发送邮件(包装 wp_mail,统一发件人)。
123 *
124 * @param string $to 收件人。
125 * @param string $subject 主题。
126 * @param string $body HTML 正文。
127 * @return bool
128 */
129 public static function send_mail( $to, $subject, $body ) {
130 $headers = array( 'Content-Type: text/html; charset=UTF-8' );
131 return wp_mail( $to, $subject, $body, $headers );
132 }
133}