ActionMigrator.php
1 year ago
ActionScheduler_DBStoreMigrator.php
1 year ago
BatchFetcher.php
1 year ago
Config.php
4 years ago
Controller.php
1 year ago
DryRun_ActionMigrator.php
1 year ago
DryRun_LogMigrator.php
1 year ago
LogMigrator.php
1 year ago
Runner.php
1 year ago
Scheduler.php
1 year ago
index.php
3 years ago
Controller.php
114 lines
| 1 | <?php |
| 2 | namespace Action_Scheduler\Migration; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use ActionScheduler_DataController; |
| 5 | use ActionScheduler_LoggerSchema; |
| 6 | use ActionScheduler_StoreSchema; |
| 7 | use Action_Scheduler\WP_CLI\ProgressBar; |
| 8 | class Controller { |
| 9 | private static $instance; |
| 10 | private $migration_scheduler; |
| 11 | private $store_classname; |
| 12 | private $logger_classname; |
| 13 | private $migrate_custom_store; |
| 14 | protected function __construct( Scheduler $migration_scheduler ) { |
| 15 | $this->migration_scheduler = $migration_scheduler; |
| 16 | $this->store_classname = ''; |
| 17 | } |
| 18 | public function get_store_class( $class ) { |
| 19 | if ( \ActionScheduler_DataController::is_migration_complete() ) { |
| 20 | return \ActionScheduler_DataController::DATASTORE_CLASS; |
| 21 | } elseif ( \ActionScheduler_Store::DEFAULT_CLASS !== $class ) { |
| 22 | $this->store_classname = $class; |
| 23 | return $class; |
| 24 | } else { |
| 25 | return 'ActionScheduler_HybridStore'; |
| 26 | } |
| 27 | } |
| 28 | public function get_logger_class( $class ) { |
| 29 | \ActionScheduler_Store::instance(); |
| 30 | if ( $this->has_custom_datastore() ) { |
| 31 | $this->logger_classname = $class; |
| 32 | return $class; |
| 33 | } else { |
| 34 | return \ActionScheduler_DataController::LOGGER_CLASS; |
| 35 | } |
| 36 | } |
| 37 | public function has_custom_datastore() { |
| 38 | return (bool) $this->store_classname; |
| 39 | } |
| 40 | public function schedule_migration() { |
| 41 | $logging_tables = new ActionScheduler_LoggerSchema(); |
| 42 | $store_tables = new ActionScheduler_StoreSchema(); |
| 43 | if ( |
| 44 | ActionScheduler_DataController::is_migration_complete() |
| 45 | || $this->migration_scheduler->is_migration_scheduled() |
| 46 | || ! $store_tables->tables_exist() |
| 47 | || ! $logging_tables->tables_exist() |
| 48 | ) { |
| 49 | return; |
| 50 | } |
| 51 | $this->migration_scheduler->schedule_migration(); |
| 52 | } |
| 53 | public function get_migration_config_object() { |
| 54 | static $config = null; |
| 55 | if ( ! $config ) { |
| 56 | $source_store = $this->store_classname ? new $this->store_classname() : new \ActionScheduler_wpPostStore(); |
| 57 | $source_logger = $this->logger_classname ? new $this->logger_classname() : new \ActionScheduler_wpCommentLogger(); |
| 58 | $config = new Config(); |
| 59 | $config->set_source_store( $source_store ); |
| 60 | $config->set_source_logger( $source_logger ); |
| 61 | $config->set_destination_store( new \ActionScheduler_DBStoreMigrator() ); |
| 62 | $config->set_destination_logger( new \ActionScheduler_DBLogger() ); |
| 63 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 64 | $config->set_progress_bar( new ProgressBar( '', 0 ) ); |
| 65 | } |
| 66 | } |
| 67 | return apply_filters( 'action_scheduler/migration_config', $config ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 68 | } |
| 69 | public function hook_admin_notices() { |
| 70 | if ( ! $this->allow_migration() || \ActionScheduler_DataController::is_migration_complete() ) { |
| 71 | return; |
| 72 | } |
| 73 | add_action( 'admin_notices', array( $this, 'display_migration_notice' ), 10, 0 ); |
| 74 | } |
| 75 | public function display_migration_notice() { |
| 76 | printf( '<div class="notice notice-warning"><p>%s</p></div>', esc_html__( 'Action Scheduler migration in progress. The list of scheduled actions may be incomplete.', 'action-scheduler' ) ); |
| 77 | } |
| 78 | private function hook() { |
| 79 | add_filter( 'action_scheduler_store_class', array( $this, 'get_store_class' ), 100, 1 ); |
| 80 | add_filter( 'action_scheduler_logger_class', array( $this, 'get_logger_class' ), 100, 1 ); |
| 81 | add_action( 'init', array( $this, 'maybe_hook_migration' ) ); |
| 82 | add_action( 'wp_loaded', array( $this, 'schedule_migration' ) ); |
| 83 | // Action Scheduler may be displayed as a Tools screen or WooCommerce > Status administration screen. |
| 84 | add_action( 'load-tools_page_action-scheduler', array( $this, 'hook_admin_notices' ), 10, 0 ); |
| 85 | add_action( 'load-woocommerce_page_wc-status', array( $this, 'hook_admin_notices' ), 10, 0 ); |
| 86 | } |
| 87 | public function maybe_hook_migration() { |
| 88 | if ( ! $this->allow_migration() || \ActionScheduler_DataController::is_migration_complete() ) { |
| 89 | return; |
| 90 | } |
| 91 | $this->migration_scheduler->hook(); |
| 92 | } |
| 93 | public function allow_migration() { |
| 94 | if ( ! \ActionScheduler_DataController::dependencies_met() ) { |
| 95 | return false; |
| 96 | } |
| 97 | if ( null === $this->migrate_custom_store ) { |
| 98 | $this->migrate_custom_store = apply_filters( 'action_scheduler_migrate_data_store', false ); |
| 99 | } |
| 100 | return ( ! $this->has_custom_datastore() ) || $this->migrate_custom_store; |
| 101 | } |
| 102 | public static function init() { |
| 103 | if ( \ActionScheduler_DataController::dependencies_met() ) { |
| 104 | self::instance()->hook(); |
| 105 | } |
| 106 | } |
| 107 | public static function instance() { |
| 108 | if ( ! isset( self::$instance ) ) { |
| 109 | self::$instance = new static( new Scheduler() ); |
| 110 | } |
| 111 | return self::$instance; |
| 112 | } |
| 113 | } |
| 114 |