PluginProbe
UpdraftCentral Dashboard / 0.8.7
UpdraftCentral Dashboard v0.8.7
0.8.33 0.7.2 0.7.3 0.7.4 0.8.0 0.8.1 0.8.10 0.8.11 0.8.12 0.8.13 0.8.14 0.8.15 0.8.16 0.8.17 0.8.18 0.8.19 0.8.2 0.8.20 0.8.21 0.8.22 0.8.23 0.8.24 0.8.25 0.8.26 0.8.27 All 51 releases
updraftcentral / classes / activation.php

activation.php in UpdraftCentral Dashboard 0.8.7, at classes/activation.php

254 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $user_cron_table = $our_prefix.'user_cron';
118 if ($user_cron_table !== $wpdb->get_var("SHOW TABLES LIKE '".$user_cron_table."'")) {
119 if (!function_exists('dbDelta')) include_once ABSPATH.'wp-admin/includes/upgrade.php';
120
121 $create_tables = 'CREATE TABLE '.$our_prefix."user_cron (
122 id bigint(20) NOT NULL auto_increment,
123 user_id bigint(20) NOT NULL,
124 last_run bigint(20) DEFAULT 0,
125 PRIMARY KEY (id),
126 KEY user_id (user_id),
127 KEY last_run (last_run)
128 ) $collate;
129 ";
130
131 dbDelta($create_tables);
132 }
133
134
135 // Remove previously scheduled (deprecated) "updraftcentra_cron" events in WP-Cron
136 // Cleanup previous (no longer needed) crons, since we're now using a single cron
137 // for the site's cron process instead of per user.
138 if (!function_exists('_get_cron_array')) include ABSPATH.WPINC.'/cron.php';
139
140 $crons = _get_cron_array();
141 if (!empty($crons)) {
142 foreach ($crons as $timestamp => $cron) {
143 foreach ($cron as $key => $value) {
144 if (preg_match('#^updraftcentral#', $key)) {
145 foreach ($value as $serialized_key => $schedule) {
146 wp_clear_scheduled_hook($key, $schedule['args']);
147 }
148 }
149 }
150 }
151 }
152 }
153
154 /**
155 * Add the 'created' column to the sitemeta table
156 *
157 * @return void
158 */
159 public static function update_063_add_created_column_to_sitemeta() {
160 global $wpdb;
161 $our_prefix = $wpdb->base_prefix.self::$table_prefix;
162 $wpdb->query('ALTER TABLE '.$our_prefix.'sitemeta ADD created bigint(20) DEFAULT 0 AFTER meta_value');
163 }
164
165 /**
166 * Add the 'admin_url' column to the sites table
167 *
168 * @return void
169 */
170 public static function update_038_add_admin_url_column_to_sites() {
171 global $wpdb;
172 $our_prefix = $wpdb->base_prefix.self::$table_prefix;
173 $wpdb->query('ALTER TABLE '.$our_prefix.'sites ADD admin_url varchar(300) AFTER url');
174 }
175
176 /**
177 * Creates 3 tables to use with UpdraftCentral
178 *
179 * @return void
180 */
181 public static function create_tables() {
182 global $wpdb;
183
184 $our_prefix = $wpdb->base_prefix.self::$table_prefix;
185 $collate = self::get_collation();
186
187 include_once ABSPATH.'wp-admin/includes/upgrade.php';
188
189 // Important: obey the magical/arbitrary rules for formatting this stuff: https://codex.wordpress.org/Creating_Tables_with_Plugins
190 // Otherwise, you get SQL errors and unwanted header output warnings when activating
191
192 $create_tables = 'CREATE TABLE '.$our_prefix."sites (
193 site_id bigint(20) NOT NULL auto_increment,
194 user_id bigint(20) NOT NULL,
195 url varchar(300) NOT NULL,
196 admin_url varchar(300),
197 key_local_private blob,
198 key_remote_public blob,
199 key_name_indicator varchar(200) NOT NULL,
200 description text,
201 sequence_id bigint(20) DEFAULT 0,
202 remote_user_id bigint(20) NOT NULL,
203 remote_user_login varchar(60),
204 remote_site_id bigint(20) DEFAULT 0,
205 connection_method varchar(30),
206 send_cors_headers tinyint(1) DEFAULT 1,
207 PRIMARY KEY (site_id),
208 KEY user_id (user_id)
209 ) $collate;
210 ";
211 // KEY attribute_name (attribute_name)
212 dbDelta($create_tables);
213
214 $create_tables = 'CREATE TABLE '.$our_prefix."site_temporary_keys (
215 key_id bigint(20) NOT NULL auto_increment,
216 key_local_private blob,
217 key_remote_public blob,
218 created bigint(20),
219 PRIMARY KEY (key_id),
220 KEY created (created)
221 ) $collate;
222 ";
223
224 dbDelta($create_tables);
225
226 $max_index_length = 191;
227 $create_tables = 'CREATE TABLE '.$our_prefix."sitemeta (
228 meta_id bigint(20) NOT NULL auto_increment,
229 site_id bigint(20) NOT NULL default '0',
230 meta_key varchar(255) default NULL,
231 meta_value longtext,
232 created bigint(20) DEFAULT 0,
233 PRIMARY KEY (meta_id),
234 KEY meta_key (meta_key($max_index_length)),
235 KEY site_id (site_id)
236 ) $collate;
237 ";
238
239 dbDelta($create_tables);
240
241 $create_tables = 'CREATE TABLE '.$our_prefix."user_cron (
242 id bigint(20) NOT NULL auto_increment,
243 user_id bigint(20) NOT NULL,
244 last_run bigint(20) DEFAULT 0,
245 PRIMARY KEY (id),
246 KEY user_id (user_id),
247 KEY last_run (last_run)
248 ) $collate;
249 ";
250
251 dbDelta($create_tables);
252 }
253 }
254