PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.7.6
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.7.6
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / vendor / woocommerce / action-scheduler / classes / abstracts / ActionScheduler.php

ActionScheduler.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.7.6, at vendor/woocommerce/action-scheduler/classes/abstracts/ActionScheduler.php

396 lines 10.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use Action_Scheduler\WP_CLI\Migration_Command;
4 use Action_Scheduler\Migration\Controller;
5
6 /**
7 * Class ActionScheduler
8 *
9 * @codeCoverageIgnore
10 */
11 abstract class ActionScheduler {
12
13 /**
14 * Plugin file path.
15 *
16 * @var string
17 */
18 private static $plugin_file = '';
19
20 /**
21 * ActionScheduler_ActionFactory instance.
22 *
23 * @var ActionScheduler_ActionFactory
24 */
25 private static $factory = null;
26
27 /**
28 * Data store is initialized.
29 *
30 * @var bool
31 */
32 private static $data_store_initialized = false;
33
34 /**
35 * Factory.
36 */
37 public static function factory() {
38 if ( ! isset( self::$factory ) ) {
39 self::$factory = new ActionScheduler_ActionFactory();
40 }
41 return self::$factory;
42 }
43
44 /**
45 * Get Store instance.
46 */
47 public static function store() {
48 return ActionScheduler_Store::instance();
49 }
50
51 /**
52 * Get Lock instance.
53 */
54 public static function lock() {
55 return ActionScheduler_Lock::instance();
56 }
57
58 /**
59 * Get Logger instance.
60 */
61 public static function logger() {
62 return ActionScheduler_Logger::instance();
63 }
64
65 /**
66 * Get QueueRunner instance.
67 */
68 public static function runner() {
69 return ActionScheduler_QueueRunner::instance();
70 }
71
72 /**
73 * Get AdminView instance.
74 */
75 public static function admin_view() {
76 return ActionScheduler_AdminView::instance();
77 }
78
79 /**
80 * Get the absolute system path to the plugin directory, or a file therein
81 *
82 * @static
83 * @param string $path Path relative to plugin directory.
84 * @return string
85 */
86 public static function plugin_path( $path ) {
87 $base = dirname( self::$plugin_file );
88 if ( $path ) {
89 return trailingslashit( $base ) . $path;
90 } else {
91 return untrailingslashit( $base );
92 }
93 }
94
95 /**
96 * Get the absolute URL to the plugin directory, or a file therein
97 *
98 * @static
99 * @param string $path Path relative to plugin directory.
100 * @return string
101 */
102 public static function plugin_url( $path ) {
103 return plugins_url( $path, self::$plugin_file );
104 }
105
106 /**
107 * Autoload.
108 *
109 * @param string $class Class name.
110 */
111 public static function autoload( $class ) {
112 $d = DIRECTORY_SEPARATOR;
113 $classes_dir = self::plugin_path( 'classes' . $d );
114 $separator = strrpos( $class, '\\' );
115 if ( false !== $separator ) {
116 if ( 0 !== strpos( $class, 'Action_Scheduler' ) ) {
117 return;
118 }
119 $class = substr( $class, $separator + 1 );
120 }
121
122 if ( 'Deprecated' === substr( $class, -10 ) ) {
123 $dir = self::plugin_path( 'deprecated' . $d );
124 } elseif ( self::is_class_abstract( $class ) ) {
125 $dir = $classes_dir . 'abstracts' . $d;
126 } elseif ( self::is_class_migration( $class ) ) {
127 $dir = $classes_dir . 'migration' . $d;
128 } elseif ( 'Schedule' === substr( $class, -8 ) ) {
129 $dir = $classes_dir . 'schedules' . $d;
130 } elseif ( 'Action' === substr( $class, -6 ) ) {
131 $dir = $classes_dir . 'actions' . $d;
132 } elseif ( 'Schema' === substr( $class, -6 ) ) {
133 $dir = $classes_dir . 'schema' . $d;
134 } elseif ( strpos( $class, 'ActionScheduler' ) === 0 ) {
135 $segments = explode( '_', $class );
136 $type = isset( $segments[1] ) ? $segments[1] : '';
137
138 switch ( $type ) {
139 case 'WPCLI':
140 $dir = $classes_dir . 'WP_CLI' . $d;
141 break;
142 case 'DBLogger':
143 case 'DBStore':
144 case 'HybridStore':
145 case 'wpPostStore':
146 case 'wpCommentLogger':
147 $dir = $classes_dir . 'data-stores' . $d;
148 break;
149 default:
150 $dir = $classes_dir;
151 break;
152 }
153 } elseif ( self::is_class_cli( $class ) ) {
154 $dir = $classes_dir . 'WP_CLI' . $d;
155 } elseif ( strpos( $class, 'CronExpression' ) === 0 ) {
156 $dir = self::plugin_path( 'lib' . $d . 'cron-expression' . $d );
157 } elseif ( strpos( $class, 'WP_Async_Request' ) === 0 ) {
158 $dir = self::plugin_path( 'lib' . $d );
159 } else {
160 return;
161 }
162
163 if ( file_exists( $dir . "{$class}.php" ) ) {
164 include $dir . "{$class}.php";
165 return;
166 }
167 }
168
169 /**
170 * Initialize the plugin
171 *
172 * @static
173 * @param string $plugin_file Plugin file path.
174 */
175 public static function init( $plugin_file ) {
176 self::$plugin_file = $plugin_file;
177 spl_autoload_register( array( __CLASS__, 'autoload' ) );
178
179 /**
180 * Fires in the early stages of Action Scheduler init hook.
181 */
182 do_action( 'action_scheduler_pre_init' );
183
184 require_once self::plugin_path( 'functions.php' );
185 ActionScheduler_DataController::init();
186
187 $store = self::store();
188 $logger = self::logger();
189 $runner = self::runner();
190 $admin_view = self::admin_view();
191
192 // Ensure initialization on plugin activation.
193 if ( ! did_action( 'init' ) ) {
194 // phpcs:ignore Squiz.PHP.CommentedOutCode
195 add_action( 'init', array( $admin_view, 'init' ), 0, 0 ); // run before $store::init().
196 add_action( 'init', array( $store, 'init' ), 1, 0 );
197 add_action( 'init', array( $logger, 'init' ), 1, 0 );
198 add_action( 'init', array( $runner, 'init' ), 1, 0 );
199
200 add_action(
201 'init',
202 /**
203 * Runs after the active store's init() method has been called.
204 *
205 * It would probably be preferable to have $store->init() (or it's parent method) set this itself,
206 * once it has initialized, however that would cause problems in cases where a custom data store is in
207 * use and it has not yet been updated to follow that same logic.
208 */
209 function () {
210 self::$data_store_initialized = true;
211
212 /**
213 * Fires when Action Scheduler is ready: it is safe to use the procedural API after this point.
214 *
215 * @since 3.5.5
216 */
217 do_action( 'action_scheduler_init' );
218 },
219 1
220 );
221 } else {
222 $admin_view->init();
223 $store->init();
224 $logger->init();
225 $runner->init();
226 self::$data_store_initialized = true;
227
228 /**
229 * Fires when Action Scheduler is ready: it is safe to use the procedural API after this point.
230 *
231 * @since 3.5.5
232 */
233 do_action( 'action_scheduler_init' );
234 }
235
236 if ( apply_filters( 'action_scheduler_load_deprecated_functions', true ) ) {
237 require_once self::plugin_path( 'deprecated/functions.php' );
238 }
239
240 if ( defined( 'WP_CLI' ) && WP_CLI ) {
241 WP_CLI::add_command( 'action-scheduler', 'ActionScheduler_WPCLI_Scheduler_command' );
242 WP_CLI::add_command( 'action-scheduler', 'ActionScheduler_WPCLI_Clean_Command' );
243 if ( ! ActionScheduler_DataController::is_migration_complete() && Controller::instance()->allow_migration() ) {
244 $command = new Migration_Command();
245 $command->register();
246 }
247 }
248
249 /**
250 * Handle WP comment cleanup after migration.
251 */
252 if ( is_a( $logger, 'ActionScheduler_DBLogger' ) && ActionScheduler_DataController::is_migration_complete() && ActionScheduler_WPCommentCleaner::has_logs() ) {
253 ActionScheduler_WPCommentCleaner::init();
254 }
255
256 add_action( 'action_scheduler/migration_complete', 'ActionScheduler_WPCommentCleaner::maybe_schedule_cleanup' );
257 }
258
259 /**
260 * Check whether the AS data store has been initialized.
261 *
262 * @param string $function_name The name of the function being called. Optional. Default `null`.
263 * @return bool
264 */
265 public static function is_initialized( $function_name = null ) {
266 if ( ! self::$data_store_initialized && ! empty( $function_name ) ) {
267 $message = sprintf(
268 /* translators: %s function name. */
269 __( '%s() was called before the Action Scheduler data store was initialized', 'action-scheduler' ),
270 esc_attr( $function_name )
271 );
272 _doing_it_wrong( esc_html( $function_name ), esc_html( $message ), '3.1.6' );
273 }
274
275 return self::$data_store_initialized;
276 }
277
278 /**
279 * Determine if the class is one of our abstract classes.
280 *
281 * @since 3.0.0
282 *
283 * @param string $class The class name.
284 *
285 * @return bool
286 */
287 protected static function is_class_abstract( $class ) {
288 static $abstracts = array(
289 'ActionScheduler' => true,
290 'ActionScheduler_Abstract_ListTable' => true,
291 'ActionScheduler_Abstract_QueueRunner' => true,
292 'ActionScheduler_Abstract_Schedule' => true,
293 'ActionScheduler_Abstract_RecurringSchedule' => true,
294 'ActionScheduler_Lock' => true,
295 'ActionScheduler_Logger' => true,
296 'ActionScheduler_Abstract_Schema' => true,
297 'ActionScheduler_Store' => true,
298 'ActionScheduler_TimezoneHelper' => true,
299 );
300
301 return isset( $abstracts[ $class ] ) && $abstracts[ $class ];
302 }
303
304 /**
305 * Determine if the class is one of our migration classes.
306 *
307 * @since 3.0.0
308 *
309 * @param string $class The class name.
310 *
311 * @return bool
312 */
313 protected static function is_class_migration( $class ) {
314 static $migration_segments = array(
315 'ActionMigrator' => true,
316 'BatchFetcher' => true,
317 'DBStoreMigrator' => true,
318 'DryRun' => true,
319 'LogMigrator' => true,
320 'Config' => true,
321 'Controller' => true,
322 'Runner' => true,
323 'Scheduler' => true,
324 );
325
326 $segments = explode( '_', $class );
327 $segment = isset( $segments[1] ) ? $segments[1] : $class;
328
329 return isset( $migration_segments[ $segment ] ) && $migration_segments[ $segment ];
330 }
331
332 /**
333 * Determine if the class is one of our WP CLI classes.
334 *
335 * @since 3.0.0
336 *
337 * @param string $class The class name.
338 *
339 * @return bool
340 */
341 protected static function is_class_cli( $class ) {
342 static $cli_segments = array(
343 'QueueRunner' => true,
344 'Command' => true,
345 'ProgressBar' => true,
346 );
347
348 $segments = explode( '_', $class );
349 $segment = isset( $segments[1] ) ? $segments[1] : $class;
350
351 return isset( $cli_segments[ $segment ] ) && $cli_segments[ $segment ];
352 }
353
354 /**
355 * Clone.
356 */
357 final public function __clone() {
358 trigger_error( 'Singleton. No cloning allowed!', E_USER_ERROR ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
359 }
360
361 /**
362 * Wakeup.
363 */
364 final public function __wakeup() {
365 trigger_error( 'Singleton. No serialization allowed!', E_USER_ERROR ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
366 }
367
368 /**
369 * Construct.
370 */
371 final private function __construct() {}
372
373 /** Deprecated **/
374
375 /**
376 * Get DateTime object.
377 *
378 * @param null|string $when Date/time string.
379 * @param string $timezone Timezone string.
380 */
381 public static function get_datetime_object( $when = null, $timezone = 'UTC' ) {
382 _deprecated_function( __METHOD__, '2.0', 'wcs_add_months()' );
383 return as_get_datetime_object( $when, $timezone );
384 }
385
386 /**
387 * Issue deprecated warning if an Action Scheduler function is called in the shutdown hook.
388 *
389 * @param string $function_name The name of the function being called.
390 * @deprecated 3.1.6.
391 */
392 public static function check_shutdown_hook( $function_name ) {
393 _deprecated_function( __FUNCTION__, '3.1.6' );
394 }
395 }
396