PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.6.5
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.6.5
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.6.5, at vendor/woocommerce/action-scheduler/classes/abstracts/ActionScheduler.php

401 lines 10.9 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 WP_CLI::add_command( 'action-scheduler action', '\Action_Scheduler\WP_CLI\Action_Command' );
244 WP_CLI::add_command( 'action-scheduler', '\Action_Scheduler\WP_CLI\System_Command' );
245 if ( ! ActionScheduler_DataController::is_migration_complete() && Controller::instance()->allow_migration() ) {
246 $command = new Migration_Command();
247 $command->register();
248 }
249 }
250
251 /**
252 * Handle WP comment cleanup after migration.
253 */
254 if ( is_a( $logger, 'ActionScheduler_DBLogger' ) && ActionScheduler_DataController::is_migration_complete() && ActionScheduler_WPCommentCleaner::has_logs() ) {
255 ActionScheduler_WPCommentCleaner::init();
256 }
257
258 add_action( 'action_scheduler/migration_complete', 'ActionScheduler_WPCommentCleaner::maybe_schedule_cleanup' );
259 }
260
261 /**
262 * Check whether the AS data store has been initialized.
263 *
264 * @param string $function_name The name of the function being called. Optional. Default `null`.
265 * @return bool
266 */
267 public static function is_initialized( $function_name = null ) {
268 if ( ! self::$data_store_initialized && ! empty( $function_name ) ) {
269 $message = sprintf(
270 /* translators: %s function name. */
271 __( '%s() was called before the Action Scheduler data store was initialized', 'action-scheduler' ),
272 esc_attr( $function_name )
273 );
274 _doing_it_wrong( esc_html( $function_name ), esc_html( $message ), '3.1.6' );
275 }
276
277 return self::$data_store_initialized;
278 }
279
280 /**
281 * Determine if the class is one of our abstract classes.
282 *
283 * @since 3.0.0
284 *
285 * @param string $class The class name.
286 *
287 * @return bool
288 */
289 protected static function is_class_abstract( $class ) {
290 static $abstracts = array(
291 'ActionScheduler' => true,
292 'ActionScheduler_Abstract_ListTable' => true,
293 'ActionScheduler_Abstract_QueueRunner' => true,
294 'ActionScheduler_Abstract_Schedule' => true,
295 'ActionScheduler_Abstract_RecurringSchedule' => true,
296 'ActionScheduler_Lock' => true,
297 'ActionScheduler_Logger' => true,
298 'ActionScheduler_Abstract_Schema' => true,
299 'ActionScheduler_Store' => true,
300 'ActionScheduler_TimezoneHelper' => true,
301 'ActionScheduler_WPCLI_Command' => true,
302 );
303
304 return isset( $abstracts[ $class ] ) && $abstracts[ $class ];
305 }
306
307 /**
308 * Determine if the class is one of our migration classes.
309 *
310 * @since 3.0.0
311 *
312 * @param string $class The class name.
313 *
314 * @return bool
315 */
316 protected static function is_class_migration( $class ) {
317 static $migration_segments = array(
318 'ActionMigrator' => true,
319 'BatchFetcher' => true,
320 'DBStoreMigrator' => true,
321 'DryRun' => true,
322 'LogMigrator' => true,
323 'Config' => true,
324 'Controller' => true,
325 'Runner' => true,
326 'Scheduler' => true,
327 );
328
329 $segments = explode( '_', $class );
330 $segment = isset( $segments[1] ) ? $segments[1] : $class;
331
332 return isset( $migration_segments[ $segment ] ) && $migration_segments[ $segment ];
333 }
334
335 /**
336 * Determine if the class is one of our WP CLI classes.
337 *
338 * @since 3.0.0
339 *
340 * @param string $class The class name.
341 *
342 * @return bool
343 */
344 protected static function is_class_cli( $class ) {
345 static $cli_segments = array(
346 'QueueRunner' => true,
347 'Command' => true,
348 'ProgressBar' => true,
349 '\Action_Scheduler\WP_CLI\Action_Command' => true,
350 '\Action_Scheduler\WP_CLI\System_Command' => true,
351 );
352
353 $segments = explode( '_', $class );
354 $segment = isset( $segments[1] ) ? $segments[1] : $class;
355
356 return isset( $cli_segments[ $segment ] ) && $cli_segments[ $segment ];
357 }
358
359 /**
360 * Clone.
361 */
362 final public function __clone() {
363 trigger_error( 'Singleton. No cloning allowed!', E_USER_ERROR ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
364 }
365
366 /**
367 * Wakeup.
368 */
369 final public function __wakeup() {
370 trigger_error( 'Singleton. No serialization allowed!', E_USER_ERROR ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
371 }
372
373 /**
374 * Construct.
375 */
376 final private function __construct() {}
377
378 /** Deprecated **/
379
380 /**
381 * Get DateTime object.
382 *
383 * @param null|string $when Date/time string.
384 * @param string $timezone Timezone string.
385 */
386 public static function get_datetime_object( $when = null, $timezone = 'UTC' ) {
387 _deprecated_function( __METHOD__, '2.0', 'wcs_add_months()' );
388 return as_get_datetime_object( $when, $timezone );
389 }
390
391 /**
392 * Issue deprecated warning if an Action Scheduler function is called in the shutdown hook.
393 *
394 * @param string $function_name The name of the function being called.
395 * @deprecated 3.1.6.
396 */
397 public static function check_shutdown_hook( $function_name ) {
398 _deprecated_function( __FUNCTION__, '3.1.6' );
399 }
400 }
401