This repository has been archived by the owner on Mar 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
DB_Store.php
636 lines (536 loc) · 18.8 KB
/
DB_Store.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
<?php
namespace Action_Scheduler\Custom_Tables;
use ActionScheduler_Action;
use ActionScheduler_ActionClaim;
use ActionScheduler_CanceledAction;
use ActionScheduler_FinishedAction;
use ActionScheduler_NullAction;
use ActionScheduler_NullSchedule;
use ActionScheduler_Store;
use ActionScheduler_TimezoneHelper;
class DB_Store extends ActionScheduler_Store {
/**
* @codeCoverageIgnore
*/
public function init() {
$table_maker = new DB_Store_Table_Maker();
$table_maker->register_tables();
}
public function save_action( ActionScheduler_Action $action, \DateTime $date = null ) {
try {
/** @var \wpdb $wpdb */
global $wpdb;
$data = [
'hook' => $action->get_hook(),
'status' => ( $action->is_finished() ? self::STATUS_COMPLETE : self::STATUS_PENDING ),
'scheduled_date_gmt' => $this->get_scheduled_date_string( $action, $date ),
'scheduled_date_local' => $this->get_scheduled_date_string_local( $action, $date ),
'args' => json_encode( $action->get_args() ),
'schedule' => serialize( $action->get_schedule() ),
'group_id' => $this->get_group_id( $action->get_group() ),
];
$wpdb->insert( $wpdb->actionscheduler_actions, $data );
$action_id = $wpdb->insert_id;
if ( is_wp_error( $action_id ) ) {
throw new RuntimeException( $action_id->get_error_message() );
}
elseif ( empty( $action_id ) ) {
throw new RuntimeException( $wpdb->last_error ? $wpdb->last_error : __( 'Database error.', 'action-scheduler' ) );
}
do_action( 'action_scheduler_stored_action', $action_id );
return $action_id;
} catch ( \Exception $e ) {
throw new \RuntimeException( sprintf( __( 'Error saving action: %s', 'action-scheduler' ), $e->getMessage() ), 0 );
}
}
/**
* Get a group's ID based on its name/slug.
*
* @param string $slug The string name of a group.
* @param bool $create_if_not_exists Whether to create the group if it does not already exist. Default, true - create the group.
* @return int The group's ID, if it exists or is created, or 0 if it does not exist and is not created.
*/
protected function get_group_id( $slug, $create_if_not_exists = true ) {
if ( empty( $slug ) ) {
return 0;
}
/** @var \wpdb $wpdb */
global $wpdb;
$group_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT group_id FROM {$wpdb->actionscheduler_groups} WHERE slug=%s", $slug ) );
if ( empty( $group_id ) && $create_if_not_exists ) {
$group_id = $this->create_group( $slug );
}
return $group_id;
}
protected function create_group( $slug ) {
/** @var \wpdb $wpdb */
global $wpdb;
$wpdb->insert( $wpdb->actionscheduler_groups, [ 'slug' => $slug ] );
return (int) $wpdb->insert_id;
}
public function fetch_action( $action_id ) {
/** @var \wpdb $wpdb */
global $wpdb;
$data = $wpdb->get_row( $wpdb->prepare(
"SELECT a.*, g.slug AS `group` FROM {$wpdb->actionscheduler_actions} a LEFT JOIN {$wpdb->actionscheduler_groups} g ON a.group_id=g.group_id WHERE a.action_id=%d",
$action_id
) );
if ( empty( $data ) ) {
return $this->get_null_action();
}
return $this->make_action_from_db_record( $data );
}
protected function get_null_action() {
return new ActionScheduler_NullAction();
}
protected function make_action_from_db_record( $data ) {
$hook = $data->hook;
$args = json_decode( $data->args, true );
$schedule = unserialize( $data->schedule );
if ( empty( $schedule ) ) {
$schedule = new ActionScheduler_NullSchedule();
}
$group = $data->group ? $data->group : '';
if ( $data->status == self::STATUS_PENDING ) {
$action = new ActionScheduler_Action( $hook, $args, $schedule, $group );
} elseif ( $data->status == self::STATUS_CANCELED ) {
$action = new ActionScheduler_CanceledAction( $hook, $args, $schedule, $group );
} else {
$action = new ActionScheduler_FinishedAction( $hook, $args, $schedule, $group );
}
return $action;
}
/**
* @param string $hook
* @param array $params
*
* @return string ID of the next action matching the criteria or NULL if not found
*/
public function find_action( $hook, $params = [] ) {
$params = wp_parse_args( $params, [
'args' => null,
'status' => self::STATUS_PENDING,
'group' => '',
] );
/** @var wpdb $wpdb */
global $wpdb;
$query = "SELECT a.action_id FROM {$wpdb->actionscheduler_actions} a";
$args = [];
if ( ! empty( $params[ 'group' ] ) ) {
$query .= " INNER JOIN {$wpdb->actionscheduler_groups} g ON g.group_id=a.group_id AND g.slug=%s";
$args[] = $params[ 'group' ];
}
$query .= " WHERE a.hook=%s";
$args[] = $hook;
if ( ! is_null( $params[ 'args' ] ) ) {
$query .= " AND a.args=%s";
$args[] = json_encode( $params[ 'args' ] );
}
$order = 'ASC';
if ( ! empty( $params[ 'status' ] ) ) {
$query .= " AND a.status=%s";
$args[] = $params[ 'status' ];
if ( self::STATUS_PENDING == $params[ 'status' ] ) {
$order = 'ASC'; // Find the next action that matches
} else {
$order = 'DESC'; // Find the most recent action that matches
}
}
$query .= " ORDER BY scheduled_date_gmt $order LIMIT 1";
$query = $wpdb->prepare( $query, $args );
$id = $wpdb->get_var( $query );
return $id;
}
/**
* Returns the SQL statement to query (or count) actions.
*
* @param array $query Filtering options
* @param string $select_or_count Whether the SQL should select and return the IDs or just the row count
*
* @return string SQL statement. The returned SQL is already properly escaped.
*/
protected function get_query_actions_sql( array $query, $select_or_count = 'select' ) {
if ( ! in_array( $select_or_count, array( 'select', 'count' ) ) ) {
throw new \InvalidArgumentException( __( 'Invalid valud for select or count parameter. Cannot query actions.', 'action-scheduler' ) );
}
$query = wp_parse_args( $query, [
'hook' => '',
'args' => null,
'date' => null,
'date_compare' => '<=',
'modified' => null,
'modified_compare' => '<=',
'group' => '',
'status' => '',
'claimed' => null,
'per_page' => 5,
'offset' => 0,
'orderby' => 'date',
'order' => 'ASC',
] );
/** @var \wpdb $wpdb */
global $wpdb;
$sql = ( 'count' === $select_or_count ) ? 'SELECT count(a.action_id)' : 'SELECT a.action_id';
$sql .= " FROM {$wpdb->actionscheduler_actions} a";
$sql_params = [];
if ( ! empty( $query[ 'group' ] ) || 'group' === $query[ 'orderby' ] ) {
$sql .= " LEFT JOIN {$wpdb->actionscheduler_groups} g ON g.group_id=a.group_id";
}
$sql .= " WHERE 1=1";
if ( ! empty( $query[ 'group' ] ) ) {
$sql .= " AND g.slug=%s";
$sql_params[] = $query[ 'group' ];
}
if ( $query[ 'hook' ] ) {
$sql .= " AND a.hook=%s";
$sql_params[] = $query[ 'hook' ];
}
if ( ! is_null( $query[ 'args' ] ) ) {
$sql .= " AND a.args=%s";
$sql_params[] = json_encode( $query[ 'args' ] );
}
if ( $query[ 'status' ] ) {
$sql .= " AND a.status=%s";
$sql_params[] = $query[ 'status' ];
}
if ( $query[ 'date' ] instanceof \DateTime ) {
$date = clone $query[ 'date' ];
$date->setTimezone( new \DateTimeZone( 'UTC' ) );
$date_string = $date->format( 'Y-m-d H:i:s' );
$comparator = $this->validate_sql_comparator( $query[ 'date_compare' ] );
$sql .= " AND a.scheduled_date_gmt $comparator %s";
$sql_params[] = $date_string;
}
if ( $query[ 'modified' ] instanceof \DateTime ) {
$modified = clone $query[ 'modified' ];
$modified->setTimezone( new \DateTimeZone( 'UTC' ) );
$date_string = $modified->format( 'Y-m-d H:i:s' );
$comparator = $this->validate_sql_comparator( $query[ 'modified_compare' ] );
$sql .= " AND a.last_attempt_gmt $comparator %s";
$sql_params[] = $date_string;
}
if ( $query[ 'claimed' ] === true ) {
$sql .= " AND a.claim_id != 0";
} elseif ( $query[ 'claimed' ] === false ) {
$sql .= " AND a.claim_id = 0";
} elseif ( ! is_null( $query[ 'claimed' ] ) ) {
$sql .= " AND a.claim_id = %d";
$sql_params[] = $query[ 'claimed' ];
}
if ( ! empty( $query['search'] ) ) {
$sql .= " AND (a.hook LIKE %s OR a.args LIKE %s";
for( $i = 0; $i < 2; $i++ ) {
$sql_params[] = sprintf( '%%%s%%', $query['search'] );
}
$search_claim_id = (int) $query['search'];
if ( $search_claim_id ) {
$sql .= ' OR a.claim_id = %d';
$sql_params[] = $search_claim_id;
}
$sql .= ')';
}
if ( 'select' === $select_or_count ) {
switch ( $query['orderby'] ) {
case 'hook':
$orderby = 'a.hook';
break;
case 'group':
$orderby = 'g.slug';
break;
case 'modified':
$orderby = 'a.last_attempt_gmt';
break;
case 'date':
default:
$orderby = 'a.scheduled_date_gmt';
break;
}
if ( strtoupper( $query[ 'order' ] ) == 'ASC' ) {
$order = 'ASC';
} else {
$order = 'DESC';
}
$sql .= " ORDER BY $orderby $order";
if ( $query[ 'per_page' ] > 0 ) {
$sql .= " LIMIT %d, %d";
$sql_params[] = $query[ 'offset' ];
$sql_params[] = $query[ 'per_page' ];
}
}
if ( ! empty( $sql_params ) ) {
$sql = $wpdb->prepare( $sql, $sql_params );
}
return $sql;
}
/**
* @param array $query
* @param string $query_type Whether to select or count the results. Default, select.
* @return null|string|array The IDs of actions matching the query
*/
public function query_actions( $query = [], $query_type = 'select' ) {
/** @var wpdb $wpdb */
global $wpdb;
$sql = $this->get_query_actions_sql( $query, $query_type );
return ( 'count' === $query_type ) ? $wpdb->get_var( $sql ) : $wpdb->get_col( $sql );
}
/**
* Get a count of all actions in the store, grouped by status
*
* @return array Set of 'status' => int $count pairs for statuses with 1 or more actions of that status.
*/
public function action_counts() {
global $wpdb;
$sql = "SELECT a.status, count(a.status) as 'count'";
$sql .= " FROM {$wpdb->actionscheduler_actions} a";
$sql .= " GROUP BY a.status";
$actions_count_by_status = array();
$action_stati_and_labels = $this->get_status_labels();
foreach ( $wpdb->get_results( $sql ) as $action_data ) {
// Ignore any actions with invalid status
if ( array_key_exists( $action_data->status, $action_stati_and_labels ) ) {
$actions_count_by_status[ $action_data->status ] = $action_data->count;
}
}
return $actions_count_by_status;
}
/**
* @param string $action_id
*
* @throws \InvalidArgumentException
* @return void
*/
public function cancel_action( $action_id ) {
/** @var \wpdb $wpdb */
global $wpdb;
$updated = $wpdb->update(
$wpdb->actionscheduler_actions,
[ 'status' => self::STATUS_CANCELED ],
[ 'action_id' => $action_id ],
[ '%s' ],
[ '%d' ]
);
if ( empty( $updated ) ) {
throw new \InvalidArgumentException( sprintf( __( 'Unidentified action %s', 'action-scheduler' ), $action_id ) );
}
do_action( 'action_scheduler_canceled_action', $action_id );
}
public function delete_action( $action_id ) {
/** @var \wpdb $wpdb */
global $wpdb;
$deleted = $wpdb->delete( $wpdb->actionscheduler_actions, [ 'action_id' => $action_id ], [ '%d' ] );
if ( empty( $deleted ) ) {
throw new \InvalidArgumentException( sprintf( __( 'Unidentified action %s', 'action-scheduler' ), $action_id ) );
}
do_action( 'action_scheduler_deleted_action', $action_id );
}
/**
* @param string $action_id
*
* @throws \InvalidArgumentException
* @return \DateTime The local date the action is scheduled to run, or the date that it ran.
*/
public function get_date( $action_id ) {
$date = $this->get_date_gmt( $action_id );
ActionScheduler_TimezoneHelper::set_local_timezone( $date );
return $date;
}
/**
* @param string $action_id
*
* @throws \InvalidArgumentException
* @return \DateTime The GMT date the action is scheduled to run, or the date that it ran.
*/
protected function get_date_gmt( $action_id ) {
/** @var \wpdb $wpdb */
global $wpdb;
$record = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->actionscheduler_actions} WHERE action_id=%d", $action_id ) );
if ( empty( $record ) ) {
throw new \InvalidArgumentException( sprintf( __( 'Unidentified action %s', 'action-scheduler' ), $action_id ) );
}
if ( $record->status == self::STATUS_PENDING ) {
return as_get_datetime_object( $record->scheduled_date_gmt );
} else {
return as_get_datetime_object( $record->last_attempt_gmt );
}
}
/**
* @param int $max_actions
* @param \DateTime $before_date Jobs must be schedule before this date. Defaults to now.
*
* @return ActionScheduler_ActionClaim
*/
public function stake_claim( $max_actions = 10, \DateTime $before_date = null, $hooks = array(), $group = '' ) {
$claim_id = $this->generate_claim_id();
$this->claim_actions( $claim_id, $max_actions, $before_date, $hooks, $group );
$action_ids = $this->find_actions_by_claim_id( $claim_id );
return new ActionScheduler_ActionClaim( $claim_id, $action_ids );
}
protected function generate_claim_id() {
/** @var \wpdb $wpdb */
global $wpdb;
$now = as_get_datetime_object();
$wpdb->insert( $wpdb->actionscheduler_claims, [ 'date_created_gmt' => $now->format( 'Y-m-d H:i:s' ) ] );
return $wpdb->insert_id;
}
/**
* @param string $claim_id
* @param int $limit
* @param \DateTime $before_date Should use UTC timezone.
*
* @return int The number of actions that were claimed
* @throws \RuntimeException
*/
protected function claim_actions( $claim_id, $limit, \DateTime $before_date = null, $hooks = array(), $group = '' ) {
/** @var \wpdb $wpdb */
global $wpdb;
$now = as_get_datetime_object();
$date = is_null( $before_date ) ? $now : clone $before_date;
// can't use $wpdb->update() because of the <= condition
$update = "UPDATE {$wpdb->actionscheduler_actions} SET claim_id=%d, last_attempt_gmt=%s, last_attempt_local=%s";
$params = array(
$claim_id,
$now->format( 'Y-m-d H:i:s' ),
current_time( 'mysql' ),
);
$where = "WHERE claim_id = 0 AND scheduled_date_gmt <= %s AND status=%s";
$params[] = $date->format( 'Y-m-d H:i:s' );
$params[] = self::STATUS_PENDING;
if ( ! empty( $hooks ) ) {
$placeholders = array_fill( 0, count( $hooks ), '%s' );
$where .= ' AND hook IN (' . join( ', ', $placeholders ) . ')';
$params = array_merge( $params, array_values( $hooks ) );
}
if ( ! empty( $group ) ) {
$group_id = $this->get_group_id( $group, false );
// throw exception if no matching group found, this matches ActionScheduler_wpPostStore's behaviour
if ( empty( $group_id ) ) {
throw new InvalidArgumentException( sprintf( __( 'The group "%s" does not exist.', 'action-scheduler' ), $group ) );
}
$where .= ' AND group_id = %d';
$params[] = $group_id;
}
$order = "ORDER BY attempts ASC, scheduled_date_gmt ASC, action_id ASC LIMIT %d";
$params[] = $limit;
$sql = $wpdb->prepare( "{$update} {$where} {$order}", $params );
$rows_affected = $wpdb->query( $sql );
if ( $rows_affected === false ) {
throw new \RuntimeException( __( 'Unable to claim actions. Database error.', 'action-scheduler' ) );
}
return (int) $rows_affected;
}
/**
* @return int
*/
public function get_claim_count() {
global $wpdb;
$sql = "SELECT COUNT(DISTINCT claim_id) FROM {$wpdb->actionscheduler_actions} WHERE claim_id != 0 AND status IN ( %s, %s)";
$sql = $wpdb->prepare( $sql, [ self::STATUS_PENDING, self::STATUS_RUNNING ] );
return (int) $wpdb->get_var( $sql );
}
/**
* Return an action's claim ID, as stored in the claim_id column
*
* @param string $action_id
* @return mixed
*/
public function get_claim_id( $action_id ) {
/** @var \wpdb $wpdb */
global $wpdb;
$sql = "SELECT claim_id FROM {$wpdb->actionscheduler_actions} WHERE action_id=%d";
$sql = $wpdb->prepare( $sql, $action_id );
return (int) $wpdb->get_var( $sql );
}
/**
* @param string $claim_id
*
* @return int[]
*/
public function find_actions_by_claim_id( $claim_id ) {
/** @var \wpdb $wpdb */
global $wpdb;
$sql = "SELECT action_id FROM {$wpdb->actionscheduler_actions} WHERE claim_id=%d";
$sql = $wpdb->prepare( $sql, $claim_id );
$action_ids = $wpdb->get_col( $sql );
return array_map( 'intval', $action_ids );
}
public function release_claim( ActionScheduler_ActionClaim $claim ) {
/** @var \wpdb $wpdb */
global $wpdb;
$wpdb->update( $wpdb->actionscheduler_actions, [ 'claim_id' => 0 ], [ 'claim_id' => $claim->get_id() ], [ '%d' ], [ '%d' ] );
$wpdb->delete( $wpdb->actionscheduler_claims, [ 'claim_id' => $claim->get_id() ], [ '%d' ] );
}
/**
* @param string $action_id
*
* @return void
*/
public function unclaim_action( $action_id ) {
/** @var \wpdb $wpdb */
global $wpdb;
$wpdb->update(
$wpdb->actionscheduler_actions,
[ 'claim_id' => 0 ],
[ 'action_id' => $action_id ],
[ '%s' ],
[ '%d' ]
);
}
public function mark_failure( $action_id ) {
/** @var \wpdb $wpdb */
global $wpdb;
$updated = $wpdb->update(
$wpdb->actionscheduler_actions,
[ 'status' => self::STATUS_FAILED ],
[ 'action_id' => $action_id ],
[ '%s' ],
[ '%d' ]
);
if ( empty( $updated ) ) {
throw new \InvalidArgumentException( sprintf( __( 'Unidentified action %s', 'action-scheduler' ), $action_id ) );
}
}
/**
* @param string $action_id
*
* @return void
*/
public function log_execution( $action_id ) {
/** @var \wpdb $wpdb */
global $wpdb;
$sql = "UPDATE {$wpdb->actionscheduler_actions} SET attempts = attempts+1, status=%s, last_attempt_gmt = %s, last_attempt_local = %s WHERE action_id = %d";
$sql = $wpdb->prepare( $sql, self::STATUS_RUNNING, current_time( 'mysql', true ), current_time( 'mysql' ), $action_id );
$wpdb->query( $sql );
}
public function mark_complete( $action_id ) {
/** @var \wpdb $wpdb */
global $wpdb;
$updated = $wpdb->update(
$wpdb->actionscheduler_actions,
[
'status' => self::STATUS_COMPLETE,
'last_attempt_gmt' => current_time( 'mysql', true ),
'last_attempt_local' => current_time( 'mysql' ),
],
[ 'action_id' => $action_id ],
[ '%s' ],
[ '%d' ]
);
if ( empty( $updated ) ) {
throw new \InvalidArgumentException( sprintf( __( 'Unidentified action %s', 'action-scheduler' ), $action_id ) );
}
}
public function get_status( $action_id ) {
/** @var \wpdb $wpdb */
global $wpdb;
$sql = "SELECT status FROM {$wpdb->actionscheduler_actions} WHERE action_id=%d";
$sql = $wpdb->prepare( $sql, $action_id );
$status = $wpdb->get_var( $sql );
if ( $status === null ) {
throw new \InvalidArgumentException( __( 'Invalid action ID. No status found.', 'action-scheduler' ) );
} elseif ( empty( $status ) ) {
throw new \RuntimeException( __( 'Unknown status found for action.', 'action-scheduler' ) );
} else {
return $status;
}
}
}