PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.0
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.0, at includes/Core/Database/DB.php

251 lines 10.9 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 /**
13 * Database Migration
14 */
15 final class DB {
16 /**
17 * Undocumented function
18 *
19 * @return void
20 */
21 public static function migrate() {
22 global $wpdb;
23 global $bitforms_db_version;
24 $collate = '';
25
26 if ($wpdb->has_cap('collation')) {
27 if (!empty($wpdb->charset)) {
28 $collate .= "DEFAULT CHARACTER SET $wpdb->charset";
29 }
30
31 if (!empty($wpdb->collate)) {
32 $collate .= " COLLATE $wpdb->collate";
33 }
34 }
35
36 $max_index_length = 191;
37
38 $table_schema = [
39 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}bitforms_reports` (
40 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
41 `category` varchar(50) NOT NULL,
42 `type` varchar(20) NOT NULL,
43 `context` varchar(20) NOT NULL,
44 `details` longtext DEFAULT NULL,
45 `isDefault` tinyint(1) DEFAULT 0,/* 0 not default, 1 default */
46 `user_id` bigint(20) unsigned DEFAULT NULL,
47 `user_ip` int(11) unsigned DEFAULT NULL,
48 `user_device` varchar(50) DEFAULT NULL,
49 `created_at` datetime DEFAULT NULL,
50 `updated_at` datetime DEFAULT NULL,
51 PRIMARY KEY (`id`)
52 ) $collate;",
53
54 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}bitforms_form` (
55 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
56 `form_name` varchar(255) DEFAULT NULL,
57 `form_content` longtext DEFAULT NULL,
58 `builder_helper_state` longtext DEFAULT NULL,
59 `atomic_class_map` longtext DEFAULT NULL,
60 `generated_script_page_ids` longtext DEFAULT NULL,
61 `user_id` bigint(20) unsigned DEFAULT NULL,
62 `user_ip` int(11) unsigned DEFAULT NULL,
63 `views` int(11) unsigned DEFAULT 0,
64 `entries` int(11) unsigned DEFAULT 0,
65 `user_device` varchar(50) DEFAULT NULL,
66 `status` tinyint(1) DEFAULT 1,/* 0 not published, 1 published, 2 trashed */
67 `created_at` datetime DEFAULT NULL,
68 `updated_at` datetime DEFAULT NULL,
69 PRIMARY KEY (`id`)
70 ) $collate;",
71
72 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}bitforms_workflows` (
73 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
74 `workflow_name` varchar(255) NOT NULL,
75 `workflow_type` varchar(50) NOT NULL,
76 `workflow_run` varchar(50) NOT NULL,
77 `workflow_behaviour` varchar(25) NOT NULL,
78 `workflow_condition` longtext DEFAULT NULL,
79 `workflow_order` int(11) unsigned DEFAULT NULL,
80 `form_id` bigint(20) unsigned DEFAULT NULL,
81 `user_id` bigint(20) unsigned DEFAULT NULL,
82 `user_ip` int(11) unsigned DEFAULT NULL,
83 `user_location` text DEFAULT NULL,
84 `user_device` varchar(50) DEFAULT NULL,
85 `created_at` datetime DEFAULT NULL,
86 `updated_at` datetime DEFAULT NULL,
87 PRIMARY KEY (`id`),
88 KEY `form_id` (`form_id`)
89 ) $collate;",
90
91 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}bitforms_success_messages` (
92 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
93 `message_title` varchar(255) DEFAULT NULL,
94 `message_content` longtext DEFAULT NULL,
95 `message_config` longtext DEFAULT NULL,
96 `form_id` bigint(20) unsigned DEFAULT NULL,
97 `user_id` bigint(20) unsigned DEFAULT NULL,
98 `user_ip` int(11) unsigned DEFAULT NULL,
99 `created_at` datetime DEFAULT NULL,
100 `updated_at` datetime DEFAULT NULL,
101 PRIMARY KEY (`id`)
102 ) $collate;",
103
104 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}bitforms_email_template` (
105 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
106 `title` text DEFAULT NULL,
107 `sub` text DEFAULT NULL,
108 `body` longtext DEFAULT NULL,
109 `form_id` bigint(20) unsigned DEFAULT NULL,
110 `user_id` bigint(20) unsigned DEFAULT NULL,
111 `user_ip` int(11) unsigned DEFAULT NULL,
112 `created_at` datetime DEFAULT NULL,
113 `updated_at` datetime DEFAULT NULL,
114 PRIMARY KEY (`id`)
115 ) $collate;",
116
117 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}bitforms_integration` (
118 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
119 `category` varchar(50) NOT NULL,
120 `integration_name` varchar(255) DEFAULT NULL,
121 `integration_type` varchar(50) NOT NULL,
122 `integration_details` longtext DEFAULT NULL,
123 `form_id` bigint(20) unsigned DEFAULT NULL, /* form_id = 0 means all/app */
124 `user_id` bigint(20) unsigned DEFAULT NULL,
125 `user_ip` int(11) unsigned DEFAULT NULL,
126 `status` tinyint(1) DEFAULT 1,/* 0 disabled, 1 published, 2 trashed */
127 `created_at` datetime DEFAULT NULL,
128 `updated_at` datetime DEFAULT NULL,
129 PRIMARY KEY (`id`)
130 ) $collate;",
131
132 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}bitforms_form_entries` (
133 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
134 `form_id` bigint(20) unsigned DEFAULT NULL,
135 `user_id` bigint(20) unsigned DEFAULT NULL,
136 `user_ip` varchar(255) DEFAULT NULL,
137 `user_location` text DEFAULT NULL,
138 `user_device` varchar(50) DEFAULT NULL,
139 `referer` varchar(255) DEFAULT NULL,
140 `status` tinyint(1) DEFAULT 0,/* 0 not viewed, 1 viewed, 2 trashed */
141 `created_at` datetime DEFAULT NULL,
142 `updated_at` datetime DEFAULT NULL,
143 PRIMARY KEY (`id`),
144 KEY `form_id` (`form_id`)
145 ) $collate;",
146
147 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}bitforms_form_entrymeta` (
148 `meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
149 `bitforms_form_entry_id` bigint(20) unsigned DEFAULT NULL,
150 `meta_key` varchar(255) DEFAULT NULL,
151 `meta_value` longtext,
152 PRIMARY KEY (`meta_id`),
153 KEY `meta_key` (`meta_key`({$max_index_length})),
154 KEY `entry_id` (`bitforms_form_entry_id`)
155 ) $collate;",
156
157 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}bitforms_form_entry_log` (
158 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
159 `user_id` int(11) NOT NULL,
160 `log_type` varchar(50) DEFAULT NULL,
161 `ip` int(11) DEFAULT NULL,
162 `action_type` varchar(50) DEFAULT NULL,
163 `form_entry_id` bigint(20) DEFAULT NULL,
164 `form_id` bigint(20) DEFAULT NULL,
165 `content` longtext,
166 `created_at` DATETIME NOT NULL,
167 PRIMARY KEY (`id`),
168 KEY `form_id` (`form_id`),
169 KEY `form_entry_id` (`form_entry_id`)
170 ) $collate;",
171
172 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}bitforms_form_log_details` (
173 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
174 `log_id` bigint(20) NOT NULL,
175 `integration_id` bigint(20) DEFAULT NULL,
176 `api_type` varchar(255) DEFAULT NULL,
177 `response_type` varchar(50) DEFAULT NULL,
178 `response_obj` LONGTEXT DEFAULT NULL,
179 `created_at` DATETIME NOT NULL,
180 PRIMARY KEY (`id`),
181 KEY `log_id` (`log_id`),
182 KEY `integration_id` (`integration_id`)
183 ) $collate;",
184 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}bitforms_form_entry_relatedinfo` (
185 `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
186 `info_type` VARCHAR(50) NOT NULL,
187 `info_details` LONGTEXT NULL,
188 `form_id` BIGINT(20) UNSIGNED NOT NULL,
189 `entry_id` BIGINT(20) UNSIGNED NOT NULL,
190 `user_id` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
191 `user_ip` INT(11) UNSIGNED NULL DEFAULT NULL,
192 `status` INT(1) UNSIGNED NOT NULL DEFAULT '1',
193 `created_at` DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
194 `updated_at` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
195 PRIMARY KEY (`id`),
196 KEY `form_id` (`form_id`),
197 KEY `entry_id` (`entry_id`)
198 ) $collate;",
199 ];
200
201 $table_schema_update = [
202 "ALTER TABLE `{$wpdb->prefix}bitforms_form_entry_log`
203 CHANGE COLUMN `log_type` `log_type` VARCHAR(50) NULL DEFAULT NULL,
204 ADD COLUMN `action_type` VARCHAR(50) NULL DEFAULT NULL,
205 CHANGE COLUMN `meta_key` `content` LONGTEXT NULL,
206 CHANGE COLUMN `created_at` `created_at` DATETIME NOT NULL",
207 "ALTER TABLE `{$wpdb->prefix}bitforms_form_log_details`
208 CHANGE COLUMN `api_type` `api_type` VARCHAR(255) NULL DEFAULT NULL,
209 CHANGE COLUMN `response_obj` `response_obj` LONGTEXT NULL,
210 CHANGE COLUMN `created_at` `created_at` DATETIME NOT NULL",
211 ];
212
213 include_once ABSPATH . 'wp-admin/includes/upgrade.php';
214 foreach ($table_schema as $table) {
215 dbDelta($table);
216 }
217 $installed_db_version = get_site_option('bitforms_db_version');
218 if ($installed_db_version && version_compare('1.1', $installed_db_version, '>=')) {
219 // if new db version is equal or higher than 1.1
220 foreach ($table_schema_update as $table) {
221 $wpdb->query($table);
222 }
223 }
224 if ($installed_db_version && version_compare('1.3', $installed_db_version, '>=')) {
225 // if new db version is equal or higher than 1.3
226 $wpdb->query("ALTER TABLE `{$wpdb->prefix}bitforms_form_entries` CHANGE `user_ip` `user_ip` VARCHAR(255) NULL DEFAULT NULL");
227 }
228
229 if ($installed_db_version && version_compare('2.0', $installed_db_version, '>=')) {
230 $wpdb->query(
231 "ALTER TABLE `{$wpdb->prefix}bitforms_success_messages`
232 ADD `message_config` LONGTEXT NULL AFTER `message_content`"
233 );
234 $wpdb->query(
235 "ALTER TABLE `{$wpdb->prefix}bitforms_form`
236 ADD `builder_helper_state` LONGTEXT NULL AFTER `form_content`,
237 ADD `atomic_class_map` LONGTEXT NULL AFTER `builder_helper_state`,
238 ADD `generated_script_page_ids` LONGTEXT NULL AFTER `atomic_class_map`"
239 );
240 $wpdb->query(
241 "ALTER TABLE `{$wpdb->prefix}bitforms_workflows`
242 ADD `workflow_order` INT(11) DEFAULT NULL AFTER `workflow_condition`"
243 );
244 }
245 update_site_option(
246 'bitforms_db_version',
247 $bitforms_db_version
248 );
249 }
250 }
251