| 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 |
); |
| 33 |
|
| 34 |
/** |
| 35 |
* Initialize properties |
| 36 |
* |
| 37 |
* Initializes table prefix property with a custom prefix |
| 38 |
* |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
public static function init() { |
| 42 |
self::$table_prefix = defined('UPDRAFTCENTRAL_TABLE_PREFIX') ? UPDRAFTCENTRAL_TABLE_PREFIX : 'updraftcentral_'; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Sets table prefix, creates 3 tables and updates options table with |
| 47 |
* db structure version |
| 48 |
* |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public static function install() { |
| 52 |
self::init(); |
| 53 |
self::create_tables(); |
| 54 |
update_option('updraftcentral_dbversion', UpdraftCentral()->version); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Checks installed version and current version and make necessary changes to |
| 59 |
* database table structure |
| 60 |
* |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
public static function check_updates() { |
| 64 |
self::init(); |
| 65 |
$our_version = UpdraftCentral()->version; |
| 66 |
$db_version = get_option('updraftcentral_dbversion'); |
| 67 |
if (!$db_version || version_compare($our_version, $db_version, '>')) { |
| 68 |
foreach (self::$db_updates as $version => $updates) { |
| 69 |
if (version_compare($version, $db_version, '>')) { |
| 70 |
foreach ($updates as $update) { |
| 71 |
call_user_func(array(__CLASS__, $update)); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
update_option('updraftcentral_dbversion', UpdraftCentral()->version); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Add the 'created' column to the sitemeta table |
| 81 |
* |
| 82 |
* @return void |
| 83 |
*/ |
| 84 |
public static function update_063_add_created_column_to_sitemeta() { |
| 85 |
global $wpdb; |
| 86 |
$our_prefix = $wpdb->base_prefix.self::$table_prefix; |
| 87 |
$wpdb->query('ALTER TABLE '.$our_prefix.'sitemeta ADD created bigint(20) DEFAULT 0 AFTER meta_value'); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Add the 'admin_url' column to the sites table |
| 92 |
* |
| 93 |
* @return void |
| 94 |
*/ |
| 95 |
public static function update_038_add_admin_url_column_to_sites() { |
| 96 |
global $wpdb; |
| 97 |
$our_prefix = $wpdb->base_prefix.self::$table_prefix; |
| 98 |
$wpdb->query('ALTER TABLE '.$our_prefix.'sites ADD admin_url varchar(300) AFTER url'); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Creates 3 tables to use with UpdraftCentral |
| 103 |
* |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
public static function create_tables() { |
| 107 |
global $wpdb; |
| 108 |
|
| 109 |
// $wpdb->hide_errors(); |
| 110 |
|
| 111 |
$our_prefix = $wpdb->base_prefix.self::$table_prefix; |
| 112 |
$collate = ''; |
| 113 |
|
| 114 |
if ($wpdb->has_cap('collation')) { |
| 115 |
if (!empty($wpdb->charset)) { |
| 116 |
$collate .= "DEFAULT CHARACTER SET $wpdb->charset"; |
| 117 |
} |
| 118 |
if (!empty($wpdb->collate)) { |
| 119 |
$collate .= " COLLATE $wpdb->collate"; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
include_once ABSPATH.'wp-admin/includes/upgrade.php'; |
| 124 |
|
| 125 |
// Important: obey the magical/arbitrary rules for formatting this stuff: https://codex.wordpress.org/Creating_Tables_with_Plugins |
| 126 |
// Otherwise, you get SQL errors and unwanted header output warnings when activating |
| 127 |
|
| 128 |
$create_tables = 'CREATE TABLE '.$our_prefix."sites ( |
| 129 |
site_id bigint(20) NOT NULL auto_increment, |
| 130 |
user_id bigint(20) NOT NULL, |
| 131 |
url varchar(300) NOT NULL, |
| 132 |
admin_url varchar(300), |
| 133 |
key_local_private blob, |
| 134 |
key_remote_public blob, |
| 135 |
key_name_indicator varchar(200) NOT NULL, |
| 136 |
description text, |
| 137 |
sequence_id bigint(20) DEFAULT 0, |
| 138 |
remote_user_id bigint(20) NOT NULL, |
| 139 |
remote_user_login varchar(60), |
| 140 |
remote_site_id bigint(20) DEFAULT 0, |
| 141 |
connection_method varchar(30), |
| 142 |
send_cors_headers tinyint(1) DEFAULT 1, |
| 143 |
PRIMARY KEY (site_id), |
| 144 |
KEY user_id (user_id) |
| 145 |
) $collate; |
| 146 |
"; |
| 147 |
// KEY attribute_name (attribute_name) |
| 148 |
dbDelta($create_tables); |
| 149 |
|
| 150 |
$create_tables = 'CREATE TABLE '.$our_prefix."site_temporary_keys ( |
| 151 |
key_id bigint(20) NOT NULL auto_increment, |
| 152 |
key_local_private blob, |
| 153 |
key_remote_public blob, |
| 154 |
created bigint(20), |
| 155 |
PRIMARY KEY (key_id), |
| 156 |
KEY created (created) |
| 157 |
) $collate; |
| 158 |
"; |
| 159 |
|
| 160 |
dbDelta($create_tables); |
| 161 |
|
| 162 |
$max_index_length = 191; |
| 163 |
$create_tables = 'CREATE TABLE '.$our_prefix."sitemeta ( |
| 164 |
meta_id bigint(20) NOT NULL auto_increment, |
| 165 |
site_id bigint(20) NOT NULL default '0', |
| 166 |
meta_key varchar(255) default NULL, |
| 167 |
meta_value longtext, |
| 168 |
created bigint(20) DEFAULT 0, |
| 169 |
PRIMARY KEY (meta_id), |
| 170 |
KEY meta_key (meta_key($max_index_length)), |
| 171 |
KEY site_id (site_id) |
| 172 |
) $collate; |
| 173 |
"; |
| 174 |
|
| 175 |
dbDelta($create_tables); |
| 176 |
} |
| 177 |
} |
| 178 |
|