PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.11.0
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.11.0
4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / BackgroundProcessing / Action.php
wp-staging / Framework / BackgroundProcessing Last commit date
Exceptions 1 day ago Job 1 day ago Action.php 1 day ago BackgroundProcessingServiceProvider.php 1 day ago Demo.php 1 day ago FeatureDetection.php 1 day ago Queue.php 1 day ago QueueActionAware.php 1 day ago QueueProcessor.php 1 day ago WithQueueAwareness.php 1 day ago
Action.php
334 lines
1 <?php
2
3
4
5
6
7
8
9 namespace WPStaging\Framework\BackgroundProcessing;
10
11 use BadMethodCallException;
12 use WPStaging\Framework\BackgroundProcessing\Exceptions\QueueException;
13
14 /**
15 * Class Action
16 *
17 * @package WPStaging\Framework\BackgroundProcessing
18 *
19 * @property int $id The Action id, the unique, auto-increment value identifying its row.
20 * @property string $action The action name.
21 * @property string $jobId The Job the action belongs to.
22 * @property int $priority The Action priority, lower is executed first (like WP Filters API).
23 * @property array $args A set of arguments for the action.
24 * @property string $status The current Action status in the context of the Queue.
25 * @property string $claimedAt The date and time, in the site timezone, the Action was last claimed for processing.
26 * @property string $updatedAt The date and time, in the site timezone, the Action was last updated.
27 * @property mixed|null $custom Custom data attached to the Action.
28 */
29 class Action
30 {
31 use WithQueueAwareness;
32
33
34
35
36
37
38 private $id;
39
40
41
42
43
44
45
46 private $action;
47
48
49
50
51
52
53 private $jobId;
54
55
56
57
58
59
60
61 public $priority;
62
63
64
65
66
67
68
69 private $args;
70
71
72
73
74
75
76
77 public $status;
78
79
80
81
82
83
84
85 private $claimedAt;
86
87
88
89
90
91
92
93 public $updatedAt;
94
95
96
97
98
99
100 private $custom;
101
102
103
104
105
106
107 private $response;
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130 public function __construct(
131 $id,
132 $action,
133 array $args = [],
134 $jobId = 'default',
135 $priority = 0,
136 $status = null,
137 $claimedAt = null,
138 $updatedAt = null,
139 $custom = null,
140 $response = null
141 ) {
142 if (!is_numeric($id) && absint($id) == $id) {
143 throw new QueueException('Id MUST be a positive integer.');
144 }
145
146 if ((string)$action === '') {
147 throw new QueueException('Action MUST be a non-empty string.');
148 }
149
150 if ((string)$jobId === '') {
151 throw new QueueException('Job ID MUST be a non-empty string.');
152 }
153
154 $priority = is_numeric($priority) && (int)$priority == $priority ?
155 (int)$priority
156 : $this->getDefaultPriority();
157
158 $this->id = $id;
159 $this->action = $action;
160 $this->args = $args;
161 $this->jobId = $jobId;
162 $this->priority = $priority;
163 $this->status = $status;
164 $this->claimedAt = $claimedAt;
165 $this->updatedAt = $updatedAt;
166 $this->custom = $custom;
167 $this->response = $response;
168 }
169
170
171
172
173
174
175
176
177
178
179 public static function fromDbRow(array $dbRow)
180 {
181 $id = (int)$dbRow['id'];
182 $action = (string)$dbRow['action'];
183 $jobId = isset($dbRow['jobId']) ? (string)($dbRow['jobId']) : null;
184 $priority = isset($dbRow['priority']) ? (int)$dbRow['priority'] : self::getDefaultPriority();
185 $args = isset($dbRow['args']) ? (array)maybe_unserialize($dbRow['args']) : [];
186 $status = isset($dbRow['status']) ? (string)$dbRow['status'] : Queue::STATUS_READY;
187 $claimedAt = isset($dbRow['claimed_at']) ? (string)$dbRow['claimed_at'] : null;
188 $updatedAt = isset($dbRow['updated_at']) ? (string)$dbRow['updated_at'] : null;
189 $custom = isset($dbRow['custom']) ? maybe_unserialize($dbRow['custom']) : null;
190 $response = isset($dbRow['response']) ? maybe_unserialize($dbRow['response']) : null;
191
192 return new self($id, $action, $args, $jobId, $priority, $status, $claimedAt, $updatedAt, $custom, $response);
193 }
194
195
196
197
198
199
200
201
202
203
204 public function __get($name)
205 {
206 if (!property_exists($this, $name)) {
207 throw new BadMethodCallException("The Action object does not have an accessible property '{$name}'.");
208 }
209
210 return isset($this->{$name}) ? $this->{$name} : null;
211 }
212
213
214
215
216
217
218
219
220
221 public function __set($name, $value)
222 {
223 throw new BadMethodCallException("The Action object is immutable: its properties can be set only when building it.");
224 }
225
226
227
228
229
230
231
232
233
234
235
236
237
238 public function equals(Action $toCompare, array $compareFieldsExclude = [])
239 {
240 $compareFields = array_diff(
241 ['id', 'action', 'jobId', 'priority', 'args', 'status'],
242 $compareFieldsExclude
243 );
244
245 foreach ($compareFields as $prop) {
246 if (!$this->{$prop} == $toCompare->{$prop}) {
247 return false;
248 }
249 }
250
251 return true;
252 }
253
254
255
256
257
258
259
260 public function toArray()
261 {
262 return [
263 'id' => $this->id,
264 'action' => $this->action,
265 'jobId' => $this->jobId,
266 'priority' => $this->priority,
267 'args' => $this->args,
268 'status' => $this->status,
269 'claimedAt' => $this->claimedAt,
270 'updatedAt' => $this->updatedAt,
271 'custom' => $this->custom,
272 'response' => $this->response,
273 ];
274 }
275
276
277
278
279
280
281
282
283
284
285 public function alter(array $alterations)
286 {
287 $clone = clone $this;
288
289 foreach ($alterations as $key => $value) {
290 $clone->{$key} = $value;
291 }
292
293 return $clone;
294 }
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314 public static function sort($actionOne, $actionTwo)
315 {
316 $objectOne = (object)$actionOne;
317 $objectTwo = (object)$actionTwo;
318
319 if ($objectOne->priority !== $objectTwo->priority) {
320 return $objectOne->priority > $objectTwo->priority ? 1 : -1;
321 }
322
323 if ($objectOne->action !== $objectTwo->action) {
324 return $objectOne->action > $objectTwo->action ? 1 : -1;
325 }
326
327 if ($objectOne->jobId !== $objectTwo->jobId) {
328 return $objectOne->jobId > $objectTwo->jobId ? 1 : -1;
329 }
330
331 return 0;
332 }
333 }
334