wp-optimize
/
vendor
/
team-updraft
/
common-libs
/
src
/
updraft-tasks
/
class-updraft-task-manager-commands.php
class-updraft-task-manager-commands.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-task-manager-commands.php
| 1 | <?php |
| 2 | /** |
| 3 | * The AJAX Commands manager class |
| 4 | */ |
| 5 | |
| 6 | if (!defined('ABSPATH')) die('Access denied.'); |
| 7 | |
| 8 | if (!defined('Updraft_Task_Manager_Commands_1_0')) : |
| 9 | |
| 10 | class Updraft_Task_Manager_Commands_1_0 { |
| 11 | |
| 12 | protected $task_manager; |
| 13 | |
| 14 | /** |
| 15 | * Constructor |
| 16 | * |
| 17 | * @param Updraft_Task_Manager_1_4 $task_manager The task manager instance |
| 18 | */ |
| 19 | public function __construct($task_manager) { |
| 20 | $this->task_manager = $task_manager; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * A list of allowed commands via AJAX |
| 25 | * |
| 26 | * @return array - List of allowed commands |
| 27 | */ |
| 28 | public static function get_allowed_ajax_commands() { |
| 29 | |
| 30 | $commands = array( |
| 31 | 'process_task', |
| 32 | 'get_task_status', |
| 33 | 'end_task', |
| 34 | 'process_queue', |
| 35 | 'get_active_tasks', |
| 36 | 'clean_up_old_tasks', |
| 37 | ); |
| 38 | |
| 39 | return apply_filters('updraft_task_manager_allowed_ajax_commands', $commands); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Process a single task in the queue |
| 44 | * |
| 45 | * @param array $data data passed via AJAX |
| 46 | * @return void|WP_Error status of the operation |
| 47 | */ |
| 48 | public function process_task($data) { |
| 49 | |
| 50 | if (!isset($data['task_id'])) |
| 51 | return new WP_Error('id_missing', 'Task ID is missing or invalid'); |
| 52 | |
| 53 | $task_id = (int) $data['task_id']; |
| 54 | |
| 55 | $response = apply_filters('updraft_task_manager_process_task_response', "Processing task: {$task_id}", $task_id); |
| 56 | $this->close_browser_connection($response); |
| 57 | $this->task_manager->process_task($task_id); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Process a single task in the queue |
| 62 | * |
| 63 | * @param array $data data passed via AJAX |
| 64 | * @return String - status of task or false if none found |
| 65 | */ |
| 66 | public function get_task_status($data) { |
| 67 | |
| 68 | if (!isset($data['task_id'])) |
| 69 | return new WP_Error('id_missing', 'Task ID is missing or invalid'); |
| 70 | |
| 71 | $task_id = (int) $data['task_id']; |
| 72 | |
| 73 | return $this->task_manager->get_task_status($task_id); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Ends a given task |
| 78 | * |
| 79 | * @param array $data data passed via AJAX |
| 80 | * @return boolean - Status of the operation. |
| 81 | */ |
| 82 | public function end_task($data) { |
| 83 | |
| 84 | if (!isset($data['task_id'])) |
| 85 | return new WP_Error('id_missing', 'Task ID is missing or invalid'); |
| 86 | |
| 87 | $task_id = (int) $data['task_id']; |
| 88 | |
| 89 | $status = $this->task_manager->end_task($task_id); |
| 90 | |
| 91 | if (!$status) return new WP_Error('end_task_failed', 'Task is already ended'); |
| 92 | |
| 93 | $response = apply_filters('updraft_task_manager_end_task_response', "Successfully ended task with id : {$task_id}", $task_id); |
| 94 | $this->close_browser_connection($response); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Fetches a list of all active tasks |
| 99 | * |
| 100 | * @param array $data data passed via AJAX |
| 101 | * @return Mixed - array of UpdraftPlus_Task ojects or NULL if none found |
| 102 | */ |
| 103 | public function get_active_tasks($data) { |
| 104 | |
| 105 | if (!isset($data['type'])) |
| 106 | return new WP_Error('type_missing', 'Task type is missing or invalid'); |
| 107 | |
| 108 | $type = $data['type']; |
| 109 | $tasks = $this->task_manager->get_active_tasks($type); |
| 110 | |
| 111 | $ids = array(); |
| 112 | |
| 113 | if ($tasks) { |
| 114 | foreach ($tasks as $task) { |
| 115 | array_push($ids, $task->get_id()); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | $response = apply_filters('updraft_task_manager_get_active_tasks_response', $ids, $type); |
| 120 | return $response; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Cleans out all complete tasks from the DB. |
| 125 | * |
| 126 | * @param array $data data passed via AJAX |
| 127 | * @return void|WP_Error status of the operation |
| 128 | */ |
| 129 | public function clean_up_old_tasks($data) { |
| 130 | |
| 131 | if (!isset($data['type'])) |
| 132 | return new WP_Error('type_missing', 'Task type is missing or invalid'); |
| 133 | |
| 134 | $type = $data['type']; |
| 135 | $status = $this->task_manager->clean_up_old_tasks($type); |
| 136 | |
| 137 | if (!$status) return new WP_Error('clean_up_failed', 'Queue is already empty or the task type invalid'); |
| 138 | |
| 139 | $response = apply_filters('updraft_task_manager_clean_up_old_tasks_response', "Cleaned up old tasks of type : $type", $type); |
| 140 | $this->close_browser_connection($response); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Processes a queue of a specific type of task |
| 145 | * |
| 146 | * @param array $data data passed via AJAX |
| 147 | * @return void|WP_Error status of the operation |
| 148 | */ |
| 149 | public function process_queue($data) { |
| 150 | if (!isset($data['type'])) |
| 151 | return new WP_Error('type_missing', 'Task type is missing or invalid'); |
| 152 | |
| 153 | $type = $data['type']; |
| 154 | |
| 155 | $response = apply_filters('updraft_task_manager_process_queue_response', "Processing queue of type {$type}", $type); |
| 156 | |
| 157 | $this->close_browser_connection(json_encode($response)); |
| 158 | $status = $this->task_manager->process_queue($type); |
| 159 | |
| 160 | if (!$status) |
| 161 | return new WP_Error('process_queue_operation_failed', 'Failed to process the queue'); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Close browser connection so that it can resume AJAX polling |
| 166 | * |
| 167 | * @param array $txt Response to browser |
| 168 | * @return void |
| 169 | */ |
| 170 | public function close_browser_connection($txt = '') { |
| 171 | header('Content-Length: '.((!empty($txt)) ? 5+strlen($txt) : '0')); |
| 172 | header('Connection: close'); |
| 173 | header('Content-Encoding: none'); |
| 174 | if (session_id()) session_write_close(); |
| 175 | echo "\r\n\r\n"; |
| 176 | echo $txt; |
| 177 | |
| 178 | $levels = ob_get_level(); |
| 179 | |
| 180 | for ($i = 0; $i < $levels; $i++) { |
| 181 | ob_end_flush(); |
| 182 | } |
| 183 | |
| 184 | flush(); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | endif; |
| 189 |