-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-queues.php
More file actions
222 lines (194 loc) · 5.77 KB
/
wp-queues.php
File metadata and controls
222 lines (194 loc) · 5.77 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
<?php
/**
* WP Queues
*
* Run queues and background jobs and tasks in WordPress
*
* @link github.com/elhardoum/wp-queues
* @version 1.0
*/
class WP_Queues
{
/**
* Give your WP_Queues a unique identifier
* helps isolate filters and options
*/
public $unique = 'my_schedules';
/**
* WP_Queues at run time register actions from
* a certain range. Define this range by giving
* the start number and end number
*
* The point from this is not to have 2 events
* conflict with each other, while we assign a
* random number (from your range) to their
* hook and meta.
*/
public $rand_min = 1;
/**
* WP_Queues at run time register actions from
* a certain range. Define this range by giving
* the start number and end number
*
* The point from this is not to have 2 events
* conflict with each other, while we assign a
* random number (from your range) to their
* hook and meta.
*/
public $rand_max = 100;
/**
* You can still specify the number of seconds
* after which the event will be executed, but
* for a default value we make it run 5 seconds
* after fired.
*/
public $after_seconds = 5;
/**
* By default, wp crons are meant to be
* run in a repeated way with a certain
* interval. But, wp_queues will only run
* on first job and then stops. Defaults
* to 60 seconds in case the event occurs
* an error at first run
*/
public $cron_interval = 60;
/**
* Regular expression patterns to identify
* an event from its tag name, and call
* its own callback at run
*/
public $patterns = array();
/**
* class init
*
* You must hook this method to an early hook
* like plugins_loaded or init
*/
public function init()
{
add_filter('cron_schedules', array($this, 'pushCronInterval'));
foreach ( range($this->rand_min, $this->rand_max) as $i ) {
add_action("wp_queues_{$this->unique}_schedules_{$i}", array($this, 'events'));
}
}
/**
* Schedule an event
*
* @param $identifier a name for the event
* @param $seconds optional, run event after x seconds, default $this->after_seconds
*/
public function schedule($identifier, $seconds=null)
{
$i = rand($this->rand_min, $this->rand_max);
$tag = "wp_queues_{$this->unique}_schedules_{$i}";
if ( !is_numeric($seconds) ) {
$seconds = $this->after_seconds;
}
if( !wp_next_scheduled( $tag ) ) {
wp_schedule_event( time() + intval($seconds), 'wp_queues_interval', $tag );
}
$opt = "wp_queues_{$this->unique}_schedules_{$i}";
$schedules = (array) get_site_option($opt, null);
$schedules[] = $identifier;
$schedules = array_filter($schedules);
$schedules = array_unique($schedules);
// save
update_site_option($opt, $schedules);
return $this;
}
/**
* Add custom interval to cron schedules
*/
function pushCronInterval($vals)
{
return array_merge(array(
'wp_queues_interval' => array(
'interval' => $this->cron_interval,
'display' => __('WP_Queues interval', 'wp-queues')
)
), $vals);
}
/**
* Hooked into the cron job to fetch for
* events based on the action hook name
*/
public function events()
{
$tag = current_filter();
$id = (int) str_replace("wp_queues_{$this->unique}_schedules_", '', $tag);
if ( !$id )
return;
$opt = "wp_queues_{$this->unique}_schedules_{$id}";
$schedules = (array) get_site_option($opt, null);
if ( !$schedules ) {
delete_site_option($opt);
} else {
foreach ( $schedules as $id ) {
// core process
$this->run($id);
}
}
// delete
delete_site_option($opt);
// clear out
wp_unschedule_event( wp_next_scheduled( $tag ), $tag );
}
/**
* Get a list of regex patterns
*
* Filter with wp_queues_event_patterns
* or set class var $patterns with regex
*/
public function patterns()
{
$this->patterns = apply_filters('wp_queues_event_patterns', $this->patterns);
return $this->patterns;
}
/**
* Run an event based on its name
*
* uses regex to match the event name with
* existing callbacks
*
* @uses patterns to get avail regex patterns
*/
public function run($id)
{
$patterns = $this->patterns();
if ( !$patterns )
return;
foreach ( $patterns as $p=>$c ) {
preg_match($p, $id, $args);
if ( $args ) {
if ( isset($args[1]) ) {
$args = array_slice($args, 1);
}
if ( is_callable($c) ) {
call_user_func_array($c, $args);
}
}
}
}
/**
* Clear all pending cron jobs upon deactivation
*
* This process is a must.
*/
public function deactivation()
{
foreach ( range($this->rand_min, $this->rand_max) as $i ) {
$tag = "wp_queues_{$this->unique}_schedules_{$i}";
wp_clear_scheduled_hook($tag);
}
}
/**
* Clean up orphaned options from database
*
* @return number of deleted options
*/
public function cleanup()
{
global $wpdb;
return $wpdb->query("DELETE FROM {$wpdb->options} WHERE `option_name` LIKE 'wp_queues_{$this->unique}_%'");
}
}