-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclasses.upgrade.php
230 lines (174 loc) · 5.87 KB
/
classes.upgrade.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
<?php
class BU_Section_Editing_Upgrader {
/**
* Perform any data modifications as needed based on version diff
*/
public function upgrade( $existing_version ) {
// Every version bump will trigger re-population of roles
$this->populate_roles();
// Pre-alpha release
if ( version_compare( $existing_version, '0.2', '<' ) ) {
$this->upgrade_02();
}
if ( version_compare( $existing_version, '0.3', '<' ) ) {
$this->upgrade_03();
}
// Post alpha release
if ( version_compare( $existing_version, '0.4', '<' ) ) {
$this->upgrade_04();
}
if ( version_compare( $existing_version, '0.6', '<' ) ) {
$this->upgrade_06();
}
}
/**
* Install default section editor role and capability set
*/
public function populate_roles() {
// Allow plugins to skip installation of section editor role
$create_section_editor = apply_filters( 'buse_create_section_editor_role', true );
if ( $create_section_editor ) {
$role = get_role( 'section_editor' );
if ( empty( $role ) ) {
add_role( 'section_editor', 'Section Editor' );
}
$role = get_role( 'section_editor' );
$role->add_cap( 'upload_files' );
$role->add_cap( 'read' );
$role->add_cap( 'read_private_posts' );
$role->add_cap( 'read_private_pages' );
$role->add_cap( 'edit_posts' );
$role->add_cap( 'edit_others_posts' );
$role->add_cap( 'edit_private_posts' );
$role->add_cap( 'edit_pages' );
$role->add_cap( 'edit_others_pages' );
$role->add_cap( 'edit_private_pages' );
$role->add_cap( 'delete_posts' );
$role->add_cap( 'delete_pages' );
$role->add_cap( 'level_1' );
$role->add_cap( 'level_0' );
// Add post type specific section editing capabilities
BU_Section_Editing_Plugin::$caps->add_caps( $role );
// Allow others to customize default section editor caps
do_action( 'buse_section_editor_caps', $role );
}
do_action( 'buse_populate_roles' );
}
/**
* Switched data structure for perms
*/
private function upgrade_02() {
global $wpdb;
// Upgrade (0.1 -> 0.2)
$patterns = array( '/^(\d+)$/', '/^(\d+)-denied$/' );
$replacements = array( '${1}:allowed', '${1}:denied' );
// Fetch existing values
$posts = $wpdb->get_results(
$wpdb->prepare(
"SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key = %s",
BU_Group_Permissions::META_KEY
)
);
// Loop through and update
foreach ( $posts as $post ) {
$result = preg_replace( $patterns, $replacements, $post->meta_value );
update_post_meta( $post->post_id, BU_Group_Permissions::META_KEY, $result, $post->meta_value );
}
}
/**
* Switched data structure for perms (again)
*/
private function upgrade_03() {
global $wpdb;
// Upgrade (0.2 -> 0.3)
$patterns = array( '/^(\d+):allowed$/' );
$replacements = array( '${1}' );
// Fetch existing values
$allowed_posts = $wpdb->get_results(
$wpdb->prepare(
"SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value LIKE %s",
BU_Group_Permissions::META_KEY,
'%:allowed'
)
);
foreach ( $allowed_posts as $post ) {
$new_meta_value = preg_replace( $patterns, $replacements, $post->meta_value );
update_post_meta( $post->post_id, BU_Group_Permissions::META_KEY, $new_meta_value, $post->meta_value );
}
// Fetch existing values
$denied_posts = $wpdb->get_results(
$wpdb->prepare(
"SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value LIKE %s",
BU_Group_Permissions::META_KEY,
'%denied'
)
);
// Loop through and update
foreach ( $denied_posts as $post ) {
delete_post_meta( $post->post_id, BU_Group_Permissions::META_KEY, $post->meta_value );
}
// Role/cap changes in 04b54ea79c1bc935eee5ce04118812c1d8dad229
if ( $role = get_role( 'section_editor' ) ) {
$role->remove_cap( 'edit_published_posts' );
$role->remove_cap( 'edit_published_pages' );
$role->remove_cap( 'delete_others_pages' );
$role->remove_cap( 'delete_others_posts' );
$role->remove_cap( 'delete_published_posts' );
$role->remove_cap( 'delete_published_pages' );
$role->remove_cap( 'publish_pages' );
$role->remove_cap( 'publish_posts' );
$role->add_cap( 'delete_published_in_section' );
$role->add_cap( 'edit_published_in_section' );
$role->add_cap( 'publish_in_section' );
}
}
/**
* Switched from options -> custom post type posts for group storage
*/
private function upgrade_04() {
global $wpdb;
// Migrate groups schema
$groups = get_option( '_bu_section_groups' );
if ( $groups ) {
$gc = BU_Edit_Groups::get_instance();
foreach ( $groups as $groupdata ) {
// Need to remove pre-existing ID and let wp_insert_post do its thing
$old_id = $groupdata['id'];
unset( $groupdata['id'] );
// Convert to new structure
$group = $gc->add_group( $groupdata );
// Grab all post IDS that have permissions set for this group
$posts_to_update = $wpdb->get_col(
$wpdb->prepare(
"SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s",
BU_Group_Permissions::META_KEY,
$old_id
)
);
// Update one by one
foreach ( $posts_to_update as $pid ) {
update_post_meta( $pid, BU_Group_Permissions::META_KEY, $group->id, $old_id );
}
}
// Cleanup
delete_option( '_bu_section_groups' );
delete_option( '_bu_section_groups_index' );
}
}
/**
* Switched from <action>_published_in_section to <action>_<post_type>_in_section
* Changes made in caps branch
*/
private function upgrade_06() {
// Role/cap mods introduced in 114fcedf80ebdb0ef93948f41a6984006ff74031
if ( $role = get_role( 'section_editor' ) ) {
$role->remove_cap( 'delete_published_in_section' );
$role->remove_cap( 'edit_published_in_section' );
$role->remove_cap( 'publish_in_section' );
$caps = BU_Section_Editing_Plugin::$caps->get_caps();
foreach ( $caps as $cap ) {
$role->add_cap( $cap );
}
}
}
}