PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.9
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.9
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / team-updraft / common-libs / src / updraft-tasks / class-updraft-tasks-activation.php

class-updraft-tasks-activation.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.9, at vendor/team-updraft/common-libs/src/updraft-tasks/class-updraft-tasks-activation.php

208 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Initialise the tasks module and create the needed DB tables
4 */
5
6 if (!defined('ABSPATH')) die('Access denied.');
7
8 if (!class_exists('Updraft_Tasks_Activation')) :
9
10 class Updraft_Tasks_Activation {
11
12 private static $table_prefix;
13
14 /**
15 * Format: key=<version>, value=array of method names to call
16 * Example Usage:
17 * private static $db_updates = array(
18 * '1.0.1' => array(
19 * 'update_101_add_new_column',
20 * ),
21 * );
22 *
23 * @var Mixed
24 */
25 private static $db_updates = array(
26 '0.0.1' => array('create_tables'),
27 '1.0.1' => array('add_attempts_and_class_identifier'),
28 '1.1' => array('add_lock_column'),
29 );
30
31
32 const UPDRAFT_TASKS_DB_VERSION = '1.1';
33
34 /**
35 * Initialise this class
36 */
37 public static function init_db() {
38 self::$table_prefix = defined('UPDRAFT_TASKS_TABLE_PREFIX') ? UPDRAFT_TASKS_TABLE_PREFIX : 'tm_';
39 }
40
41 /**
42 * This is the class entry point
43 */
44 public static function install() {
45 self::init_db();
46 self::create_tables();
47 // we need walk through all updates when install at first.
48 self::check_updates();
49 }
50
51 /**
52 * Check needed tables in data base and if one of them doesn't exist force reinstall.
53 */
54 public static function reinstall_if_needed() {
55 static $done = false;
56
57 if ($done) return;
58
59 if (!self::check_if_tables_exist()) self::reinstall();
60
61 $done = true;
62 }
63
64 /**
65 * Drop database version variable from option from database and run install again.
66 */
67 public static function reinstall() {
68 if (is_multisite()) {
69 delete_site_option('updraft_task_manager_dbversion');
70 } else {
71 delete_option('updraft_task_manager_dbversion');
72 }
73 self::install();
74 }
75
76 /**
77 * Check if needed task manager tables exist.
78 *
79 * @return bool
80 */
81 public static function check_if_tables_exist() {
82 global $wpdb;
83 self::init_db();
84 $our_prefix = $wpdb->base_prefix.self::$table_prefix;
85 $tables = array($our_prefix.'tasks', $our_prefix.'taskmeta');
86
87 foreach ($tables as $table) {
88 $query = "SHOW TABLES LIKE '{$table}'";
89 $tables = $wpdb->get_results($query, ARRAY_A);
90 if (!is_array($tables) || 0 == count($tables)) return false;
91 }
92
93 return true;
94 }
95
96 /**
97 * See if any database schema updates are needed, and perform them if so.
98 * Example Usage:
99 * public static function update_101_add_new_column() {
100 * $wpdb = $GLOBALS['wpdb'];
101 * $wpdb->query('ALTER TABLE tm_tasks ADD task_expiry varchar(300) AFTER id');
102 * }
103 */
104 public static function check_updates() {
105 self::init_db();
106 $our_version = self::get_version();
107 if (is_multisite()) {
108 $db_version = get_site_option('updraft_task_manager_dbversion');
109 } else {
110 $db_version = get_option('updraft_task_manager_dbversion');
111 }
112 if (!$db_version || version_compare($our_version, $db_version, '>')) {
113 foreach (self::$db_updates as $version => $updates) {
114 if (version_compare($version, $db_version, '>')) {
115 foreach ($updates as $update) {
116 call_user_func(array(__CLASS__, $update));
117 }
118 }
119 }
120 if (is_multisite()) {
121 update_site_option('updraft_task_manager_dbversion', self::get_version());
122 } else {
123 update_option('updraft_task_manager_dbversion', self::get_version());
124 }
125 }
126 }
127
128 /**
129 * Returns the current version of the plugin
130 */
131 public static function get_version() {
132 return self::UPDRAFT_TASKS_DB_VERSION;
133 }
134
135 /**
136 * Create the database tables
137 */
138 public static function create_tables() {
139
140 $wpdb = $GLOBALS['wpdb'];
141
142 $our_prefix = $wpdb->base_prefix.self::$table_prefix;
143 $collate = '';
144
145 if ($wpdb->has_cap('collation')) {
146 if (!empty($wpdb->charset)) {
147 $collate .= "DEFAULT CHARACTER SET $wpdb->charset";
148 }
149 if (!empty($wpdb->collate)) {
150 $collate .= " COLLATE $wpdb->collate";
151 }
152 }
153
154 include_once ABSPATH.'wp-admin/includes/upgrade.php';
155
156 // Important: obey the magical/arbitrary rules for formatting this stuff: https://codex.wordpress.org/Creating_Tables_with_Plugins
157 // Otherwise, you get SQL errors and unwanted header output warnings when activating
158
159 $create_tables = 'CREATE TABLE '.$our_prefix."tasks (
160 task_id bigint(20) NOT NULL auto_increment,
161 user_id bigint(20) NOT NULL,
162 type varchar(300) NOT NULL,
163 description varchar(300),
164 PRIMARY KEY (task_id),
165 KEY user_id (user_id),
166 time_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
167 status varchar(300)
168 ) $collate;
169 ";
170 // KEY attribute_name (attribute_name)
171 dbDelta($create_tables);
172
173 $max_index_length = 191;
174
175 $create_tables = 'CREATE TABLE '.$our_prefix."taskmeta (
176 meta_id bigint(20) NOT NULL auto_increment,
177 task_id bigint(20) NOT NULL default '0',
178 meta_key varchar(255) DEFAULT NULL,
179 meta_value longtext,
180 PRIMARY KEY (meta_id),
181 KEY meta_key (meta_key($max_index_length)),
182 KEY task_id (task_id)
183 ) $collate;
184 ";
185
186 dbDelta($create_tables);
187 }
188
189 public static function add_attempts_and_class_identifier() {
190 $wpdb = $GLOBALS['wpdb'];
191 $our_prefix = $wpdb->base_prefix.self::$table_prefix;
192
193 $wpdb->query("ALTER TABLE ".$our_prefix."tasks CHANGE COLUMN `task_id` `id` INT NOT NULL");
194 $wpdb->query("ALTER TABLE ".$our_prefix."tasks MODIFY COLUMN `id` INT auto_increment");
195 $wpdb->query("ALTER TABLE ".$our_prefix."tasks ADD attempts INT DEFAULT 0 AFTER type");
196 $wpdb->query("ALTER TABLE ".$our_prefix."tasks ADD class_identifier varchar(300) DEFAULT 0 AFTER type");
197 }
198
199 public static function add_lock_column() {
200 $wpdb = $GLOBALS['wpdb'];
201 $our_prefix = $wpdb->base_prefix.self::$table_prefix;
202 $wpdb->query('ALTER TABLE '.$our_prefix.'tasks ADD last_locked_at BIGINT DEFAULT 0 AFTER time_created');
203 }
204
205 }
206
207 endif;
208