AsBackgroundJobsFacade.php
123 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\Support\Facades\ActionScheduler; |
| 4 | |
| 5 | use ActionScheduler; |
| 6 | |
| 7 | /** |
| 8 | * @see https://actionscheduler.org/ |
| 9 | * |
| 10 | * @since 3.6.0 |
| 11 | */ |
| 12 | class AsBackgroundJobsFacade |
| 13 | { |
| 14 | /** |
| 15 | * @since 3.6.0 |
| 16 | * |
| 17 | * @param string $hook The hook to trigger. |
| 18 | * @param array $args Arguments to pass when the hook triggers. |
| 19 | * @param string $group The group to assign this job to. |
| 20 | * @param bool $unique Whether the action should be unique. |
| 21 | * @param int $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255. |
| 22 | * |
| 23 | * @return int The action ID. Zero if there was an error scheduling the action. |
| 24 | */ |
| 25 | public function enqueueAsyncAction( |
| 26 | string $hook, |
| 27 | array $args, |
| 28 | string $group, |
| 29 | bool $unique = false, |
| 30 | int $priority = 10 |
| 31 | ): int { |
| 32 | /** |
| 33 | * We are checking if an action with the same hook, args, and group already was enqueued previously |
| 34 | * to prevent duplicated jobs. IMPORTANT: this is different from the $unique parameter which is used |
| 35 | * only to restrict the creation of multiple actions with the same hook. |
| 36 | */ |
| 37 | $enqueuedAction = $this->getActionByHookArgsGroup($hook, $args, $group, 'ids'); |
| 38 | if (empty($enqueuedAction)) { |
| 39 | return as_enqueue_async_action($hook, $args, $group, $unique, $priority); |
| 40 | } else { |
| 41 | return $enqueuedAction[0]; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @since 3.6.0 |
| 47 | * |
| 48 | * @param string $hook The hook to trigger. |
| 49 | * @param array $args Arguments to pass when the hook triggers. |
| 50 | * @param string $group The group to assign this job to. |
| 51 | * @param string $returnFormat OBJECT, ARRAY_A, or ids. |
| 52 | * @param string $status ActionScheduler_Store::STATUS_COMPLETE or ActionScheduler_Store::STATUS_PENDING |
| 53 | * |
| 54 | * @return array |
| 55 | */ |
| 56 | public function getActionByHookArgsGroup( |
| 57 | string $hook, |
| 58 | array $args, |
| 59 | string $group, |
| 60 | string $returnFormat = OBJECT, |
| 61 | string $status = '' |
| 62 | ): array { |
| 63 | $args = [ |
| 64 | 'hook' => $hook, |
| 65 | 'args' => $args, |
| 66 | 'group' => $group, |
| 67 | 'per_page' => 1, |
| 68 | ]; |
| 69 | |
| 70 | if ( ! empty($status)) { |
| 71 | $args['status'] = $status; |
| 72 | } |
| 73 | |
| 74 | return as_get_scheduled_actions($args, $returnFormat); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @since 3.6.0 |
| 79 | * @since 4.0.0 - switch parameter $status position with $returnFormat position |
| 80 | * |
| 81 | * @param string $group The group to assign this job to. |
| 82 | * @param string $status ActionScheduler_Store::STATUS_COMPLETE or ActionScheduler_Store::STATUS_PENDING |
| 83 | * @param string $returnFormat OBJECT, ARRAY_A, or ids. |
| 84 | * |
| 85 | * @return array |
| 86 | */ |
| 87 | public function getActionsByGroup(string $group, string $status = '', string $returnFormat = OBJECT): array |
| 88 | { |
| 89 | $args = [ |
| 90 | 'group' => $group, |
| 91 | 'per_page' => 0, |
| 92 | 'order' => 'DESC', |
| 93 | ]; |
| 94 | |
| 95 | if ( ! empty($status)) { |
| 96 | $args['status'] = $status; |
| 97 | } |
| 98 | |
| 99 | return as_get_scheduled_actions($args, $returnFormat); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @since 3.6.0 |
| 104 | * |
| 105 | * @param string $group The group to assign this job to. |
| 106 | * @param string $status ActionScheduler_Store::STATUS_COMPLETE or ActionScheduler_Store::STATUS_PENDING |
| 107 | * |
| 108 | * @return int Total deleted actions. |
| 109 | */ |
| 110 | public function deleteActionsByGroup(string $group, string $status = ''): int |
| 111 | { |
| 112 | $actions = $this->getActionsByGroup($group, $status, 'ids'); |
| 113 | |
| 114 | $deletedActions = 0; |
| 115 | foreach ($actions as $actionID) { |
| 116 | ActionScheduler::store()->delete_action($actionID); |
| 117 | $deletedActions++; |
| 118 | } |
| 119 | |
| 120 | return $deletedActions; |
| 121 | } |
| 122 | } |
| 123 |