-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathEvent.php
More file actions
342 lines (299 loc) · 9.16 KB
/
Copy pathEvent.php
File metadata and controls
342 lines (299 loc) · 9.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
/**
* Base class for cron events.
*/
namespace Crontrol\Event;
use Crontrol\Event\PHPCronEvent;
use Crontrol\Event\URLCronEvent;
use Crontrol\Event\CoreCronEvent;
use Crontrol\Event\ActionSchedulerEvent;
use Crontrol\Event\StandardEvent;
use Crontrol\Exception\UnknownScheduleException;
/**
* Base class for cron events.
*/
abstract class Event {
/**
* The hook name of the cron event.
*
* @var string
*/
public string $hook;
/**
* The Unix timestamp when the event should run.
*
* @var int
*/
public int $timestamp;
/**
* The event signature.
*
* @var string
*/
public string $sig;
/**
* The arguments to pass to the hook's callback function.
*
* @var mixed[]
*/
public array $args;
/**
* The schedule name or null for one-time events.
*
* @var string|null
*/
public $schedule;
/**
* The interval time in seconds for the schedule. Only present for recurring events.
*
* @var int|null
*/
public $interval;
/**
* Constructor.
*
* @param string $hook The hook name of the cron event.
* @param int $timestamp The Unix timestamp (UTC) when the event should run.
* @param string $sig The event signature.
* @param mixed[] $args The arguments to pass to the hook's callback function.
* @param string|null $schedule The schedule name or null for one-time events.
* @param int|null $interval The interval time in seconds for the schedule. Only present for recurring events.
*/
protected function __construct( string $hook, int $timestamp, string $sig, array $args, ?string $schedule, ?int $interval ) {
$this->hook = $hook;
$this->timestamp = $timestamp;
$this->sig = $sig;
$this->args = $args;
$this->schedule = $schedule;
$this->interval = $interval;
}
/**
* Factory method to create appropriate Event instance.
*
* @param string $hook The hook name of the cron event.
* @param int $timestamp The Unix timestamp (UTC) when the event should run.
* @param string $sig The event signature.
* @param mixed[] $args The arguments to pass to the hook's callback function.
* @param string|null $schedule The schedule name or null for one-time events.
* @param int|null $interval The interval time in seconds for the schedule. Only present for recurring events.
* @return self The appropriate Event instance.
* @phpstan-return (
* $hook is PHPCronEvent::HOOK_NAME ? PHPCronEvent :
* $hook is URLCronEvent::HOOK_NAME ? URLCronEvent :
* $hook is ActionSchedulerEvent::HOOK_NAME ? ActionSchedulerEvent :
* (CoreCronEvent|StandardEvent)
* )
*/
public static function create( string $hook, int $timestamp, string $sig, array $args, ?string $schedule, ?int $interval ): self {
if ( PHPCronEvent::HOOK_NAME === $hook ) {
return new PHPCronEvent( $hook, $timestamp, $sig, $args, $schedule, $interval );
}
if ( URLCronEvent::HOOK_NAME === $hook ) {
return new URLCronEvent( $hook, $timestamp, $sig, $args, $schedule, $interval );
}
if ( ActionSchedulerEvent::HOOK_NAME === $hook ) {
return new ActionSchedulerEvent( $hook, $timestamp, $sig, $args, $schedule, $interval );
}
if ( in_array( $hook, \Crontrol\get_all_core_hooks(), true ) ) {
return new CoreCronEvent( $hook, $timestamp, $sig, $args, $schedule, $interval );
}
return new StandardEvent( $hook, $timestamp, $sig, $args, $schedule, $interval );
}
/**
* Factory method to create a new empty Event instance with default values.
*
* @return self A new StandardEvent instance with default empty values.
*/
public static function create_new(): self {
return self::create( '', time(), '', array(), null, null );
}
/**
* Factory method to create an immediate Event instance (timestamp = 1).
*
* @param string $hook The hook name of the cron event.
* @param mixed[] $args The arguments to pass to the hook's callback function.
* @return self The appropriate Event instance set to run immediately.
*/
public static function create_immediate( string $hook, array $args = array() ): self {
return self::create( $hook, 1, '', $args, null, null );
}
/**
* Check if this is a recurring event.
*
* @return bool True if this is a recurring event, false otherwise.
*/
public function is_recurring(): bool {
return is_string( $this->schedule );
}
/**
* Get the registered callbacks for this event's hook.
*
* @return array<int,array<string,mixed>> Array of callbacks attached to the hook.
* @phpstan-return array<int,array{
* priority: int,
* callback: array<string,mixed>,
* }>
*/
public function get_callbacks(): array {
return \Crontrol\get_hook_callbacks( $this->hook );
}
/**
* Get the next run time in local timezone.
*
* @param string $format The date format string. Defaults to 'c' (ISO 8601).
* @return string The formatted date in local timezone.
*/
public function get_next_run_local( string $format = 'c' ): string {
return get_date_from_gmt( gmdate( 'Y-m-d H:i:s', $this->timestamp ), $format );
}
/**
* Get the next run time in UTC.
*
* @param string $format The date format string. Defaults to 'c' (ISO 8601).
* @return string The formatted date in UTC.
*/
public function get_next_run_utc( string $format = 'c' ): string {
return gmdate( $format, $this->timestamp );
}
/**
* Check if this is a PHP cron event.
*
* @return bool True if this is a PHP cron event, false otherwise.
*/
public function is_php_cron(): bool {
return false;
}
/**
* Check if this is a URL cron event.
*
* @return bool True if this is a URL cron event, false otherwise.
*/
public function is_url_cron(): bool {
return false;
}
/**
* Check if this is a WP Crontrol managed event (PHP or URL).
*
* @return bool True if this is a WP Crontrol managed event, false otherwise.
*/
public function is_crontrol_event(): bool {
return false;
}
/**
* Check if this event's hook is paused.
*
* @return bool True if the event's hook is paused, false otherwise.
*/
public function is_paused(): bool {
$paused = get_option( \Crontrol\PAUSED_OPTION );
if ( ! is_array( $paused ) ) {
return false;
}
return array_key_exists( $this->hook, $paused );
}
/**
* Check if this event is late (past its scheduled time by more than 10 minutes).
*
* @return bool True if the event is late, false otherwise.
*/
public function is_late(): bool {
$until = $this->timestamp - time();
return ( $until < ( 0 - ( 10 * MINUTE_IN_SECONDS ) ) );
}
/**
* Check if this event's schedule is too frequent (interval less than WP_CRON_LOCK_TIMEOUT).
*
* @return bool True if the event's schedule is too frequent, false otherwise.
*/
public function is_too_frequent(): bool {
if ( ! $this->schedule ) {
return false;
}
$schedules = \Crontrol\Schedule\get();
if ( ! isset( $schedules[ $this->schedule ] ) ) {
return false;
}
return $schedules[ $this->schedule ]['is_too_frequent'];
}
/**
* Check if this event has integrity failures (corrupted data).
*
* @return bool True if the event has integrity failures, false otherwise.
*/
public function integrity_failed(): bool {
return false;
}
/**
* Check if this event has any errors (syntax errors, URL errors, or integrity failures).
*
* @return bool True if the event has errors, false otherwise.
*/
public function has_error(): bool {
return false;
}
/**
* Check if this event is a persistent WordPress core hook.
*
* @return bool True if this is a persistent core hook, false otherwise.
*/
public function is_persistent_core_hook(): bool {
return false;
}
/**
* Get the schedule name for this event.
*
* @return string The schedule display name.
* @throws UnknownScheduleException If schedule is unknown.
*/
public function get_schedule_name(): string {
if ( ! $this->is_recurring() ) {
return __( 'Non-repeating', 'wp-crontrol' );
}
$schedules = \Crontrol\Schedule\get();
if ( isset( $schedules[ $this->schedule ] ) ) {
return isset( $schedules[ $this->schedule ]['display'] ) ? $schedules[ $this->schedule ]['display'] : $schedules[ $this->schedule ]['name'];
}
throw new UnknownScheduleException(
sprintf(
/* translators: %s: Schedule name */
__( 'Unknown (%s)', 'wp-crontrol' ),
$this->schedule
)
);
}
/**
* Check if this is a WordPress core cron event.
*
* @return bool True if this is a WordPress core cron event, false otherwise.
*/
public function is_core_cron(): bool {
return false;
}
/**
* Check if this is an Action Scheduler cron event.
*
* @return bool True if this is an Action Scheduler cron event, false otherwise.
*/
public function is_action_scheduler_cron(): bool {
return false;
}
/**
* Check if this event is protected.
*
* @return bool True if the event is protected, false otherwise.
*/
public function is_protected(): bool {
return false;
}
/**
* Check if this event is scheduled to run immediately via "Run now".
*
* Events with timestamp 1 are scheduled to run immediately and only appear
* in the event list when there's a problem with the event runner.
*
* @return bool True if the event is scheduled to run immediately, false otherwise.
*/
public function is_immediate(): bool {
return $this->timestamp === 1;
}
}