|
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
|
|
|
|
|
73
|
|
|
/** |
|
74
|
3 |
|
* Registers new cron schedules |
|
75
|
3 |
|
* |
|
76
|
3 |
|
* @since 1.3.2 |
|
77
|
3 |
|
* @access public |
|
78
|
3 |
|
* |
|
79
|
|
|
* @param array $schedules An array of non-default cron schedules. |
|
80
|
|
|
* |
|
81
|
|
|
* @return array An array of non-default cron schedules. |
|
82
|
|
|
*/ |
|
83
|
|
|
public function __add_schedules( $schedules = array() ) { |
|
|
|
|
|
|
84
|
|
|
// Adds once weekly to the existing schedules. |
|
85
|
|
|
$schedules['weekly'] = array( |
|
86
|
|
|
'interval' => 604800, |
|
87
|
3 |
|
'display' => __( 'Once Weekly', 'give' ), |
|
88
|
3 |
|
); |
|
89
|
3 |
|
|
|
90
|
3 |
|
return $schedules; |
|
91
|
3 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Schedules our events |
|
95
|
|
|
* |
|
96
|
|
|
* @since 1.3.2 |
|
97
|
|
|
* @access public |
|
98
|
|
|
* |
|
99
|
|
|
* @return void |
|
100
|
|
|
*/ |
|
101
|
|
|
public function __schedule_events() { |
|
|
|
|
|
|
102
|
|
|
$this->weekly_events(); |
|
103
|
|
|
$this->daily_events(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Schedule weekly events |
|
108
|
|
|
* |
|
109
|
|
|
* @since 1.3.2 |
|
110
|
|
|
* @access private |
|
111
|
|
|
* |
|
112
|
|
|
* @return void |
|
113
|
|
|
*/ |
|
114
|
|
|
private function weekly_events() { |
|
115
|
|
|
if ( ! wp_next_scheduled( 'give_weekly_scheduled_events' ) ) { |
|
116
|
|
|
wp_schedule_event( current_time( 'timestamp' ), 'weekly', 'give_weekly_scheduled_events' ); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Schedule daily events |
|
122
|
|
|
* |
|
123
|
|
|
* @since 1.3.2 |
|
124
|
|
|
* @access private |
|
125
|
|
|
* |
|
126
|
|
|
* @return void |
|
127
|
|
|
*/ |
|
128
|
|
|
private function daily_events() { |
|
129
|
|
|
if ( ! wp_next_scheduled( 'give_daily_scheduled_events' ) ) { |
|
130
|
|
|
wp_schedule_event( current_time( 'timestamp' ), 'daily', 'give_daily_scheduled_events' ); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* get cron job action name |
|
136
|
|
|
* |
|
137
|
|
|
* @since 1.8.13 |
|
138
|
|
|
* @access public |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $type |
|
141
|
|
|
* |
|
142
|
|
|
* @return string |
|
143
|
|
|
*/ |
|
144
|
|
|
public static function get_cron_action( $type = 'weekly' ) { |
|
145
|
|
|
switch ( $type ) { |
|
146
|
|
|
case 'daily': |
|
147
|
|
|
$cron_action = 'give_daily_scheduled_events'; |
|
148
|
|
|
break; |
|
149
|
|
|
|
|
150
|
|
|
default: |
|
151
|
|
|
$cron_action = 'give_weekly_scheduled_events'; |
|
152
|
|
|
break; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return $cron_action; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Add action to cron action |
|
160
|
|
|
* |
|
161
|
|
|
* @since 1.8.13 |
|
162
|
|
|
* @access private |
|
163
|
|
|
* |
|
164
|
|
|
* @param $action |
|
165
|
|
|
* @param string $type |
|
166
|
|
|
*/ |
|
167
|
|
|
private static function add_event( $action, $type = 'weekly' ) { |
|
168
|
|
|
$cron_event = self::get_cron_action( $type ); |
|
169
|
|
|
add_action( $cron_event, $action ); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Add weekly event |
|
174
|
|
|
* |
|
175
|
|
|
* @since 1.8.13 |
|
176
|
|
|
* @access public |
|
177
|
|
|
* |
|
178
|
|
|
* @param $action |
|
179
|
|
|
*/ |
|
180
|
|
|
public static function add_weekly_event( $action ) { |
|
181
|
|
|
self::add_event( $action, 'weekly' ); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Add daily event |
|
186
|
|
|
* |
|
187
|
|
|
* @since 1.8.13 |
|
188
|
|
|
* @access public |
|
189
|
|
|
* |
|
190
|
|
|
* @param $action |
|
191
|
|
|
*/ |
|
192
|
|
|
public static function add_daily_event( $action ) { |
|
193
|
|
|
self::add_event( $action, 'daily' ); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
// Initiate class. |
|
198
|
|
|
Give_Cron::get_instance(); |