ActionMigrator.php
2 years ago
ActionScheduler_DBStoreMigrator.php
2 years ago
BatchFetcher.php
2 years ago
Config.php
2 years ago
Controller.php
2 years ago
DryRun_ActionMigrator.php
2 years ago
DryRun_LogMigrator.php
2 years ago
LogMigrator.php
2 years ago
Runner.php
2 years ago
Scheduler.php
2 years ago
Controller.php
227 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Action_Scheduler\Migration; |
| 4 | |
| 5 | use ActionScheduler_DataController; |
| 6 | use ActionScheduler_LoggerSchema; |
| 7 | use ActionScheduler_StoreSchema; |
| 8 | use Action_Scheduler\WP_CLI\ProgressBar; |
| 9 | |
| 10 | /** |
| 11 | * Class Controller |
| 12 | * |
| 13 | * The main plugin/initialization class for migration to custom tables. |
| 14 | * |
| 15 | * @package Action_Scheduler\Migration |
| 16 | * |
| 17 | * @since 3.0.0 |
| 18 | * |
| 19 | * @codeCoverageIgnore |
| 20 | */ |
| 21 | class Controller { |
| 22 | private static $instance; |
| 23 | |
| 24 | /** @var Action_Scheduler\Migration\Scheduler */ |
| 25 | private $migration_scheduler; |
| 26 | |
| 27 | /** @var string */ |
| 28 | private $store_classname; |
| 29 | |
| 30 | /** @var string */ |
| 31 | private $logger_classname; |
| 32 | |
| 33 | /** @var bool */ |
| 34 | private $migrate_custom_store; |
| 35 | |
| 36 | /** |
| 37 | * Controller constructor. |
| 38 | * |
| 39 | * @param Scheduler $migration_scheduler Migration scheduler object. |
| 40 | */ |
| 41 | protected function __construct( Scheduler $migration_scheduler ) { |
| 42 | $this->migration_scheduler = $migration_scheduler; |
| 43 | $this->store_classname = ''; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Set the action store class name. |
| 48 | * |
| 49 | * @param string $class Classname of the store class. |
| 50 | * |
| 51 | * @return string |
| 52 | */ |
| 53 | public function get_store_class( $class ) { |
| 54 | if ( \ActionScheduler_DataController::is_migration_complete() ) { |
| 55 | return \ActionScheduler_DataController::DATASTORE_CLASS; |
| 56 | } elseif ( \ActionScheduler_Store::DEFAULT_CLASS !== $class ) { |
| 57 | $this->store_classname = $class; |
| 58 | return $class; |
| 59 | } else { |
| 60 | return 'ActionScheduler_HybridStore'; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Set the action logger class name. |
| 66 | * |
| 67 | * @param string $class Classname of the logger class. |
| 68 | * |
| 69 | * @return string |
| 70 | */ |
| 71 | public function get_logger_class( $class ) { |
| 72 | \ActionScheduler_Store::instance(); |
| 73 | |
| 74 | if ( $this->has_custom_datastore() ) { |
| 75 | $this->logger_classname = $class; |
| 76 | return $class; |
| 77 | } else { |
| 78 | return \ActionScheduler_DataController::LOGGER_CLASS; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Get flag indicating whether a custom datastore is in use. |
| 84 | * |
| 85 | * @return bool |
| 86 | */ |
| 87 | public function has_custom_datastore() { |
| 88 | return (bool) $this->store_classname; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Set up the background migration process. |
| 93 | * |
| 94 | * @return void |
| 95 | */ |
| 96 | public function schedule_migration() { |
| 97 | $logging_tables = new ActionScheduler_LoggerSchema(); |
| 98 | $store_tables = new ActionScheduler_StoreSchema(); |
| 99 | |
| 100 | /* |
| 101 | * In some unusual cases, the expected tables may not have been created. In such cases |
| 102 | * we do not schedule a migration as doing so will lead to fatal error conditions. |
| 103 | * |
| 104 | * In such cases the user will likely visit the Tools > Scheduled Actions screen to |
| 105 | * investigate, and will see appropriate messaging (this step also triggers an attempt |
| 106 | * to rebuild any missing tables). |
| 107 | * |
| 108 | * @see https://github.com/woocommerce/action-scheduler/issues/653 |
| 109 | */ |
| 110 | if ( |
| 111 | ActionScheduler_DataController::is_migration_complete() |
| 112 | || $this->migration_scheduler->is_migration_scheduled() |
| 113 | || ! $store_tables->tables_exist() |
| 114 | || ! $logging_tables->tables_exist() |
| 115 | ) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | $this->migration_scheduler->schedule_migration(); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Get the default migration config object |
| 124 | * |
| 125 | * @return ActionScheduler\Migration\Config |
| 126 | */ |
| 127 | public function get_migration_config_object() { |
| 128 | static $config = null; |
| 129 | |
| 130 | if ( ! $config ) { |
| 131 | $source_store = $this->store_classname ? new $this->store_classname() : new \ActionScheduler_wpPostStore(); |
| 132 | $source_logger = $this->logger_classname ? new $this->logger_classname() : new \ActionScheduler_wpCommentLogger(); |
| 133 | |
| 134 | $config = new Config(); |
| 135 | $config->set_source_store( $source_store ); |
| 136 | $config->set_source_logger( $source_logger ); |
| 137 | $config->set_destination_store( new \ActionScheduler_DBStoreMigrator() ); |
| 138 | $config->set_destination_logger( new \ActionScheduler_DBLogger() ); |
| 139 | |
| 140 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 141 | $config->set_progress_bar( new ProgressBar( '', 0 ) ); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return apply_filters( 'action_scheduler/migration_config', $config ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Hook dashboard migration notice. |
| 150 | */ |
| 151 | public function hook_admin_notices() { |
| 152 | if ( ! $this->allow_migration() || \ActionScheduler_DataController::is_migration_complete() ) { |
| 153 | return; |
| 154 | } |
| 155 | add_action( 'admin_notices', array( $this, 'display_migration_notice' ), 10, 0 ); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Show a dashboard notice that migration is in progress. |
| 160 | */ |
| 161 | public function display_migration_notice() { |
| 162 | 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' ) ); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Add store classes. Hook migration. |
| 167 | */ |
| 168 | private function hook() { |
| 169 | add_filter( 'action_scheduler_store_class', array( $this, 'get_store_class' ), 100, 1 ); |
| 170 | add_filter( 'action_scheduler_logger_class', array( $this, 'get_logger_class' ), 100, 1 ); |
| 171 | add_action( 'init', array( $this, 'maybe_hook_migration' ) ); |
| 172 | add_action( 'wp_loaded', array( $this, 'schedule_migration' ) ); |
| 173 | |
| 174 | // Action Scheduler may be displayed as a Tools screen or WooCommerce > Status administration screen |
| 175 | add_action( 'load-tools_page_action-scheduler', array( $this, 'hook_admin_notices' ), 10, 0 ); |
| 176 | add_action( 'load-woocommerce_page_wc-status', array( $this, 'hook_admin_notices' ), 10, 0 ); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Possibly hook the migration scheduler action. |
| 181 | * |
| 182 | * @author Jeremy Pry |
| 183 | */ |
| 184 | public function maybe_hook_migration() { |
| 185 | if ( ! $this->allow_migration() || \ActionScheduler_DataController::is_migration_complete() ) { |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | $this->migration_scheduler->hook(); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Allow datastores to enable migration to AS tables. |
| 194 | */ |
| 195 | public function allow_migration() { |
| 196 | if ( ! \ActionScheduler_DataController::dependencies_met() ) { |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | if ( null === $this->migrate_custom_store ) { |
| 201 | $this->migrate_custom_store = apply_filters( 'action_scheduler_migrate_data_store', false ); |
| 202 | } |
| 203 | |
| 204 | return ( ! $this->has_custom_datastore() ) || $this->migrate_custom_store; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Proceed with the migration if the dependencies have been met. |
| 209 | */ |
| 210 | public static function init() { |
| 211 | if ( \ActionScheduler_DataController::dependencies_met() ) { |
| 212 | self::instance()->hook(); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Singleton factory. |
| 218 | */ |
| 219 | public static function instance() { |
| 220 | if ( ! isset( self::$instance ) ) { |
| 221 | self::$instance = new static( new Scheduler() ); |
| 222 | } |
| 223 | |
| 224 | return self::$instance; |
| 225 | } |
| 226 | } |
| 227 |