説明なし

ActionScheduler.php 8.5KB

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