| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 4 |
|
| 5 |
class WP_Optimize_Queue_Task extends Updraft_Task_1_2 { |
| 6 |
|
| 7 |
private $queue_id; |
| 8 |
|
| 9 |
/** |
| 10 |
* Function to run. |
| 11 |
* |
| 12 |
* @var array|int |
| 13 |
*/ |
| 14 |
public $task; |
| 15 |
|
| 16 |
/** |
| 17 |
* Params for $task. |
| 18 |
* |
| 19 |
* @var array |
| 20 |
*/ |
| 21 |
public $params; |
| 22 |
|
| 23 |
/** |
| 24 |
* Function to run after $task done. |
| 25 |
* |
| 26 |
* @var mixed |
| 27 |
*/ |
| 28 |
public $callback; |
| 29 |
|
| 30 |
/** |
| 31 |
* Task priority. |
| 32 |
* |
| 33 |
* @var int |
| 34 |
*/ |
| 35 |
public $priority; |
| 36 |
|
| 37 |
/** |
| 38 |
* WP_Optimize_Queue_Task constructor. |
| 39 |
* |
| 40 |
* @param array|int $task |
| 41 |
* @param array $params |
| 42 |
* @param callable $callback |
| 43 |
* @param int $priority |
| 44 |
*/ |
| 45 |
public function __construct($task, $params = array(), $callback = '', $priority = 0) { |
| 46 |
$this->queue_id = 'queue'; |
| 47 |
|
| 48 |
if (is_numeric($task)) { |
| 49 |
$this->load($task); |
| 50 |
} else { |
| 51 |
$this->task = $task; |
| 52 |
$this->params = $params; |
| 53 |
$this->callback = $callback; |
| 54 |
$this->priority = $priority; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Set queue id. |
| 60 |
* |
| 61 |
* @param string $queue_id |
| 62 |
*/ |
| 63 |
public function set_queue_id($queue_id) { |
| 64 |
$this->queue_id = $queue_id; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Get queue id. |
| 69 |
* |
| 70 |
* @return string |
| 71 |
*/ |
| 72 |
public function get_queue_id() { |
| 73 |
return $this->queue_id; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Run task. |
| 78 |
*/ |
| 79 |
public function run() { |
| 80 |
$task = $this->prepare_function($this->task); |
| 81 |
|
| 82 |
if (is_callable($task)) { |
| 83 |
$result = call_user_func_array($task, $this->params); |
| 84 |
|
| 85 |
if ('' !== $this->callback) { |
| 86 |
$callback = $this->prepare_function($this->callback); |
| 87 |
|
| 88 |
if (is_callable($callback)) call_user_func($callback, $result); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Save task to database. |
| 95 |
* |
| 96 |
* @return bool|int |
| 97 |
*/ |
| 98 |
public function save() { |
| 99 |
global $wpdb; |
| 100 |
|
| 101 |
// we use one task queue for all users. |
| 102 |
$user_id = 0; |
| 103 |
$description = 'Queue #'.$this->queue_id.' task '.(is_array($this->task) ? join('->', $this->task) : $this->task); |
| 104 |
|
| 105 |
$wpdb->query( |
| 106 |
$wpdb->prepare("INSERT INTO {$wpdb->base_prefix}tm_tasks (user_id, description, status) VALUES (%d, %s, %s)", $user_id, $description, 'active') |
| 107 |
); |
| 108 |
|
| 109 |
$task_id = $wpdb->insert_id; |
| 110 |
|
| 111 |
if (!$task_id) |
| 112 |
return false; |
| 113 |
|
| 114 |
$_task = $wpdb->get_row( |
| 115 |
$wpdb->prepare("SELECT * FROM `{$wpdb->base_prefix}tm_tasks` WHERE id = %d LIMIT 1", $task_id) |
| 116 |
); |
| 117 |
|
| 118 |
if (!$_task) |
| 119 |
return false; |
| 120 |
|
| 121 |
$this->set_id($task_id); |
| 122 |
|
| 123 |
Updraft_Task_Meta::update_task_meta($task_id, 'queue_id', $this->queue_id); |
| 124 |
|
| 125 |
Updraft_Task_Meta::update_task_meta($task_id, 'task', array( |
| 126 |
'task' => $this->task, |
| 127 |
'params' => $this->params, |
| 128 |
'callback' => $this->callback |
| 129 |
)); |
| 130 |
|
| 131 |
Updraft_Task_Meta::update_task_meta($task_id, 'priority', $this->priority); |
| 132 |
|
| 133 |
return $task_id; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Load task from database by id. |
| 138 |
* |
| 139 |
* @param int $task_id |
| 140 |
* @return bool |
| 141 |
*/ |
| 142 |
public function load($task_id) { |
| 143 |
global $wpdb; |
| 144 |
|
| 145 |
$task_info = $wpdb->get_results($wpdb->prepare("SELECT meta_key, meta_value FROM `{$wpdb->base_prefix}tm_tasks` t JOIN `{$wpdb->base_prefix}tm_taskmeta` tm ON t.id = tm.task_id WHERE t.id=%d;", $task_id), ARRAY_A); |
| 146 |
|
| 147 |
if (empty($task_info)) return false; |
| 148 |
|
| 149 |
$this->set_id($task_id); |
| 150 |
|
| 151 |
foreach ($task_info as $info) { |
| 152 |
if ('task' === $info['meta_key']) { |
| 153 |
$_info = WP_Optimize_Utils::maybe_unserialize($info['meta_value']); |
| 154 |
|
| 155 |
if (is_array($_info)) { |
| 156 |
foreach ($_info as $key => $value) { |
| 157 |
$this->{$key} = $value; |
| 158 |
} |
| 159 |
} |
| 160 |
} else { |
| 161 |
$this->{$info['meta_key']} = WP_Optimize_Utils::maybe_unserialize($info['meta_value']); |
| 162 |
} |
| 163 |
} |
| 164 |
return true; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Return information about task as a string. |
| 169 |
* |
| 170 |
* @return string |
| 171 |
*/ |
| 172 |
public function get_info() { |
| 173 |
return 'Task: '.(is_array($this->task) ? implode('->', $this->task) : $this->task); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Check callback $function and create a class instance in case when class name passed instead of object |
| 178 |
* |
| 179 |
* @param array|string $function |
| 180 |
* @return array |
| 181 |
*/ |
| 182 |
private function prepare_function($function) { |
| 183 |
|
| 184 |
if (is_array($function) && is_string($function[0])) { |
| 185 |
|
| 186 |
$reflection_method = new ReflectionMethod($function[0], $function[1]); |
| 187 |
|
| 188 |
if (!$reflection_method->isStatic()) { |
| 189 |
$function[0] = new $function[0]; |
| 190 |
} |
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
return $function; |
| 195 |
} |
| 196 |
} |
| 197 |
|