PluginProbe
Webling / 3.2.0
Webling v3.2.0
trunk 2.0 3.0 3.0.1 3.0.2 3.0.3 3.1.0 3.1.1 3.1.2 3.2.0 3.2.1 3.3.0 3.4.0 3.4.1 3.5.0 3.6.0 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.9.0 3.9.1 3.9.2
webling / src / setup / setup.php

setup.php in Webling 3.2.0, at src/setup/setup.php

367 lines 11.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 function webling_plugin_setup() {
5 webling_plugin_update_check();
6 }
7
8 function webling_plugin_update_check() {
9 $installed_db_version = get_option('webling-db-version', 0);
10
11 // abort if the installed version is the current version
12 if ($installed_db_version == WEBLING_DB_VERSION) {
13 return;
14 }
15
16 if ($installed_db_version < 2) {
17 webling_plugin_initial_setup();
18 }
19 if ($installed_db_version < 3) {
20 webling_plugin_setup_upgrade_v3();
21 }
22 if ($installed_db_version < 4) {
23 webling_plugin_setup_upgrade_v4();
24 }
25 if ($installed_db_version < 5) {
26 webling_plugin_setup_upgrade_v5();
27 }
28 }
29
30 // ************* Version Upgrade Functions *************
31
32
33 // upgrade to db version 5
34 // add custom memberlist template fields
35 function webling_plugin_setup_upgrade_v5() {
36 global $wpdb;
37
38 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
39
40 $charset_collate = $wpdb->get_charset_collate();
41
42 // update memberlists table
43 // add custom template fields
44 $table_name = $wpdb->prefix . 'webling_memberlists';
45 $sql = "CREATE TABLE $table_name (
46 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
47 `title` varchar(255) DEFAULT NULL,
48 `show_all_groups` tinyint(1) NOT NULL DEFAULT '0',
49 `groups` text,
50 `fields` text,
51 `sortorder` enum('ASC','DESC') DEFAULT 'ASC',
52 `sortfield` varchar(255) DEFAULT NULL,
53 `class` varchar(255) DEFAULT NULL,
54 `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
55 `created_at` timestamp NULL DEFAULT NULL,
56 `design` enum('LIST','CUSTOM') NOT NULL DEFAULT 'LIST',
57 `custom_template` text,
58 PRIMARY KEY (`id`)
59 ) $charset_collate;";
60 dbDelta( $sql );
61
62 update_option("webling-db-version", 5);
63 }
64
65 // upgrade to db version 4
66 // fixing schema for MySQL 5.5 (can not have more than one AUTO CURRENT_TIMESTAMP field)
67 function webling_plugin_setup_upgrade_v4() {
68 global $wpdb;
69
70 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
71
72 $charset_collate = $wpdb->get_charset_collate();
73
74 // update forms table (or create if it failed till now)
75 // only updated_at field is now auto-updating
76 $table_name = $wpdb->prefix . 'webling_forms';
77 $sql = "CREATE TABLE $table_name (
78 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
79 `title` varchar(255) DEFAULT '',
80 `group_id` int(11) NOT NULL,
81 `notification_email` varchar(255) DEFAULT NULL,
82 `confirmation_email_enabled` tinyint(1) NOT NULL DEFAULT '0',
83 `confirmation_email_webling_field` int(11) DEFAULT NULL,
84 `confirmation_email_subject` varchar(255) DEFAULT '',
85 `confirmation_email_text` text,
86 `confirmation_text` text,
87 `submit_button_text` varchar(255) DEFAULT NULL,
88 `class` varchar(255) DEFAULT NULL,
89 `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
90 `created_at` timestamp NULL DEFAULT NULL,
91 PRIMARY KEY (`id`)
92 ) $charset_collate;";
93 dbDelta( $sql );
94
95 // change definitions on old installations
96 $wpdb->query( "ALTER TABLE $table_name MODIFY COLUMN `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" );
97 $wpdb->query( "ALTER TABLE $table_name MODIFY COLUMN `created_at` timestamp NULL DEFAULT NULL" );
98
99 // update memberlists table (or create if it failed till now)
100 // only updated_at field is now auto-updating
101 $table_name = $wpdb->prefix . 'webling_memberlists';
102 $sql = "CREATE TABLE $table_name (
103 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
104 `title` varchar(255) DEFAULT NULL,
105 `show_all_groups` tinyint(1) NOT NULL DEFAULT '0',
106 `groups` text,
107 `fields` text,
108 `sortorder` enum('ASC','DESC') DEFAULT 'ASC',
109 `sortfield` varchar(255) DEFAULT NULL,
110 `class` varchar(255) DEFAULT NULL,
111 `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
112 `created_at` timestamp NULL DEFAULT NULL,
113 PRIMARY KEY (`id`)
114 ) $charset_collate;";
115 dbDelta( $sql );
116
117 // change definitions on old installations
118 $wpdb->query( "ALTER TABLE $table_name MODIFY COLUMN `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" );
119 $wpdb->query( "ALTER TABLE $table_name MODIFY COLUMN `created_at` timestamp NULL DEFAULT NULL" );
120
121 // update form fields table (or create if it failed till now)
122 // remove updated_at and created_at fields, there is no use for them
123 $table_name = $wpdb->prefix . 'webling_form_fields';
124 $sql = "CREATE TABLE $table_name (
125 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
126 `form_id` int(11) NOT NULL,
127 `order` int(11) NOT NULL,
128 `webling_field_id` int(11) NOT NULL,
129 `type` varchar(50) NOT NULL DEFAULT 'text',
130 `required` tinyint(1) NOT NULL DEFAULT '0',
131 `field_name` varchar(255) NOT NULL DEFAULT '',
132 `field_name_position` enum('TOP','LEFT','HIDDEN') NOT NULL DEFAULT 'TOP',
133 `placeholder_text` varchar(255) DEFAULT NULL,
134 `description_text` text,
135 `class` varchar(255) DEFAULT NULL,
136 PRIMARY KEY (`id`),
137 KEY `form_id` (`form_id`)
138 ) $charset_collate;";
139 dbDelta( $sql );
140
141 // remove updated_at and created_at on existing installations
142 $table_name = $wpdb->prefix . 'webling_form_fields';
143 $wpdb->query( "ALTER TABLE $table_name DROP COLUMN `updated_at`" );
144 $wpdb->query( "ALTER TABLE $table_name DROP COLUMN `created_at`" );
145
146 update_option("webling-db-version", 4);
147 }
148
149
150 // upgrade to db version 3
151 // add confirmation email fields
152 function webling_plugin_setup_upgrade_v3() {
153 global $wpdb;
154
155 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
156
157 $charset_collate = $wpdb->get_charset_collate();
158
159 // update forms table
160 $table_name = $wpdb->prefix . 'webling_forms';
161 $sql = "CREATE TABLE $table_name (
162 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
163 `title` varchar(255) DEFAULT '',
164 `group_id` int(11) NOT NULL,
165 `notification_email` varchar(255) DEFAULT NULL,
166 `confirmation_email_enabled` tinyint(1) NOT NULL DEFAULT '0',
167 `confirmation_email_webling_field` int(11) DEFAULT NULL,
168 `confirmation_email_subject` varchar(255) DEFAULT '',
169 `confirmation_email_text` text,
170 `confirmation_text` text,
171 `submit_button_text` varchar(255) DEFAULT NULL,
172 `class` varchar(255) DEFAULT NULL,
173 `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
174 `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
175 PRIMARY KEY (`id`)
176 ) $charset_collate;";
177 dbDelta( $sql );
178
179 update_option("webling-db-version", 3);
180 }
181
182 // upgrade to db version 2
183 function webling_plugin_initial_setup() {
184 global $wpdb;
185
186 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
187
188 $charset_collate = $wpdb->get_charset_collate();
189
190 // create cache table
191 $table_name = $wpdb->prefix . 'webling_cache';
192 $sql = "CREATE TABLE $table_name (
193 id mediumint(9) NOT NULL,
194 type VARCHAR(20) NOT NULL,
195 data text NOT NULL,
196 PRIMARY KEY (id)
197 ) $charset_collate;";
198 dbDelta( $sql );
199
200 // create memberlists table
201 $table_name = $wpdb->prefix . 'webling_memberlists';
202 $sql = "CREATE TABLE $table_name (
203 id int(11) unsigned NOT NULL AUTO_INCREMENT,
204 title varchar(255) DEFAULT NULL,
205 show_all_groups tinyint(1) NOT NULL DEFAULT '0',
206 groups text,
207 fields text,
208 sortorder enum('ASC','DESC') DEFAULT 'ASC',
209 sortfield varchar(255) DEFAULT NULL,
210 class varchar(255) DEFAULT NULL,
211 updated_at timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
212 created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
213 PRIMARY KEY (`id`)
214 ) $charset_collate;";
215 dbDelta( $sql );
216
217 // create forms table
218 $table_name = $wpdb->prefix . 'webling_forms';
219 $sql = "CREATE TABLE $table_name (
220 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
221 `title` varchar(255) DEFAULT '',
222 `group_id` int(11) NOT NULL,
223 `notification_email` varchar(255) DEFAULT NULL,
224 `confirmation_text` text,
225 `submit_button_text` varchar(255) DEFAULT NULL,
226 `class` varchar(255) DEFAULT NULL,
227 `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
228 `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
229 PRIMARY KEY (`id`)
230 ) $charset_collate;";
231 dbDelta( $sql );
232
233 // create form fields table
234 $table_name = $wpdb->prefix . 'webling_form_fields';
235 $sql = "CREATE TABLE $table_name (
236 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
237 `form_id` int(11) NOT NULL,
238 `order` int(11) NOT NULL,
239 `webling_field_id` int(11) NOT NULL,
240 `type` varchar(50) NOT NULL DEFAULT 'text',
241 `required` tinyint(1) NOT NULL DEFAULT '0',
242 `field_name` varchar(255) NOT NULL DEFAULT '',
243 `field_name_position` enum('TOP','LEFT','HIDDEN') NOT NULL DEFAULT 'TOP',
244 `placeholder_text` varchar(255) DEFAULT NULL,
245 `description_text` text,
246 `class` varchar(255) DEFAULT NULL,
247 `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
248 `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
249 PRIMARY KEY (`id`),
250 KEY `form_id` (`form_id`)
251 ) $charset_collate;";
252 dbDelta( $sql );
253
254
255 // migrate old shortcodes to new format
256 $defaultOptions = array(
257 'host' => '',
258 'apikey' => '',
259 'css' => ''
260 );
261 $options = get_option('webling-options', $defaultOptions);
262 if (isset($options['fields'])) {
263 $old_fields = explode(',', $options['fields']);
264 } else {
265 $old_fields = ['Vorname'];
266 }
267 $firstfield = $old_fields[0];
268
269
270 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_content LIKE '%[webling_memberlist%'
271 and (
272 post_status = 'publish' or
273 post_status = 'draft' or
274 post_status = 'pending' or
275 post_status = 'private' or
276 post_status = 'future'
277 )";
278 $posts = $wpdb->get_results($sql, 'ARRAY_A');
279 foreach ($posts as $post) {
280 $original_content = $post['post_content'];
281 $hasError = false;
282
283 preg_match_all("/\[webling_memberlist(.*)\]/", $post['post_content'], $matches);
284 foreach ($matches[0] as $index => $match) {
285 $shortcode = $match;
286 $arguments = shortcode_parse_atts($matches[1][$index]);
287
288 // abort if there is already an id argument (migration probably done aready)
289 if (isset($arguments['id'])) {
290 $hasError = true;
291 continue;
292 }
293
294 $groups = array();
295 if (isset($arguments['groups'])) {
296 $groupIds = explode(',', $arguments['groups']);
297 if(is_array($groupIds)) {
298 foreach ($groupIds as $groupId) {
299 if (intval($groupId) > 0) {
300 $groups[] = intval($groupId);
301 }
302 }
303 }
304 }
305
306 // create new list
307 $wpdb->query(
308 $wpdb->prepare("
309 INSERT INTO {$wpdb->prefix}webling_memberlists
310 (
311 `title`,
312 `show_all_groups`,
313 `groups`,
314 `fields`,
315 `class`,
316 `sortfield`,
317 `sortorder`
318 ) VALUES (
319 %s,
320 %s,
321 %s,
322 %s,
323 %s,
324 %s,
325 %s
326 )",
327 'Mitgliederliste',
328 (count($groups) > 0 ? 0 : 1),
329 serialize($groups),
330 json_encode($old_fields),
331 '',
332 $firstfield,
333 'ASC'
334 )
335 );
336
337 $list_id = $wpdb->insert_id;
338 $new_shortcode = '[webling_memberlist id="'.$list_id.'"]';
339
340 $pos = strpos($original_content, $shortcode);
341 if ($pos !== false) {
342 $original_content = substr_replace($original_content, $new_shortcode, $pos, strlen($shortcode));
343 }
344 }
345
346 if (!$hasError) {
347 wp_update_post(
348 [
349 'ID' => $post['ID'],
350 'post_content' => $original_content,
351 ]
352 );
353
354 // add a new revision as a backup if anything goes wrong
355 wp_save_post_revision($post['ID']);
356 }
357 }
358
359 // unset "fields" in webling-options as it is not used anymore
360 unset($options['fields']);
361 update_option('webling-options', $options);
362
363 // update version
364 add_option("webling-db-version", 2);
365 update_option("webling-db-version", 2);
366 }
367