← All changes
|
vendor/team-updraft/common-libs/src/updraft-tasks/class-updraft-tasks-activation.php
+102
-5
3.2.3
→
4.7.0
View file →
| @@ -31,8 +31,25 @@ | ||
| 31 | 31 | |
| 32 | 32 | const UPDRAFT_TASKS_DB_VERSION = '1.1'; |
| 33 | 33 | |
| 34 | 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 | + /** | |
| 35 | 52 | * Initialise this class |
| 36 | 53 | */ |
| 37 | 54 | public static function init_db() { |
| 38 | 55 | self::$table_prefix = defined('UPDRAFT_TASKS_TABLE_PREFIX') ? UPDRAFT_TASKS_TABLE_PREFIX : 'tm_'; |
| @@ -64,14 +81,41 @@ | ||
| 64 | 81 | /** |
| 65 | 82 | * Drop database version variable from option from database and run install again. |
| 66 | 83 | */ |
| 67 | 84 | 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'); | |
| 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(); | |
| 72 | 110 | } |
| 73 | - self::install(); | |
| 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(); | |
| 74 | 118 | } |
| 75 | 119 | |
| 76 | 120 | /** |
| 77 | 121 | * Check if needed task manager tables exist. |
| @@ -79,8 +123,9 @@ | ||
| 79 | 123 | * @return bool |
| 80 | 124 | */ |
| 81 | 125 | public static function check_if_tables_exist() { |
| 82 | 126 | global $wpdb; |
| 127 | + self::init_db(); | |
| 83 | 128 | $our_prefix = $wpdb->base_prefix.self::$table_prefix; |
| 84 | 129 | $tables = array($our_prefix.'tasks', $our_prefix.'taskmeta'); |
| 85 | 130 | |
| 86 | 131 | foreach ($tables as $table) { |
| @@ -200,7 +245,59 @@ | ||
| 200 | 245 | $our_prefix = $wpdb->base_prefix.self::$table_prefix; |
| 201 | 246 | $wpdb->query('ALTER TABLE '.$our_prefix.'tasks ADD last_locked_at BIGINT DEFAULT 0 AFTER time_created'); |
| 202 | 247 | } |
| 203 | 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 | + } | |
| 204 | 301 | } |
| 205 | 302 | |
| 206 | 303 | endif; |