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

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