forked from intoxstudio/restrict-user-access
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_updates.php
142 lines (120 loc) · 3.33 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
<?php
/**
* @package Restrict User Access
* @author Joachim Jensen <jv@intox.dk>
* @license GPLv3
* @copyright 2017 by Joachim Jensen
*/
if (!defined('ABSPATH')) {
exit;
}
if(is_admin()) {
$rua_db_updater = new WP_DB_Updater('rua_plugin_version',RUA_App::PLUGIN_VERSION);
$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');
/**
* 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 = array();
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;
}
}
//eol