PluginProbe
WPIDE – File Manager & Code Editor / trunk
WPIDE – File Manager & Code Editor vtrunk
3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 3.4 All 54 releases
wpide / App / Tasks / Task.php

Task.php in WPIDE – File Manager & Code Editor trunk, at App/Tasks/Task.php

210 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace WPIDE\App\Tasks;
3
4 use ActionScheduler_DBStore;
5 use ActionScheduler_Store;
6 use Exception;
7 use WPIDE\App\App;
8
9 abstract class Task
10 {
11 protected static $option_key = 'wpide_tasks';
12 protected static $store;
13
14 public static function register()
15 {
16
17 add_action(self::name(), [get_called_class(), 'handler'], 10, 2);
18 }
19
20 /**
21 * @throws Exception
22 */
23 public static function dispatch($args): array
24 {
25 if(!function_exists('as_enqueue_async_action')) {
26 throw new Exception('Cannot dispatch '.self::name().' task! Action Scheduler library was not found!');
27 }
28
29 $task_id = uniqid();
30 $action_id = as_enqueue_async_action(self::name(), [$task_id, $args ]);
31
32 error_log( '#'.$task_id.': Task Dispatched: ' . self::name());
33 error_log( '#'.$task_id.': Task Args: ' . json_encode($args));
34
35 self::add($task_id, $action_id);
36
37 return [
38 'task_id' => $task_id,
39 'action_id' => $action_id
40 ];
41 }
42
43 protected static function queueStore(): ActionScheduler_DBStore
44 {
45 if(empty(self::$store)) {
46 self::$store = new ActionScheduler_DBStore();
47 }
48
49 return self::$store;
50 }
51
52 public static function getStatusLabels(): array
53 {
54
55 $store = self::queueStore();
56
57 return $store->get_status_labels();
58 }
59
60 public static function getQueue(): array
61 {
62
63 $queue = get_option(self::$option_key, []);
64
65 $store = self::queueStore();
66 $labels = self::getStatusLabels();
67
68 $completed_actions = $store->query_actions([
69 'hook' => self::name(),
70 'status' => ActionScheduler_Store::STATUS_COMPLETE
71 ]);
72 $running_actions = $store->query_actions([
73 'hook' => self::name(),
74 'status' => ActionScheduler_Store::STATUS_RUNNING
75 ]);
76 $pending_actions = $store->query_actions([
77 'hook' => self::name(),
78 'status' => ActionScheduler_Store::STATUS_PENDING
79 ]);
80 $cancelled_actions = $store->query_actions([
81 'hook' => self::name(),
82 'status' => ActionScheduler_Store::STATUS_CANCELED
83 ]);
84 $failed_actions = $store->query_actions([
85 'hook' => self::name(),
86 'status' => ActionScheduler_Store::STATUS_FAILED
87 ]);
88
89 $updated = 0;
90 $deleted = [];
91 $queue = array_map(function($item) use (&$updated, &$deleted, $labels, $failed_actions, $cancelled_actions, $pending_actions, $running_actions, $completed_actions) {
92
93 if(in_array($item['action_id'], $completed_actions)) {
94 $status = ActionScheduler_Store::STATUS_COMPLETE;
95 }else if(in_array($item['action_id'], $running_actions)) {
96 $status = ActionScheduler_Store::STATUS_RUNNING;
97 }else if(in_array($item['action_id'], $pending_actions)) {
98 $status = ActionScheduler_Store::STATUS_PENDING;
99 }else if(in_array($item['action_id'], $cancelled_actions)) {
100 $status = ActionScheduler_Store::STATUS_CANCELED;
101 }else if(in_array($item['action_id'], $failed_actions)) {
102 $status = ActionScheduler_Store::STATUS_FAILED;
103 }else{
104 $status = $item['status'];
105 $deleted[] = $item['task_id'];
106 }
107
108 if($item['status'] !== $status) {
109 $item['status'] = $status;
110 $item['statusLabel'] = $labels[$status];
111 $updated++;
112 }
113
114 return $item;
115
116 }, $queue);
117
118 if(count($deleted) > 0) {
119 $queue = array_filter($queue, function($item) use($deleted) {
120 return !in_array($item['task_id'], $deleted);
121 });
122 }
123
124 if($updated > 0 || count($deleted) > 0) {
125 self::saveQueue($queue);
126 }
127
128 return $queue;
129 }
130
131 public static function saveQueue($queue = []) {
132 update_option(self::$option_key, $queue);
133 }
134
135 public static function add($task_id, $action_id) {
136
137 $queue = self::getQueue();
138
139 $labels = self::getStatusLabels();
140
141 $queue[$task_id] = [
142 'name' => self::niceName(),
143 'task_id' => $task_id,
144 'action_id' => $action_id,
145 'status' => ActionScheduler_Store::STATUS_PENDING,
146 'statusLabel' => $labels[ActionScheduler_Store::STATUS_PENDING],
147 'return' => null
148 ];
149
150 self::saveQueue($queue);
151 }
152
153 public static function remove($task_id, $action_id) {
154
155 $store = self::queueStore();
156 $store->delete_action($action_id);
157
158 $queue = self::getQueue();
159 if(isset($queue[$task_id])) {
160 unset($queue[$task_id]);
161 self::saveQueue($queue);
162 }
163 }
164
165 public static function update($task_id, $args): bool
166 {
167
168 $queue = self::getQueue();
169
170 if(isset($queue[$task_id])) {
171 $queue[$task_id] = array_merge($queue[$task_id], $args);
172 self::saveQueue($queue);
173 return true;
174 }
175
176 return false;
177 }
178
179 public static function name(): string
180 {
181 return get_called_class();
182 }
183
184 public static function niceName()
185 {
186 $array = explode('\\', self::name());
187 return array_pop($array);
188 }
189
190 abstract public static function handle($task_id, $args);
191
192 public static function handler($task_id, $args = []) {
193
194 if(!defined('WPIDE_DOING_TASK')) {
195 define('WPIDE_DOING_TASK', true);
196 }
197
198 error_log( '#'.$task_id.': Task Started: ' . self::name());
199 error_log( '#'.$task_id.': Task Args: ' . json_encode($args));
200
201 App::instance()->bootstrap();
202 $return = call_user_func([get_called_class(), 'handle'], $task_id, $args);
203
204 self::update($task_id, [
205 'return' => $return
206 ]);
207
208 error_log( '#'.$task_id.': Task Completed: ' . self::name());
209 }
210 }