| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('UD_CENTRAL_DIR')) die('Security check'); |
| 4 |
|
| 5 |
/** |
| 6 |
* This class sets up database upon plugin activation |
| 7 |
* |
| 8 |
* Creates 3 tables with prefix upon plugin activation. |
| 9 |
* Also handles table structure changes |
| 10 |
*/ |
| 11 |
class UpdraftCentral_Activation { |
| 12 |
|
| 13 |
/** |
| 14 |
* Original wpdb object |
| 15 |
* |
| 16 |
* @var wpdb $original_wpdb |
| 17 |
*/ |
| 18 |
private static $original_wpdb; |
| 19 |
|
| 20 |
/** |
| 21 |
* Table prefix string |
| 22 |
* |
| 23 |
* @var string $table_prefix |
| 24 |
*/ |
| 25 |
private static $table_prefix; |
| 26 |
|
| 27 |
/** |
| 28 |
* A array of method that handle table structure upgrades |
| 29 |
* |
| 30 |
* @var array $db_updates |
| 31 |
*/ |
| 32 |
private static $db_updates = array( |
| 33 |
'0.3.8' => array( |
| 34 |
'update_038_add_admin_url_column_to_sites', |
| 35 |
), |
| 36 |
'0.6.3' => array( |
| 37 |
'update_063_add_created_column_to_sitemeta', |
| 38 |
), |
| 39 |
'0.7.1' => array( |
| 40 |
'update_071_create_user_cron_table', |
| 41 |
), |
| 42 |
'0.8.19' => array( |
| 43 |
'update_0819_create_events_table', |
| 44 |
), |
| 45 |
'0.8.23' => array( |
| 46 |
'update_0823_add_event_item_column_to_events', |
| 47 |
), |
| 48 |
); |
| 49 |
|
| 50 |
/** |
| 51 |
* Retrieves the string containing the character set |
| 52 |
* and collation to use during the table creation process |
| 53 |
* |
| 54 |
* @return string |
| 55 |
*/ |
| 56 |
private static function get_collation() { |
| 57 |
global $wpdb; |
| 58 |
|
| 59 |
$collate = ''; |
| 60 |
|
| 61 |
if ($wpdb->has_cap('collation')) { |
| 62 |
if (!empty($wpdb->charset)) { |
| 63 |
$collate .= "DEFAULT CHARACTER SET $wpdb->charset"; |
| 64 |
} |
| 65 |
if (!empty($wpdb->collate)) { |
| 66 |
$collate .= " COLLATE $wpdb->collate"; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return $collate; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Initialize properties |
| 75 |
* |
| 76 |
* Initializes table prefix property with a custom prefix |
| 77 |
* |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
public static function init() { |
| 81 |
self::$table_prefix = defined('UPDRAFTCENTRAL_TABLE_PREFIX') ? UPDRAFTCENTRAL_TABLE_PREFIX : 'updraftcentral_'; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Sets table prefix, creates 3 tables and updates options table with |
| 86 |
* db structure version |
| 87 |
* |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
public static function install() { |
| 91 |
self::init(); |
| 92 |
self::create_tables(); |
| 93 |
update_option('updraftcentral_dbversion', UpdraftCentral()->version); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Checks installed version and current version and make necessary changes to |
| 98 |
* database table structure |
| 99 |
* |
| 100 |
* @return void |
| 101 |
*/ |
| 102 |
public static function check_updates() { |
| 103 |
self::init(); |
| 104 |
$our_version = UpdraftCentral()->version; |
| 105 |
$db_version = get_option('updraftcentral_dbversion'); |
| 106 |
if (!$db_version || version_compare($our_version, $db_version, '>')) { |
| 107 |
foreach (self::$db_updates as $version => $updates) { |
| 108 |
if (version_compare($version, $db_version, '>')) { |
| 109 |
foreach ($updates as $update) { |
| 110 |
call_user_func(array(__CLASS__, $update)); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
do_action('updraftcentral_version_updated', UpdraftCentral()->version); |
| 115 |
} |
| 116 |
update_option('updraftcentral_dbversion', UpdraftCentral()->version); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Creates the "events" table |
| 121 |
* |
| 122 |
* @return void |
| 123 |
*/ |
| 124 |
public static function update_0819_create_events_table() { |
| 125 |
global $wpdb; |
| 126 |
|
| 127 |
// Preserve the original wpdb object prior to substituting |
| 128 |
// with UpdraftCentral's own wpdb instance |
| 129 |
$wpdb = self::preserve_wpdb($wpdb); |
| 130 |
|
| 131 |
$our_prefix = $wpdb->base_prefix.self::$table_prefix; |
| 132 |
$collate = self::get_collation(); |
| 133 |
|
| 134 |
if (!function_exists('dbDelta')) include_once ABSPATH.'wp-admin/includes/upgrade.php'; |
| 135 |
$create_tables = 'CREATE TABLE '.$our_prefix."events ( |
| 136 |
event_id bigint(20) NOT NULL auto_increment, |
| 137 |
time int NOT NULL, |
| 138 |
site_id bigint(20) NOT NULL, |
| 139 |
event_type text NOT NULL, |
| 140 |
event_name text NOT NULL, |
| 141 |
event_data mediumtext, |
| 142 |
event_status text NOT NULL, |
| 143 |
event_result_data mediumtext, |
| 144 |
PRIMARY KEY (event_id), |
| 145 |
KEY site_id (site_id) |
| 146 |
) $collate; |
| 147 |
"; |
| 148 |
|
| 149 |
dbDelta($create_tables); |
| 150 |
|
| 151 |
// Restoring $wpdb to its original object |
| 152 |
$wpdb = self::restore_wpdb(); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Creates the "user_cron" table and some housekeeping of clearing the previously |
| 157 |
* registered (deprecated) cron events |
| 158 |
* |
| 159 |
* @return void |
| 160 |
*/ |
| 161 |
public static function update_071_create_user_cron_table() { |
| 162 |
global $wpdb; |
| 163 |
|
| 164 |
// Preserve the original wpdb object prior to substituting |
| 165 |
// with UpdraftCentral's own wpdb instance |
| 166 |
$wpdb = self::preserve_wpdb($wpdb); |
| 167 |
|
| 168 |
$our_prefix = $wpdb->base_prefix.self::$table_prefix; |
| 169 |
$collate = self::get_collation(); |
| 170 |
|
| 171 |
$user_cron_table = $our_prefix.'user_cron'; |
| 172 |
if ($user_cron_table !== $wpdb->get_var("SHOW TABLES LIKE '".$user_cron_table."'")) { |
| 173 |
if (!function_exists('dbDelta')) include_once ABSPATH.'wp-admin/includes/upgrade.php'; |
| 174 |
|
| 175 |
$create_tables = 'CREATE TABLE '.$our_prefix."user_cron ( |
| 176 |
id bigint(20) NOT NULL auto_increment, |
| 177 |
user_id bigint(20) NOT NULL, |
| 178 |
last_run bigint(20) DEFAULT 0, |
| 179 |
PRIMARY KEY (id), |
| 180 |
KEY user_id (user_id), |
| 181 |
KEY last_run (last_run) |
| 182 |
) $collate; |
| 183 |
"; |
| 184 |
|
| 185 |
dbDelta($create_tables); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
// Remove previously scheduled (deprecated) "updraftcentra_cron" events in WP-Cron |
| 190 |
// Cleanup previous (no longer needed) crons, since we're now using a single cron |
| 191 |
// for the site's cron process instead of per user. |
| 192 |
if (!function_exists('_get_cron_array')) include ABSPATH.WPINC.'/cron.php'; |
| 193 |
|
| 194 |
$crons = _get_cron_array(); |
| 195 |
if (!empty($crons)) { |
| 196 |
foreach ($crons as $cron) { |
| 197 |
foreach ($cron as $key => $value) { |
| 198 |
if (preg_match('#^updraftcentral#', $key)) { |
| 199 |
foreach ($value as $schedule) { |
| 200 |
wp_clear_scheduled_hook($key, $schedule['args']); |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
// Restoring $wpdb to its original object |
| 208 |
$wpdb = self::restore_wpdb(); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Add the 'created' column to the sitemeta table |
| 213 |
* |
| 214 |
* @return void |
| 215 |
*/ |
| 216 |
public static function update_063_add_created_column_to_sitemeta() { |
| 217 |
$ucdb = UpdraftCentral()->db; |
| 218 |
|
| 219 |
$our_prefix = $ucdb->base_prefix.self::$table_prefix; |
| 220 |
$ucdb->query('ALTER TABLE '.$our_prefix.'sitemeta ADD created bigint(20) DEFAULT 0 AFTER meta_value'); |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Add the 'admin_url' column to the sites table |
| 225 |
* |
| 226 |
* @return void |
| 227 |
*/ |
| 228 |
public static function update_038_add_admin_url_column_to_sites() { |
| 229 |
$ucdb = UpdraftCentral()->db; |
| 230 |
|
| 231 |
$our_prefix = $ucdb->base_prefix.self::$table_prefix; |
| 232 |
$ucdb->query('ALTER TABLE '.$our_prefix.'sites ADD admin_url varchar(300) AFTER url'); |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Add the 'event_item' column to the events table |
| 237 |
* |
| 238 |
* @return void |
| 239 |
*/ |
| 240 |
public static function update_0823_add_event_item_column_to_events() { |
| 241 |
$ucdb = UpdraftCentral()->db; |
| 242 |
|
| 243 |
$our_prefix = $ucdb->base_prefix.self::$table_prefix; |
| 244 |
$ucdb->query('ALTER TABLE '.$our_prefix.'events ADD event_item varchar(300)'); |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Creates 3 tables to use with UpdraftCentral |
| 249 |
* |
| 250 |
* @return void |
| 251 |
*/ |
| 252 |
public static function create_tables() { |
| 253 |
global $wpdb; |
| 254 |
|
| 255 |
// Preserve the original wpdb object prior to substituting |
| 256 |
// with UpdraftCentral's own wpdb instance |
| 257 |
$wpdb = self::preserve_wpdb($wpdb); |
| 258 |
|
| 259 |
$our_prefix = $wpdb->base_prefix.self::$table_prefix; |
| 260 |
$collate = self::get_collation(); |
| 261 |
|
| 262 |
include_once ABSPATH.'wp-admin/includes/upgrade.php'; |
| 263 |
|
| 264 |
// Important: obey the magical/arbitrary rules for formatting this stuff: https://codex.wordpress.org/Creating_Tables_with_Plugins |
| 265 |
// Otherwise, you get SQL errors and unwanted header output warnings when activating |
| 266 |
|
| 267 |
$create_tables = 'CREATE TABLE '.$our_prefix."sites ( |
| 268 |
site_id bigint(20) NOT NULL auto_increment, |
| 269 |
user_id bigint(20) NOT NULL, |
| 270 |
url varchar(300) NOT NULL, |
| 271 |
admin_url varchar(300), |
| 272 |
key_local_private blob, |
| 273 |
key_remote_public blob, |
| 274 |
key_name_indicator varchar(200) NOT NULL, |
| 275 |
description text, |
| 276 |
sequence_id bigint(20) DEFAULT 0, |
| 277 |
remote_user_id bigint(20) NOT NULL, |
| 278 |
remote_user_login varchar(60), |
| 279 |
remote_site_id bigint(20) DEFAULT 0, |
| 280 |
connection_method varchar(30), |
| 281 |
send_cors_headers tinyint(1) DEFAULT 1, |
| 282 |
PRIMARY KEY (site_id), |
| 283 |
KEY user_id (user_id) |
| 284 |
) $collate; |
| 285 |
"; |
| 286 |
// KEY attribute_name (attribute_name) |
| 287 |
dbDelta($create_tables); |
| 288 |
|
| 289 |
$create_tables = 'CREATE TABLE '.$our_prefix."site_temporary_keys ( |
| 290 |
key_id bigint(20) NOT NULL auto_increment, |
| 291 |
key_local_private blob, |
| 292 |
key_remote_public blob, |
| 293 |
created bigint(20), |
| 294 |
PRIMARY KEY (key_id), |
| 295 |
KEY created (created) |
| 296 |
) $collate; |
| 297 |
"; |
| 298 |
|
| 299 |
dbDelta($create_tables); |
| 300 |
|
| 301 |
$max_index_length = 191; |
| 302 |
$create_tables = 'CREATE TABLE '.$our_prefix."sitemeta ( |
| 303 |
meta_id bigint(20) NOT NULL auto_increment, |
| 304 |
site_id bigint(20) NOT NULL default '0', |
| 305 |
meta_key varchar(255) default NULL, |
| 306 |
meta_value longtext, |
| 307 |
created bigint(20) DEFAULT 0, |
| 308 |
PRIMARY KEY (meta_id), |
| 309 |
KEY meta_key (meta_key($max_index_length)), |
| 310 |
KEY site_id (site_id) |
| 311 |
) $collate; |
| 312 |
"; |
| 313 |
|
| 314 |
dbDelta($create_tables); |
| 315 |
|
| 316 |
$create_tables = 'CREATE TABLE '.$our_prefix."user_cron ( |
| 317 |
id bigint(20) NOT NULL auto_increment, |
| 318 |
user_id bigint(20) NOT NULL, |
| 319 |
last_run bigint(20) DEFAULT 0, |
| 320 |
PRIMARY KEY (id), |
| 321 |
KEY user_id (user_id), |
| 322 |
KEY last_run (last_run) |
| 323 |
) $collate; |
| 324 |
"; |
| 325 |
|
| 326 |
dbDelta($create_tables); |
| 327 |
|
| 328 |
$create_tables = 'CREATE TABLE '.$our_prefix."events ( |
| 329 |
event_id bigint(20) NOT NULL auto_increment, |
| 330 |
time int NOT NULL, |
| 331 |
site_id bigint(20) NOT NULL, |
| 332 |
event_type text NOT NULL, |
| 333 |
event_name text NOT NULL, |
| 334 |
event_data mediumtext, |
| 335 |
event_status text NOT NULL, |
| 336 |
event_result_data mediumtext, |
| 337 |
event_item varchar(300), |
| 338 |
PRIMARY KEY (event_id), |
| 339 |
KEY site_id (site_id) |
| 340 |
) $collate; |
| 341 |
"; |
| 342 |
|
| 343 |
dbDelta($create_tables); |
| 344 |
|
| 345 |
// Restoring $wpdb to its original object |
| 346 |
$wpdb = self::restore_wpdb(); |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Preserves the original wpdb instance for later restoration. Returns |
| 351 |
* the UpdraftCentral()->db wpdb instance used for UpdraftCentral tables. |
| 352 |
* |
| 353 |
* @param wpdb $wpdb |
| 354 |
* @return wpdb|null |
| 355 |
*/ |
| 356 |
private static function preserve_wpdb($wpdb) { |
| 357 |
self::$original_wpdb = $wpdb; |
| 358 |
return UpdraftCentral()->db; |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* Restores the original wpdb instance. |
| 363 |
* |
| 364 |
* @return wpdb|null |
| 365 |
*/ |
| 366 |
private static function restore_wpdb() { |
| 367 |
return self::$original_wpdb; |
| 368 |
} |
| 369 |
} |
| 370 |
|