| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Action_Scheduler\Migration; |
| 5 |
|
| 6 |
/** |
| 7 |
* Class Runner |
| 8 |
* |
| 9 |
* @package Action_Scheduler\Migration |
| 10 |
* |
| 11 |
* @since 3.0.0 |
| 12 |
* |
| 13 |
* @codeCoverageIgnore |
| 14 |
*/ |
| 15 |
class Runner { |
| 16 |
/** |
| 17 |
* Source store instance. |
| 18 |
* |
| 19 |
* @var ActionScheduler_Store |
| 20 |
*/ |
| 21 |
private $source_store; |
| 22 |
|
| 23 |
/** |
| 24 |
* Destination store instance. |
| 25 |
* |
| 26 |
* @var ActionScheduler_Store |
| 27 |
*/ |
| 28 |
private $destination_store; |
| 29 |
|
| 30 |
/** |
| 31 |
* Source logger instance. |
| 32 |
* |
| 33 |
* @var ActionScheduler_Logger |
| 34 |
*/ |
| 35 |
private $source_logger; |
| 36 |
|
| 37 |
/** |
| 38 |
* Destination logger instance. |
| 39 |
* |
| 40 |
* @var ActionScheduler_Logger |
| 41 |
*/ |
| 42 |
private $destination_logger; |
| 43 |
|
| 44 |
/** |
| 45 |
* Batch fetcher instance. |
| 46 |
* |
| 47 |
* @var BatchFetcher |
| 48 |
*/ |
| 49 |
private $batch_fetcher; |
| 50 |
|
| 51 |
/** |
| 52 |
* Action migrator instance. |
| 53 |
* |
| 54 |
* @var ActionMigrator |
| 55 |
*/ |
| 56 |
private $action_migrator; |
| 57 |
|
| 58 |
/** |
| 59 |
* Log migrator instance. |
| 60 |
* |
| 61 |
* @var LogMigrator |
| 62 |
*/ |
| 63 |
private $log_migrator; |
| 64 |
|
| 65 |
/** |
| 66 |
* Progress bar instance. |
| 67 |
* |
| 68 |
* @var ProgressBar |
| 69 |
*/ |
| 70 |
private $progress_bar; |
| 71 |
|
| 72 |
/** |
| 73 |
* Runner constructor. |
| 74 |
* |
| 75 |
* @param Config $config Migration configuration object. |
| 76 |
*/ |
| 77 |
public function __construct( Config $config ) { |
| 78 |
$this->source_store = $config->get_source_store(); |
| 79 |
$this->destination_store = $config->get_destination_store(); |
| 80 |
$this->source_logger = $config->get_source_logger(); |
| 81 |
$this->destination_logger = $config->get_destination_logger(); |
| 82 |
|
| 83 |
$this->batch_fetcher = new BatchFetcher( $this->source_store ); |
| 84 |
if ( $config->get_dry_run() ) { |
| 85 |
$this->log_migrator = new DryRun_LogMigrator( $this->source_logger, $this->destination_logger ); |
| 86 |
$this->action_migrator = new DryRun_ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator ); |
| 87 |
} else { |
| 88 |
$this->log_migrator = new LogMigrator( $this->source_logger, $this->destination_logger ); |
| 89 |
$this->action_migrator = new ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator ); |
| 90 |
} |
| 91 |
|
| 92 |
if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 93 |
$this->progress_bar = $config->get_progress_bar(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Run migration batch. |
| 99 |
* |
| 100 |
* @param int $batch_size Optional batch size. Default 10. |
| 101 |
* |
| 102 |
* @return int Size of batch processed. |
| 103 |
*/ |
| 104 |
public function run( $batch_size = 10 ) { |
| 105 |
$batch = $this->batch_fetcher->fetch( $batch_size ); |
| 106 |
$batch_size = count( $batch ); |
| 107 |
|
| 108 |
if ( ! $batch_size ) { |
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
if ( $this->progress_bar ) { |
| 113 |
/* translators: %d: amount of actions */ |
| 114 |
$this->progress_bar->set_message( sprintf( _n( 'Migrating %d action', 'Migrating %d actions', $batch_size, 'action-scheduler' ), $batch_size ) ); |
| 115 |
$this->progress_bar->set_count( $batch_size ); |
| 116 |
} |
| 117 |
|
| 118 |
$this->migrate_actions( $batch ); |
| 119 |
|
| 120 |
return $batch_size; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Migration a batch of actions. |
| 125 |
* |
| 126 |
* @param array $action_ids List of action IDs to migrate. |
| 127 |
*/ |
| 128 |
public function migrate_actions( array $action_ids ) { |
| 129 |
do_action( 'action_scheduler/migration_batch_starting', $action_ids ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 130 |
|
| 131 |
\ActionScheduler::logger()->unhook_stored_action(); |
| 132 |
$this->destination_logger->unhook_stored_action(); |
| 133 |
|
| 134 |
foreach ( $action_ids as $source_action_id ) { |
| 135 |
$destination_action_id = $this->action_migrator->migrate( $source_action_id ); |
| 136 |
if ( $destination_action_id ) { |
| 137 |
$this->destination_logger->log( |
| 138 |
$destination_action_id, |
| 139 |
sprintf( |
| 140 |
/* translators: 1: source action ID 2: source store class 3: destination action ID 4: destination store class */ |
| 141 |
__( 'Migrated action with ID %1$d in %2$s to ID %3$d in %4$s', 'action-scheduler' ), |
| 142 |
$source_action_id, |
| 143 |
get_class( $this->source_store ), |
| 144 |
$destination_action_id, |
| 145 |
get_class( $this->destination_store ) |
| 146 |
) |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
if ( $this->progress_bar ) { |
| 151 |
$this->progress_bar->tick(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
if ( $this->progress_bar ) { |
| 156 |
$this->progress_bar->finish(); |
| 157 |
} |
| 158 |
|
| 159 |
\ActionScheduler::logger()->hook_stored_action(); |
| 160 |
|
| 161 |
do_action( 'action_scheduler/migration_batch_complete', $action_ids ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Initialize destination store and logger. |
| 166 |
*/ |
| 167 |
public function init_destination() { |
| 168 |
$this->destination_store->init(); |
| 169 |
$this->destination_logger->init(); |
| 170 |
} |
| 171 |
} |
| 172 |
|