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