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.22, at vendor/team-updraft/common-libs/src/updraft-tasks/class-updraft-tasks-activation.php
| 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 the use of Task Manager library |
| 36 | * Example Usage: |
| 37 | * Updraft_Tasks_Activation::init(plugin_basename(__FILE__)); |
| 38 | * Updraft_Tasks_Activation::reinstall_if_needed(); |
| 39 | * |
| 40 | * @param string $plugin_slug Plugin slug |
| 41 | * |
| 42 | * @return void |
| 43 | */ |
| 44 | public static function init($plugin_slug) { |
| 45 | $used_by_plugins = self::get_used_by_plugins(); |
| 46 | if (!in_array($plugin_slug, $used_by_plugins)) { |
| 47 | self::update_used_by_plugins(array_merge($used_by_plugins, array($plugin_slug))); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Initialise this class |
| 53 | */ |
| 54 | public static function init_db() { |
| 55 | self::$table_prefix = defined('UPDRAFT_TASKS_TABLE_PREFIX') ? UPDRAFT_TASKS_TABLE_PREFIX : 'tm_'; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * This is the class entry point |
| 60 | */ |
| 61 | public static function install() { |
| 62 | self::init_db(); |
| 63 | self::create_tables(); |
| 64 | // we need walk through all updates when install at first. |
| 65 | self::check_updates(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Check needed tables in data base and if one of them doesn't exist force reinstall. |
| 70 | */ |
| 71 | public static function reinstall_if_needed() { |
| 72 | static $done = false; |
| 73 | |
| 74 | if ($done) return; |
| 75 | |
| 76 | if (!self::check_if_tables_exist()) self::reinstall(); |
| 77 | |
| 78 | $done = true; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Drop database version variable from option from database and run install again. |
| 83 | */ |
| 84 | public static function reinstall() { |
| 85 | self::delete_db_version_variable(); |
| 86 | self::install(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Delete database version variable from options table |
| 91 | */ |
| 92 | public static function delete_db_version_variable() { |
| 93 | delete_site_option('updraft_task_manager_dbversion'); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Drop database tables and version variable from option from database |
| 98 | * |
| 99 | * @param string $plugin_slug Plugin slug |
| 100 | * |
| 101 | * @return void |
| 102 | */ |
| 103 | public static function uninstall($plugin_slug) { |
| 104 | self::delete_used_by_plugins($plugin_slug); |
| 105 | $used_by_plugins = self::get_used_by_plugins(); |
| 106 | if (!empty($used_by_plugins)) return; |
| 107 | self::delete_db_version_variable(); |
| 108 | if (empty(self::$table_prefix)) { |
| 109 | self::init_db(); |
| 110 | } |
| 111 | global $wpdb; |
| 112 | $tables = array('tasks', 'taskmeta'); |
| 113 | foreach($tables as $table) { |
| 114 | $table_name = $wpdb->prefix . self::$table_prefix . $table; |
| 115 | $wpdb->query("DROP TABLE IF EXISTS $table_name"); |
| 116 | } |
| 117 | self::delete_used_by_plugins(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Check if needed task manager tables exist. |
| 122 | * |
| 123 | * @return bool |
| 124 | */ |
| 125 | public static function check_if_tables_exist() { |
| 126 | global $wpdb; |
| 127 | self::init_db(); |
| 128 | $our_prefix = $wpdb->base_prefix.self::$table_prefix; |
| 129 | $tables = array($our_prefix.'tasks', $our_prefix.'taskmeta'); |
| 130 | |
| 131 | foreach ($tables as $table) { |
| 132 | $query = "SHOW TABLES LIKE '{$table}'"; |
| 133 | $tables = $wpdb->get_results($query, ARRAY_A); |
| 134 | if (!is_array($tables) || 0 == count($tables)) return false; |
| 135 | } |
| 136 | |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * See if any database schema updates are needed, and perform them if so. |
| 142 | * Example Usage: |
| 143 | * public static function update_101_add_new_column() { |
| 144 | * $wpdb = $GLOBALS['wpdb']; |
| 145 | * $wpdb->query('ALTER TABLE tm_tasks ADD task_expiry varchar(300) AFTER id'); |
| 146 | * } |
| 147 | */ |
| 148 | public static function check_updates() { |
| 149 | self::init_db(); |
| 150 | $our_version = self::get_version(); |
| 151 | if (is_multisite()) { |
| 152 | $db_version = get_site_option('updraft_task_manager_dbversion'); |
| 153 | } else { |
| 154 | $db_version = get_option('updraft_task_manager_dbversion'); |
| 155 | } |
| 156 | if (!$db_version || version_compare($our_version, $db_version, '>')) { |
| 157 | foreach (self::$db_updates as $version => $updates) { |
| 158 | if (version_compare($version, $db_version, '>')) { |
| 159 | foreach ($updates as $update) { |
| 160 | call_user_func(array(__CLASS__, $update)); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | if (is_multisite()) { |
| 165 | update_site_option('updraft_task_manager_dbversion', self::get_version()); |
| 166 | } else { |
| 167 | update_option('updraft_task_manager_dbversion', self::get_version()); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Returns the current version of the plugin |
| 174 | */ |
| 175 | public static function get_version() { |
| 176 | return self::UPDRAFT_TASKS_DB_VERSION; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Create the database tables |
| 181 | */ |
| 182 | public static function create_tables() { |
| 183 | |
| 184 | $wpdb = $GLOBALS['wpdb']; |
| 185 | |
| 186 | $our_prefix = $wpdb->base_prefix.self::$table_prefix; |
| 187 | $collate = ''; |
| 188 | |
| 189 | if ($wpdb->has_cap('collation')) { |
| 190 | if (!empty($wpdb->charset)) { |
| 191 | $collate .= "DEFAULT CHARACTER SET $wpdb->charset"; |
| 192 | } |
| 193 | if (!empty($wpdb->collate)) { |
| 194 | $collate .= " COLLATE $wpdb->collate"; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | include_once ABSPATH.'wp-admin/includes/upgrade.php'; |
| 199 | |
| 200 | // Important: obey the magical/arbitrary rules for formatting this stuff: https://codex.wordpress.org/Creating_Tables_with_Plugins |
| 201 | // Otherwise, you get SQL errors and unwanted header output warnings when activating |
| 202 | |
| 203 | $create_tables = 'CREATE TABLE '.$our_prefix."tasks ( |
| 204 | task_id bigint(20) NOT NULL auto_increment, |
| 205 | user_id bigint(20) NOT NULL, |
| 206 | type varchar(300) NOT NULL, |
| 207 | description varchar(300), |
| 208 | PRIMARY KEY (task_id), |
| 209 | KEY user_id (user_id), |
| 210 | time_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
| 211 | status varchar(300) |
| 212 | ) $collate; |
| 213 | "; |
| 214 | // KEY attribute_name (attribute_name) |
| 215 | dbDelta($create_tables); |
| 216 | |
| 217 | $max_index_length = 191; |
| 218 | |
| 219 | $create_tables = 'CREATE TABLE '.$our_prefix."taskmeta ( |
| 220 | meta_id bigint(20) NOT NULL auto_increment, |
| 221 | task_id bigint(20) NOT NULL default '0', |
| 222 | meta_key varchar(255) DEFAULT NULL, |
| 223 | meta_value longtext, |
| 224 | PRIMARY KEY (meta_id), |
| 225 | KEY meta_key (meta_key($max_index_length)), |
| 226 | KEY task_id (task_id) |
| 227 | ) $collate; |
| 228 | "; |
| 229 | |
| 230 | dbDelta($create_tables); |
| 231 | } |
| 232 | |
| 233 | public static function add_attempts_and_class_identifier() { |
| 234 | $wpdb = $GLOBALS['wpdb']; |
| 235 | $our_prefix = $wpdb->base_prefix.self::$table_prefix; |
| 236 | |
| 237 | $wpdb->query("ALTER TABLE ".$our_prefix."tasks CHANGE COLUMN `task_id` `id` INT NOT NULL"); |
| 238 | $wpdb->query("ALTER TABLE ".$our_prefix."tasks MODIFY COLUMN `id` INT auto_increment"); |
| 239 | $wpdb->query("ALTER TABLE ".$our_prefix."tasks ADD attempts INT DEFAULT 0 AFTER type"); |
| 240 | $wpdb->query("ALTER TABLE ".$our_prefix."tasks ADD class_identifier varchar(300) DEFAULT 0 AFTER type"); |
| 241 | } |
| 242 | |
| 243 | public static function add_lock_column() { |
| 244 | $wpdb = $GLOBALS['wpdb']; |
| 245 | $our_prefix = $wpdb->base_prefix.self::$table_prefix; |
| 246 | $wpdb->query('ALTER TABLE '.$our_prefix.'tasks ADD last_locked_at BIGINT DEFAULT 0 AFTER time_created'); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Get an array of plugin slugs that uses this library |
| 251 | * |
| 252 | * @return array |
| 253 | */ |
| 254 | private static function get_used_by_plugins() { |
| 255 | return get_site_option('updraft_task_manager_plugins', array()); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Update the array of plugin slugs that uses this library |
| 260 | * |
| 261 | * @param array $used_by_plugins An array of plugin slugs |
| 262 | */ |
| 263 | private static function update_used_by_plugins($used_by_plugins) { |
| 264 | if (is_multisite()) { |
| 265 | update_site_option('updraft_task_manager_plugins', $used_by_plugins); |
| 266 | } else { |
| 267 | update_option('updraft_task_manager_plugins', $used_by_plugins); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Removes either given plugin slug. If plugin slug is not provided removes option itself |
| 273 | * |
| 274 | * @param string $plugin_slug Plugin slug |
| 275 | */ |
| 276 | private static function delete_used_by_plugins($plugin_slug = '') { |
| 277 | if (!empty($plugin_slug)) { |
| 278 | $used_by_plugins = self::get_used_by_plugins(); |
| 279 | $used_by_plugins = self::remove_plugin_from_array($used_by_plugins, $plugin_slug); |
| 280 | self::update_used_by_plugins($used_by_plugins); |
| 281 | } else { |
| 282 | delete_site_option('updraft_task_manager_plugins'); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Remove given plugin slug from an array of plugin slugs |
| 288 | * |
| 289 | * @param array $used_by_plugins An array of plugin slugs |
| 290 | * @param string $plugin_slug Plugin slug |
| 291 | * |
| 292 | * @return array |
| 293 | */ |
| 294 | private static function remove_plugin_from_array($used_by_plugins, $plugin_slug) { |
| 295 | $key = array_search($plugin_slug, $used_by_plugins); |
| 296 | if (false !== $key) { |
| 297 | unset($used_by_plugins[$key]); |
| 298 | } |
| 299 | return $used_by_plugins; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | endif; |
| 304 |