Açıklama Yok

Runner.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace Action_Scheduler\Migration;
  3. /**
  4. * Class Runner
  5. *
  6. * @package Action_Scheduler\Migration
  7. *
  8. * @since 3.0.0
  9. *
  10. * @codeCoverageIgnore
  11. */
  12. class Runner {
  13. /** @var ActionScheduler_Store */
  14. private $source_store;
  15. /** @var ActionScheduler_Store */
  16. private $destination_store;
  17. /** @var ActionScheduler_Logger */
  18. private $source_logger;
  19. /** @var ActionScheduler_Logger */
  20. private $destination_logger;
  21. /** @var BatchFetcher */
  22. private $batch_fetcher;
  23. /** @var ActionMigrator */
  24. private $action_migrator;
  25. /** @var LogMigrator */
  26. private $log_migrator;
  27. /** @var ProgressBar */
  28. private $progress_bar;
  29. /**
  30. * Runner constructor.
  31. *
  32. * @param Config $config Migration configuration object.
  33. */
  34. public function __construct( Config $config ) {
  35. $this->source_store = $config->get_source_store();
  36. $this->destination_store = $config->get_destination_store();
  37. $this->source_logger = $config->get_source_logger();
  38. $this->destination_logger = $config->get_destination_logger();
  39. $this->batch_fetcher = new BatchFetcher( $this->source_store );
  40. if ( $config->get_dry_run() ) {
  41. $this->log_migrator = new DryRun_LogMigrator( $this->source_logger, $this->destination_logger );
  42. $this->action_migrator = new DryRun_ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator );
  43. } else {
  44. $this->log_migrator = new LogMigrator( $this->source_logger, $this->destination_logger );
  45. $this->action_migrator = new ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator );
  46. }
  47. if ( defined( 'WP_CLI' ) && WP_CLI ) {
  48. $this->progress_bar = $config->get_progress_bar();
  49. }
  50. }
  51. /**
  52. * Run migration batch.
  53. *
  54. * @param int $batch_size Optional batch size. Default 10.
  55. *
  56. * @return int Size of batch processed.
  57. */
  58. public function run( $batch_size = 10 ) {
  59. $batch = $this->batch_fetcher->fetch( $batch_size );
  60. $batch_size = count( $batch );
  61. if ( ! $batch_size ) {
  62. return 0;
  63. }
  64. if ( $this->progress_bar ) {
  65. /* translators: %d: amount of actions */
  66. $this->progress_bar->set_message( sprintf( _n( 'Migrating %d action', 'Migrating %d actions', $batch_size, 'woocommerce' ), number_format_i18n( $batch_size ) ) );
  67. $this->progress_bar->set_count( $batch_size );
  68. }
  69. $this->migrate_actions( $batch );
  70. return $batch_size;
  71. }
  72. /**
  73. * Migration a batch of actions.
  74. *
  75. * @param array $action_ids List of action IDs to migrate.
  76. */
  77. public function migrate_actions( array $action_ids ) {
  78. do_action( 'action_scheduler/migration_batch_starting', $action_ids );
  79. \ActionScheduler::logger()->unhook_stored_action();
  80. $this->destination_logger->unhook_stored_action();
  81. foreach ( $action_ids as $source_action_id ) {
  82. $destination_action_id = $this->action_migrator->migrate( $source_action_id );
  83. if ( $destination_action_id ) {
  84. $this->destination_logger->log( $destination_action_id, sprintf(
  85. /* translators: 1: source action ID 2: source store class 3: destination action ID 4: destination store class */
  86. __( 'Migrated action with ID %1$d in %2$s to ID %3$d in %4$s', 'woocommerce' ),
  87. $source_action_id,
  88. get_class( $this->source_store ),
  89. $destination_action_id,
  90. get_class( $this->destination_store )
  91. ) );
  92. }
  93. if ( $this->progress_bar ) {
  94. $this->progress_bar->tick();
  95. }
  96. }
  97. if ( $this->progress_bar ) {
  98. $this->progress_bar->finish();
  99. }
  100. \ActionScheduler::logger()->hook_stored_action();
  101. do_action( 'action_scheduler/migration_batch_complete', $action_ids );
  102. }
  103. /**
  104. * Initialize destination store and logger.
  105. */
  106. public function init_destination() {
  107. $this->destination_store->init();
  108. $this->destination_logger->init();
  109. }
  110. }