|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* Class JobParameters |
|
5
|
|
|
* |
|
6
|
|
|
* Class for storing information about a job. Takes care of serializing the |
|
7
|
|
|
* data for storing in a wordpress option |
|
8
|
|
|
* |
|
9
|
|
|
* @package Event Espresso |
|
10
|
|
|
* @subpackage batch |
|
11
|
|
|
* @author Mike Nelson |
|
12
|
|
|
* @since 4.8.26 |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
namespace EventEspressoBatchRequest\Helpers; |
|
16
|
|
|
|
|
17
|
|
|
if ( ! defined('EVENT_ESPRESSO_VERSION')) { exit('No direct script access allowed'); } |
|
18
|
|
|
|
|
19
|
|
|
class JobParameters { |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* status indicating the job should continue |
|
23
|
|
|
*/ |
|
24
|
|
|
const status_continue = 'continue'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* status indicated the job has been completed successfully and should be cleaned up next |
|
28
|
|
|
*/ |
|
29
|
|
|
const status_complete = 'complete'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* status indicating there was an error and the job should be cleaned up |
|
33
|
|
|
*/ |
|
34
|
|
|
const status_error = 'error'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* status indicating the job has been cleaned up, and so this is probably the last |
|
38
|
|
|
* time you'll see this job |
|
39
|
|
|
*/ |
|
40
|
|
|
const status_cleaned_up = 'cleaned_up'; |
|
41
|
|
|
|
|
42
|
|
|
const wp_option_prefix = 'ee_job_parameters_'; |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* String uniquely identifying the job |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $_job_id; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $_classname; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* |
|
60
|
|
|
* @var array |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $_request_data; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Array of any extra data we want to remember about this request, that |
|
66
|
|
|
* wasn't necessarily past in with the request data |
|
67
|
|
|
* @var array |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $_extra_data; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Estimate of how many units HAVE been processed |
|
73
|
|
|
* @var int |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $_units_processed = 0; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @var string |
|
79
|
|
|
*/ |
|
80
|
|
|
protected $_status; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* The size of the total job in whatever units you want. |
|
84
|
|
|
* If you can't provide an estimate leave as 0. |
|
85
|
|
|
* Once _units_processed equals _job_size, we should be done |
|
86
|
|
|
* @var int |
|
87
|
|
|
*/ |
|
88
|
|
|
protected $_job_size = 0; |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $job_id |
|
95
|
|
|
* @param string $classname |
|
96
|
|
|
* @param array $request_data |
|
97
|
|
|
* @param array $extra_data |
|
98
|
|
|
*/ |
|
99
|
|
|
function __construct( $job_id, $classname, $request_data, $extra_data = array() ) { |
|
100
|
|
|
$this->set_job_id( $job_id ); |
|
101
|
|
|
$this->set_classname( $classname ); |
|
102
|
|
|
$this->set_request_data( $request_data ); |
|
103
|
|
|
$this->set_extra_data( $extra_data ); |
|
104
|
|
|
$this->set_status( JobParameters::status_continue ); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Returns the array of strings of valid stati |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
|
|
public static function valid_stati() { |
|
114
|
|
|
return array( |
|
115
|
|
|
JobParameters::status_complete, |
|
116
|
|
|
JobParameters::status_continue, |
|
117
|
|
|
JobParameters::status_error, |
|
118
|
|
|
JobParameters::status_cleaned_up, |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Saves this option to the database (wordpress options table) |
|
126
|
|
|
* @param boolean $first |
|
127
|
|
|
* @return boolean success |
|
128
|
|
|
*/ |
|
129
|
|
|
function save( $first = false ) { |
|
130
|
|
|
$object_vars = wp_json_encode( get_object_vars( $this ) ); |
|
131
|
|
|
if( $first ) { |
|
132
|
|
|
return add_option( $this->option_name(), $object_vars, null, 'no' ); |
|
133
|
|
|
} else{ |
|
134
|
|
|
return update_option( $this->option_name(), $object_vars ); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Deletes the job from teh database, although this object is still usable |
|
142
|
|
|
* for the rest of the request |
|
143
|
|
|
* @return boolean |
|
144
|
|
|
*/ |
|
145
|
|
|
function delete() { |
|
146
|
|
|
return delete_option( $this->option_name() ); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Loads the specified job from the database |
|
153
|
|
|
* @param string $job_id |
|
154
|
|
|
* @return JobParameters |
|
155
|
|
|
* @throws BatchRequestException |
|
156
|
|
|
*/ |
|
157
|
|
|
static function load( $job_id ) { |
|
158
|
|
|
$job_parameter_vars = json_decode( get_option( JobParameters::wp_option_prefix . $job_id ), true ); |
|
159
|
|
|
if( |
|
160
|
|
|
! is_array( $job_parameter_vars ) || |
|
161
|
|
|
! isset( $job_parameter_vars[ '_classname' ] ) || |
|
162
|
|
|
! isset( $job_parameter_vars[ '_request_data' ] ) |
|
163
|
|
|
) { |
|
164
|
|
|
throw new BatchRequestException( |
|
165
|
|
|
sprintf( |
|
166
|
|
|
__('Could not retrieve job %1$s from the Wordpress options table, and so the job could not continue. The wordpress option was %2$s', 'event_espresso'), |
|
167
|
|
|
$job_id, |
|
168
|
|
|
get_option( JobParameters::wp_option_prefix . $job_id ) |
|
169
|
|
|
) |
|
170
|
|
|
); |
|
171
|
|
|
} |
|
172
|
|
|
$job_parameters = new JobParameters( |
|
173
|
|
|
$job_id, |
|
174
|
|
|
$job_parameter_vars[ '_classname' ], |
|
175
|
|
|
$job_parameter_vars[ '_request_data'] ); |
|
176
|
|
|
foreach( $job_parameter_vars as $key => $value ) { |
|
177
|
|
|
$job_parameters->$key = $value; |
|
178
|
|
|
} |
|
179
|
|
|
return $job_parameters; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Gets the job's unique string |
|
186
|
|
|
* @return string |
|
187
|
|
|
*/ |
|
188
|
|
|
function job_id() { |
|
189
|
|
|
return $this->_job_id; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Gets the classname that should run this job |
|
196
|
|
|
* @return string |
|
197
|
|
|
*/ |
|
198
|
|
|
function classname() { |
|
199
|
|
|
return $this->_classname; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
|
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Gets the original array of $_REQUEST data for this job |
|
206
|
|
|
* @return array |
|
207
|
|
|
*/ |
|
208
|
|
|
function request_data() { |
|
209
|
|
|
return $this->_request_data; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
|
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Gets a single item from the request data |
|
216
|
|
|
* @param string $key |
|
217
|
|
|
* @param string|array $default |
|
218
|
|
|
* @return string|array |
|
219
|
|
|
*/ |
|
220
|
|
|
function request_datum( $key, $default = '' ) { |
|
221
|
|
|
if( isset( $this->_request_data[ $key ] ) ) { |
|
222
|
|
|
return $this->_request_data[ $key ]; |
|
223
|
|
|
} else { |
|
224
|
|
|
return $default; |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
|
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Gets a single item from the extra data |
|
232
|
|
|
* @param string $key |
|
233
|
|
|
* @param string|array $default |
|
234
|
|
|
* @return string|array |
|
235
|
|
|
*/ |
|
236
|
|
|
function extra_datum( $key, $default = '' ) { |
|
237
|
|
|
if( isset( $this->_extra_data[ $key ] ) ) { |
|
238
|
|
|
return $this->_extra_data[ $key ]; |
|
239
|
|
|
} else { |
|
240
|
|
|
return $default; |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
|
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Adds an extra piece of extra data that we're going to want later during the job |
|
248
|
|
|
* @param string $key |
|
249
|
|
|
* @param string|int|array|null $value almost any extra data you want to store |
|
250
|
|
|
*/ |
|
251
|
|
|
function add_extra_data( $key, $value ) { |
|
252
|
|
|
$this->_extra_data[ $key ] = $value; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
|
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Array of any extra data we want to store |
|
259
|
|
|
* @return array |
|
260
|
|
|
*/ |
|
261
|
|
|
function extra_data() { |
|
262
|
|
|
return $this->_extra_data; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
|
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Returns the job size, in whatever units you want |
|
269
|
|
|
* @return int |
|
270
|
|
|
*/ |
|
271
|
|
|
function job_size() { |
|
272
|
|
|
return $this->_job_size; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
|
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Sets the job size. You decide what units to use |
|
279
|
|
|
* @param int $size |
|
280
|
|
|
*/ |
|
281
|
|
|
function set_job_size( $size ) { |
|
282
|
|
|
$this->_job_size = $size; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
|
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* The number of "units" processed, in the same units as the "job size" |
|
289
|
|
|
* @return int |
|
290
|
|
|
*/ |
|
291
|
|
|
function units_processed() { |
|
292
|
|
|
return $this->_units_processed; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
|
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* Marks more units as processed |
|
299
|
|
|
* @param int $newly_processed |
|
300
|
|
|
* @return int updated units processed |
|
301
|
|
|
*/ |
|
302
|
|
|
function mark_processed( $newly_processed ) { |
|
303
|
|
|
$this->_units_processed += $newly_processed; |
|
304
|
|
|
return $this->_units_processed; |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
|
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* Sets the total count of units processed. You might prefer to use mark_processed |
|
311
|
|
|
* @param int $total_units_processed |
|
312
|
|
|
*/ |
|
313
|
|
|
function set_units_processed( $total_units_processed ) { |
|
314
|
|
|
$this->_units_processed = $total_units_processed; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
|
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* Sets the job's ID |
|
321
|
|
|
* @param string $job_id |
|
322
|
|
|
*/ |
|
323
|
|
|
function set_job_id( $job_id ) { |
|
324
|
|
|
$this->_job_id = $job_id; |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
|
|
328
|
|
|
|
|
329
|
|
|
/** |
|
330
|
|
|
* sets the classname |
|
331
|
|
|
* @param string $classname |
|
332
|
|
|
*/ |
|
333
|
|
|
function set_classname( $classname ) { |
|
334
|
|
|
$this->_classname = $classname; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
|
|
338
|
|
|
|
|
339
|
|
|
/** |
|
340
|
|
|
* Sets the request data |
|
341
|
|
|
* @param array $request_data |
|
342
|
|
|
*/ |
|
343
|
|
|
function set_request_data( $request_data ) { |
|
344
|
|
|
$this->_request_data = $request_data; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
|
|
348
|
|
|
|
|
349
|
|
|
/** |
|
350
|
|
|
* Sets the array of extra data we want to store on this request |
|
351
|
|
|
* @param array $extra_data |
|
352
|
|
|
*/ |
|
353
|
|
|
function set_extra_data( $extra_data ) { |
|
354
|
|
|
$this->_extra_data = $extra_data; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
|
|
358
|
|
|
|
|
359
|
|
|
/** |
|
360
|
|
|
* Gets the name of the wordpress option that should store these job parameters |
|
361
|
|
|
* @return string |
|
362
|
|
|
*/ |
|
363
|
|
|
function option_name() { |
|
364
|
|
|
return JobParameters::wp_option_prefix . $this->job_id(); |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
|
|
368
|
|
|
|
|
369
|
|
|
/** |
|
370
|
|
|
* Gets the job\s current status. One of JobParameters::valid_stati(); |
|
371
|
|
|
* @return string |
|
372
|
|
|
*/ |
|
373
|
|
|
public function status() { |
|
374
|
|
|
return $this->_status; |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
|
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* |
|
381
|
|
|
* @param string $status on eof JobParameters::valid_stati() |
|
382
|
|
|
*/ |
|
383
|
|
|
public function set_status( $status ) { |
|
384
|
|
|
$this->_status = $status; |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
|
|
|
|
388
|
|
|
|
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
|