| 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 |
if ($installed_db_version <= 1) { |
| 12 |
webling_plugin_initial_setup(); |
| 13 |
} |
| 14 |
} |
| 15 |
|
| 16 |
// ************* Version Upgrade Functions ************* |
| 17 |
|
| 18 |
// upgrade to db version 1 |
| 19 |
function webling_plugin_initial_setup() { |
| 20 |
global $wpdb; |
| 21 |
|
| 22 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 23 |
|
| 24 |
$charset_collate = $wpdb->get_charset_collate(); |
| 25 |
|
| 26 |
// create cache table |
| 27 |
$table_name = $wpdb->prefix . 'webling_cache'; |
| 28 |
$sql = "CREATE TABLE $table_name ( |
| 29 |
id mediumint(9) NOT NULL, |
| 30 |
type VARCHAR(20) NOT NULL, |
| 31 |
data text NOT NULL, |
| 32 |
PRIMARY KEY (id) |
| 33 |
) $charset_collate;"; |
| 34 |
dbDelta( $sql ); |
| 35 |
|
| 36 |
// create memberlists table |
| 37 |
$table_name = $wpdb->prefix . 'webling_memberlists'; |
| 38 |
$sql = "CREATE TABLE $table_name ( |
| 39 |
id int(11) unsigned NOT NULL AUTO_INCREMENT, |
| 40 |
title varchar(255) DEFAULT NULL, |
| 41 |
show_all_groups tinyint(1) NOT NULL DEFAULT '0', |
| 42 |
groups text, |
| 43 |
fields text, |
| 44 |
sortorder enum('ASC','DESC') DEFAULT 'ASC', |
| 45 |
sortfield varchar(255) DEFAULT NULL, |
| 46 |
class varchar(255) DEFAULT NULL, |
| 47 |
updated_at timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, |
| 48 |
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 49 |
PRIMARY KEY (`id`) |
| 50 |
) $charset_collate;"; |
| 51 |
dbDelta( $sql ); |
| 52 |
|
| 53 |
// create forms table |
| 54 |
$table_name = $wpdb->prefix . 'webling_forms'; |
| 55 |
$sql = "CREATE TABLE $table_name ( |
| 56 |
`id` int(11) unsigned NOT NULL AUTO_INCREMENT, |
| 57 |
`title` varchar(255) DEFAULT '', |
| 58 |
`group_id` int(11) NOT NULL, |
| 59 |
`notification_email` varchar(255) DEFAULT NULL, |
| 60 |
`confirmation_text` text, |
| 61 |
`submit_button_text` varchar(255) DEFAULT NULL, |
| 62 |
`class` varchar(255) DEFAULT NULL, |
| 63 |
`updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, |
| 64 |
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 65 |
PRIMARY KEY (`id`) |
| 66 |
) $charset_collate;"; |
| 67 |
dbDelta( $sql ); |
| 68 |
|
| 69 |
// create form fields table |
| 70 |
$table_name = $wpdb->prefix . 'webling_form_fields'; |
| 71 |
$sql = "CREATE TABLE $table_name ( |
| 72 |
`id` int(11) unsigned NOT NULL AUTO_INCREMENT, |
| 73 |
`form_id` int(11) NOT NULL, |
| 74 |
`order` int(11) NOT NULL, |
| 75 |
`webling_field_id` int(11) NOT NULL, |
| 76 |
`type` varchar(50) NOT NULL DEFAULT 'text', |
| 77 |
`required` tinyint(1) NOT NULL DEFAULT '0', |
| 78 |
`field_name` varchar(255) NOT NULL DEFAULT '', |
| 79 |
`field_name_position` enum('TOP','LEFT','HIDDEN') NOT NULL DEFAULT 'TOP', |
| 80 |
`placeholder_text` varchar(255) DEFAULT NULL, |
| 81 |
`description_text` text, |
| 82 |
`class` varchar(255) DEFAULT NULL, |
| 83 |
`updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, |
| 84 |
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 85 |
PRIMARY KEY (`id`), |
| 86 |
KEY `form_id` (`form_id`) |
| 87 |
) $charset_collate;"; |
| 88 |
dbDelta( $sql ); |
| 89 |
|
| 90 |
|
| 91 |
// migrate old shortcodes to new format |
| 92 |
$defaultOptions = array( |
| 93 |
'host' => '', |
| 94 |
'apikey' => '', |
| 95 |
'css' => '' |
| 96 |
); |
| 97 |
$options = get_option('webling-options', $defaultOptions); |
| 98 |
if (isset($options['fields'])) { |
| 99 |
$old_fields = explode(',', $options['fields']); |
| 100 |
} else { |
| 101 |
$old_fields = ['Vorname']; |
| 102 |
} |
| 103 |
$firstfield = $old_fields[0]; |
| 104 |
|
| 105 |
|
| 106 |
$sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_content LIKE '%[webling_memberlist%' |
| 107 |
and ( |
| 108 |
post_status = 'publish' or |
| 109 |
post_status = 'draft' or |
| 110 |
post_status = 'pending' or |
| 111 |
post_status = 'private' or |
| 112 |
post_status = 'future' |
| 113 |
)"; |
| 114 |
$posts = $wpdb->get_results($sql, 'ARRAY_A'); |
| 115 |
foreach ($posts as $post) { |
| 116 |
$original_content = $post['post_content']; |
| 117 |
$hasError = false; |
| 118 |
|
| 119 |
preg_match_all("/\[webling_memberlist(.*)\]/", $post['post_content'], $matches); |
| 120 |
foreach ($matches[0] as $index => $match) { |
| 121 |
$shortcode = $match; |
| 122 |
$arguments = shortcode_parse_atts($matches[1][$index]); |
| 123 |
|
| 124 |
// abort if there is already an id argument (migration probably done aready) |
| 125 |
if (isset($arguments['id'])) { |
| 126 |
$hasError = true; |
| 127 |
continue; |
| 128 |
} |
| 129 |
|
| 130 |
$groups = array(); |
| 131 |
if (isset($arguments['groups'])) { |
| 132 |
$groupIds = explode(',', $arguments['groups']); |
| 133 |
if(is_array($groupIds)) { |
| 134 |
foreach ($groupIds as $groupId) { |
| 135 |
if (intval($groupId) > 0) { |
| 136 |
$groups[] = intval($groupId); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
// create new list |
| 143 |
$wpdb->query( |
| 144 |
$wpdb->prepare(" |
| 145 |
INSERT INTO {$wpdb->prefix}webling_memberlists |
| 146 |
( |
| 147 |
`title`, |
| 148 |
`show_all_groups`, |
| 149 |
`groups`, |
| 150 |
`fields`, |
| 151 |
`class`, |
| 152 |
`sortfield`, |
| 153 |
`sortorder` |
| 154 |
) VALUES ( |
| 155 |
%s, |
| 156 |
%s, |
| 157 |
%s, |
| 158 |
%s, |
| 159 |
%s, |
| 160 |
%s, |
| 161 |
%s |
| 162 |
)", |
| 163 |
'Mitgliederliste', |
| 164 |
(count($groups) > 0 ? 0 : 1), |
| 165 |
serialize($groups), |
| 166 |
json_encode($old_fields), |
| 167 |
'', |
| 168 |
$firstfield, |
| 169 |
'ASC' |
| 170 |
) |
| 171 |
); |
| 172 |
|
| 173 |
$list_id = $wpdb->insert_id; |
| 174 |
$new_shortcode = '[webling_memberlist id="'.$list_id.'"]'; |
| 175 |
|
| 176 |
$pos = strpos($original_content, $shortcode); |
| 177 |
if ($pos !== false) { |
| 178 |
$original_content = substr_replace($original_content, $new_shortcode, $pos, strlen($shortcode)); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
if (!$hasError) { |
| 183 |
wp_update_post( |
| 184 |
[ |
| 185 |
'ID' => $post['ID'], |
| 186 |
'post_content' => $original_content, |
| 187 |
] |
| 188 |
); |
| 189 |
|
| 190 |
// add a new revision as a backup if anything goes wrong |
| 191 |
wp_save_post_revision($post['ID']); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
// unset "fields" in webling-options as it is not used anymore |
| 196 |
unset($options['fields']); |
| 197 |
update_option('webling-options', $options); |
| 198 |
|
| 199 |
// update version |
| 200 |
add_option("webling-db-version", WEBLING_DB_VERSION); |
| 201 |
update_option("webling-db-version", WEBLING_DB_VERSION); |
| 202 |
} |
| 203 |
|