PluginProbe
WANotifier for Forms and Actions / 3.1.1
WANotifier for Forms and Actions v3.1.1
3.1.1 3.1.0 3.0.4 2.7.10 2.7.11 2.7.12 2.7.13 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 3.0.0 3.0.1 3.0.2 3.0.3 trunk 0.1.0 0.1.1 1.0.0 1.0.1 1.0.2 All 67 releases
notifier / libraries / action-scheduler / classes / migration / ActionMigrator.php

ActionMigrator.php in WANotifier for Forms and Actions 3.1.1, at libraries/action-scheduler/classes/migration/ActionMigrator.php

127 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 namespace Action_Scheduler\Migration;
5
6 /**
7 * Class ActionMigrator
8 *
9 * @package Action_Scheduler\Migration
10 *
11 * @since 3.0.0
12 *
13 * @codeCoverageIgnore
14 */
15 class ActionMigrator {
16 /**
17 * Source store instance.
18 *
19 * @var ActionScheduler_Store
20 */
21 private $source;
22
23 /**
24 * Destination store instance.
25 *
26 * @var ActionScheduler_Store
27 */
28 private $destination;
29
30 /**
31 * LogMigrator instance.
32 *
33 * @var LogMigrator
34 */
35 private $log_migrator;
36
37 /**
38 * ActionMigrator constructor.
39 *
40 * @param \ActionScheduler_Store $source_store Source store object.
41 * @param \ActionScheduler_Store $destination_store Destination store object.
42 * @param LogMigrator $log_migrator Log migrator object.
43 */
44 public function __construct( \ActionScheduler_Store $source_store, \ActionScheduler_Store $destination_store, LogMigrator $log_migrator ) {
45 $this->source = $source_store;
46 $this->destination = $destination_store;
47 $this->log_migrator = $log_migrator;
48 }
49
50 /**
51 * Migrate an action.
52 *
53 * @param int $source_action_id Action ID.
54 *
55 * @return int 0|new action ID
56 * @throws \RuntimeException When unable to delete action from the source store.
57 */
58 public function migrate( $source_action_id ) {
59 try {
60 $action = $this->source->fetch_action( $source_action_id );
61 $status = $this->source->get_status( $source_action_id );
62 } catch ( \Exception $e ) {
63 $action = null;
64 $status = '';
65 }
66
67 if ( is_null( $action ) || empty( $status ) || ! $action->get_schedule()->get_date() ) {
68 // null action or empty status means the fetch operation failed or the action didn't exist.
69 // null schedule means it's missing vital data.
70 // delete it and move on.
71 try {
72 $this->source->delete_action( $source_action_id );
73 } catch ( \Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
74 // nothing to do, it didn't exist in the first place.
75 }
76 do_action( 'action_scheduler/no_action_to_migrate', $source_action_id, $this->source, $this->destination ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
77
78 return 0;
79 }
80
81 try {
82
83 // Make sure the last attempt date is set correctly for completed and failed actions.
84 $last_attempt_date = ( \ActionScheduler_Store::STATUS_PENDING !== $status ) ? $this->source->get_date( $source_action_id ) : null;
85
86 $destination_action_id = $this->destination->save_action( $action, null, $last_attempt_date );
87 } catch ( \Exception $e ) {
88 do_action( 'action_scheduler/migrate_action_failed', $source_action_id, $this->source, $this->destination ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
89
90 return 0; // could not save the action in the new store.
91 }
92
93 try {
94 switch ( $status ) {
95 case \ActionScheduler_Store::STATUS_FAILED:
96 $this->destination->mark_failure( $destination_action_id );
97 break;
98 case \ActionScheduler_Store::STATUS_CANCELED:
99 $this->destination->cancel_action( $destination_action_id );
100 break;
101 }
102
103 $this->log_migrator->migrate( $source_action_id, $destination_action_id );
104 $this->source->delete_action( $source_action_id );
105
106 $test_action = $this->source->fetch_action( $source_action_id );
107 if ( ! is_a( $test_action, 'ActionScheduler_NullAction' ) ) {
108 // translators: %s is an action ID.
109 throw new \RuntimeException( sprintf( __( 'Unable to remove source migrated action %s', 'action-scheduler' ), $source_action_id ) );
110 }
111 do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
112
113 return $destination_action_id;
114 } catch ( \Exception $e ) {
115 // could not delete from the old store.
116 $this->source->mark_migrated( $source_action_id );
117
118 // phpcs:disable WordPress.NamingConventions.ValidHookName.UseUnderscores
119 do_action( 'action_scheduler/migrate_action_incomplete', $source_action_id, $destination_action_id, $this->source, $this->destination );
120 do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination );
121 // phpcs:enable
122
123 return $destination_action_id;
124 }
125 }
126 }
127