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