WP_CLI
11 months ago
abstracts
11 months ago
actions
1 year ago
data-stores
11 months ago
migration
1 year ago
schedules
1 year ago
schema
11 months ago
ActionScheduler_ActionClaim.php
1 year ago
ActionScheduler_ActionFactory.php
1 year ago
ActionScheduler_AdminView.php
1 year ago
ActionScheduler_AsyncRequest_QueueRunner.php
1 year ago
ActionScheduler_Compatibility.php
1 year ago
ActionScheduler_DataController.php
11 months ago
ActionScheduler_DateTime.php
1 year ago
ActionScheduler_Exception.php
3 years ago
ActionScheduler_FatalErrorMonitor.php
1 year ago
ActionScheduler_InvalidActionException.php
1 year ago
ActionScheduler_ListTable.php
1 year ago
ActionScheduler_LogEntry.php
1 year ago
ActionScheduler_NullLogEntry.php
1 year ago
ActionScheduler_OptionLock.php
1 year ago
ActionScheduler_QueueCleaner.php
1 year ago
ActionScheduler_QueueRunner.php
1 year ago
ActionScheduler_RecurringActionScheduler.php
11 months ago
ActionScheduler_SystemInformation.php
1 year ago
ActionScheduler_Versions.php
1 year ago
ActionScheduler_WPCommentCleaner.php
1 year ago
ActionScheduler_wcSystemStatus.php
11 months ago
ActionScheduler_RecurringActionScheduler.php
82 lines
| 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 | if ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) { |
| 28 | add_action( 'action_scheduler_init', array( $this, 'schedule_recurring_scheduler_hook' ) ); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Schedule the recurring `action_scheduler_ensure_recurring_actions` action if not already scheduled. |
| 34 | * |
| 35 | * @return void |
| 36 | */ |
| 37 | public function schedule_recurring_scheduler_hook(): void { |
| 38 | if ( false === wp_cache_get( 'as_is_ensure_recurring_actions_scheduled' ) ) { |
| 39 | if ( ! as_has_scheduled_action( self::RUN_SCHEDULED_RECURRING_ACTIONS_HOOK ) ) { |
| 40 | as_schedule_recurring_action( |
| 41 | time(), |
| 42 | DAY_IN_SECONDS, |
| 43 | self::RUN_SCHEDULED_RECURRING_ACTIONS_HOOK, |
| 44 | [], |
| 45 | 'ActionScheduler', |
| 46 | true, |
| 47 | 20 |
| 48 | ); |
| 49 | } |
| 50 | wp_cache_set( 'as_is_ensure_recurring_actions_scheduled', true, HOUR_IN_SECONDS ); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Trigger the hook to allow other plugins to schedule their recurring actions. |
| 56 | * |
| 57 | * @return void |
| 58 | */ |
| 59 | public function run_recurring_scheduler_hook(): void { |
| 60 | /** |
| 61 | * Fires to allow extensions to verify and ensure their recurring actions are scheduled. |
| 62 | * |
| 63 | * This action is scheduled to trigger once every 24 hrs for the purpose of having 3rd party plugins verify that |
| 64 | * any previously scheduled recurring actions are still scheduled. Because recurring actions could stop getting |
| 65 | * rescheduled by default due to excessive failures, database issues, or other interruptions, extensions can use |
| 66 | * this hook to check for the existence of their recurring actions and reschedule them if necessary. |
| 67 | * |
| 68 | * Example usage: |
| 69 | * |
| 70 | * add_action('action_scheduler_ensure_recurring_actions', function() { |
| 71 | * // Check if the recurring action is scheduled, and reschedule if missing. |
| 72 | * if ( ! as_has_scheduled_action('my_recurring_action') ) { |
| 73 | * as_schedule_recurring_action( time(), HOUR_IN_SECONDS, 'my_recurring_action' ); |
| 74 | * } |
| 75 | * }); |
| 76 | * |
| 77 | * @since 3.9.3 |
| 78 | */ |
| 79 | do_action( 'action_scheduler_ensure_recurring_actions' ); |
| 80 | } |
| 81 | } |
| 82 |