jetformbuilder
/
vendor
/
woocommerce
/
action-scheduler
/
classes
/
abstracts
/
ActionScheduler.php
ActionScheduler.php
1 month ago
ActionScheduler_Abstract_ListTable.php
1 year ago
ActionScheduler_Abstract_QueueRunner.php
1 month ago
ActionScheduler_Abstract_RecurringSchedule.php
1 year ago
ActionScheduler_Abstract_Schedule.php
1 year ago
ActionScheduler_Abstract_Schema.php
1 year ago
ActionScheduler_Lock.php
1 year ago
ActionScheduler_Logger.php
1 year ago
ActionScheduler_Store.php
1 year ago
ActionScheduler_TimezoneHelper.php
1 year ago
ActionScheduler_WPCLI_Command.php
1 year ago
ActionScheduler.php
404 lines
| 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 | $recurring_action_scheduler = new ActionScheduler_RecurringActionScheduler(); |
| 192 | |
| 193 | // Ensure initialization on plugin activation. |
| 194 | if ( ! did_action( 'init' ) ) { |
| 195 | // phpcs:ignore Squiz.PHP.CommentedOutCode |
| 196 | add_action( 'init', array( $admin_view, 'init' ), 0, 0 ); // run before $store::init(). |
| 197 | add_action( 'init', array( $store, 'init' ), 1, 0 ); |
| 198 | add_action( 'init', array( $logger, 'init' ), 1, 0 ); |
| 199 | add_action( 'init', array( $runner, 'init' ), 1, 0 ); |
| 200 | add_action( 'init', array( $recurring_action_scheduler, 'init' ), 1, 0 ); |
| 201 | |
| 202 | add_action( |
| 203 | 'init', |
| 204 | /** |
| 205 | * Runs after the active store's init() method has been called. |
| 206 | * |
| 207 | * It would probably be preferable to have $store->init() (or it's parent method) set this itself, |
| 208 | * once it has initialized, however that would cause problems in cases where a custom data store is in |
| 209 | * use and it has not yet been updated to follow that same logic. |
| 210 | */ |
| 211 | function () { |
| 212 | self::$data_store_initialized = true; |
| 213 | |
| 214 | /** |
| 215 | * Fires when Action Scheduler is ready: it is safe to use the procedural API after this point. |
| 216 | * |
| 217 | * @since 3.5.5 |
| 218 | */ |
| 219 | do_action( 'action_scheduler_init' ); |
| 220 | }, |
| 221 | 1 |
| 222 | ); |
| 223 | } else { |
| 224 | $admin_view->init(); |
| 225 | $store->init(); |
| 226 | $logger->init(); |
| 227 | $runner->init(); |
| 228 | $recurring_action_scheduler->init(); |
| 229 | self::$data_store_initialized = true; |
| 230 | |
| 231 | /** |
| 232 | * Fires when Action Scheduler is ready: it is safe to use the procedural API after this point. |
| 233 | * |
| 234 | * @since 3.5.5 |
| 235 | */ |
| 236 | do_action( 'action_scheduler_init' ); |
| 237 | } |
| 238 | |
| 239 | if ( apply_filters( 'action_scheduler_load_deprecated_functions', true ) ) { |
| 240 | require_once self::plugin_path( 'deprecated/functions.php' ); |
| 241 | } |
| 242 | |
| 243 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 244 | WP_CLI::add_command( 'action-scheduler', 'ActionScheduler_WPCLI_Scheduler_command' ); |
| 245 | WP_CLI::add_command( 'action-scheduler', 'ActionScheduler_WPCLI_Clean_Command' ); |
| 246 | WP_CLI::add_command( 'action-scheduler action', '\Action_Scheduler\WP_CLI\Action_Command' ); |
| 247 | WP_CLI::add_command( 'action-scheduler', '\Action_Scheduler\WP_CLI\System_Command' ); |
| 248 | if ( ! ActionScheduler_DataController::is_migration_complete() && Controller::instance()->allow_migration() ) { |
| 249 | $command = new Migration_Command(); |
| 250 | $command->register(); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Handle WP comment cleanup after migration. |
| 256 | */ |
| 257 | if ( is_a( $logger, 'ActionScheduler_DBLogger' ) && ActionScheduler_DataController::is_migration_complete() && ActionScheduler_WPCommentCleaner::has_logs() ) { |
| 258 | ActionScheduler_WPCommentCleaner::init(); |
| 259 | } |
| 260 | |
| 261 | add_action( 'action_scheduler/migration_complete', 'ActionScheduler_WPCommentCleaner::maybe_schedule_cleanup' ); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Check whether the AS data store has been initialized. |
| 266 | * |
| 267 | * @param string $function_name The name of the function being called. Optional. Default `null`. |
| 268 | * @return bool |
| 269 | */ |
| 270 | public static function is_initialized( $function_name = null ) { |
| 271 | if ( ! self::$data_store_initialized && ! empty( $function_name ) ) { |
| 272 | $message = sprintf( |
| 273 | /* translators: %s function name. */ |
| 274 | __( '%s() was called before the Action Scheduler data store was initialized', 'action-scheduler' ), |
| 275 | esc_attr( $function_name ) |
| 276 | ); |
| 277 | _doing_it_wrong( esc_html( $function_name ), esc_html( $message ), '3.1.6' ); |
| 278 | } |
| 279 | |
| 280 | return self::$data_store_initialized; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Determine if the class is one of our abstract classes. |
| 285 | * |
| 286 | * @since 3.0.0 |
| 287 | * |
| 288 | * @param string $class The class name. |
| 289 | * |
| 290 | * @return bool |
| 291 | */ |
| 292 | protected static function is_class_abstract( $class ) { |
| 293 | static $abstracts = array( |
| 294 | 'ActionScheduler' => true, |
| 295 | 'ActionScheduler_Abstract_ListTable' => true, |
| 296 | 'ActionScheduler_Abstract_QueueRunner' => true, |
| 297 | 'ActionScheduler_Abstract_Schedule' => true, |
| 298 | 'ActionScheduler_Abstract_RecurringSchedule' => true, |
| 299 | 'ActionScheduler_Lock' => true, |
| 300 | 'ActionScheduler_Logger' => true, |
| 301 | 'ActionScheduler_Abstract_Schema' => true, |
| 302 | 'ActionScheduler_Store' => true, |
| 303 | 'ActionScheduler_TimezoneHelper' => true, |
| 304 | 'ActionScheduler_WPCLI_Command' => true, |
| 305 | ); |
| 306 | |
| 307 | return isset( $abstracts[ $class ] ) && $abstracts[ $class ]; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Determine if the class is one of our migration classes. |
| 312 | * |
| 313 | * @since 3.0.0 |
| 314 | * |
| 315 | * @param string $class The class name. |
| 316 | * |
| 317 | * @return bool |
| 318 | */ |
| 319 | protected static function is_class_migration( $class ) { |
| 320 | static $migration_segments = array( |
| 321 | 'ActionMigrator' => true, |
| 322 | 'BatchFetcher' => true, |
| 323 | 'DBStoreMigrator' => true, |
| 324 | 'DryRun' => true, |
| 325 | 'LogMigrator' => true, |
| 326 | 'Config' => true, |
| 327 | 'Controller' => true, |
| 328 | 'Runner' => true, |
| 329 | 'Scheduler' => true, |
| 330 | ); |
| 331 | |
| 332 | $segments = explode( '_', $class ); |
| 333 | $segment = isset( $segments[1] ) ? $segments[1] : $class; |
| 334 | |
| 335 | return isset( $migration_segments[ $segment ] ) && $migration_segments[ $segment ]; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Determine if the class is one of our WP CLI classes. |
| 340 | * |
| 341 | * @since 3.0.0 |
| 342 | * |
| 343 | * @param string $class The class name. |
| 344 | * |
| 345 | * @return bool |
| 346 | */ |
| 347 | protected static function is_class_cli( $class ) { |
| 348 | static $cli_segments = array( |
| 349 | 'QueueRunner' => true, |
| 350 | 'Command' => true, |
| 351 | 'ProgressBar' => true, |
| 352 | '\Action_Scheduler\WP_CLI\Action_Command' => true, |
| 353 | '\Action_Scheduler\WP_CLI\System_Command' => true, |
| 354 | ); |
| 355 | |
| 356 | $segments = explode( '_', $class ); |
| 357 | $segment = isset( $segments[1] ) ? $segments[1] : $class; |
| 358 | |
| 359 | return isset( $cli_segments[ $segment ] ) && $cli_segments[ $segment ]; |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Clone. |
| 364 | */ |
| 365 | final public function __clone() { |
| 366 | trigger_error( 'Singleton. No cloning allowed!', E_USER_ERROR ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Wakeup. |
| 371 | */ |
| 372 | final public function __wakeup() { |
| 373 | trigger_error( 'Singleton. No serialization allowed!', E_USER_ERROR ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Construct. |
| 378 | */ |
| 379 | final private function __construct() {} |
| 380 | |
| 381 | /** Deprecated **/ |
| 382 | |
| 383 | /** |
| 384 | * Get DateTime object. |
| 385 | * |
| 386 | * @param null|string $when Date/time string. |
| 387 | * @param string $timezone Timezone string. |
| 388 | */ |
| 389 | public static function get_datetime_object( $when = null, $timezone = 'UTC' ) { |
| 390 | _deprecated_function( __METHOD__, '2.0', 'wcs_add_months()' ); |
| 391 | return as_get_datetime_object( $when, $timezone ); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Issue deprecated warning if an Action Scheduler function is called in the shutdown hook. |
| 396 | * |
| 397 | * @param string $function_name The name of the function being called. |
| 398 | * @deprecated 3.1.6. |
| 399 | */ |
| 400 | public static function check_shutdown_hook( $function_name ) { |
| 401 | _deprecated_function( __FUNCTION__, '3.1.6' ); |
| 402 | } |
| 403 | } |
| 404 |