PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.2.8
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.2.8
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / inc / classes / class-imagify-abstract-cron.php

class-imagify-abstract-cron.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.2.8, at inc/classes/class-imagify-abstract-cron.php

271 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Basis class that handles events.
5 *
6 * @since 1.7
7 * @author Grégory Viguier
8 */
9 abstract class Imagify_Abstract_Cron {
10 /**
11 * Class version.
12 *
13 * @var string
14 * @since 1.7
15 */
16 const VERSION = '1.0';
17
18 /**
19 * Cron name.
20 *
21 * @var string
22 * @since 1.7
23 * @access protected
24 */
25 protected $event_name = '';
26
27 /**
28 * Cron recurrence.
29 *
30 * @var string
31 * @since 1.7
32 * @access protected
33 */
34 protected $event_recurrence = '';
35
36 /**
37 * Cron time.
38 *
39 * @var string
40 * @since 1.7
41 * @access protected
42 */
43 protected $event_time = '';
44
45 /** ----------------------------------------------------------------------------------------- */
46 /** INIT ==================================================================================== */
47 /** ----------------------------------------------------------------------------------------- */
48
49 /**
50 * Launch hooks.
51 *
52 * @since 1.7
53 * @access public
54 * @author Grégory Viguier
55 */
56 public function init() {
57 add_action( 'init', [ $this, 'schedule_event' ] );
58 add_action( $this->get_event_name(), [ $this, 'do_event' ] );
59 add_filter( 'cron_schedules', [ $this, 'maybe_add_recurrence' ] );
60
61 if ( did_action( static::get_deactivation_hook_name() ) ) {
62 $this->unschedule_event();
63 } else {
64 add_action( static::get_deactivation_hook_name(), [ $this, 'unschedule_event' ] );
65 }
66 }
67
68
69 /** ----------------------------------------------------------------------------------------- */
70 /** HOOKS =================================================================================== */
71 /** ----------------------------------------------------------------------------------------- */
72
73 /**
74 * Initiate the event.
75 *
76 * @since 1.7
77 * @access public
78 * @author Grégory Viguier
79 */
80 public function schedule_event() {
81 if ( ! wp_next_scheduled( $this->get_event_name() ) ) {
82 wp_schedule_event( $this->get_event_timestamp(), $this->get_event_recurrence(), $this->get_event_name() );
83 }
84 }
85
86 /**
87 * The event action.
88 *
89 * @since 1.7
90 * @access public
91 * @author Grégory Viguier
92 */
93 abstract public function do_event();
94
95 /**
96 * Unschedule the event at plugin or submodule deactivation.
97 *
98 * @since 1.7
99 * @access public
100 * @author Grégory Viguier
101 */
102 public function unschedule_event() {
103 wp_clear_scheduled_hook( $this->get_event_name() );
104 }
105
106 /**
107 * Add the event recurrence schedule.
108 *
109 * @since 1.7
110 * @access public
111 * @see wp_get_schedules()
112 * @author Grégory Viguier
113 *
114 * @param array $schedules An array of non-default cron schedules. Default empty.
115 *
116 * @return array
117 */
118 public function maybe_add_recurrence( $schedules ) {
119 $default_schedules = [
120 'hourly' => 1,
121 'twicedaily' => 1,
122 'daily' => 1,
123 ];
124
125 $event_recurrence = $this->get_event_recurrence();
126
127 if ( ! empty( $schedules[ $event_recurrence ] ) || ! empty( $default_schedules[ $event_recurrence ] ) ) {
128 return $schedules;
129 }
130
131 $recurrences = [
132 'weekly' => [
133 'interval' => WEEK_IN_SECONDS,
134 'display' => __( 'Once Weekly', 'imagify' ),
135 ],
136 ];
137
138 if ( method_exists( $this, 'get_event_recurrence_attributes' ) ) {
139 $recurrences[ $event_recurrence ] = $this->get_event_recurrence_attributes();
140 }
141
142 if ( ! empty( $recurrences[ $event_recurrence ] ) ) {
143 $schedules[ $event_recurrence ] = $recurrences[ $event_recurrence ];
144 }
145
146 return $schedules;
147 }
148
149
150 /** ----------------------------------------------------------------------------------------- */
151 /** TOOLS =================================================================================== */
152 /** ----------------------------------------------------------------------------------------- */
153
154 /**
155 * Get the cron name.
156 *
157 * @since 1.7
158 * @access public
159 * @author Grégory Viguier
160 *
161 * @return string
162 */
163 public function get_event_name() {
164 return $this->event_name;
165 }
166
167 /**
168 * Get the cron recurrence.
169 *
170 * @since 1.7
171 * @access public
172 * @author Grégory Viguier
173 *
174 * @return string
175 */
176 public function get_event_recurrence() {
177 /**
178 * Filter the recurrence of the event.
179 *
180 * @since 1.7
181 * @author Grégory Viguier
182 *
183 * @param string $event_recurrence The recurrence.
184 * @param string $event_name Name of the event this recurrence is used for.
185 */
186 return apply_filters( 'imagify_event_recurrence', $this->event_recurrence, $this->get_event_name() );
187 }
188
189 /**
190 * Get the time to schedule the event.
191 *
192 * @since 1.7
193 * @access public
194 * @author Grégory Viguier
195 *
196 * @return string
197 */
198 public function get_event_time() {
199 /**
200 * Filter the time at which the event is triggered (WordPress time).
201 *
202 * @since 1.7
203 * @author Grégory Viguier
204 *
205 * @param string $event_time A 24H formated time: `hour:minute`.
206 * @param string $event_name Name of the event this time is used for.
207 */
208 return apply_filters( 'imagify_event_time', $this->event_time, $this->get_event_name() );
209 }
210
211 /**
212 * Get the timestamp to schedule the event.
213 *
214 * @since 1.7
215 * @access public
216 * @author Grégory Viguier
217 *
218 * @return int Timestamp.
219 */
220 public function get_event_timestamp() {
221 return self::get_next_timestamp( $this->get_event_time() );
222 }
223
224 /**
225 * Get the timestamp of the next (event) date for the given hour.
226 *
227 * @since 1.7
228 * @author Grégory Viguier
229 * @source secupress_get_next_cron_timestamp()
230 *
231 * @param string $event_time Time when the event callback should be triggered (WordPress time), formated like `hh:mn` (hour:minute).
232 *
233 * @return int Timestamp.
234 */
235 public static function get_next_timestamp( $event_time = '00:00' ) {
236 $current_time_int = (int) gmdate( 'Gis' );
237 $event_time_int = (int) str_replace( ':', '', $event_time . '00' );
238 $event_time = explode( ':', $event_time );
239 $event_hour = (int) $event_time[0];
240 $event_minute = (int) $event_time[1];
241 $offset = get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
242
243 if ( $event_time_int <= $current_time_int ) {
244 // The event time is passed, we need to schedule the event tomorrow.
245 return mktime( $event_hour, $event_minute, 0, (int) gmdate( 'n' ), (int) gmdate( 'j' ) + 1 ) - $offset;
246 }
247
248 // We haven't passed the event time yet, schedule the event today.
249 return mktime( $event_hour, $event_minute, 0 ) - $offset;
250 }
251
252 /**
253 * Get the deactivation hook name.
254 *
255 * @since 1.9
256 * @access public
257 * @author Grégory Viguier
258 *
259 * @return string
260 */
261 public static function get_deactivation_hook_name() {
262 static $deactivation_hook;
263
264 if ( ! isset( $deactivation_hook ) ) {
265 $deactivation_hook = 'deactivate_' . plugin_basename( IMAGIFY_FILE );
266 }
267
268 return $deactivation_hook;
269 }
270 }
271