wp-optimize
/
vendor
/
team-updraft
/
common-libs
/
src
/
updraft-tasks
/
class-updraft-task-manager.php
class-updraft-task-manager.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.4.2, at vendor/team-updraft/common-libs/src/updraft-tasks/class-updraft-task-manager.php
| 1 | <?php |
| 2 | /** |
| 3 | * A task manager that locks and processes the task queue |
| 4 | */ |
| 5 | |
| 6 | if (!defined('ABSPATH')) die('Access denied.'); |
| 7 | |
| 8 | if (!class_exists('Updraft_Task_Manager_1_4')) : |
| 9 | |
| 10 | abstract class Updraft_Task_Manager_1_4 { |
| 11 | |
| 12 | protected $loggers; |
| 13 | |
| 14 | public $commands; |
| 15 | |
| 16 | private $queue_semaphore; |
| 17 | |
| 18 | /** |
| 19 | * Set this to the number of seconds for the lock timeout, or 0 to not use a lock |
| 20 | */ |
| 21 | protected $use_per_task_lock = 0; |
| 22 | |
| 23 | /** |
| 24 | * The Task Manager constructor |
| 25 | */ |
| 26 | public function __construct() { |
| 27 | |
| 28 | if (!class_exists('Updraft_Task_1_2')) require_once('class-updraft-task.php'); |
| 29 | if (!class_exists('Updraft_Task_Manager_Commands_1_0')) require_once('class-updraft-task-manager-commands.php'); |
| 30 | if (!class_exists('Updraft_Semaphore_3_0')) require_once(dirname(__FILE__).'/../updraft-semaphore/class-updraft-semaphore.php'); |
| 31 | if (!class_exists('Updraft_Tasks_Activation')) require_once(dirname(__FILE__).'/class-updraft-tasks-activation.php'); |
| 32 | |
| 33 | $this->commands = new Updraft_Task_Manager_Commands_1_0($this); |
| 34 | |
| 35 | do_action('updraft_task_manager_loaded', $this); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Process a single task in the queue |
| 40 | * |
| 41 | * @param int|Updraft_Task - $task Task ID or Updraft_Task object. |
| 42 | * @return boolean|WP_Error - status of task or error if task not found |
| 43 | */ |
| 44 | public function process_task($task) { |
| 45 | |
| 46 | if (!is_a($task, 'Updraft_Task_1_2')) { |
| 47 | $task_id = (int) $task; |
| 48 | $task = $this->get_task_instance($task_id); |
| 49 | } |
| 50 | |
| 51 | if (!$task) return new WP_Error('id_invalid', 'Task not found or ID is invalid'); |
| 52 | |
| 53 | return $task->attempt(apply_filters('updraft_task_lock_for', $this->use_per_task_lock, $this)); |
| 54 | |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Gets a list of all tasks that matches the $status flag |
| 59 | * |
| 60 | * @param int|Updraft_Task - $task Task ID or Updraft_Task object. |
| 61 | * @return String|WP_Error - status of task or error if task not found. |
| 62 | */ |
| 63 | public function get_task_status($task) { |
| 64 | |
| 65 | if (!($task instanceof Updraft_Task_1_2)) { |
| 66 | $task_id = (int) $task; |
| 67 | $task = $this->get_task_instance($task_id); |
| 68 | } |
| 69 | |
| 70 | if (!$task) return new WP_Error('id_invalid', 'Task not found or ID is invalid'); |
| 71 | |
| 72 | return $task->get_status(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Ends a given task |
| 77 | * |
| 78 | * @param int|Updraft_Task - $task Task ID or Updraft_Task object. |
| 79 | * @return boolean|WP_Error - Status of the operation or error if task not found. |
| 80 | */ |
| 81 | public function end_task($task) { |
| 82 | |
| 83 | if (!($task instanceof Updraft_Task_1_2)) { |
| 84 | $task_id = (int) $task; |
| 85 | $task = $this->get_task_instance($task_id); |
| 86 | } |
| 87 | |
| 88 | if (!$task) return new WP_Error('id_invalid', 'Task not found or ID is invalid'); |
| 89 | |
| 90 | return $task->complete(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Process a the queue of a specifed task type |
| 95 | * |
| 96 | * @param string $type queue type to process |
| 97 | * @return bool true on success, false otherwise |
| 98 | */ |
| 99 | public function process_queue($type) { |
| 100 | |
| 101 | $task_list = $this->get_active_tasks($type); |
| 102 | $total = is_array($task_list) ? count($task_list) : 0; |
| 103 | |
| 104 | if (1 > $total) { |
| 105 | $this->log(sprintf('The queue for tasks of type "%s" is empty. Aborting!', $type)); |
| 106 | return true; |
| 107 | } else { |
| 108 | $this->log(sprintf('A total of %d tasks of type %s found and will be processed in this iteration', $total, $type)); |
| 109 | } |
| 110 | |
| 111 | $this->queue_semaphore = new Updraft_Semaphore_3_0($type); |
| 112 | |
| 113 | // Prevent PHP warning which trigger from semaphore class set_loggers method if $this->loggers is empty |
| 114 | if (!empty($this->loggers)) { |
| 115 | $this->queue_semaphore->set_loggers($this->loggers); |
| 116 | } |
| 117 | |
| 118 | if (!$this->queue_semaphore->lock()) { |
| 119 | |
| 120 | $this->log(sprintf('Failed to gain semaphore lock (%s) - another process is already processing the queue - aborting (if this is wrong - i.e. if the other process crashed without removing the lock, then another can be started after 1 minute', $type)); |
| 121 | |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | $done = 0; |
| 126 | foreach ($task_list as $task) { |
| 127 | $this->process_task($task); |
| 128 | $done++; |
| 129 | /** |
| 130 | * Filters if the queue should be interrupted. Used after processing each task. |
| 131 | * |
| 132 | * @param boolean $interrupt_queue - If the queue should be interrupted. Default to FALSE |
| 133 | * @param object $task - The current task object |
| 134 | * @param object $task_manager - The task manager instance |
| 135 | */ |
| 136 | if (apply_filters('updraft_interrupt_tasks_queue_'.$type, false, $task, $this)) { |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | $this->queue_semaphore->release(); |
| 142 | $this->log(sprintf('Successfully processed the queue (%s). %d tasks were processed out of %d.', $type, $done, $total)); |
| 143 | $this->queue_semaphore->delete(); |
| 144 | |
| 145 | return $done == $total; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Cleans out all complete tasks from the DB. |
| 150 | * |
| 151 | * @param String $type type of the task |
| 152 | */ |
| 153 | public function clean_up_old_tasks($type) { |
| 154 | $completed_tasks = $this->get_completed_tasks($type); |
| 155 | |
| 156 | if (!$completed_tasks) return false; |
| 157 | |
| 158 | $this->log(sprintf('Cleaning up tasks of type (%s). A total of %d tasks will be deleted.', $type, count($completed_tasks))); |
| 159 | |
| 160 | foreach ($completed_tasks as $task) { |
| 161 | $task->delete_meta(); |
| 162 | $task->delete(); |
| 163 | } |
| 164 | |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Delete all tasks from queue. |
| 170 | * |
| 171 | * @param string $type |
| 172 | * |
| 173 | * @return boolean|integer Number of rows deleted, or (boolean)false upon error |
| 174 | */ |
| 175 | public function delete_tasks($task_type) { |
| 176 | global $wpdb; |
| 177 | |
| 178 | $sql = "DELETE t, tm FROM `{$wpdb->base_prefix}tm_tasks` t LEFT JOIN `{$wpdb->base_prefix}tm_taskmeta` tm ON t.id = tm.task_id WHERE t.type = '{$task_type}'"; |
| 179 | |
| 180 | return $wpdb->query($sql); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Get count of completed and all tasks. |
| 185 | * |
| 186 | * @return array - [ ['complete_tasks' => , 'all_tasks' => ] ] |
| 187 | */ |
| 188 | public function get_status($task_type) { |
| 189 | global $wpdb; |
| 190 | |
| 191 | $query = $wpdb->prepare( |
| 192 | "SELECT complete_tasks, all_tasks FROM (SELECT COUNT(*) AS complete_tasks FROM {$wpdb->base_prefix}tm_tasks WHERE `type` = %s AND `status` = %s) a, (SELECT COUNT(*) AS all_tasks FROM {$wpdb->base_prefix}tm_tasks WHERE `type` = %s) b", |
| 193 | array( |
| 194 | $task_type, |
| 195 | 'complete', |
| 196 | $task_type, |
| 197 | ) |
| 198 | ); |
| 199 | |
| 200 | $status = $wpdb->get_row($query, ARRAY_A); |
| 201 | |
| 202 | if (empty($status)) { |
| 203 | $status = array( |
| 204 | 'complete_tasks' => 0, |
| 205 | 'all_tasks' => 0, |
| 206 | ); |
| 207 | } |
| 208 | |
| 209 | return $status; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Fetches a list of all active tasks |
| 214 | * |
| 215 | * @param String $type type of the task |
| 216 | * @return Mixed - array of Task ojects or NULL if none found |
| 217 | */ |
| 218 | public function get_active_tasks($type) { |
| 219 | return $this->get_tasks('active', $type); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Gets a list of all completed tasks |
| 224 | * |
| 225 | * @param String $type type of the task |
| 226 | * @return Mixed - array of Task ojects or NULL if none found |
| 227 | */ |
| 228 | public function get_completed_tasks($type) { |
| 229 | return $this->get_tasks('complete', $type); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Gets a list of all tasks that matches the $status flag |
| 234 | * |
| 235 | * @param String $status - status of tasks to return, defaults to all tasks |
| 236 | * @param String $type - type of task |
| 237 | * |
| 238 | * @return Mixed - array of Task objects or NULL if none found |
| 239 | */ |
| 240 | public function get_tasks($status, $type) { |
| 241 | global $wpdb; |
| 242 | |
| 243 | $tasks = array(); |
| 244 | |
| 245 | if (array_key_exists($status, Updraft_Task_1_2::get_allowed_statuses())) { |
| 246 | $sql = $wpdb->prepare("SELECT * FROM {$wpdb->base_prefix}tm_tasks WHERE status = %s AND type = %s", $status, $type); |
| 247 | } else { |
| 248 | $sql = $wpdb->prepare("SELECT * FROM {$wpdb->base_prefix}tm_tasks WHERE type = %s", $type); |
| 249 | } |
| 250 | |
| 251 | $_tasks = $wpdb->get_results($sql); |
| 252 | |
| 253 | if (!$_tasks) { |
| 254 | // if we got an error then check if task manager tables are in the database |
| 255 | // and recreate them if needed. |
| 256 | if ($wpdb->last_error) { |
| 257 | Updraft_Tasks_Activation::reinstall_if_needed(); |
| 258 | } |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | |
| 263 | foreach ($_tasks as $_task) { |
| 264 | $task = $this->get_task_instance($_task->id); |
| 265 | if ($task) array_push($tasks, $task); |
| 266 | } |
| 267 | |
| 268 | return $tasks; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Retrieve the task instance using its ID |
| 273 | * |
| 274 | * @access public |
| 275 | * |
| 276 | * @global wpdb $wpdb WordPress database abstraction object. |
| 277 | * |
| 278 | * @param int $task_id Task ID. |
| 279 | * @return Task|Boolean Task object, false otherwise. |
| 280 | */ |
| 281 | public function get_task_instance($task_id) { |
| 282 | global $wpdb; |
| 283 | |
| 284 | $task_id = (int) $task_id; |
| 285 | if (!$task_id) return false; |
| 286 | |
| 287 | $sql = $wpdb->prepare("SELECT * FROM {$wpdb->base_prefix}tm_tasks WHERE id = %d LIMIT 1", $task_id); |
| 288 | $_task = $wpdb->get_row($sql); |
| 289 | |
| 290 | if (!$_task) |
| 291 | return false; |
| 292 | |
| 293 | $class_identifier = $_task->class_identifier; |
| 294 | |
| 295 | if (class_exists($class_identifier)) { |
| 296 | $task_instance = new $class_identifier($_task); |
| 297 | $task_instance->set_loggers($this->loggers); |
| 298 | return $task_instance; |
| 299 | } |
| 300 | |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Sets the logger for this instance. |
| 306 | * |
| 307 | * @param array $loggers - the loggers for this task |
| 308 | */ |
| 309 | public function set_loggers($loggers) { |
| 310 | foreach ($loggers as $logger) { |
| 311 | $this->add_logger($logger); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Add a logger to loggers list |
| 317 | * |
| 318 | * @param Object $logger - a logger for the instance |
| 319 | */ |
| 320 | public function add_logger($logger) { |
| 321 | $this->loggers[] = $logger; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Return list of loggers |
| 326 | * |
| 327 | * @return array |
| 328 | */ |
| 329 | public function get_loggers() { |
| 330 | return $this->loggers; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Captures and logs any interesting messages |
| 335 | * |
| 336 | * @param String $message - the error message |
| 337 | * @param String $error_type - the error type |
| 338 | */ |
| 339 | public function log($message, $error_type = 'info') { |
| 340 | if (isset($this->loggers)) { |
| 341 | foreach ($this->loggers as $logger) { |
| 342 | $logger->log($message, $error_type); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | } |
| 348 | |
| 349 | endif; |
| 350 |