|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Manage the task queue. |
|
4
|
|
|
* |
|
5
|
|
|
* @class Object_Sync_Sf_Queue |
|
6
|
|
|
* @package Object_Sync_Salesforce |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
defined( 'ABSPATH' ) || exit; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Object_Sync_Sf_Queue class. |
|
13
|
|
|
*/ |
|
14
|
|
|
class Object_Sync_Sf_Queue { |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Current version of the plugin |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
public $version; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The main plugin file |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
public $file; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Global object of `$wpdb`, the WordPress database |
|
32
|
|
|
* |
|
33
|
|
|
* @var object |
|
34
|
|
|
*/ |
|
35
|
|
|
public $wpdb; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The plugin's slug so we can include it when necessary |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
public $slug; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The plugin's prefix when saving options to the database |
|
46
|
|
|
* |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
public $option_prefix; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Array of what classes in the plugin can be scheduled to occur with `wp_cron` events |
|
53
|
|
|
* |
|
54
|
|
|
* @var array |
|
55
|
|
|
*/ |
|
56
|
|
|
public $schedulable_classes; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Constructor for queue class |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct() { |
|
62
|
|
|
$this->version = object_sync_for_salesforce()->version; |
|
63
|
|
|
$this->file = object_sync_for_salesforce()->file; |
|
64
|
|
|
$this->wpdb = object_sync_for_salesforce()->wpdb; |
|
65
|
|
|
$this->slug = object_sync_for_salesforce()->slug; |
|
66
|
|
|
$this->option_prefix = object_sync_for_salesforce()->option_prefix; |
|
67
|
|
|
|
|
68
|
|
|
$this->schedulable_classes = object_sync_for_salesforce()->schedulable_classes; |
|
69
|
|
|
|
|
70
|
|
|
$this->add_actions(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Add actions |
|
75
|
|
|
*/ |
|
76
|
|
|
private function add_actions() { |
|
77
|
|
|
add_filter( 'action_scheduler_queue_runner_batch_size', array( $this, 'action_scheduler_batch_size' ) ); |
|
|
|
|
|
|
78
|
|
|
add_filter( 'action_scheduler_queue_runner_concurrent_batches', array( $this, 'action_scheduler_concurrent_batches' ) ); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Set the batch size. |
|
83
|
|
|
* |
|
84
|
|
|
* @param int $batch_size how big the batch is. |
|
85
|
|
|
* @return int $batch_size |
|
86
|
|
|
*/ |
|
87
|
|
|
public function action_scheduler_batch_size( $batch_size ) { |
|
|
|
|
|
|
88
|
|
|
// default for this library is 20 so that is where we start. |
|
89
|
|
|
$batch_size = filter_var( get_option( $this->option_prefix . 'action_scheduler_batch_size', 20 ), FILTER_VALIDATE_INT ); |
|
|
|
|
|
|
90
|
|
|
return $batch_size; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Set the number of concurrent batches that can run. |
|
95
|
|
|
* |
|
96
|
|
|
* @param int $concurrent_batches how many batches can run at once. |
|
97
|
|
|
* @return int $concurrent_batches |
|
98
|
|
|
*/ |
|
99
|
|
|
public function action_scheduler_concurrent_batches( $concurrent_batches ) { |
|
|
|
|
|
|
100
|
|
|
// default for this library is 5 so that is where we start. |
|
101
|
|
|
$concurrent_batches = filter_var( get_option( $this->option_prefix . 'action_scheduler_concurrent_batches', 5 ), FILTER_VALIDATE_INT ); |
|
|
|
|
|
|
102
|
|
|
return $concurrent_batches; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Get all the schedules with their frequencies, sorted |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $unit The unit of time. |
|
109
|
|
|
* @param string $sort Which direction to sort. |
|
110
|
|
|
* @return array $this->schedulable_classes |
|
111
|
|
|
*/ |
|
112
|
|
|
public function get_frequencies( $unit = 'seconds', $sort = 'asc' ) { |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
foreach ( $this->schedulable_classes as $key => $schedule ) { |
|
115
|
|
|
$this->schedulable_classes[ $key ]['frequency'] = $this->get_frequency( $key, 'seconds' ); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
if ( 'asc' === $sort ) { |
|
119
|
|
|
uasort( |
|
120
|
|
|
$this->schedulable_classes, |
|
121
|
|
|
function ( $a, $b ) { |
|
122
|
|
|
// we want zero values at the top of an ascending sort. |
|
123
|
|
|
if ( 0 === $a['frequency'] ) { |
|
124
|
|
|
return 1; |
|
125
|
|
|
} |
|
126
|
|
|
if ( 0 === $b['frequency'] ) { |
|
127
|
|
|
return -1; |
|
128
|
|
|
} |
|
129
|
|
|
return $a['frequency'] - $b['frequency']; |
|
130
|
|
|
} |
|
131
|
|
|
); |
|
132
|
|
|
} else { |
|
133
|
|
|
uasort( |
|
134
|
|
|
$this->schedulable_classes, |
|
135
|
|
|
function ( $a, $b ) { |
|
136
|
|
|
return $b['frequency'] - $a['frequency']; |
|
137
|
|
|
} |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $this->schedulable_classes; |
|
142
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Get a single schedule item's frequency |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $name The name of the schedule. |
|
149
|
|
|
* @param string $unit The unit of time. |
|
150
|
|
|
* @return int How often it runs in that unit of time |
|
151
|
|
|
*/ |
|
152
|
|
|
public function get_frequency( $name, $unit ) { |
|
153
|
|
|
$schedule_number = get_option( $this->option_prefix . $name . '_schedule_number', '' ); |
|
|
|
|
|
|
154
|
|
|
$schedule_unit = get_option( $this->option_prefix . $name . '_schedule_unit', '' ); |
|
155
|
|
|
|
|
156
|
|
|
// make sure we have something saved in the options so it doesn't fail. |
|
157
|
|
|
if ( '' === $schedule_number ) { |
|
158
|
|
|
$schedule_number = 0; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
if ( '' === $schedule_unit ) { |
|
162
|
|
|
$schedule_unit = 'minutes'; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
switch ( $schedule_unit ) { |
|
166
|
|
|
case 'minutes': |
|
167
|
|
|
$seconds = 60; |
|
168
|
|
|
$minutes = 1; |
|
169
|
|
|
break; |
|
170
|
|
|
case 'hours': |
|
171
|
|
|
$seconds = 3600; |
|
172
|
|
|
$minutes = 60; |
|
173
|
|
|
break; |
|
174
|
|
|
case 'days': |
|
175
|
|
|
$seconds = 86400; |
|
176
|
|
|
$minutes = 1440; |
|
177
|
|
|
break; |
|
178
|
|
|
default: |
|
179
|
|
|
$seconds = 0; |
|
180
|
|
|
$minutes = 0; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
$schedule_number = filter_var( $schedule_number, FILTER_SANITIZE_NUMBER_INT ); |
|
184
|
|
|
$total = $$unit * $schedule_number; |
|
185
|
|
|
|
|
186
|
|
|
return $total; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Enqueue an action to run one time, as soon as possible |
|
191
|
|
|
* |
|
192
|
|
|
* @param string $hook The hook to trigger. |
|
193
|
|
|
* @param array $args Arguments to pass when the hook triggers. |
|
194
|
|
|
* @param string $group The group to assign this job to. |
|
195
|
|
|
* @return string The action ID. |
|
196
|
|
|
*/ |
|
197
|
|
|
public function add( $hook, $args = array(), $group = '' ) { |
|
198
|
|
|
return $this->schedule_single( time(), $hook, $args, $group ); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Schedule an action to run once at some time in the future |
|
203
|
|
|
* |
|
204
|
|
|
* @param int $timestamp When the job will run. |
|
205
|
|
|
* @param string $hook The hook to trigger. |
|
206
|
|
|
* @param array $args Arguments to pass when the hook triggers. |
|
207
|
|
|
* @param string $group The group to assign this job to. |
|
208
|
|
|
* @return string The action ID. |
|
209
|
|
|
*/ |
|
210
|
|
|
public function schedule_single( $timestamp, $hook, $args = array(), $group = '' ) { |
|
211
|
|
|
return as_schedule_single_action( $timestamp, $hook, $args, $group ); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Schedule a recurring action |
|
216
|
|
|
* |
|
217
|
|
|
* @param int $timestamp When the first instance of the job will run. |
|
218
|
|
|
* @param int $interval_in_seconds How long to wait between runs. |
|
219
|
|
|
* @param string $hook The hook to trigger. |
|
220
|
|
|
* @param array $args Arguments to pass when the hook triggers. |
|
221
|
|
|
* @param string $group The group to assign this job to. |
|
222
|
|
|
* @return string The action ID. |
|
223
|
|
|
*/ |
|
224
|
|
|
public function schedule_recurring( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '' ) { |
|
225
|
|
|
return as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args, $group ); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Schedule an action that recurs on a cron-like schedule. |
|
230
|
|
|
* |
|
231
|
|
|
* @param int $timestamp The schedule will start on or after this time. |
|
232
|
|
|
* @param string $cron_schedule A cron-link schedule string. |
|
233
|
|
|
* @see http://en.wikipedia.org/wiki/Cron |
|
234
|
|
|
* * * * * * * |
|
235
|
|
|
* ┬ ┬ ┬ ┬ ┬ ┬ |
|
236
|
|
|
* | | | | | | |
|
237
|
|
|
* | | | | | + year [optional] |
|
238
|
|
|
* | | | | +----- day of week (0 - 7) (Sunday=0 or 7) |
|
239
|
|
|
* | | | +---------- month (1 - 12) |
|
240
|
|
|
* | | +--------------- day of month (1 - 31) |
|
241
|
|
|
* | +-------------------- hour (0 - 23) |
|
242
|
|
|
* +------------------------- min (0 - 59) |
|
243
|
|
|
* @param string $hook The hook to trigger. |
|
244
|
|
|
* @param array $args Arguments to pass when the hook triggers. |
|
245
|
|
|
* @param string $group The group to assign this job to. |
|
246
|
|
|
* @return string The action ID |
|
247
|
|
|
*/ |
|
248
|
|
|
public function schedule_cron( $timestamp, $cron_schedule, $hook, $args = array(), $group = '' ) { |
|
249
|
|
|
return as_schedule_cron_action( $timestamp, $cron_schedule, $hook, $args, $group ); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Dequeue all actions with a matching hook (and optionally matching args and group) so they are not run. |
|
254
|
|
|
* |
|
255
|
|
|
* Any recurring actions with a matching hook will also be cancelled, not just the next scheduled action. |
|
256
|
|
|
* |
|
257
|
|
|
* Technically, one action in a recurring or Cron action is scheduled at any one point in time. The next |
|
258
|
|
|
* in the sequence is scheduled after the previous one is run, so only the next scheduled action needs to |
|
259
|
|
|
* be cancelled/dequeued to stop the sequence. |
|
260
|
|
|
* |
|
261
|
|
|
* @param string $hook The hook that the job will trigger. |
|
262
|
|
|
* @param array $args Args that would have been passed to the job. |
|
263
|
|
|
* @param string $group Group name. |
|
264
|
|
|
*/ |
|
265
|
|
|
public function cancel( $hook, $args = array(), $group = '' ) { |
|
266
|
|
|
as_unschedule_action( $hook, $args, $group ); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Get the date and time for the next scheduled occurence of an action with a given hook |
|
271
|
|
|
* (an optionally that matches certain args and group), if any. |
|
272
|
|
|
* |
|
273
|
|
|
* @param string $hook Hook name. |
|
274
|
|
|
* @param array $args Arguments. |
|
275
|
|
|
* @param string $group Group name. |
|
276
|
|
|
* @return timestamp|null The date and time for the next occurrence, or null if there is no pending, scheduled action for the given hook. |
|
|
|
|
|
|
277
|
|
|
*/ |
|
278
|
|
|
public function get_next( $hook, $args = null, $group = '' ) { |
|
279
|
|
|
|
|
280
|
|
|
$next_timestamp = as_next_scheduled_action( $hook, $args, $group ); |
|
281
|
|
|
|
|
282
|
|
|
if ( false !== $next_timestamp ) { |
|
283
|
|
|
return $next_timestamp; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
return null; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Find scheduled actions |
|
291
|
|
|
* |
|
292
|
|
|
* @param array $args Possible arguments, with their default values: |
|
293
|
|
|
* 'hook' => '' - the name of the action that will be triggered |
|
294
|
|
|
* 'args' => null - the args array that will be passed with the action |
|
295
|
|
|
* 'date' => null - the scheduled date of the action. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime(). Used in UTC timezone. |
|
296
|
|
|
* 'date_compare' => '<=' - operator for testing "date". accepted values are '!=', '>', '>=', '<', '<=', '=' |
|
297
|
|
|
* 'modified' => null - the date the action was last updated. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime(). Used in UTC timezone. |
|
298
|
|
|
* 'modified_compare' => '<=' - operator for testing "modified". accepted values are '!=', '>', '>=', '<', '<=', '=' |
|
299
|
|
|
* 'group' => '' - the group the action belongs to |
|
300
|
|
|
* 'status' => '' - ActionScheduler_Store::STATUS_COMPLETE or ActionScheduler_Store::STATUS_PENDING |
|
301
|
|
|
* 'claimed' => null - TRUE to find claimed actions, FALSE to find unclaimed actions, a string to find a specific claim ID |
|
302
|
|
|
* 'per_page' => 5 - Number of results to return |
|
303
|
|
|
* 'offset' => 0 |
|
304
|
|
|
* 'orderby' => 'date' - accepted values are 'hook', 'group', 'modified', or 'date' |
|
305
|
|
|
* 'order' => 'ASC'. |
|
306
|
|
|
* |
|
307
|
|
|
* @param string $return_format OBJECT, ARRAY_A, or ids. |
|
308
|
|
|
* @return array |
|
309
|
|
|
*/ |
|
310
|
|
|
public function search( $args = array(), $return_format = OBJECT ) { |
|
|
|
|
|
|
311
|
|
|
return as_get_scheduled_actions( $args, $return_format ); |
|
312
|
|
|
} |
|
313
|
|
|
} |
|
314
|
|
|
|