better-staridc-MNBT
1<?php
2/**
3 * 激活/停用逻辑:建表、默认配置、定时任务注册。
4 *
5 * @package MnbtWp
6 */
7
8namespace MnbtWp;
9
10if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12}
13
14/**
15 * 激活器。
16 */
17class Activator {
18
19 /**
20 * 激活:建表 + 默认设置 + 注册定时任务。
21 */
22 public static function activate() {
23 self::create_tables();
24 self::defaults();
25 self::schedule_cron();
26 }
27
28 /**
29 * 停用:清理定时任务(数据保留)。
30 */
31 public static function deactivate() {
32 self::clear_cron();
33 }
34
35 /**
36 * 创建插件数据表。
37 */
38 private static function create_tables() {
39 global $wpdb;
40
41 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
42
43 $charset_collate = $wpdb->get_charset_collate();
44 $prefix = $wpdb->prefix;
45
46 $tables = array();
47
48 // MNBT 节点连接配置(可多节点)。
49 $tables[] = "CREATE TABLE {$prefix}mnbt_providers (
50 id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
51 name varchar(100) NOT NULL DEFAULT '',
52 api_url varchar(255) NOT NULL DEFAULT '',
53 btdh varchar(50) NOT NULL DEFAULT '',
54 mn_key_enc varchar(255) NOT NULL DEFAULT '',
55 mn_keye_enc varchar(255) NOT NULL DEFAULT '',
56 mn_vs int(11) NOT NULL DEFAULT 15,
57 qk varchar(20) NOT NULL DEFAULT 'true',
58 created_at datetime DEFAULT NULL,
59 updated_at datetime DEFAULT NULL,
60 PRIMARY KEY (id),
61 KEY qk (qk)
62 ) $charset_collate;";
63
64 // 主机记录(本地状态缓存,购买开通后写入)。
65 $tables[] = "CREATE TABLE {$prefix}mnbt_hosts (
66 id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
67 provider_id bigint(20) unsigned NOT NULL DEFAULT 0,
68 order_id bigint(20) unsigned NOT NULL DEFAULT 0,
69 user_id bigint(20) unsigned NOT NULL DEFAULT 0,
70 username varchar(64) NOT NULL DEFAULT '',
71 password_enc varchar(255) NOT NULL DEFAULT '',
72 site_domain varchar(128) NOT NULL DEFAULT '',
73 status varchar(20) NOT NULL DEFAULT 'pending',
74 expire_date varchar(20) NOT NULL DEFAULT '',
75 quota_json text NOT NULL,
76 last_remind datetime DEFAULT NULL,
77 created_at datetime DEFAULT NULL,
78 updated_at datetime DEFAULT NULL,
79 PRIMARY KEY (id),
80 KEY user_id (user_id),
81 KEY username (username),
82 KEY status (status)
83 ) $charset_collate;";
84
85 // API 调用日志(脱敏)。
86 $tables[] = "CREATE TABLE {$prefix}mnbt_api_log (
87 id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
88 provider_id bigint(20) unsigned NOT NULL DEFAULT 0,
89 action varchar(50) NOT NULL DEFAULT '',
90 params_json text NOT NULL,
91 code int(11) NOT NULL DEFAULT 0,
92 msg text NOT NULL,
93 duration float NOT NULL DEFAULT 0,
94 created_at datetime DEFAULT NULL,
95 PRIMARY KEY (id),
96 KEY action (action),
97 KEY created_at (created_at)
98 ) $charset_collate;";
99
100 foreach ( $tables as $sql ) {
101 dbDelta( $sql );
102 }
103 }
104
105 /**
106 * 默认设置。
107 */
108 private static function defaults() {
109 if ( false === get_option( 'mnbtwp_settings' ) ) {
110 add_option(
111 'mnbtwp_settings',
112 array(
113 'expire_days' => 3, // 到期前提醒天数。
114 'grace_days' => 0, // 宽限期(天)。
115 'keep_data_on_del' => 1, // 卸载时保留数据。
116 )
117 );
118 }
119 }
120
121 /**
122 * 注册定时任务:每日到期维护 + 每 10 分钟状态同步。
123 */
124 public static function schedule_cron() {
125 if ( ! wp_next_scheduled( 'mnbtwp_daily_maintenance' ) ) {
126 wp_schedule_event( time() + 60, 'daily', 'mnbtwp_daily_maintenance' );
127 }
128 if ( ! wp_next_scheduled( 'mnbtwp_sync_hosts' ) ) {
129 wp_schedule_event( time() + 120, 'twicedaily', 'mnbtwp_sync_hosts' );
130 }
131 }
132
133 /**
134 * 清理定时任务。
135 */
136 private static function clear_cron() {
137 foreach ( array( 'mnbtwp_daily_maintenance', 'mnbtwp_sync_hosts' ) as $hook ) {
138 $timestamp = wp_next_scheduled( $hook );
139 if ( $timestamp ) {
140 wp_unschedule_event( $timestamp, $hook );
141 }
142 }
143 }
144}