PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.21.4
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.21.4
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Core / Database / DB.php

DB.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.21.4, at includes/Core/Database/DB.php

296 lines 12.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class For Database Migration
5 *
6 * @category Database
7 * @author BitCode Developer <developer@bitcode.pro>
8 */
9
10 namespace BitCode\BitForm\Core\Database;
11
12 use BitCode\BitForm\Core\Util\Log;
13
14 /**
15 * Database Migration
16 */
17 final class DB
18 {
19 /**
20 * Undocumented function
21 *
22 * @return void
23 */
24 public static function migrate()
25 {
26 global $wpdb;
27 global $bitforms_db_version;
28 $collate = '';
29
30 if ($wpdb->has_cap('collation')) {
31 if (!empty($wpdb->charset)) {
32 $collate .= "DEFAULT CHARACTER SET $wpdb->charset";
33 }
34
35 if (!empty($wpdb->collate)) {
36 $collate .= " COLLATE $wpdb->collate";
37 }
38 }
39
40 $max_index_length = 191;
41
42 $table_schema = [
43 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}bitforms_reports` (
44 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
45 `category` varchar(50) NOT NULL,
46 `type` varchar(20) NOT NULL,
47 `context` varchar(20) NOT NULL,
48 `details` longtext DEFAULT NULL,
49 `isDefault` tinyint(1) DEFAULT 0,/* 0 not default, 1 default */
50 `user_id` bigint(20) unsigned DEFAULT NULL,
51 `user_ip` int(11) unsigned DEFAULT NULL,
52 `user_device` varchar(50) DEFAULT NULL,
53 `created_at` datetime DEFAULT NULL,
54 `updated_at` datetime DEFAULT NULL,
55 PRIMARY KEY (`id`)
56 ) $collate;",
57
58 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}bitforms_form` (
59 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
60 `form_name` varchar(255) DEFAULT NULL,
61 `form_content` longtext DEFAULT NULL,
62 `builder_helper_state` longtext DEFAULT NULL,
63 `atomic_class_map` longtext DEFAULT NULL,
64 `generated_script_page_ids` longtext DEFAULT NULL,
65 `user_id` bigint(20) unsigned DEFAULT NULL,
66 `user_ip` int(11) unsigned DEFAULT NULL,
67 `views` int(11) unsigned DEFAULT 0,
68 `entries` int(11) unsigned DEFAULT 0,
69 `user_device` varchar(50) DEFAULT NULL,
70 `status` tinyint(1) DEFAULT 1,/* 0 not published, 1 published, 2 trashed */
71 `created_at` datetime DEFAULT NULL,
72 `updated_at` datetime DEFAULT NULL,
73 PRIMARY KEY (`id`)
74 ) $collate;",
75
76 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}bitforms_workflows` (
77 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
78 `workflow_name` varchar(255) NOT NULL,
79 `workflow_type` varchar(50) NOT NULL,
80 `workflow_run` varchar(50) NOT NULL,
81 `workflow_behaviour` varchar(25) NOT NULL,
82 `workflow_condition` longtext DEFAULT NULL,
83 `workflow_order` int(11) unsigned DEFAULT NULL,
84 `workflow_status` tinyint(1) DEFAULT 1,/* 0 disable, 1 enable, 2 enable in field settings */
85 `form_id` bigint(20) unsigned DEFAULT NULL,
86 `user_id` bigint(20) unsigned DEFAULT NULL,
87 `user_ip` int(11) unsigned DEFAULT NULL,
88 `user_location` text DEFAULT NULL,
89 `user_device` varchar(50) DEFAULT NULL,
90 `created_at` datetime DEFAULT NULL,
91 `updated_at` datetime DEFAULT NULL,
92 PRIMARY KEY (`id`),
93 KEY `form_id` (`form_id`)
94 ) $collate;",
95
96 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}bitforms_success_messages` (
97 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
98 `message_title` varchar(255) DEFAULT NULL,
99 `message_content` longtext DEFAULT NULL,
100 `message_config` longtext DEFAULT NULL,
101 `form_id` bigint(20) unsigned DEFAULT NULL,
102 `user_id` bigint(20) unsigned DEFAULT NULL,
103 `user_ip` int(11) unsigned DEFAULT NULL,
104 `created_at` datetime DEFAULT NULL,
105 `updated_at` datetime DEFAULT NULL,
106 PRIMARY KEY (`id`)
107 ) $collate;",
108
109 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}bitforms_email_template` (
110 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
111 `title` text DEFAULT NULL,
112 `sub` text DEFAULT NULL,
113 `body` longtext DEFAULT NULL,
114 `form_id` bigint(20) unsigned DEFAULT NULL,
115 `user_id` bigint(20) unsigned DEFAULT NULL,
116 `user_ip` int(11) unsigned DEFAULT NULL,
117 `created_at` datetime DEFAULT NULL,
118 `updated_at` datetime DEFAULT NULL,
119 PRIMARY KEY (`id`)
120 ) $collate;",
121
122 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}bitforms_integration` (
123 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
124 `category` varchar(50) NOT NULL,
125 `integration_name` varchar(255) DEFAULT NULL,
126 `integration_type` varchar(50) NOT NULL,
127 `integration_details` longtext DEFAULT NULL,
128 `form_id` bigint(20) unsigned DEFAULT NULL, /* form_id = 0 means all/app */
129 `user_id` bigint(20) unsigned DEFAULT NULL,
130 `user_ip` int(11) unsigned DEFAULT NULL,
131 `status` tinyint(1) DEFAULT 1,/* 0 disabled, 1 published, 2 trashed */
132 `created_at` datetime DEFAULT NULL,
133 `updated_at` datetime DEFAULT NULL,
134 PRIMARY KEY (`id`)
135 ) $collate;",
136
137 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}bitforms_form_entries` (
138 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
139 `form_id` bigint(20) unsigned DEFAULT NULL,
140 `user_id` bigint(20) unsigned DEFAULT NULL,
141 `user_ip` varchar(255) DEFAULT NULL,
142 `user_location` text DEFAULT NULL,
143 `user_device` varchar(50) DEFAULT NULL,
144 `referer` TEXT DEFAULT NULL,
145 `status` tinyint(1) DEFAULT 0,/* 0 not viewed, 1 viewed, 2 trashed */
146 `created_at` datetime DEFAULT NULL,
147 `updated_at` datetime DEFAULT NULL,
148 PRIMARY KEY (`id`),
149 KEY `form_id` (`form_id`)
150 ) $collate;",
151
152 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}bitforms_form_entrymeta` (
153 `meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
154 `bitforms_form_entry_id` bigint(20) unsigned DEFAULT NULL,
155 `meta_key` varchar(255) DEFAULT NULL,
156 `meta_value` longtext,
157 PRIMARY KEY (`meta_id`),
158 KEY `meta_key` (`meta_key`({$max_index_length})),
159 KEY `entry_id` (`bitforms_form_entry_id`)
160 ) $collate;",
161
162 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}bitforms_form_entry_log` (
163 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
164 `user_id` int(11) NOT NULL,
165 `log_type` varchar(50) DEFAULT NULL,
166 `ip` int(11) DEFAULT NULL,
167 `action_type` varchar(50) DEFAULT NULL,
168 `form_entry_id` bigint(20) DEFAULT NULL,
169 `form_id` bigint(20) DEFAULT NULL,
170 `content` longtext,
171 `created_at` DATETIME NOT NULL,
172 PRIMARY KEY (`id`),
173 KEY `form_id` (`form_id`),
174 KEY `form_entry_id` (`form_entry_id`)
175 ) $collate;",
176
177 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}bitforms_form_log_details` (
178 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
179 `log_id` bigint(20) NOT NULL,
180 `integration_id` bigint(20) DEFAULT NULL,
181 `api_type` varchar(255) DEFAULT NULL,
182 `response_type` varchar(50) DEFAULT NULL,
183 `response_obj` LONGTEXT DEFAULT NULL,
184 `created_at` DATETIME NOT NULL,
185 PRIMARY KEY (`id`),
186 KEY `log_id` (`log_id`),
187 KEY `integration_id` (`integration_id`)
188 ) $collate;",
189 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}bitforms_form_entry_relatedinfo` (
190 `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
191 `info_type` VARCHAR(50) NOT NULL,
192 `info_details` LONGTEXT NULL,
193 `form_id` BIGINT(20) UNSIGNED NOT NULL,
194 `entry_id` BIGINT(20) UNSIGNED NOT NULL,
195 `user_id` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
196 `user_ip` INT(11) UNSIGNED NULL DEFAULT NULL,
197 `status` INT(1) UNSIGNED NOT NULL DEFAULT '1',
198 `created_at` DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
199 `updated_at` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
200 PRIMARY KEY (`id`),
201 KEY `form_id` (`form_id`),
202 KEY `entry_id` (`entry_id`)
203 ) $collate;"
204 ];
205
206 $table_schema_update = [
207 "ALTER TABLE `{$wpdb->prefix}bitforms_form_entry_log`
208 MODIFY `log_type` VARCHAR(50) NULL DEFAULT NULL,
209 MODIFY `created_at` DATETIME NOT NULL,
210 CHANGE COLUMN `meta_key` `content` LONGTEXT NULL,
211 ADD COLUMN `action_type` VARCHAR(50) NULL DEFAULT NULL,
212 ADD COLUMN `content` LONGTEXT NULL AFTER `action_type`",
213 "ALTER TABLE `{$wpdb->prefix}bitforms_form_log_details`
214 MODIFY `api_type` VARCHAR(255) NULL DEFAULT NULL,
215 MODIFY `response_obj` LONGTEXT NULL,
216 MODIFY `created_at` DATETIME NOT NULL"
217 ];
218
219 try {
220 include_once ABSPATH . 'wp-admin/includes/upgrade.php';
221 foreach ($table_schema as $table) {
222 dbDelta($table);
223 }
224 $installed_db_version = get_site_option('bitforms_db_version');
225 if ($installed_db_version && version_compare('1.1', $installed_db_version, '>=')) {
226 // if new db version is equal or higher than 1.1
227 foreach ($table_schema_update as $query) {
228 try {
229 $wpdb->query($query);
230 } catch (\Exception $e) {
231 Log::debug_log("Update failed: {$e->getMessage()} in query: {$query}");
232 }
233 }
234 }
235 if ($installed_db_version && version_compare('1.3', $installed_db_version, '>=')) {
236 // if new db version is equal or higher than 1.3
237 $wpdb->query("ALTER TABLE `{$wpdb->prefix}bitforms_form_entries` MODIFY `user_ip` VARCHAR(255) NULL DEFAULT NULL");
238 }
239
240 if ($installed_db_version && version_compare('2.0', $installed_db_version, '>=')) {
241 $table_name = $wpdb->prefix . 'bitforms_success_messages';
242 if (null === $wpdb->get_var("SHOW COLUMNS FROM `{$table_name}` LIKE 'message_config'")) {
243 $wpdb->query(
244 "ALTER TABLE `{$table_name}`
245 ADD COLUMN `message_config` LONGTEXT NULL AFTER `message_content`"
246 );
247 }
248 $table_name = $wpdb->prefix . 'bitforms_form';
249 if (null === $wpdb->get_var("SHOW COLUMNS FROM `{$table_name}` LIKE 'builder_helper_state'")) {
250 $wpdb->query(
251 "ALTER TABLE `{$table_name}`
252 ADD COLUMN `builder_helper_state` LONGTEXT NULL AFTER `form_content`,
253 ADD COLUMN `atomic_class_map` LONGTEXT NULL AFTER `builder_helper_state`,
254 ADD COLUMN `generated_script_page_ids` LONGTEXT NULL AFTER `atomic_class_map`"
255 );
256 }
257 $table_name = $wpdb->prefix . 'bitforms_workflows';
258 if (null === $wpdb->get_var("SHOW COLUMNS FROM `{$table_name}` LIKE 'workflow_order'")) {
259 $wpdb->query(
260 "ALTER TABLE `{$table_name}`
261 ADD COLUMN `workflow_order` INT(11) DEFAULT NULL AFTER `workflow_condition`"
262 );
263 }
264 }
265
266 if ($installed_db_version && version_compare('2.2', $installed_db_version, '>=')) {
267 $table_name = $wpdb->prefix . 'bitforms_form_entries';
268 if (null === $wpdb->get_var("SHOW COLUMNS FROM `{$table_name}` LIKE 'referer'")) {
269 $wpdb->query(
270 "ALTER TABLE `{$table_name}`
271 ADD COLUMN `referer` TEXT NULL DEFAULT NULL"
272 );
273 }
274 }
275
276 if ($installed_db_version && version_compare('2.4', $installed_db_version, '>=')) {
277 $table_name = $wpdb->prefix . 'bitforms_workflows';
278 if (null === $wpdb->get_var("SHOW COLUMNS FROM `{$table_name}` LIKE 'workflow_status'")) {
279 $wpdb->query(
280 "ALTER TABLE `{$table_name}`
281 ADD COLUMN `workflow_status` TINYINT(1) DEFAULT 1 NOT NULL AFTER `workflow_order`"
282 );
283 }
284 }
285
286 update_site_option(
287 'bitforms_db_version',
288 $bitforms_db_version
289 );
290 } catch (\Exception $e) {
291 // Log the error to a file or the database
292 Log::debug_log('Error during DB migration: ' . $e->getMessage());
293 }
294 }
295 }
296