-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMembershipLevelsExtension.php
More file actions
328 lines (290 loc) · 10.4 KB
/
Copy pathMembershipLevelsExtension.php
File metadata and controls
328 lines (290 loc) · 10.4 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
<?php
namespace Jankx\Extensions\MembershipLevels;
use Jankx\Extensions\AbstractExtension;
class MembershipLevelsExtension extends AbstractExtension
{
protected static $instance;
const LEVELS_OPTION = 'jankx_membership_levels';
const CRITERIA_OPTION = 'jankx_membership_criteria';
const USER_LEVEL_META = 'jankx_membership_level';
public function __construct()
{
$this->register_autoloader();
parent::__construct();
}
protected function register_autoloader()
{
spl_autoload_register(function ($class) {
$prefix = 'Jankx\\Extensions\\MembershipLevels\\';
$base_dir = __DIR__ . '/src/';
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}
$relative_class = substr($class, $len);
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
if (file_exists($file)) {
require $file;
}
});
}
public function init(): void
{
self::$instance = $this;
}
public static function get_instance(): ?self
{
return self::$instance;
}
public function register_hooks(): void
{
// CPT for membership levels
(new PostTypes\MembershipLevelPostType())->register();
// Seed default levels on first activation
add_action('admin_init', [$this, 'maybeSeedDefaultLevels']);
// Admin pages
if (is_admin()) {
(new Admin\SettingsPage())->register();
}
// REST API
add_action('rest_api_init', [$this, 'registerRestRoutes']);
// Hook into order completed to auto-evaluate membership
add_action('jankx/ecommerce/checkout/completed', [$this, 'onCheckoutCompleted']);
add_action('jankx/ecommerce/payment/paid', [$this, 'onPaymentPaid']);
// Manual level assignment via admin
add_action('show_user_profile', [$this, 'renderUserLevelField']);
add_action('edit_user_profile', [$this, 'renderUserLevelField']);
add_action('personal_options_update', [$this, 'saveUserLevelField']);
add_action('edit_user_profile_update', [$this, 'saveUserLevelField']);
}
/**
* Seed default membership level CPT posts on first run
*/
public function maybeSeedDefaultLevels(): void
{
// Manual trigger: ?run_seed=1
if (isset($_GET['run_seed']) && current_user_can('manage_options')) {
delete_option('jankx_membership_levels_seeded');
}
$done = get_option('jankx_membership_levels_seeded', false);
if ($done) {
return;
}
$this->seedDefaultLevels();
update_option('jankx_membership_levels_seeded', true);
}
protected function seedDefaultLevels(): void
{
$defaults = self::getLevels();
foreach ($defaults as $slug => $level) {
// Check if already exists by slug meta
$existing = get_posts([
'post_type' => 'membership_level',
'meta_query' => [
['key' => '_level_slug', 'value' => $slug],
],
'numberposts' => 1,
'fields' => 'ids',
]);
if (!empty($existing)) {
continue;
}
$postId = wp_insert_post([
'post_type' => 'membership_level',
'post_title' => $level['name'],
'post_content' => $level['description'] ?? '',
'post_status' => 'publish',
]);
if ($postId && !is_wp_error($postId)) {
update_post_meta($postId, '_level_slug', $slug);
update_post_meta($postId, '_level_color', $level['color'] ?? '#999999');
update_post_meta($postId, '_level_priority', $level['priority'] ?? 0);
update_post_meta($postId, '_level_discount', $level['discount'] ?? 0);
update_post_meta($postId, '_level_criteria', $level['criteria'] ?? []);
}
}
}
/**
* Register REST API routes
*/
public function registerRestRoutes(): void
{
$controller = new Api\RestApiController();
$controller->registerRoutes();
}
/**
* Auto-evaluate membership level after checkout
*/
public function onCheckoutCompleted($order): void
{
$userId = $order->getCustomerId();
if (!$userId) {
return;
}
$this->evaluateAndSetLevel($userId);
}
/**
* Auto-evaluate membership level after payment
*/
public function onPaymentPaid($order, $transactionId = ''): void
{
$userId = $order->getCustomerId();
if (!$userId) {
return;
}
$this->evaluateAndSetLevel($userId);
}
/**
* Evaluate criteria and set membership level for a user
*/
public function evaluateAndSetLevel(int $userId): string
{
$engine = new Engine\RuleEngine();
$levels = self::getLevels();
$stats = $engine->getUserStats($userId);
$level = $engine->findBestLevel($userId, $levels, $stats);
if ($level) {
$this->setUserLevel($userId, $level);
}
return $level ?? '';
}
/**
* Set membership level for a user
*/
public function setUserLevel(int $userId, string $level): bool
{
return update_user_meta($userId, self::USER_LEVEL_META, $level);
}
/**
* Get membership level for a user
*/
public function getUserLevel(int $userId): string
{
return get_user_meta($userId, self::USER_LEVEL_META, true) ?: 'bronze';
}
/**
* Get all defined membership levels
*/
public static function getLevels(): array
{
$defaults = [
'bronze' => [
'name' => 'Bronze',
'description' => 'Thành viên mới',
'color' => '#CD7F32',
'priority' => 0,
'criteria' => [],
],
'silver' => [
'name' => 'Silver',
'description' => 'Thành viên bạc',
'color' => '#C0C0C0',
'priority' => 10,
'criteria' => [
'total_orders' => ['min' => 3],
'total_spent' => ['min' => 5000000],
],
],
'gold' => [
'name' => 'Gold',
'description' => 'Thành viên vàng',
'color' => '#FFD700',
'priority' => 20,
'criteria' => [
'total_orders' => ['min' => 10],
'total_spent' => ['min' => 20000000],
],
],
'diamond' => [
'name' => 'Diamond',
'description' => 'Thành viên kim cương',
'color' => '#B9F2FF',
'priority' => 30,
'criteria' => [
'total_orders' => ['min' => 20],
'total_spent' => ['min' => 50000000],
],
],
];
$custom = get_option(self::LEVELS_OPTION, []);
return wp_parse_args($custom, $defaults);
}
/**
* Get all criteria definitions
*/
public static function getCriteria(): array
{
$defaults = [
'total_orders' => [
'label' => 'Tổng số đơn hàng',
'description' => 'Tổng số đơn hàng đã hoàn thành',
'type' => 'number',
],
'total_spent' => [
'label' => 'Tổng chi tiêu',
'description' => 'Tổng số tiền đã thanh toán',
'type' => 'currency',
],
'account_age_days' => [
'label' => 'Tuổi tài khoản (ngày)',
'description' => 'Số ngày kể từ khi đăng ký',
'type' => 'number',
],
'last_order_days' => [
'label' => 'Ngày đặt hàng gần nhất',
'description' => 'Số ngày kể từ đơn hàng cuối cùng',
'type' => 'number',
],
];
$custom = get_option(self::CRITERIA_OPTION, []);
return wp_parse_args($custom, $defaults);
}
/**
* Render membership level field on user profile
*/
public function renderUserLevelField($user): void
{
$currentLevel = $this->getUserLevel($user->ID);
$levels = self::getLevels();
$levelData = $levels[$currentLevel] ?? $levels['bronze'];
$isAdmin = current_user_can('manage_options');
?>
<h2>Hạng thành viên</h2>
<table class="form-table">
<tr>
<th><label for="jankx_membership_level">Hạng hiện tại</label></th>
<td>
<?php if ($isAdmin): ?>
<select id="jankx_membership_level" name="jankx_membership_level">
<?php foreach ($levels as $slug => $level): ?>
<option value="<?php echo esc_attr($slug); ?>" <?php selected($currentLevel, $slug); ?>>
<?php echo esc_html($level['name']); ?>
</option>
<?php endforeach; ?>
</select>
<p class="description">Chọn hạng thành viên thủ công. Bỏ trống để hệ thống tự động đánh giá.</p>
<?php else: ?>
<span style="display:inline-block;width:12px;height:12px;border-radius:50;background:<?php echo esc_attr($levelData['color']); ?>;vertical-align:middle;margin-right:6px;"></span>
<strong><?php echo esc_html($levelData['name']); ?></strong>
<?php endif; ?>
</td>
</tr>
</table>
<?php
}
/**
* Save membership level field from user profile
*/
public function saveUserLevelField(int $userId): void
{
if (!current_user_can('manage_options')) {
return;
}
if (isset($_POST['jankx_membership_level'])) {
$level = sanitize_text_field($_POST['jankx_membership_level']);
if ($level) {
$this->setUserLevel($userId, $level);
}
}
}
}