PluginProbe ʕ •ᴥ•ʔ
Backup Migration / 2.1.7
Backup Migration v2.1.7
2.1.7 2.1.6 2.1.5.2 trunk 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.6.1 1.4.7 1.4.8 1.4.9 1.4.9.1 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.5.1
backup-backup / includes / cron / class-task-manager.php
backup-backup / includes / cron Last commit date
tasks 3 weeks ago bootstrap.php 3 weeks ago class-abstract-task.php 3 weeks ago class-task-manager.php 3 weeks ago handler.php 3 weeks ago
class-task-manager.php
166 lines
1 <?php
2
3 // Namespace
4 namespace BMI\Plugin\CRON;
5
6 // Exit on direct access
7 if (!defined('ABSPATH')) exit;
8
9 /**
10 * TaskManager
11 *
12 * Static registry for AbstractTask instances. The manager owns three
13 * responsibilities:
14 *
15 * 1. Registry – tasks are keyed by their hook name; duplicates replace.
16 * 2. Lifecycle – boot, activate, deactivate methods map to WP events.
17 * 3. Intervals – injects custom cron_schedules entries declared by tasks.
18 *
19 * ── Typical usage ────────────────────────────────────────────────────────────
20 *
21 * // bootstrap.php (required once, from the 'init' action)
22 * TaskManager::register(new CloudSyncTask());
23 * TaskManager::boot();
24 *
25 * // activation hook
26 * TaskManager::register(new CloudSyncTask());
27 * TaskManager::on_activation();
28 *
29 * // deactivation hook
30 * TaskManager::register(new CloudSyncTask());
31 * TaskManager::on_deactivation();
32 *
33 * ─────────────────────────────────────────────────────────────────────────────
34 */
35 class TaskManager {
36
37 /**
38 * Hook-slug => task instance map.
39 *
40 * @var AbstractTask[]
41 */
42 private static $tasks = array();
43
44 /**
45 * Guards against calling boot() more than once per request.
46 * Prevents duplicate add_action() registrations.
47 *
48 * @var bool
49 */
50 private static $booted = false;
51
52 // ── Registry ───────────────────────────────────────────────────────────────
53
54 /**
55 * Register a task. If a task with the same hook is already registered,
56 * it is silently replaced (last-write-wins).
57 *
58 * @param AbstractTask $task
59 * @return void
60 */
61 public static function register(AbstractTask $task) {
62 self::$tasks[$task->get_hook()] = $task;
63 }
64
65 /**
66 * Return all registered tasks, keyed by hook name.
67 *
68 * @return AbstractTask[]
69 */
70 public static function get_tasks() {
71 return self::$tasks;
72 }
73
74 // ── Lifecycle ──────────────────────────────────────────────────────────────
75
76 /**
77 * Boot all registered tasks.
78 *
79 * What this does:
80 * - Attaches custom interval registration to the 'cron_schedules' filter.
81 * - Binds each task's run() method to its WP action hook.
82 * - Schedules every task that has no pending event (no-op otherwise).
83 *
84 * Safe to call multiple times; subsequent calls are silently ignored.
85 * Recommended priority: 20 (after the plugin's own initializer at 15).
86 *
87 * @return void
88 */
89 public static function boot() {
90 if (self::$booted) {
91 return;
92 }
93 self::$booted = true;
94
95 // Register custom cron intervals before any scheduling calls.
96 add_filter('cron_schedules', array(__CLASS__, 'filter_cron_schedules'));
97
98 foreach (self::$tasks as $task) {
99 $task->register(); // adds the WP action hook
100 $task->schedule(); // no-op when event is already queued
101 }
102 }
103
104 /**
105 * Force-(re)schedule every task.
106 *
107 * Clears any stale schedule first so the start time is always reset to
108 * get_schedule_start() at the moment of activation. Call this from the
109 * plugin's register_activation_hook callback.
110 *
111 * @return void
112 */
113 public static function on_activation() {
114 // Ensure custom intervals are available before scheduling.
115 add_filter('cron_schedules', array(__CLASS__, 'filter_cron_schedules'));
116
117 foreach (self::$tasks as $task) {
118 $task->unschedule(); // remove any stale event from a previous activation
119 $task->schedule(); // re-queue with a fresh start time
120 }
121 }
122
123 /**
124 * Unschedule every registered task.
125 *
126 * Call from register_deactivation_hook so WordPress never fires ghost
127 * cron events after the plugin has been deactivated.
128 *
129 * @return void
130 */
131 public static function on_deactivation() {
132 foreach (self::$tasks as $task) {
133 $task->unschedule();
134 }
135 }
136
137 // ── cron_schedules filter ──────────────────────────────────────────────────
138
139 /**
140 * WordPress 'cron_schedules' filter callback.
141 *
142 * Appends any custom interval definitions declared by registered tasks.
143 * Tasks that rely on a built-in interval return null from
144 * get_custom_interval_args() and are silently skipped here.
145 *
146 * @param array $schedules Existing WP cron schedules.
147 * @return array Schedules extended with task-defined intervals.
148 */
149 public static function filter_cron_schedules(array $schedules) {
150 foreach (self::$tasks as $task) {
151 $args = $task->get_custom_interval_args();
152 if ($args === null) {
153 continue;
154 }
155
156 $slug = $task->get_interval();
157 if (!isset($schedules[$slug])) {
158 $schedules[$slug] = $args;
159 }
160 }
161
162 return $schedules;
163 }
164
165 }
166