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
ActionMigrator.php
70 lines
| 1 | <?php |
| 2 | namespace Action_Scheduler\Migration; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | class ActionMigrator { |
| 5 | private $source; |
| 6 | private $destination; |
| 7 | private $log_migrator; |
| 8 | public function __construct( \ActionScheduler_Store $source_store, \ActionScheduler_Store $destination_store, LogMigrator $log_migrator ) { |
| 9 | $this->source = $source_store; |
| 10 | $this->destination = $destination_store; |
| 11 | $this->log_migrator = $log_migrator; |
| 12 | } |
| 13 | public function migrate( $source_action_id ) { |
| 14 | try { |
| 15 | $action = $this->source->fetch_action( $source_action_id ); |
| 16 | $status = $this->source->get_status( $source_action_id ); |
| 17 | } catch ( \Exception $e ) { |
| 18 | $action = null; |
| 19 | $status = ''; |
| 20 | } |
| 21 | if ( is_null( $action ) || empty( $status ) || ! $action->get_schedule()->get_date() ) { |
| 22 | // null action or empty status means the fetch operation failed or the action didn't exist. |
| 23 | // null schedule means it's missing vital data. |
| 24 | // delete it and move on. |
| 25 | try { |
| 26 | $this->source->delete_action( $source_action_id ); |
| 27 | } catch ( \Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch |
| 28 | // nothing to do, it didn't exist in the first place. |
| 29 | } |
| 30 | do_action( 'action_scheduler/no_action_to_migrate', $source_action_id, $this->source, $this->destination ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 31 | return 0; |
| 32 | } |
| 33 | try { |
| 34 | // Make sure the last attempt date is set correctly for completed and failed actions. |
| 35 | $last_attempt_date = ( \ActionScheduler_Store::STATUS_PENDING !== $status ) ? $this->source->get_date( $source_action_id ) : null; |
| 36 | $destination_action_id = $this->destination->save_action( $action, null, $last_attempt_date ); |
| 37 | } catch ( \Exception $e ) { |
| 38 | do_action( 'action_scheduler/migrate_action_failed', $source_action_id, $this->source, $this->destination ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 39 | return 0; // could not save the action in the new store. |
| 40 | } |
| 41 | try { |
| 42 | switch ( $status ) { |
| 43 | case \ActionScheduler_Store::STATUS_FAILED: |
| 44 | $this->destination->mark_failure( $destination_action_id ); |
| 45 | break; |
| 46 | case \ActionScheduler_Store::STATUS_CANCELED: |
| 47 | $this->destination->cancel_action( $destination_action_id ); |
| 48 | break; |
| 49 | } |
| 50 | $this->log_migrator->migrate( $source_action_id, $destination_action_id ); |
| 51 | $this->source->delete_action( $source_action_id ); |
| 52 | $test_action = $this->source->fetch_action( $source_action_id ); |
| 53 | if ( ! is_a( $test_action, 'ActionScheduler_NullAction' ) ) { |
| 54 | // translators: %s is an action ID. |
| 55 | throw new \RuntimeException( sprintf( __( 'Unable to remove source migrated action %s', 'action-scheduler' ), $source_action_id ) ); |
| 56 | } |
| 57 | do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 58 | return $destination_action_id; |
| 59 | } catch ( \Exception $e ) { |
| 60 | // could not delete from the old store. |
| 61 | $this->source->mark_migrated( $source_action_id ); |
| 62 | // phpcs:disable WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 63 | do_action( 'action_scheduler/migrate_action_incomplete', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
| 64 | do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
| 65 | // phpcs:enable |
| 66 | return $destination_action_id; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 |