ActionScheduler.php
3 weeks ago
AutomationController.php
2 years ago
FilterHandler.php
1 month ago
RootStep.php
2 years ago
StepHandler.php
2 months ago
StepRunController.php
1 year ago
StepRunControllerFactory.php
1 year ago
StepRunLogger.php
2 months ago
StepRunLoggerFactory.php
2 years ago
StepScheduler.php
1 year ago
SubjectLoader.php
3 years ago
SubjectTransformerHandler.php
1 year ago
TriggerHandler.php
2 years ago
index.php
3 years ago
ActionScheduler.php
55 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Engine\Control; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use ActionScheduler_Action; |
| 9 | use ActionScheduler_Store; |
| 10 | |
| 11 | class ActionScheduler { |
| 12 | private const GROUP_ID = 'mailpoet-automation'; |
| 13 | |
| 14 | public function enqueue(string $hook, array $args = []): int { |
| 15 | $result = as_enqueue_async_action($hook, $args, self::GROUP_ID); |
| 16 | return is_int($result) ? $result : 0; |
| 17 | } |
| 18 | |
| 19 | public function schedule(int $timestamp, string $hook, array $args = []): int { |
| 20 | $result = as_schedule_single_action($timestamp, $hook, $args, self::GROUP_ID); |
| 21 | return is_int($result) ? $result : 0; |
| 22 | } |
| 23 | |
| 24 | public function hasScheduledAction(string $hook, array $args = []): bool { |
| 25 | return as_has_scheduled_action($hook, $args, self::GROUP_ID); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Unlike hasScheduledAction(), this only matches PENDING actions and ignores |
| 30 | * RUNNING ones. A recurring hook that reschedules itself from within its own |
| 31 | * handler must use this: while the handler runs, its own action has status |
| 32 | * RUNNING, so as_has_scheduled_action() would report the action as still |
| 33 | * scheduled and the next run would never be queued. |
| 34 | */ |
| 35 | public function hasPendingScheduledAction(string $hook, array $args = []): bool { |
| 36 | $actions = as_get_scheduled_actions([ |
| 37 | 'hook' => $hook, |
| 38 | 'args' => $args, |
| 39 | 'status' => ActionScheduler_Store::STATUS_PENDING, |
| 40 | 'group' => self::GROUP_ID, |
| 41 | 'per_page' => 1, |
| 42 | ], 'ids'); |
| 43 | return is_array($actions) && count($actions) > 0; |
| 44 | } |
| 45 | |
| 46 | /** @return ActionScheduler_Action[] */ |
| 47 | public function getScheduledActions(array $args = []): array { |
| 48 | return as_get_scheduled_actions(array_merge($args, ['group' => self::GROUP_ID])); |
| 49 | } |
| 50 | |
| 51 | public function unscheduleAction(string $hook, array $args = []): ?int { |
| 52 | return as_unschedule_action($hook, $args, self::GROUP_ID); |
| 53 | } |
| 54 | } |
| 55 |