/
EventsLibrary.php
258 lines (194 loc) · 5.86 KB
/
EventsLibrary.php
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
<?php
/**
* Events Library class
* Handles DB operations on events
*
* @package advanced-cron-manager
*/
namespace underDEV\AdvancedCronManager\Cron;
use underDEV\Utils;
/**
* EventsLibrary class
*/
class EventsLibrary {
/**
* Schedules class
*
* @var instance of underDEV\AdvancedCronManage\Cron\Schedules
*/
public $schedules;
/**
* Events class
*
* @var instance of underDEV\AdvancedCronManage\Cron\Events
*/
private $events;
/**
* Option name
*
* @var string
*/
private $paused_option_name;
/**
* Constructor
*
* @param Schedules $schedules Schedules object.
* @param Events $events Events object.
*/
public function __construct( Schedules $schedules, Events $events ) {
$this->schedules = $schedules;
$this->events = $events;
$this->paused_option_name = 'acm_paused_events';
}
/**
* Registers paused events
*
* @param array $events array of registered events.
* @return array array of both registered and paused events
*/
public function register_paused( $events ) {
$paused_events = get_option( $this->paused_option_name, array() );
foreach ( $paused_events as $paused_event ) {
$events[] = new Element\Event( $paused_event['hook'], $paused_event['schedule_slug'], $paused_event['interval'], $paused_event['args'], $paused_event['execution_timestamp'], $this->events->is_protected( $paused_event['hook'] ), true );
}
return $events;
}
/**
* Inserts new event
*
* @param string $hook action hook name.
* @param int $execution_timestamp UTC timestamp for first execution.
* @param string $schedule_slug Schedule slug.
* @param array $args arguments.
* @param boolean $new if event is new.
* @return mixed array with errors on error or true
*/
public function insert( $hook, $execution_timestamp, $schedule_slug, $args, $new = true ) {
$errors = array();
if ( empty( $hook ) ) {
$errors[] = __( 'Please, provide a hook for your Event', 'advanced-cron-manager' );
}
$schedule = $this->schedules->get_schedule( $schedule_slug );
if ( $schedule->slug !== $schedule_slug ) {
// Translators: schedule slug.
$errors[] = sprintf( __( 'Schedule "%s" cannot be found', 'advanced-cron-manager' ), $schedule_slug );
}
if ( ! empty( $errors ) ) {
return $errors;
}
if ( $schedule->slug === $this->schedules->get_single_event_schedule()->slug ) {
wp_schedule_single_event( $execution_timestamp, $hook, $args );
} else {
wp_schedule_event( $execution_timestamp, $schedule->slug, $hook, $args );
}
if ( $new ) {
do_action( 'advanced-cron-manager/event/scheduled', $hook, $execution_timestamp, $schedule, $args );
}
return true;
}
/**
* Removes (unschedules) the event
*
* @param mixed $thing event hash or Event object.
* @param boolean $permanently if unschedule is permanent.
* @return mixed array with errors on error or true
*/
public function unschedule( $thing, $permanently = true ) {
$errors = array();
if ( is_string( $thing ) ) {
$event = $this->events->get_event_by_hash( $thing );
} else {
$event = $thing;
}
if ( false === $event ) {
$errors[] = __( 'Event not found and cannot be unscheduled', 'advanced-cron-manager' );
return $errors;
}
wp_unschedule_event( $event->next_call, $event->hook, $event->args );
if ( $permanently ) {
do_action( 'advanced-cron-manager/event/unscheduled', $event );
}
return true;
}
/**
* Pauses the event
*
* @param mixed $thing event hash or Event object.
* @return mixed array with errors on error or true
*/
public function pause( $thing ) {
$errors = array();
if ( is_string( $thing ) ) {
$event = $this->events->get_event_by_hash( $thing );
} else {
$event = $thing;
}
if ( false === $event ) {
$errors[] = __( 'Event not found and cannot be paused', 'advanced-cron-manager' );
}
if ( $event->protected ) {
$errors[] = __( 'Event is protected and cannot be paused', 'advanced-cron-manager' );
}
if ( ! empty( $errors ) ) {
return $errors;
}
// add to paused option.
$this->add_to_paused( $event );
// unschedule.
$this->unschedule( $event, false );
return true;
}
/**
* Unpauses the event
*
* @param mixed $thing event hash or Event object.
* @return mixed array with errors on error or true
*/
public function unpause( $thing ) {
$errors = array();
if ( is_string( $thing ) ) {
$event = $this->events->get_event_by_hash( $thing );
} else {
$event = $thing;
}
if ( false === $event ) {
$errors[] = __( 'Event not found and cannot be unpaused', 'advanced-cron-manager' );
}
if ( ! empty( $errors ) ) {
return $errors;
}
// remove from paused option.
$this->remove_from_paused( $event );
// schedule.
$result = $this->insert( $event->hook, $event->next_call, $event->schedule, $event->args, false );
return $result;
}
/**
* Adds an event to paused events option
*
* @param object $event Event object.
*/
public function add_to_paused( $event ) {
$paused_events = get_option( $this->paused_option_name, array() );
$paused_events[ $event->hash ] = array(
'hook' => $event->hook,
'interval' => $event->interval,
'execution_timestamp' => $event->next_call,
'schedule_slug' => $event->schedule,
'args' => $event->args,
);
update_option( $this->paused_option_name, $paused_events );
}
/**
* Removes an event from paused events option
*
* @param object $event Event object.
*/
public function remove_from_paused( $event ) {
$paused_events = get_option( $this->paused_option_name, array() );
if ( isset( $paused_events[ $event->hash ] ) ) {
unset( $paused_events[ $event->hash ] );
update_option( $this->paused_option_name, $paused_events );
}
}
}