1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* VideoPress playback module markup generator. |
4
|
|
|
* |
5
|
|
|
* @since 1.3 |
6
|
|
|
*/ |
7
|
|
|
class VideoPress_Scheduler { |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The name of the function used to run the cleanup cron. |
11
|
|
|
*/ |
12
|
|
|
const CLEANUP_CRON_METHOD = 'videopress_cleanup_media_library'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var VideoPress_Scheduler |
16
|
|
|
**/ |
17
|
|
|
private static $instance = null; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* A list of all of the crons that are to be activated, along with their interval timings. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $crons = array( |
25
|
|
|
'cleanup' => array( |
26
|
|
|
'method' => self::CLEANUP_CRON_METHOD, |
27
|
|
|
'interval' => 'minutes_30', |
28
|
|
|
), |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Private VideoPress_Scheduler constructor. |
34
|
|
|
* |
35
|
|
|
* Use the VideoPress_Scheduler::init() method to get an instance. |
36
|
|
|
*/ |
37
|
|
|
private function __construct() { |
38
|
|
|
add_filter( 'cron_schedules', array( $this, 'add_30_minute_cron_interval' ) ); |
39
|
|
|
|
40
|
|
|
// Activate the cleanup cron if videopress is enabled, jetpack is activated, or jetpack is updated. |
41
|
|
|
add_action( 'jetpack_activate_module_videopress', array( $this, 'activate_all_crons' ) ); |
42
|
|
|
add_action( 'updating_jetpack_version', array( $this, 'activate_all_crons' ) ); |
43
|
|
|
add_action( 'activated_plugin', array( $this, 'activate_crons_on_jetpack_activation' ) ); |
44
|
|
|
|
45
|
|
|
// Deactivate the cron if either videopress is disabled or Jetpack is disabled. |
46
|
|
|
add_action( 'jetpack_deactivate_module_videopress', array( $this, 'deactivate_all_crons' ) ); |
47
|
|
|
register_deactivation_hook( plugin_basename( JETPACK__PLUGIN_FILE ), array( $this, 'deactivate_all_crons' ) ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Initialize the VideoPress_Scheduler and get back a singleton instance. |
52
|
|
|
* |
53
|
|
|
* @return VideoPress_Scheduler |
54
|
|
|
*/ |
55
|
|
|
public static function init() { |
56
|
|
|
if ( is_null( self::$instance ) ) { |
57
|
|
|
self::$instance = new VideoPress_Scheduler; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return self::$instance; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Adds 30 minute running interval to the cron schedules. |
65
|
|
|
* |
66
|
|
|
* @param array $current_schedules Currently defined schedules list. |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function add_30_minute_cron_interval( $current_schedules ) { |
71
|
|
|
|
72
|
|
|
// Only add the 30 minute interval if it wasn't already set. |
73
|
|
|
if ( ! isset( $current_schedules['minutes_30'] ) ) { |
74
|
|
|
$current_schedules['minutes_30'] = array( |
75
|
|
|
'interval' => 30 * MINUTE_IN_SECONDS, |
76
|
|
|
'display' => 'Every 30 minutes' |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $current_schedules; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Activate a single cron |
85
|
|
|
* |
86
|
|
|
* @param string $cron_name |
87
|
|
|
* |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public function activate_cron( $cron_name ) { |
91
|
|
|
|
92
|
|
|
if ( ! $this->is_cron_valid( $cron_name ) ) { |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if ( ! $this->check_cron( $cron_name ) ) { |
97
|
|
|
wp_schedule_event( time(), $this->crons[ $cron_name ]['interval'], $this->crons[ $cron_name ]['method'] ); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Activates widget update cron task. |
103
|
|
|
*/ |
104
|
|
|
public function activate_all_crons() { |
105
|
|
|
|
106
|
|
|
if ( ! Jetpack::is_module_active( 'videopress' ) ) { |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
foreach ( $this->crons as $cron_name => $cron ) { |
111
|
|
|
if ( ! $this->check_cron( $cron_name ) ) { |
112
|
|
|
wp_schedule_event( time(), $cron['interval'], $cron['method'] ); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Only activate the crons if it is Jetpack that was activated. |
119
|
|
|
* |
120
|
|
|
* @param string $plugin_file_name |
121
|
|
|
*/ |
122
|
|
|
public function activate_crons_on_jetpack_activation( $plugin_file_name ) { |
123
|
|
|
|
124
|
|
|
if ( plugin_basename( JETPACK__PLUGIN_FILE ) === $plugin_file_name ) { |
125
|
|
|
$this->activate_all_crons(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Deactivates any crons associated with the VideoPress module. |
131
|
|
|
* |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
public function deactivate_cron( $cron_name ) { |
135
|
|
|
|
136
|
|
|
if ( ! $this->is_cron_valid( $cron_name ) ) { |
137
|
|
|
return false; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$next_scheduled_time = $this->check_cron( $cron_name ); |
141
|
|
|
wp_unschedule_event( $next_scheduled_time, $this->crons[ $cron_name ]['method'] ); |
142
|
|
|
|
143
|
|
|
return true; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Deactivates any crons associated with the VideoPress module.. |
148
|
|
|
*/ |
149
|
|
|
public function deactivate_all_crons() { |
150
|
|
|
|
151
|
|
|
foreach ( $this->crons as $cron_name => $cron ) { |
152
|
|
|
$this->deactivate_cron( $cron_name ); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Is the given cron job currently active? |
158
|
|
|
* |
159
|
|
|
* If so, return when it will next run, |
160
|
|
|
* |
161
|
|
|
* @param string $cron_name |
162
|
|
|
* |
163
|
|
|
* @return int|bool Timestamp of the next run time OR false. |
164
|
|
|
*/ |
165
|
|
|
public function check_cron( $cron_name ) { |
166
|
|
|
if ( ! $this->is_cron_valid( $cron_name ) ) { |
167
|
|
|
return false; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return wp_next_scheduled( $this->crons[ $cron_name ]['method'] ); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Check that the given cron job name is valid. |
175
|
|
|
* |
176
|
|
|
* @param string $cron_name |
177
|
|
|
* |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
public function is_cron_valid( $cron_name ) { |
181
|
|
|
|
182
|
|
|
if ( ! isset( $this->crons[ $cron_name ]['method'] ) || ! isset( $this->crons[ $cron_name ]['interval'] ) ) { |
183
|
|
|
return false; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return true; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Get a list of all of the crons that are available. |
191
|
|
|
* |
192
|
|
|
* @return array |
193
|
|
|
*/ |
194
|
|
|
public function get_crons() { |
195
|
|
|
return $this->crons; |
196
|
|
|
} |
197
|
|
|
} |