| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('UD_CENTRAL_DIR')) die('Security check'); |
| 4 |
|
| 5 |
class UpdraftCentral_Activation { |
| 6 |
private static $table_prefix; |
| 7 |
|
| 8 |
private static $db_updates = array( |
| 9 |
'0.3.8' => array( |
| 10 |
'update_038_add_admin_url_column_to_sites', |
| 11 |
), |
| 12 |
); |
| 13 |
|
| 14 |
public static function init() { |
| 15 |
self::$table_prefix = defined('UPDRAFTCENTRAL_TABLE_PREFIX') ? UPDRAFTCENTRAL_TABLE_PREFIX : 'updraftcentral_'; |
| 16 |
} |
| 17 |
|
| 18 |
public static function install() { |
| 19 |
self::init(); |
| 20 |
self::create_tables(); |
| 21 |
update_option('updraftcentral_dbversion', UpdraftCentral()->version); |
| 22 |
} |
| 23 |
|
| 24 |
public static function check_updates() { |
| 25 |
self::init(); |
| 26 |
$our_version = UpdraftCentral()->version; |
| 27 |
$db_version = get_option('updraftcentral_dbversion'); |
| 28 |
if (!$db_version || version_compare($our_version, $db_version, '>')) { |
| 29 |
foreach (self::$db_updates as $version => $updates) { |
| 30 |
if (version_compare($version, $db_version, '>')) { |
| 31 |
foreach ($updates as $update) { |
| 32 |
call_user_func(array(__CLASS__, $update)); |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
update_option('updraftcentral_dbversion', UpdraftCentral()->version); |
| 38 |
} |
| 39 |
|
| 40 |
// Add the 'admin_url' column to the sites table |
| 41 |
public static function update_038_add_admin_url_column_to_sites() { |
| 42 |
global $wpdb; |
| 43 |
$our_prefix = $wpdb->base_prefix.self::$table_prefix; |
| 44 |
$wpdb->query('ALTER TABLE '.$our_prefix.'sites ADD admin_url varchar(300) AFTER url'); |
| 45 |
} |
| 46 |
|
| 47 |
public static function create_tables() { |
| 48 |
global $wpdb; |
| 49 |
|
| 50 |
// $wpdb->hide_errors(); |
| 51 |
|
| 52 |
$our_prefix = $wpdb->base_prefix.self::$table_prefix; |
| 53 |
$collate = ''; |
| 54 |
|
| 55 |
if ($wpdb->has_cap('collation')) { |
| 56 |
if (!empty($wpdb->charset)) { |
| 57 |
$collate .= "DEFAULT CHARACTER SET $wpdb->charset"; |
| 58 |
} |
| 59 |
if (!empty($wpdb->collate)) { |
| 60 |
$collate .= " COLLATE $wpdb->collate"; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
require_once ABSPATH.'wp-admin/includes/upgrade.php'; |
| 65 |
|
| 66 |
// Important: obey the magical/arbitrary rules for formatting this stuff: https://codex.wordpress.org/Creating_Tables_with_Plugins |
| 67 |
// Otherwise, you get SQL errors and unwanted header output warnings when activating |
| 68 |
|
| 69 |
$create_tables = 'CREATE TABLE '.$our_prefix."sites ( |
| 70 |
site_id bigint(20) NOT NULL auto_increment, |
| 71 |
user_id bigint(20) NOT NULL, |
| 72 |
url varchar(300) NOT NULL, |
| 73 |
admin_url varchar(300), |
| 74 |
key_local_private blob, |
| 75 |
key_remote_public blob, |
| 76 |
key_name_indicator varchar(200) NOT NULL, |
| 77 |
description text, |
| 78 |
sequence_id bigint(20) DEFAULT 0, |
| 79 |
remote_user_id bigint(20) NOT NULL, |
| 80 |
remote_user_login varchar(60), |
| 81 |
remote_site_id bigint(20) DEFAULT 0, |
| 82 |
connection_method varchar(30), |
| 83 |
send_cors_headers tinyint(1) DEFAULT 1, |
| 84 |
PRIMARY KEY (site_id), |
| 85 |
KEY user_id (user_id), |
| 86 |
KEY url (url) |
| 87 |
) $collate; |
| 88 |
"; |
| 89 |
// KEY attribute_name (attribute_name) |
| 90 |
dbDelta($create_tables); |
| 91 |
|
| 92 |
$create_tables = 'CREATE TABLE '.$our_prefix."site_temporary_keys ( |
| 93 |
key_id bigint(20) NOT NULL auto_increment, |
| 94 |
key_local_private blob, |
| 95 |
key_remote_public blob, |
| 96 |
created bigint(20), |
| 97 |
PRIMARY KEY (key_id), |
| 98 |
KEY created (created) |
| 99 |
) $collate; |
| 100 |
"; |
| 101 |
|
| 102 |
dbDelta($create_tables); |
| 103 |
|
| 104 |
$max_index_length = 191; |
| 105 |
$create_tables = 'CREATE TABLE '.$our_prefix."sitemeta ( |
| 106 |
meta_id bigint(20) NOT NULL auto_increment, |
| 107 |
site_id bigint(20) NOT NULL default '0', |
| 108 |
meta_key varchar(255) default NULL, |
| 109 |
meta_value longtext, |
| 110 |
PRIMARY KEY (meta_id), |
| 111 |
KEY meta_key (meta_key($max_index_length)), |
| 112 |
KEY site_id (site_id) |
| 113 |
) $collate; |
| 114 |
"; |
| 115 |
|
| 116 |
dbDelta($create_tables); |
| 117 |
} |
| 118 |
} |
| 119 |
|