PluginProbe
UpdraftCentral Dashboard / 0.8.26
UpdraftCentral Dashboard v0.8.26
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.26, at classes/activation.php

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