Actions
7 months ago
ActionScheduler.php
2 months ago
RemoteExecutorHandler.php
2 years ago
index.php
3 years ago
ActionScheduler.php
54 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Cron\ActionScheduler; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\WP\Functions as WPFunctions; |
| 9 | |
| 10 | class ActionScheduler { |
| 11 | public const GROUP_ID = 'mailpoet-cron'; |
| 12 | |
| 13 | /** @var WPFunctions */ |
| 14 | private $wp; |
| 15 | |
| 16 | public function __construct( |
| 17 | WPFunctions $wp |
| 18 | ) { |
| 19 | $this->wp = $wp; |
| 20 | } |
| 21 | |
| 22 | public function scheduleRecurringAction(int $timestamp, int $interval_in_seconds, string $hook, array $args = [], bool $unique = true): int { |
| 23 | $result = as_schedule_recurring_action($timestamp, $interval_in_seconds, $hook, $args, self::GROUP_ID, $unique); |
| 24 | return is_int($result) ? $result : 0; |
| 25 | } |
| 26 | |
| 27 | public function scheduleImmediateSingleAction(string $hook, array $args = [], bool $unique = true): int { |
| 28 | $result = as_schedule_single_action($this->wp->currentTime('timestamp', true), $hook, $args, self::GROUP_ID, $unique); |
| 29 | return is_int($result) ? $result : 0; |
| 30 | } |
| 31 | |
| 32 | public function unscheduleAction(string $hook, array $args = []): ?int { |
| 33 | $id = as_unschedule_action($hook, $args, self::GROUP_ID); |
| 34 | return $id !== null ? intval($id) : null; |
| 35 | } |
| 36 | |
| 37 | public function unscheduleAllCronActions(): void { |
| 38 | // Passing only group to unschedule all by group |
| 39 | as_unschedule_all_actions('', [], self::GROUP_ID); |
| 40 | } |
| 41 | |
| 42 | public function hasScheduledAction(string $hook, array $args = []): bool { |
| 43 | return as_has_scheduled_action($hook, $args, self::GROUP_ID); |
| 44 | } |
| 45 | |
| 46 | public function isInitialized(): bool { |
| 47 | return class_exists(\ActionScheduler::class) && \ActionScheduler::is_initialized(); |
| 48 | } |
| 49 | |
| 50 | public function runAfterInitialized(callable $callback): void { |
| 51 | $this->wp->addAction('action_scheduler_init', $callback); |
| 52 | } |
| 53 | } |
| 54 |