| 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 |
} else { |
| 157 |
$admin_view->init(); |
| 158 |
$store->init(); |
| 159 |
$logger->init(); |
| 160 |
$runner->init(); |
| 161 |
} |
| 162 |
|
| 163 |
if ( apply_filters( 'action_scheduler_load_deprecated_functions', true ) ) { |
| 164 |
require_once( self::plugin_path( 'deprecated/functions.php' ) ); |
| 165 |
} |
| 166 |
|
| 167 |
if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 168 |
WP_CLI::add_command( 'action-scheduler', 'ActionScheduler_WPCLI_Scheduler_command' ); |
| 169 |
if ( ! ActionScheduler_DataController::is_migration_complete() && Controller::instance()->allow_migration() ) { |
| 170 |
$command = new Migration_Command(); |
| 171 |
$command->register(); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
self::$data_store_initialized = true; |
| 176 |
|
| 177 |
/** |
| 178 |
* Handle WP comment cleanup after migration. |
| 179 |
*/ |
| 180 |
if ( is_a( $logger, 'ActionScheduler_DBLogger' ) && ActionScheduler_DataController::is_migration_complete() && ActionScheduler_WPCommentCleaner::has_logs() ) { |
| 181 |
ActionScheduler_WPCommentCleaner::init(); |
| 182 |
} |
| 183 |
|
| 184 |
add_action( 'action_scheduler/migration_complete', 'ActionScheduler_WPCommentCleaner::maybe_schedule_cleanup' ); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Check whether the AS data store has been initialized. |
| 189 |
* |
| 190 |
* @param string $function_name The name of the function being called. Optional. Default `null`. |
| 191 |
* @return bool |
| 192 |
*/ |
| 193 |
public static function is_initialized( $function_name = null ) { |
| 194 |
if ( ! self::$data_store_initialized && ! empty( $function_name ) ) { |
| 195 |
$message = sprintf( __( '%s() was called before the Action Scheduler data store was initialized', 'action-scheduler' ), esc_attr( $function_name ) ); |
| 196 |
error_log( $message, E_WARNING ); |
| 197 |
} |
| 198 |
|
| 199 |
return self::$data_store_initialized; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Determine if the class is one of our abstract classes. |
| 204 |
* |
| 205 |
* @since 3.0.0 |
| 206 |
* |
| 207 |
* @param string $class The class name. |
| 208 |
* |
| 209 |
* @return bool |
| 210 |
*/ |
| 211 |
protected static function is_class_abstract( $class ) { |
| 212 |
static $abstracts = array( |
| 213 |
'ActionScheduler' => true, |
| 214 |
'ActionScheduler_Abstract_ListTable' => true, |
| 215 |
'ActionScheduler_Abstract_QueueRunner' => true, |
| 216 |
'ActionScheduler_Abstract_Schedule' => true, |
| 217 |
'ActionScheduler_Abstract_RecurringSchedule' => true, |
| 218 |
'ActionScheduler_Lock' => true, |
| 219 |
'ActionScheduler_Logger' => true, |
| 220 |
'ActionScheduler_Abstract_Schema' => true, |
| 221 |
'ActionScheduler_Store' => true, |
| 222 |
'ActionScheduler_TimezoneHelper' => true, |
| 223 |
); |
| 224 |
|
| 225 |
return isset( $abstracts[ $class ] ) && $abstracts[ $class ]; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Determine if the class is one of our migration classes. |
| 230 |
* |
| 231 |
* @since 3.0.0 |
| 232 |
* |
| 233 |
* @param string $class The class name. |
| 234 |
* |
| 235 |
* @return bool |
| 236 |
*/ |
| 237 |
protected static function is_class_migration( $class ) { |
| 238 |
static $migration_segments = array( |
| 239 |
'ActionMigrator' => true, |
| 240 |
'BatchFetcher' => true, |
| 241 |
'DBStoreMigrator' => true, |
| 242 |
'DryRun' => true, |
| 243 |
'LogMigrator' => true, |
| 244 |
'Config' => true, |
| 245 |
'Controller' => true, |
| 246 |
'Runner' => true, |
| 247 |
'Scheduler' => true, |
| 248 |
); |
| 249 |
|
| 250 |
$segments = explode( '_', $class ); |
| 251 |
$segment = isset( $segments[ 1 ] ) ? $segments[ 1 ] : $class; |
| 252 |
|
| 253 |
return isset( $migration_segments[ $segment ] ) && $migration_segments[ $segment ]; |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Determine if the class is one of our WP CLI classes. |
| 258 |
* |
| 259 |
* @since 3.0.0 |
| 260 |
* |
| 261 |
* @param string $class The class name. |
| 262 |
* |
| 263 |
* @return bool |
| 264 |
*/ |
| 265 |
protected static function is_class_cli( $class ) { |
| 266 |
static $cli_segments = array( |
| 267 |
'QueueRunner' => true, |
| 268 |
'Command' => true, |
| 269 |
'ProgressBar' => true, |
| 270 |
); |
| 271 |
|
| 272 |
$segments = explode( '_', $class ); |
| 273 |
$segment = isset( $segments[ 1 ] ) ? $segments[ 1 ] : $class; |
| 274 |
|
| 275 |
return isset( $cli_segments[ $segment ] ) && $cli_segments[ $segment ]; |
| 276 |
} |
| 277 |
|
| 278 |
final public function __clone() { |
| 279 |
trigger_error("Singleton. No cloning allowed!", E_USER_ERROR); |
| 280 |
} |
| 281 |
|
| 282 |
final public function __wakeup() { |
| 283 |
trigger_error("Singleton. No serialization allowed!", E_USER_ERROR); |
| 284 |
} |
| 285 |
|
| 286 |
final private function __construct() {} |
| 287 |
|
| 288 |
/** Deprecated **/ |
| 289 |
|
| 290 |
public static function get_datetime_object( $when = null, $timezone = 'UTC' ) { |
| 291 |
_deprecated_function( __METHOD__, '2.0', 'wcs_add_months()' ); |
| 292 |
return as_get_datetime_object( $when, $timezone ); |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Issue deprecated warning if an Action Scheduler function is called in the shutdown hook. |
| 297 |
* |
| 298 |
* @param string $function_name The name of the function being called. |
| 299 |
* @deprecated 3.1.6. |
| 300 |
*/ |
| 301 |
public static function check_shutdown_hook( $function_name ) { |
| 302 |
_deprecated_function( __FUNCTION__, '3.1.6' ); |
| 303 |
} |
| 304 |
} |
| 305 |
|