-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdb_updates.php
485 lines (408 loc) · 13.8 KB
/
db_updates.php
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php
/**
* @package Restrict User Access
* @author Joachim Jensen <joachim@dev.institute>
* @license GPLv3
* @copyright 2024 by Joachim Jensen
*/
defined('ABSPATH') || exit;
$rua_db_updater = RUA_App::instance()->get_updater();
$rua_db_updater->register_version_update('0.4', 'rua_update_to_04');
$rua_db_updater->register_version_update('0.13', 'rua_update_to_013');
$rua_db_updater->register_version_update('0.14', 'rua_update_to_014');
$rua_db_updater->register_version_update('0.15', 'rua_update_to_015');
$rua_db_updater->register_version_update('0.17', 'rua_update_to_017');
$rua_db_updater->register_version_update('1.1', 'rua_update_to_11');
$rua_db_updater->register_version_update('2.2.1', 'rua_update_to_221');
$rua_db_updater->register_version_update('2.4', 'rua_update_to_24');
$rua_db_updater->register_version_update('2.4.2', 'rua_update_to_242');
$rua_db_updater->register_version_update('2.5', 'rua_update_to_25');
$rua_db_updater->register_version_update('2.6', 'rua_update_to_26');
$rua_db_updater->register_version_update('2.7', 'rua_update_to_27');
$rua_db_updater->register_version_update('2.8', 'rua_update_to_28');
function rua_update_to_28()
{
//migrate "New User Default Level" setting to automator
$level_id = get_option('rua-registration-level', 0);
if(empty($level_id)) {
return true;
}
$metadata = get_post_meta($level_id, '_ca_member_automations', true);
if (!is_array($metadata) || empty($metadata)) {
$metadata = [];
}
$metadata[] = [
'name' => 'user_registration',
'value' => 1
];
update_post_meta($level_id, '_ca_member_automations', $metadata);
return true;
}
function rua_update_to_27()
{
global $wpdb;
$entities = $wpdb->get_results("SELECT c.comment_post_ID, c.comment_ID FROM $wpdb->comments c LEFT JOIN $wpdb->users u ON c.user_id = u.ID WHERE c.comment_type = 'rua_member' AND u.ID IS NULL");
if(empty($entities)) {
return true;
}
wp_defer_comment_counting(true);
foreach($entities as $entity) {
$wpdb->query($wpdb->prepare("UPDATE $wpdb->comments SET comment_type = 'rua_member_bkup' where comment_ID = %d", $entity->comment_ID));
wp_update_comment_count($entity->comment_post_ID);
}
wp_defer_comment_counting(false);
return true;
}
function rua_update_to_26()
{
if (!(defined('ICL_SITEPRESS_VERSION') || defined('POLYLANG_VERSION'))) {
return true;
}
global $wpdb;
$wpdb->query("UPDATE $wpdb->comments SET comment_type = 'rua_member_bkup' where comment_type = 'rua_member'");
$level_id_lookup = array_flip((array)$wpdb->get_col("
SELECT ID FROM $wpdb->posts WHERE post_type = 'restriction'
"));
$existing_user_level_lookup = array_flip((array)$wpdb->get_col("
SELECT CONCAT(user_id, ':', comment_post_ID) FROM $wpdb->comments WHERE comment_type = 'rua_member'
"));
$blog_prefix = $wpdb->get_blog_prefix();
$query = <<<QUERY
SELECT um.user_id, um.meta_key, um.meta_value
FROM $wpdb->usermeta um
INNER JOIN $wpdb->usermeta u ON um.user_id = u.user_id AND u.meta_key = '{$blog_prefix}capabilities'
WHERE um.meta_key LIKE '_ca_level%'
QUERY;
$usermeta = $wpdb->get_results($query);
$data_new = [];
$data_meta_map = [];
foreach ($usermeta as $meta) {
if ($meta->meta_key === '_ca_level') {
if (!isset($level_id_lookup[$meta->meta_value])) {
continue;
}
//prevent dupe
if (isset($existing_user_level_lookup[$meta->user_id . ':' . $meta->meta_value])) {
continue;
}
$data_new[] = [
'comment_approved' => 'active',
'comment_date' => '',
'comment_type' => 'rua_member',
'user_id' => $meta->user_id,
'comment_post_ID' => $meta->meta_value,
'comment_meta' => [],
];
} else {
$data_meta_map[$meta->user_id . ':' . $meta->meta_key] = $meta->meta_value;
}
}
foreach ($data_new as $data) {
$user_id = $data['user_id'];
$level_id = $data['comment_post_ID'];
$insert = $data;
//start date
if (isset($data_meta_map[$user_id . ':_ca_level_' . $level_id])) {
$insert['comment_date'] = date_i18n('Y-m-d H:i:s', $data_meta_map[$user_id . ':_ca_level_' . $level_id]);
}
//status
if (isset($data_meta_map[$user_id . ':_ca_level_status_' . $level_id])) {
$insert['comment_approved'] = $data_meta_map[$user_id . ':_ca_level_status_' . $level_id];
}
//expiry date
if (isset($data_meta_map[$user_id . ':_ca_level_expiry_' . $level_id])) {
$insert['comment_meta']['_ca_member_expiry'] = $data_meta_map[$user_id . ':_ca_level_expiry_' . $level_id];
}
wp_insert_comment($insert);
wp_update_comment_count($level_id);
}
return true;
}
function rua_update_to_25()
{
if (defined('ICL_SITEPRESS_VERSION') || defined('POLYLANG_VERSION')) {
return true;
}
global $wpdb;
$level_id_lookup = array_flip((array)$wpdb->get_col("
SELECT ID FROM $wpdb->posts WHERE post_type = 'restriction'
"));
$existing_user_level_lookup = array_flip((array)$wpdb->get_col("
SELECT CONCAT(user_id, ':', comment_post_ID) FROM $wpdb->comments WHERE comment_type = 'rua_member'
"));
$blog_prefix = $wpdb->get_blog_prefix();
$query = <<<QUERY
SELECT um.user_id, um.meta_key, um.meta_value
FROM $wpdb->usermeta um
INNER JOIN $wpdb->usermeta u ON um.user_id = u.user_id AND u.meta_key = '{$blog_prefix}capabilities'
WHERE um.meta_key LIKE '_ca_level%'
QUERY;
$usermeta = $wpdb->get_results($query);
$data_new = [];
$data_meta_map = [];
foreach ($usermeta as $meta) {
if ($meta->meta_key === '_ca_level') {
if (!isset($level_id_lookup[$meta->meta_value])) {
continue;
}
//prevent dupe
if (isset($existing_user_level_lookup[$meta->user_id . ':' . $meta->meta_value])) {
continue;
}
$data_new[] = [
'comment_approved' => 'active',
'comment_date' => '',
'comment_type' => 'rua_member',
'user_id' => $meta->user_id,
'comment_post_ID' => $meta->meta_value,
'comment_meta' => [],
];
} else {
$data_meta_map[$meta->user_id . ':' . $meta->meta_key] = $meta->meta_value;
}
}
foreach ($data_new as $data) {
$user_id = $data['user_id'];
$level_id = $data['comment_post_ID'];
$insert = $data;
//start date
if (isset($data_meta_map[$user_id . ':_ca_level_' . $level_id])) {
$insert['comment_date'] = date_i18n('Y-m-d H:i:s', $data_meta_map[$user_id . ':_ca_level_' . $level_id]);
}
//status
if (isset($data_meta_map[$user_id . ':_ca_level_status_' . $level_id])) {
$insert['comment_approved'] = $data_meta_map[$user_id . ':_ca_level_status_' . $level_id];
}
//expiry date
if (isset($data_meta_map[$user_id . ':_ca_level_expiry_' . $level_id])) {
$insert['comment_meta']['_ca_member_expiry'] = $data_meta_map[$user_id . ':_ca_level_expiry_' . $level_id];
}
wp_insert_comment($insert);
wp_update_comment_count($level_id);
}
return true;
}
/**
* Enable legacy date module and
* negated conditions if in use
*
* @since 2.4.2
*
* @return bool
*/
function rua_update_to_242()
{
global $wpdb;
$types = WPCACore::types()->get_all();
$options = [
'legacy.date_module' => [],
'legacy.negated_conditions' => []
];
$options['legacy.date_module'] = array_flip((array)$wpdb->get_col("
SELECT p.post_type FROM $wpdb->posts p
INNER JOIN $wpdb->posts c on p.ID = c.post_parent
INNER JOIN $wpdb->postmeta m on c.ID = m.post_id
WHERE c.post_type = 'condition_group' AND m.meta_key = '_ca_date'
"));
$options['legacy.negated_conditions'] = array_flip((array)$wpdb->get_col("
SELECT p.post_type FROM $wpdb->posts p
INNER JOIN $wpdb->posts c on p.ID = c.post_parent
WHERE c.post_type = 'condition_group' AND c.post_status = 'negated'
"));
foreach ($types as $type => $val) {
foreach ($options as $option => $post_types) {
if (isset($post_types[$type])) {
WPCACore::save_option($type, $option, true);
} elseif (WPCACore::get_option($type, $option, false)) {
WPCACore::save_option($type, $option, false);
}
}
}
return true;
}
/**
* Migrate role sync option to automator
*
* @return bool
*/
function rua_update_to_24()
{
global $wpdb;
$results = $wpdb->get_results("SELECT post_id,meta_value FROM $wpdb->postmeta WHERE meta_key = '_ca_role'");
foreach ($results as $result) {
if (is_numeric($result->meta_value)) {
$automator = 'login';
$value = $result->meta_value == -1 ? 'login' : 'logout';
} else {
$automator = 'user_role_sync';
$value = $result->meta_value;
}
$metadata = get_post_meta($result->post_id, '_ca_member_automations', true);
if (!is_array($metadata) || empty($metadata)) {
$metadata = [];
}
$metadata[] = [
'name' => $automator,
'value' => $value
];
update_post_meta($result->post_id, '_ca_member_automations', $metadata);
}
return true;
}
/**
* Add -1 to condition groups with select terms
* Clear condition type cache
*
* @since 2.2.1
*
* @return bool
*/
function rua_update_to_221()
{
update_option('_ca_condition_type_cache', []);
$taxonomies = array_map(function ($value) {
return "'" . esc_sql($value) . "'";
}, get_taxonomies(['public' => true]));
if (empty($taxonomies)) {
return true;
}
global $wpdb;
$condition_group_ids = array_unique((array)$wpdb->get_col("
SELECT p.ID FROM $wpdb->posts p
INNER JOIN $wpdb->term_relationships r ON r.object_id = p.ID
INNER JOIN $wpdb->term_taxonomy t ON t.term_taxonomy_id = r.term_taxonomy_id
WHERE p.post_type = 'condition_group'
AND t.taxonomy IN (" . implode(',', $taxonomies) . ')
'));
foreach ($condition_group_ids as $id) {
add_post_meta($id, '_ca_taxonomy', '-1');
}
return true;
}
/**
* Migrate rua-toolbar-hide option to levels
*
* @since 1.1
* @return bool
*/
function rua_update_to_11()
{
$hide_toolbar = get_option('rua-toolbar-hide', false);
delete_option('rua-toolbar-hide');
if (!$hide_toolbar) {
return true;
}
$app = RUA_App::instance();
$levels = $app->get_levels();
$metadata = $app->level_manager->metadata()->get('hide_admin_bar');
foreach ($levels as $level) {
$metadata->update($level->ID, 1);
}
return true;
}
/**
* Update to version 0.17
* Remove role meta for unsynced levels
*
* @since 0.17
* @return boolean
*/
function rua_update_to_017()
{
global $wpdb;
$wpdb->query("
DELETE FROM $wpdb->postmeta
WHERE meta_key = '_ca_role' AND meta_value = '-1'
");
return true;
}
/**
* Update to version 0.15
* Remove old condition settings
*
* @since 0.15
* @return boolean
*/
function rua_update_to_015()
{
global $wpdb;
$wpdb->query("
DELETE FROM $wpdb->postmeta
WHERE meta_value LIKE '_ca_sub_%'
");
return true;
}
/**
* Update to version 0.14
* Simplify auto select option
*
* @since 0.14
* @return boolean
*/
function rua_update_to_014()
{
global $wpdb;
$group_ids = $wpdb->get_col("SELECT post_id FROM $wpdb->postmeta WHERE meta_value LIKE '_ca_sub_%'");
foreach ($group_ids as $group_id) {
add_post_meta($group_id, '_ca_autoselect', 1, true);
}
return true;
}
/**
* Update to version 0.13
* Inherit condition exposure from level
* Remove level exposure
*
* @since 0.13
* @return boolean
*/
function rua_update_to_013()
{
global $wpdb;
$wpdb->query("
UPDATE $wpdb->posts AS c
INNER JOIN $wpdb->posts AS s ON s.ID = c.post_parent
INNER JOIN $wpdb->postmeta AS e ON e.post_id = s.ID
SET c.menu_order = e.meta_value
WHERE c.post_type = 'condition_group'
AND e.meta_key = '_ca_exposure'
");
$wpdb->query("
DELETE FROM $wpdb->postmeta
WHERE meta_key = '_ca_exposure'
");
wp_cache_flush();
return true;
}
/**
* Store userlevel dates with level_id
* instead of level umeta_id
*
* @since 0.4
* @return boolean
*/
function rua_update_to_04()
{
global $wpdb;
//Get levels by umeta id and level id
$levels_by_metaid = $wpdb->get_results("SELECT umeta_id,meta_value FROM $wpdb->usermeta WHERE meta_key = '_ca_level'", OBJECT_K);
$levels_by_id = [];
foreach ($levels_by_metaid as $meta_id => $level) {
$levels_by_id[$level->meta_value] = $meta_id;
}
$level_dates = $wpdb->get_results("SELECT user_id,meta_key,meta_value FROM $wpdb->usermeta WHERE meta_key LIKE '_ca_level_%'");
foreach ($level_dates as $level_date) {
$level_date_metaid = str_replace('_ca_level_', '', $level_date->meta_key);
//Check if date exists by level umeta id (old store)
//If so, move it to new
if (isset($levels_by_metaid[$level_date_metaid])) {
update_user_meta($level_date->user_id, '_ca_level_' . $levels_by_metaid[$level_date_metaid]->meta_value, $level_date->meta_value, true);
}
//Check if date exists by level id (new store)
//If not, delete it
if (!isset($levels_by_id[$level_date_metaid])) {
delete_user_meta($level_date->user_id, $level_date->meta_key);
}
}
return true;
}