| 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 |
* Table prefix string |
| 15 |
* |
| 16 |
* @var string $table_prefix |
| 17 |
*/ |
| 18 |
private static $table_prefix; |
| 19 |
|
| 20 |
/** |
| 21 |
* A array of method that handle table structure upgrades |
| 22 |
* |
| 23 |
* @var array $db_updates |
| 24 |
*/ |
| 25 |
private static $db_updates = array( |
| 26 |
'0.3.8' => array( |
| 27 |
'update_038_add_admin_url_column_to_sites', |
| 28 |
), |
| 29 |
'0.6.3' => array( |
| 30 |
'update_063_add_created_column_to_sitemeta', |
| 31 |
), |
| 32 |
'0.7.1' => array( |
| 33 |
'update_071_create_user_cron_table', |
| 34 |
), |
| 35 |
); |
| 36 |
|
| 37 |
/** |
| 38 |
* Retrieves the string containing the character set |
| 39 |
* and collation to use during the table creation process |
| 40 |
* |
| 41 |
* @return string |
| 42 |
*/ |
| 43 |
private static function get_collation() { |
| 44 |
global $wpdb; |
| 45 |
|
| 46 |
$collate = ''; |
| 47 |
|
| 48 |
if ($wpdb->has_cap('collation')) { |
| 49 |
if (!empty($wpdb->charset)) { |
| 50 |
$collate .= "DEFAULT CHARACTER SET $wpdb->charset"; |
| 51 |
} |
| 52 |
if (!empty($wpdb->collate)) { |
| 53 |
$collate .= " COLLATE $wpdb->collate"; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
return $collate; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Initialize properties |
| 62 |
* |
| 63 |
* Initializes table prefix property with a custom prefix |
| 64 |
* |
| 65 |
* @return void |
| 66 |
*/ |
| 67 |
public static function init() { |
| 68 |
self::$table_prefix = defined('UPDRAFTCENTRAL_TABLE_PREFIX') ? UPDRAFTCENTRAL_TABLE_PREFIX : 'updraftcentral_'; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Sets table prefix, creates 3 tables and updates options table with |
| 73 |
* db structure version |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
public static function install() { |
| 78 |
self::init(); |
| 79 |
self::create_tables(); |
| 80 |
update_option('updraftcentral_dbversion', UpdraftCentral()->version); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Checks installed version and current version and make necessary changes to |
| 85 |
* database table structure |
| 86 |
* |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
public static function check_updates() { |
| 90 |
self::init(); |
| 91 |
$our_version = UpdraftCentral()->version; |
| 92 |
$db_version = get_option('updraftcentral_dbversion'); |
| 93 |
if (!$db_version || version_compare($our_version, $db_version, '>')) { |
| 94 |
foreach (self::$db_updates as $version => $updates) { |
| 95 |
if (version_compare($version, $db_version, '>')) { |
| 96 |
foreach ($updates as $update) { |
| 97 |
call_user_func(array(__CLASS__, $update)); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
update_option('updraftcentral_dbversion', UpdraftCentral()->version); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Creates the "user_cron" table and some housekeeping of clearing the previously |
| 107 |
* registered (deprecated) cron events |
| 108 |
* |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
public static function update_071_create_user_cron_table() { |
| 112 |
global $wpdb; |
| 113 |
|
| 114 |
$our_prefix = $wpdb->base_prefix.self::$table_prefix; |
| 115 |
$collate = self::get_collation(); |
| 116 |
|
| 117 |
if (!function_exists('dbDelta')) include_once ABSPATH.'wp-admin/includes/upgrade.php'; |
| 118 |
|
| 119 |
$create_tables = 'CREATE TABLE '.$our_prefix."user_cron ( |
| 120 |
id bigint(20) NOT NULL auto_increment, |
| 121 |
user_id bigint(20) NOT NULL, |
| 122 |
last_run bigint(20) DEFAULT 0, |
| 123 |
PRIMARY KEY (id), |
| 124 |
KEY user_id (user_id), |
| 125 |
KEY last_run (last_run) |
| 126 |
) $collate; |
| 127 |
"; |
| 128 |
|
| 129 |
dbDelta($create_tables); |
| 130 |
|
| 131 |
|
| 132 |
// Remove previously scheduled (deprecated) "updraftcentra_cron" events in WP-Cron |
| 133 |
// Cleanup previous (no longer needed) crons, since we're now using a single cron |
| 134 |
// for the site's cron process instead of per user. |
| 135 |
if (!function_exists('_get_cron_array')) include ABSPATH.WPINC.'/cron.php'; |
| 136 |
|
| 137 |
$crons = _get_cron_array(); |
| 138 |
if (!empty($crons)) { |
| 139 |
foreach ($crons as $timestamp => $cron) { |
| 140 |
foreach ($cron as $key => $value) { |
| 141 |
if (preg_match('#^updraftcentral#', $key)) { |
| 142 |
foreach ($value as $serialized_key => $schedule) { |
| 143 |
wp_clear_scheduled_hook($key, $schedule['args']); |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Add the 'created' column to the sitemeta table |
| 153 |
* |
| 154 |
* @return void |
| 155 |
*/ |
| 156 |
public static function update_063_add_created_column_to_sitemeta() { |
| 157 |
global $wpdb; |
| 158 |
$our_prefix = $wpdb->base_prefix.self::$table_prefix; |
| 159 |
$wpdb->query('ALTER TABLE '.$our_prefix.'sitemeta ADD created bigint(20) DEFAULT 0 AFTER meta_value'); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Add the 'admin_url' column to the sites table |
| 164 |
* |
| 165 |
* @return void |
| 166 |
*/ |
| 167 |
public static function update_038_add_admin_url_column_to_sites() { |
| 168 |
global $wpdb; |
| 169 |
$our_prefix = $wpdb->base_prefix.self::$table_prefix; |
| 170 |
$wpdb->query('ALTER TABLE '.$our_prefix.'sites ADD admin_url varchar(300) AFTER url'); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Creates 3 tables to use with UpdraftCentral |
| 175 |
* |
| 176 |
* @return void |
| 177 |
*/ |
| 178 |
public static function create_tables() { |
| 179 |
global $wpdb; |
| 180 |
|
| 181 |
$our_prefix = $wpdb->base_prefix.self::$table_prefix; |
| 182 |
$collate = self::get_collation(); |
| 183 |
|
| 184 |
include_once ABSPATH.'wp-admin/includes/upgrade.php'; |
| 185 |
|
| 186 |
// Important: obey the magical/arbitrary rules for formatting this stuff: https://codex.wordpress.org/Creating_Tables_with_Plugins |
| 187 |
// Otherwise, you get SQL errors and unwanted header output warnings when activating |
| 188 |
|
| 189 |
$create_tables = 'CREATE TABLE '.$our_prefix."sites ( |
| 190 |
site_id bigint(20) NOT NULL auto_increment, |
| 191 |
user_id bigint(20) NOT NULL, |
| 192 |
url varchar(300) NOT NULL, |
| 193 |
admin_url varchar(300), |
| 194 |
key_local_private blob, |
| 195 |
key_remote_public blob, |
| 196 |
key_name_indicator varchar(200) NOT NULL, |
| 197 |
description text, |
| 198 |
sequence_id bigint(20) DEFAULT 0, |
| 199 |
remote_user_id bigint(20) NOT NULL, |
| 200 |
remote_user_login varchar(60), |
| 201 |
remote_site_id bigint(20) DEFAULT 0, |
| 202 |
connection_method varchar(30), |
| 203 |
send_cors_headers tinyint(1) DEFAULT 1, |
| 204 |
PRIMARY KEY (site_id), |
| 205 |
KEY user_id (user_id) |
| 206 |
) $collate; |
| 207 |
"; |
| 208 |
// KEY attribute_name (attribute_name) |
| 209 |
dbDelta($create_tables); |
| 210 |
|
| 211 |
$create_tables = 'CREATE TABLE '.$our_prefix."site_temporary_keys ( |
| 212 |
key_id bigint(20) NOT NULL auto_increment, |
| 213 |
key_local_private blob, |
| 214 |
key_remote_public blob, |
| 215 |
created bigint(20), |
| 216 |
PRIMARY KEY (key_id), |
| 217 |
KEY created (created) |
| 218 |
) $collate; |
| 219 |
"; |
| 220 |
|
| 221 |
dbDelta($create_tables); |
| 222 |
|
| 223 |
$max_index_length = 191; |
| 224 |
$create_tables = 'CREATE TABLE '.$our_prefix."sitemeta ( |
| 225 |
meta_id bigint(20) NOT NULL auto_increment, |
| 226 |
site_id bigint(20) NOT NULL default '0', |
| 227 |
meta_key varchar(255) default NULL, |
| 228 |
meta_value longtext, |
| 229 |
created bigint(20) DEFAULT 0, |
| 230 |
PRIMARY KEY (meta_id), |
| 231 |
KEY meta_key (meta_key($max_index_length)), |
| 232 |
KEY site_id (site_id) |
| 233 |
) $collate; |
| 234 |
"; |
| 235 |
|
| 236 |
dbDelta($create_tables); |
| 237 |
} |
| 238 |
} |
| 239 |
|