-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.php
More file actions
337 lines (290 loc) · 10.2 KB
/
Copy pathinstall.php
File metadata and controls
337 lines (290 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php
/**
* 外掛安裝腳本
*
* @package WC_Points_Rewards
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* 外掛安裝類別
*/
class WC_Points_Rewards_Install {
/**
* 執行安裝
*/
public static function install() {
// 檢查系統需求
self::check_requirements();
// 創建資料庫表格
self::create_tables();
// 設定預設選項
self::set_default_options();
// 創建預設會員等級
self::create_default_tiers();
// 設定排程任務
self::schedule_events();
// 設定重寫規則
self::setup_rewrite_rules();
// 記錄安裝時間
update_option('wc_points_rewards_installed_time', current_time('mysql'));
update_option('wc_points_rewards_version', WC_POINTS_REWARDS_VERSION);
// 清除快取
wp_cache_flush();
}
/**
* 檢查系統需求
*/
private static function check_requirements() {
// 檢查 PHP 版本
if (version_compare(PHP_VERSION, '8.0', '<')) {
wp_die(__('WooCommerce Points & Rewards 需要 PHP 8.0 或更高版本。', 'wc-points-rewards'));
}
// 檢查 WordPress 版本
if (version_compare(get_bloginfo('version'), '6.0', '<')) {
wp_die(__('WooCommerce Points & Rewards 需要 WordPress 6.0 或更高版本。', 'wc-points-rewards'));
}
// 檢查 WooCommerce
if (!class_exists('WooCommerce')) {
wp_die(__('WooCommerce Points & Rewards 需要先安裝 WooCommerce 外掛。', 'wc-points-rewards'));
}
// 檢查資料庫權限
global $wpdb;
$result = $wpdb->query("SHOW GRANTS");
if (!$result) {
wp_die(__('資料庫權限不足,無法創建表格。', 'wc-points-rewards'));
}
}
/**
* 創建資料庫表格
*/
private static function create_tables() {
require_once WC_POINTS_REWARDS_PLUGIN_DIR . 'includes/class-database.php';
WC_Points_Rewards_Database::create_tables();
}
/**
* 設定預設選項
*/
private static function set_default_options() {
$default_settings = array(
// 基本設定
'enable_points_system' => 'yes',
'points_name' => '點',
'decimal_places' => 0,
// 點數獲得設定
'points_per_amount' => 100,
'points_amount' => 1,
'enable_registration_points' => 'yes',
'registration_points' => 100,
'enable_birthday_points' => 'yes',
'birthday_points' => 200,
'points_expiry_months' => 12,
// 點數使用設定
'enable_cart_redemption' => 'yes',
'points_value' => 1,
'min_cart_total' => 0,
'max_discount_percent' => 100,
'min_points_redemption' => 1,
// 通知設定
'enable_notifications' => 'yes',
'notification_days' => 30,
'email_points_earned' => 'yes',
'email_tier_upgrade' => 'yes',
'email_points_expiry' => 'yes',
// 進階設定
'enable_debug_mode' => 'no',
'auto_cleanup_days' => 365,
'cache_duration' => 3600,
'show_points_in_menu' => 'yes'
);
add_option('wc_points_rewards_settings', $default_settings);
}
/**
* 創建預設會員等級
*/
private static function create_default_tiers() {
global $wpdb;
$table_name = $wpdb->prefix . 'wc_points_rewards_tiers';
// 檢查是否已有等級
$existing_count = $wpdb->get_var("SELECT COUNT(*) FROM $table_name");
if ($existing_count > 0) {
return;
}
$default_tiers = array(
array(
'name' => '一般會員',
'min_amount' => 0,
'bonus_percentage' => 0,
'tier_order' => 1
),
array(
'name' => '銅牌會員',
'min_amount' => 5000,
'bonus_percentage' => 10,
'tier_order' => 2
),
array(
'name' => '銀牌會員',
'min_amount' => 10000,
'bonus_percentage' => 20,
'tier_order' => 3
),
array(
'name' => '金牌會員',
'min_amount' => 20000,
'bonus_percentage' => 30,
'tier_order' => 4
),
array(
'name' => '鑽石會員',
'min_amount' => 50000,
'bonus_percentage' => 50,
'tier_order' => 5
)
);
foreach ($default_tiers as $tier) {
$wpdb->insert($table_name, $tier);
}
}
/**
* 設定排程任務
*/
private static function schedule_events() {
// 每日清理任務
if (!wp_next_scheduled('wc_points_rewards_daily_cleanup')) {
wp_schedule_event(time(), 'daily', 'wc_points_rewards_daily_cleanup');
}
// 每日通知檢查
if (!wp_next_scheduled('wc_points_rewards_notification_check')) {
wp_schedule_event(time(), 'daily', 'wc_points_rewards_notification_check');
}
// 每週報表生成
if (!wp_next_scheduled('wc_points_rewards_weekly_report')) {
wp_schedule_event(time(), 'weekly', 'wc_points_rewards_weekly_report');
}
}
/**
* 設定重寫規則
*/
private static function setup_rewrite_rules() {
// 添加我的帳戶端點
add_rewrite_endpoint('points-rewards', EP_ROOT | EP_PAGES);
add_rewrite_endpoint('points-history', EP_ROOT | EP_PAGES);
add_rewrite_endpoint('member-tier', EP_ROOT | EP_PAGES);
// 重新整理重寫規則
flush_rewrite_rules();
}
/**
* 資料庫升級
*/
public static function upgrade($old_version, $new_version) {
global $wpdb;
// 升級邏輯
if (version_compare($old_version, '1.0.0', '<')) {
// 從舊版本升級的邏輯
self::upgrade_to_100();
}
// 更新版本號
update_option('wc_points_rewards_version', $new_version);
}
/**
* 升級到 1.0.0
*/
private static function upgrade_to_100() {
// 這裡可以放置從舊版本升級的邏輯
// 例如:資料庫結構更改、數據遷移等
// 重新創建表格(會檢查並更新結構)
self::create_tables();
// 更新設定結構
$old_settings = get_option('wc_points_rewards_settings', array());
$new_settings = wp_parse_args($old_settings, self::get_default_settings());
update_option('wc_points_rewards_settings', $new_settings);
}
/**
* 獲取預設設定
*/
private static function get_default_settings() {
return array(
'enable_points_system' => 'yes',
'points_name' => '點',
'decimal_places' => 0,
'points_per_amount' => 100,
'points_amount' => 1,
'enable_registration_points' => 'yes',
'registration_points' => 100,
'enable_birthday_points' => 'yes',
'birthday_points' => 200,
'points_expiry_months' => 12,
'enable_cart_redemption' => 'yes',
'points_value' => 1,
'min_cart_total' => 0,
'max_discount_percent' => 100,
'min_points_redemption' => 1,
'enable_notifications' => 'yes',
'notification_days' => 30,
'email_points_earned' => 'yes',
'email_tier_upgrade' => 'yes',
'email_points_expiry' => 'yes',
'enable_debug_mode' => 'no',
'auto_cleanup_days' => 365,
'cache_duration' => 3600,
'show_points_in_menu' => 'yes'
);
}
/**
* 創建範例數據(僅供測試環境使用)
*/
public static function create_sample_data() {
if (!defined('WP_DEBUG') || !WP_DEBUG) {
return;
}
global $wpdb;
// 創建測試用戶
$test_users = array(
array(
'user_login' => 'test_user_1',
'user_email' => 'test1@example.com',
'display_name' => '測試用戶一',
'user_pass' => 'password123'
),
array(
'user_login' => 'test_user_2',
'user_email' => 'test2@example.com',
'display_name' => '測試用戶二',
'user_pass' => 'password123'
)
);
foreach ($test_users as $user_data) {
$user_id = wp_insert_user($user_data);
if (!is_wp_error($user_id)) {
// 為測試用戶添加點數記錄
$database = WC_Points_Rewards_Database::instance();
$database->add_points(
$user_id,
500,
'earned',
'測試數據 - 註冊贈送',
null,
date('Y-m-d H:i:s', strtotime('+1 year'))
);
$database->add_points(
$user_id,
300,
'earned',
'測試數據 - 購物獲得',
null,
date('Y-m-d H:i:s', strtotime('+1 year'))
);
$database->add_points(
$user_id,
-100,
'redeemed',
'測試數據 - 購物使用'
);
// 設定年度消費統計
$database->update_user_yearly_stats($user_id, 8000);
}
}
}
}