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
Runner.php
76 lines
| 1 | <?php |
| 2 | namespace Action_Scheduler\Migration; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | class Runner { |
| 5 | private $source_store; |
| 6 | private $destination_store; |
| 7 | private $source_logger; |
| 8 | private $destination_logger; |
| 9 | private $batch_fetcher; |
| 10 | private $action_migrator; |
| 11 | private $log_migrator; |
| 12 | private $progress_bar; |
| 13 | public function __construct( Config $config ) { |
| 14 | $this->source_store = $config->get_source_store(); |
| 15 | $this->destination_store = $config->get_destination_store(); |
| 16 | $this->source_logger = $config->get_source_logger(); |
| 17 | $this->destination_logger = $config->get_destination_logger(); |
| 18 | $this->batch_fetcher = new BatchFetcher( $this->source_store ); |
| 19 | if ( $config->get_dry_run() ) { |
| 20 | $this->log_migrator = new DryRun_LogMigrator( $this->source_logger, $this->destination_logger ); |
| 21 | $this->action_migrator = new DryRun_ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator ); |
| 22 | } else { |
| 23 | $this->log_migrator = new LogMigrator( $this->source_logger, $this->destination_logger ); |
| 24 | $this->action_migrator = new ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator ); |
| 25 | } |
| 26 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 27 | $this->progress_bar = $config->get_progress_bar(); |
| 28 | } |
| 29 | } |
| 30 | public function run( $batch_size = 10 ) { |
| 31 | $batch = $this->batch_fetcher->fetch( $batch_size ); |
| 32 | $batch_size = count( $batch ); |
| 33 | if ( ! $batch_size ) { |
| 34 | return 0; |
| 35 | } |
| 36 | if ( $this->progress_bar ) { |
| 37 | $this->progress_bar->set_message( sprintf( _n( 'Migrating %d action', 'Migrating %d actions', $batch_size, 'action-scheduler' ), $batch_size ) ); |
| 38 | $this->progress_bar->set_count( $batch_size ); |
| 39 | } |
| 40 | $this->migrate_actions( $batch ); |
| 41 | return $batch_size; |
| 42 | } |
| 43 | public function migrate_actions( array $action_ids ) { |
| 44 | do_action( 'action_scheduler/migration_batch_starting', $action_ids ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 45 | \ActionScheduler::logger()->unhook_stored_action(); |
| 46 | $this->destination_logger->unhook_stored_action(); |
| 47 | foreach ( $action_ids as $source_action_id ) { |
| 48 | $destination_action_id = $this->action_migrator->migrate( $source_action_id ); |
| 49 | if ( $destination_action_id ) { |
| 50 | $this->destination_logger->log( |
| 51 | $destination_action_id, |
| 52 | sprintf( |
| 53 | __( 'Migrated action with ID %1$d in %2$s to ID %3$d in %4$s', 'action-scheduler' ), |
| 54 | $source_action_id, |
| 55 | get_class( $this->source_store ), |
| 56 | $destination_action_id, |
| 57 | get_class( $this->destination_store ) |
| 58 | ) |
| 59 | ); |
| 60 | } |
| 61 | if ( $this->progress_bar ) { |
| 62 | $this->progress_bar->tick(); |
| 63 | } |
| 64 | } |
| 65 | if ( $this->progress_bar ) { |
| 66 | $this->progress_bar->finish(); |
| 67 | } |
| 68 | \ActionScheduler::logger()->hook_stored_action(); |
| 69 | do_action( 'action_scheduler/migration_batch_complete', $action_ids ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 70 | } |
| 71 | public function init_destination() { |
| 72 | $this->destination_store->init(); |
| 73 | $this->destination_logger->init(); |
| 74 | } |
| 75 | } |
| 76 |