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