PluginProbe
UpdraftCentral Dashboard / 0.8.21
UpdraftCentral Dashboard v0.8.21
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.21, at classes/activation.php

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