1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Cron |
4
|
|
|
* |
5
|
|
|
* @package Give |
6
|
|
|
* @subpackage Classes/Give_Cron |
7
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
8
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
9
|
|
|
* @since 1.3.2 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// Exit if accessed directly. |
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
14
|
|
|
exit; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Give_Cron Class |
20
|
|
|
* |
21
|
|
|
* This class handles scheduled events. |
22
|
|
|
* |
23
|
|
|
* @since 1.3.2 |
24
|
|
|
*/ |
25
|
|
|
class Give_Cron { |
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Instance. |
29
|
|
|
* |
30
|
|
|
* @since 1.8.13 |
31
|
|
|
* @access private |
32
|
|
|
* @var |
33
|
|
|
*/ |
34
|
|
|
private static $instance; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Singleton pattern. |
38
|
|
|
* |
39
|
|
|
* @since 1.8.13 |
40
|
|
|
* @access private |
41
|
|
|
*/ |
42
|
|
|
private function __construct() { |
43
|
|
|
} |
44
|
|
|
|
45
|
3 |
|
|
46
|
|
|
/** |
47
|
3 |
|
* Get instance. |
48
|
3 |
|
* |
49
|
3 |
|
* @since 1.8.13 |
50
|
3 |
|
* @access public |
51
|
|
|
* @return static |
52
|
3 |
|
*/ |
53
|
|
|
public static function get_instance() { |
54
|
|
|
if ( null === static::$instance ) { |
55
|
|
|
self::$instance = new static(); |
56
|
|
|
self::$instance->setup(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return self::$instance; |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
|
63
|
3 |
|
/** |
64
|
3 |
|
* Setup |
65
|
3 |
|
* |
66
|
|
|
* @since 1.8.13 |
67
|
|
|
*/ |
68
|
|
|
private function setup() { |
69
|
|
|
add_filter( 'cron_schedules', array( self::$instance, '__add_schedules' ) ); |
70
|
|
|
add_action( 'wp', array( self::$instance, '__schedule_events' ) ); |
71
|
|
|
|
72
|
|
|
// Load async event only when cron is running. |
73
|
|
|
if( defined( 'DOING_CRON' ) && DOING_CRON ) { |
|
|
|
|
74
|
3 |
|
add_action( 'init', array( self::$instance, '__load_async_events' ) ); |
75
|
3 |
|
} |
76
|
3 |
|
} |
77
|
3 |
|
|
78
|
3 |
|
|
79
|
|
|
/** |
80
|
|
|
* Load async events |
81
|
|
|
* |
82
|
|
|
* @since 1.8.13 |
83
|
|
|
*/ |
84
|
|
|
public function __load_async_events() { |
|
|
|
|
85
|
|
|
$async_events = get_option( 'give_async_events', array() ); |
86
|
|
|
|
87
|
3 |
|
// Bailout. |
88
|
3 |
|
if ( empty( $async_events ) ) { |
89
|
3 |
|
return; |
90
|
3 |
|
} |
91
|
3 |
|
|
92
|
|
|
foreach ( $async_events as $index => $event ) { |
93
|
|
|
// Set cron name. |
94
|
|
|
$cron_name = "give_async_scheduled_events_{$index}"; |
95
|
|
|
|
96
|
|
|
// Setup cron. |
97
|
|
|
wp_schedule_single_event( current_time( 'timestamp', 1 ), $cron_name, $event['params'] ); |
98
|
|
|
|
99
|
|
|
// Add cron action. |
100
|
|
|
add_action( $cron_name, $event['callback'], 10, count( $event['params'] ) ); |
101
|
|
|
add_action( $cron_name, array( $this, '__delete_async_events' ), 9999, 0 ); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Delete async cron info after run |
107
|
|
|
* |
108
|
|
|
* @since 1.8.13 |
109
|
|
|
*/ |
110
|
|
|
public function __delete_async_events() { |
|
|
|
|
111
|
|
|
$async_events = get_option( 'give_async_events', array() ); |
112
|
|
|
$cron_name_parts = explode( '_', current_action() ); |
113
|
|
|
$cron_id = end( $cron_name_parts ); |
114
|
|
|
|
115
|
|
|
if ( ! empty( $async_events[ $cron_id ] ) ) { |
116
|
|
|
unset( $async_events[ $cron_id ] ); |
117
|
|
|
update_option( 'give_async_events', $async_events ); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Registers new cron schedules |
123
|
|
|
* |
124
|
|
|
* @since 1.3.2 |
125
|
|
|
* @access public |
126
|
|
|
* |
127
|
|
|
* @param array $schedules An array of non-default cron schedules. |
128
|
|
|
* |
129
|
|
|
* @return array An array of non-default cron schedules. |
130
|
|
|
*/ |
131
|
|
|
public function __add_schedules( $schedules = array() ) { |
|
|
|
|
132
|
|
|
// Adds once weekly to the existing schedules. |
133
|
|
|
$schedules['weekly'] = array( |
134
|
|
|
'interval' => 604800, |
135
|
|
|
'display' => __( 'Once Weekly', 'give' ), |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
return $schedules; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Schedules our events |
143
|
|
|
* |
144
|
|
|
* @since 1.3.2 |
145
|
|
|
* @access public |
146
|
|
|
* |
147
|
|
|
* @return void |
148
|
|
|
*/ |
149
|
|
|
public function __schedule_events() { |
|
|
|
|
150
|
|
|
$this->weekly_events(); |
151
|
|
|
$this->daily_events(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Schedule weekly events |
156
|
|
|
* |
157
|
|
|
* @since 1.3.2 |
158
|
|
|
* @access private |
159
|
|
|
* |
160
|
|
|
* @return void |
161
|
|
|
*/ |
162
|
|
|
private function weekly_events() { |
163
|
|
|
if ( ! wp_next_scheduled( 'give_weekly_scheduled_events' ) ) { |
164
|
|
|
wp_schedule_event( current_time( 'timestamp' ), 'weekly', 'give_weekly_scheduled_events' ); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Schedule daily events |
170
|
|
|
* |
171
|
|
|
* @since 1.3.2 |
172
|
|
|
* @access private |
173
|
|
|
* |
174
|
|
|
* @return void |
175
|
|
|
*/ |
176
|
|
|
private function daily_events() { |
177
|
|
|
if ( ! wp_next_scheduled( 'give_daily_scheduled_events' ) ) { |
178
|
|
|
wp_schedule_event( current_time( 'timestamp' ), 'daily', 'give_daily_scheduled_events' ); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* get cron job action name |
184
|
|
|
* |
185
|
|
|
* @since 1.8.13 |
186
|
|
|
* @access public |
187
|
|
|
* |
188
|
|
|
* @param string $type |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
public static function get_cron_action( $type = 'weekly' ) { |
193
|
|
|
switch ( $type ) { |
194
|
|
|
case 'daily': |
195
|
|
|
$cron_action = 'give_daily_scheduled_events'; |
196
|
|
|
break; |
197
|
|
|
|
198
|
|
|
default: |
199
|
|
|
$cron_action = 'give_weekly_scheduled_events'; |
200
|
|
|
break; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $cron_action; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Add action to cron action |
208
|
|
|
* |
209
|
|
|
* @since 1.8.13 |
210
|
|
|
* @access private |
211
|
|
|
* |
212
|
|
|
* @param $action |
213
|
|
|
* @param string $type |
214
|
|
|
*/ |
215
|
|
|
private static function add_event( $action, $type = 'weekly' ) { |
216
|
|
|
$cron_event = self::get_cron_action( $type ); |
217
|
|
|
add_action( $cron_event, $action ); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Add weekly event |
222
|
|
|
* |
223
|
|
|
* @since 1.8.13 |
224
|
|
|
* @access public |
225
|
|
|
* |
226
|
|
|
* @param $action |
227
|
|
|
*/ |
228
|
|
|
public static function add_weekly_event( $action ) { |
229
|
|
|
self::add_event( $action, 'weekly' ); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Add daily event |
234
|
|
|
* |
235
|
|
|
* @since 1.8.13 |
236
|
|
|
* @access public |
237
|
|
|
* |
238
|
|
|
* @param $action |
239
|
|
|
*/ |
240
|
|
|
public static function add_daily_event( $action ) { |
241
|
|
|
self::add_event( $action, 'daily' ); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Add async event |
246
|
|
|
* Note: it is good for small jobs if you have bigger task to do then either test it or manage with custom cron job. |
247
|
|
|
* |
248
|
|
|
* @since 1.8.13 |
249
|
|
|
* @access public |
250
|
|
|
* |
251
|
|
|
* @param string $action |
252
|
|
|
* @param array $args |
253
|
|
|
*/ |
254
|
|
|
public static function add_async_event( $action, $args = array() ) { |
255
|
|
|
|
256
|
|
|
// Cache async events. |
257
|
|
|
$async_events = get_option( 'give_async_events', array() ); |
258
|
|
|
$async_events[ uniqid() ] = array( |
259
|
|
|
'callback' => $action, |
260
|
|
|
'params' => $args, |
261
|
|
|
); |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
264
|
|
|
update_option( 'give_async_events', $async_events ); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
// Initiate class. |
269
|
|
|
Give_Cron::get_instance(); |