Repository navigation
Expand file tree
/
Copy pathCronjob.php
More file actions
178 lines (150 loc) · 3.4 KB
/
Copy pathCronjob.php
File metadata and controls
178 lines (150 loc) · 3.4 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
<?php
namespace JustCoded\WP\Framework\Objects;
/**
* Class Cronjob
*
* @package JustCoded\WP\Framework\Objects
*
* @method Cronjob instance() static
*/
abstract class Cronjob {
use Singleton;
/**
* Constant for single cron
*/
const FREQUENCY_ONCE = 'single';
/**
* Constant for hourly cron
*/
const FREQUENCY_HOURLY = 'hourly';
/**
* Constant for twicedaily cron
*/
const FREQUENCY_TWICEDAILY = 'twicedaily';
/**
* Constant for daily cron
*/
const FREQUENCY_DAILY = 'daily';
/**
* Cron id
*
* @var string
*/
protected $ID;
/**
* Start time.
*
* @var int|string
*/
protected $start;
/**
* Frequency name.
*
* @var string
*/
protected $frequency;
/**
* Interval in seconds.
*
* @var int
*/
protected $interval;
/**
* Cronjob constructor.
*
* @throws \Exception
*/
protected function __construct() {
if ( empty( $this->ID ) ) {
throw new \Exception( static::class . ' class: $ID property is required' );
}
// register action hook.
add_action( $this->ID, [ $this, 'handle' ] );
$this->cron_debug();
if ( static::FREQUENCY_ONCE !== $this->frequency ) {
// deactivation hook for repeatable event.
add_action( 'switch_theme', [ $this, 'deactivate' ] );
}
if ( ! in_array( $this->frequency, $this->get_standard_frequencies(), true ) ) {
if ( empty( $this->interval ) || ! is_numeric( $this->interval ) ) {
throw new \Exception( static::class . ' class: $interval property is required and must be numeric if you use a custom schedule' );
}
// register custom schedule.
add_filter( 'cron_schedules', [ $this, 'register_custom_schedule' ], 99, 1 );
}
// register cron.
add_action( 'init', [ $this, 'register' ] );
}
/**
* Register custom schedule.
*
* @param array $schedules Non-default schedule.
*
* @return array
*/
public function register_custom_schedule( $schedules ) {
$schedules[ $this->frequency ] = [
'interval' => $this->interval,
'display' => $this->frequency,
];
return $schedules;
}
/**
* Registers cron with interval
*/
public function register() {
if ( empty( $this->start ) ) {
$this->start = time();
} elseif ( ! is_numeric( $this->start ) && is_string( $this->start ) ) {
$this->start = strtotime( $this->start );
}
if ( static::FREQUENCY_ONCE === $this->frequency ) {
wp_schedule_single_event( $this->start, $this->ID );
} elseif ( ! wp_next_scheduled( $this->ID ) ) {
wp_schedule_event( $this->start, $this->frequency, $this->ID );
}
}
/**
* Deactivate cron on theme deactivate.
*/
public function deactivate() {
if ( $start = wp_next_scheduled( $this->ID ) ) {
wp_unschedule_event( $start, $this->ID );
}
}
/**
* Get all frequency
*
* @return array
*/
protected function get_standard_frequencies() {
return [
self::FREQUENCY_HOURLY,
self::FREQUENCY_TWICEDAILY,
self::FREQUENCY_DAILY,
];
}
/**
* Debug
*
* @return bool
*/
protected function cron_debug() {
if ( ! defined( 'WP_DEBUG' ) || false === WP_DEBUG ) {
return false;
}
if ( ! isset( $_GET['do_cron'] ) || $_GET['do_cron'] !== $this->ID ) {
return false;
}
remove_action( $this->ID, [ $this, 'handle' ] );
add_action( 'init', function () {
$this->handle();
wp_die( 'Finished cronjob <code>' . esc_attr__( $this->ID ) . '</code>' );
}, 99 );
return true;
}
/**
* Handle function
*/
abstract public function handle();
}