PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.3
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.3
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.3, at vendor/team-updraft/common-libs/src/updraft-tasks/class-updraft-tasks-activation.php

207 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 $our_prefix = $wpdb->base_prefix.self::$table_prefix;
84 $tables = array($our_prefix.'tasks', $our_prefix.'taskmeta');
85
86 foreach ($tables as $table) {
87 $query = "SHOW TABLES LIKE '{$table}'";
88 $tables = $wpdb->get_results($query, ARRAY_A);
89 if (!is_array($tables) || 0 == count($tables)) return false;
90 }
91
92 return true;
93 }
94
95 /**
96 * See if any database schema updates are needed, and perform them if so.
97 * Example Usage:
98 * public static function update_101_add_new_column() {
99 * $wpdb = $GLOBALS['wpdb'];
100 * $wpdb->query('ALTER TABLE tm_tasks ADD task_expiry varchar(300) AFTER id');
101 * }
102 */
103 public static function check_updates() {
104 self::init_db();
105 $our_version = self::get_version();
106 if (is_multisite()) {
107 $db_version = get_site_option('updraft_task_manager_dbversion');
108 } else {
109 $db_version = get_option('updraft_task_manager_dbversion');
110 }
111 if (!$db_version || version_compare($our_version, $db_version, '>')) {
112 foreach (self::$db_updates as $version => $updates) {
113 if (version_compare($version, $db_version, '>')) {
114 foreach ($updates as $update) {
115 call_user_func(array(__CLASS__, $update));
116 }
117 }
118 }
119 if (is_multisite()) {
120 update_site_option('updraft_task_manager_dbversion', self::get_version());
121 } else {
122 update_option('updraft_task_manager_dbversion', self::get_version());
123 }
124 }
125 }
126
127 /**
128 * Returns the current version of the plugin
129 */
130 public static function get_version() {
131 return self::UPDRAFT_TASKS_DB_VERSION;
132 }
133
134 /**
135 * Create the database tables
136 */
137 public static function create_tables() {
138
139 $wpdb = $GLOBALS['wpdb'];
140
141 $our_prefix = $wpdb->base_prefix.self::$table_prefix;
142 $collate = '';
143
144 if ($wpdb->has_cap('collation')) {
145 if (!empty($wpdb->charset)) {
146 $collate .= "DEFAULT CHARACTER SET $wpdb->charset";
147 }
148 if (!empty($wpdb->collate)) {
149 $collate .= " COLLATE $wpdb->collate";
150 }
151 }
152
153 include_once ABSPATH.'wp-admin/includes/upgrade.php';
154
155 // Important: obey the magical/arbitrary rules for formatting this stuff: https://codex.wordpress.org/Creating_Tables_with_Plugins
156 // Otherwise, you get SQL errors and unwanted header output warnings when activating
157
158 $create_tables = 'CREATE TABLE '.$our_prefix."tasks (
159 task_id bigint(20) NOT NULL auto_increment,
160 user_id bigint(20) NOT NULL,
161 type varchar(300) NOT NULL,
162 description varchar(300),
163 PRIMARY KEY (task_id),
164 KEY user_id (user_id),
165 time_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
166 status varchar(300)
167 ) $collate;
168 ";
169 // KEY attribute_name (attribute_name)
170 dbDelta($create_tables);
171
172 $max_index_length = 191;
173
174 $create_tables = 'CREATE TABLE '.$our_prefix."taskmeta (
175 meta_id bigint(20) NOT NULL auto_increment,
176 task_id bigint(20) NOT NULL default '0',
177 meta_key varchar(255) DEFAULT NULL,
178 meta_value longtext,
179 PRIMARY KEY (meta_id),
180 KEY meta_key (meta_key($max_index_length)),
181 KEY task_id (task_id)
182 ) $collate;
183 ";
184
185 dbDelta($create_tables);
186 }
187
188 public static function add_attempts_and_class_identifier() {
189 $wpdb = $GLOBALS['wpdb'];
190 $our_prefix = $wpdb->base_prefix.self::$table_prefix;
191
192 $wpdb->query("ALTER TABLE ".$our_prefix."tasks CHANGE COLUMN `task_id` `id` INT NOT NULL");
193 $wpdb->query("ALTER TABLE ".$our_prefix."tasks MODIFY COLUMN `id` INT auto_increment");
194 $wpdb->query("ALTER TABLE ".$our_prefix."tasks ADD attempts INT DEFAULT 0 AFTER type");
195 $wpdb->query("ALTER TABLE ".$our_prefix."tasks ADD class_identifier varchar(300) DEFAULT 0 AFTER type");
196 }
197
198 public static function add_lock_column() {
199 $wpdb = $GLOBALS['wpdb'];
200 $our_prefix = $wpdb->base_prefix.self::$table_prefix;
201 $wpdb->query('ALTER TABLE '.$our_prefix.'tasks ADD last_locked_at BIGINT DEFAULT 0 AFTER time_created');
202 }
203
204 }
205
206 endif;
207