| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class ActionScheduler_RecurringActionScheduler |
| 5 |
* |
| 6 |
* This class ensures that the `action_scheduler_ensure_recurring_actions` hook is triggered on a daily interval. This |
| 7 |
* simplifies the process for other plugins to register their recurring actions without requiring each plugin to query |
| 8 |
* or schedule actions independently on every request. |
| 9 |
*/ |
| 10 |
class ActionScheduler_RecurringActionScheduler { |
| 11 |
|
| 12 |
/** |
| 13 |
* @var string The hook of the scheduled recurring action that is run to trigger the |
| 14 |
* `action_scheduler_ensure_recurring_actions` hook that plugins should use. We can't directly have the |
| 15 |
* scheduled action hook be the hook plugins should use because the actions will show as failed if no plugin |
| 16 |
* was actively hooked into it. |
| 17 |
*/ |
| 18 |
private const RUN_SCHEDULED_RECURRING_ACTIONS_HOOK = 'action_scheduler_run_recurring_actions_schedule_hook'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Initialize the instance. Should only be run on a single instance per request. |
| 22 |
* |
| 23 |
* @return void |
| 24 |
*/ |
| 25 |
public function init(): void { |
| 26 |
add_action( self::RUN_SCHEDULED_RECURRING_ACTIONS_HOOK, array( $this, 'run_recurring_scheduler_hook' ) ); |
| 27 |
// Also run the check during queue processing, so installs without admin traffic still schedule the recurring action. |
| 28 |
add_action( 'action_scheduler_before_process_queue', array( $this, 'schedule_recurring_scheduler_hook' ) ); |
| 29 |
if ( is_admin() && ! wp_doing_ajax() && ! wp_is_serving_rest_request() ) { |
| 30 |
add_action( 'action_scheduler_init', array( $this, 'schedule_recurring_scheduler_hook' ) ); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Schedule the recurring `action_scheduler_ensure_recurring_actions` action if not already scheduled. |
| 36 |
* |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function schedule_recurring_scheduler_hook(): void { |
| 40 |
if ( false === get_transient( 'as_is_ensure_recurring_actions_scheduled' ) ) { |
| 41 |
if ( ! as_has_scheduled_action( self::RUN_SCHEDULED_RECURRING_ACTIONS_HOOK ) ) { |
| 42 |
$date = ActionScheduler_TimezoneHelper::set_local_timezone( new DateTime() )->modify( 'tomorrow 3am' ); |
| 43 |
as_schedule_recurring_action( |
| 44 |
$date->getTimestamp(), |
| 45 |
DAY_IN_SECONDS, |
| 46 |
self::RUN_SCHEDULED_RECURRING_ACTIONS_HOOK, |
| 47 |
[], |
| 48 |
'ActionScheduler', |
| 49 |
true, |
| 50 |
20 |
| 51 |
); |
| 52 |
} |
| 53 |
set_transient( 'as_is_ensure_recurring_actions_scheduled', true, HOUR_IN_SECONDS ); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Trigger the hook to allow other plugins to schedule their recurring actions. |
| 59 |
* |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public function run_recurring_scheduler_hook(): void { |
| 63 |
/** |
| 64 |
* Fires to allow extensions to verify and ensure their recurring actions are scheduled. |
| 65 |
* |
| 66 |
* This action is scheduled to trigger once every 24 hrs for the purpose of having 3rd party plugins verify that |
| 67 |
* any previously scheduled recurring actions are still scheduled. Because recurring actions could stop getting |
| 68 |
* rescheduled by default due to excessive failures, database issues, or other interruptions, extensions can use |
| 69 |
* this hook to check for the existence of their recurring actions and reschedule them if necessary. |
| 70 |
* |
| 71 |
* Example usage: |
| 72 |
* |
| 73 |
* add_action('action_scheduler_ensure_recurring_actions', function() { |
| 74 |
* // Check if the recurring action is scheduled, and reschedule if missing. |
| 75 |
* if ( ! as_has_scheduled_action('my_recurring_action') ) { |
| 76 |
* as_schedule_recurring_action( time(), HOUR_IN_SECONDS, 'my_recurring_action' ); |
| 77 |
* } |
| 78 |
* }); |
| 79 |
* |
| 80 |
* @since 3.9.3 |
| 81 |
*/ |
| 82 |
do_action( 'action_scheduler_ensure_recurring_actions' ); |
| 83 |
} |
| 84 |
} |
| 85 |
|