PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 2.8.16
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v2.8.16
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / packages / action-scheduler / classes / migration / ActionMigrator.php

ActionMigrator.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 2.8.16, at packages/action-scheduler/classes/migration/ActionMigrator.php

116 lines 3.7 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 * var ActionScheduler_Store
18 */
19 private $source;
20
21 /**
22 * var ActionScheduler_Store
23 */
24 private $destination;
25
26 /**
27 * var LogMigrator
28 */
29 private $log_migrator;
30
31 /**
32 * ActionMigrator constructor.
33 *
34 * @param ActionScheduler_Store $source_store Source store object.
35 * @param ActionScheduler_Store $destination_store Destination store object.
36 * @param LogMigrator $log_migrator Log migrator object.
37 */
38 public function __construct( \ActionScheduler_Store $source_store, \ActionScheduler_Store $destination_store, LogMigrator $log_migrator ) {
39 $this->source = $source_store;
40 $this->destination = $destination_store;
41 $this->log_migrator = $log_migrator;
42 }
43
44 /**
45 * Migrate an action.
46 *
47 * @param int $source_action_id Action ID.
48 *
49 * @return int 0|new action ID
50 */
51 public function migrate( $source_action_id ) {
52 try {
53 $action = $this->source->fetch_action( $source_action_id );
54 $status = $this->source->get_status( $source_action_id );
55 } catch ( \Exception $e ) {
56 $action = null;
57 $status = '';
58 }
59
60 if ( is_null( $action ) || empty( $status ) || ! $action->get_schedule()->get_date() ) {
61 // null action or empty status means the fetch operation failed or the action didn't exist
62 // null schedule means it's missing vital data
63 // delete it and move on
64 try {
65 $this->source->delete_action( $source_action_id );
66 } catch ( \Exception $e ) {
67 // nothing to do, it didn't exist in the first place
68 }
69 do_action( 'action_scheduler/no_action_to_migrate', $source_action_id, $this->source, $this->destination );
70
71 return 0;
72 }
73
74 try {
75
76 // Make sure the last attempt date is set correctly for completed and failed actions
77 $last_attempt_date = ( $status !== \ActionScheduler_Store::STATUS_PENDING ) ? $this->source->get_date( $source_action_id ) : null;
78
79 $destination_action_id = $this->destination->save_action( $action, null, $last_attempt_date );
80 } catch ( \Exception $e ) {
81 do_action( 'action_scheduler/migrate_action_failed', $source_action_id, $this->source, $this->destination );
82
83 return 0; // could not save the action in the new store
84 }
85
86 try {
87 switch ( $status ) {
88 case \ActionScheduler_Store::STATUS_FAILED :
89 $this->destination->mark_failure( $destination_action_id );
90 break;
91 case \ActionScheduler_Store::STATUS_CANCELED :
92 $this->destination->cancel_action( $destination_action_id );
93 break;
94 }
95
96 $this->log_migrator->migrate( $source_action_id, $destination_action_id );
97 $this->source->delete_action( $source_action_id );
98
99 $test_action = $this->source->fetch_action( $source_action_id );
100 if ( ! is_a( $test_action, 'ActionScheduler_NullAction' ) ) {
101 throw new \RuntimeException( sprintf( __( 'Unable to remove source migrated action %s', 'woocommerce' ), $source_action_id ) );
102 }
103 do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination );
104
105 return $destination_action_id;
106 } catch ( \Exception $e ) {
107 // could not delete from the old store
108 $this->source->mark_migrated( $source_action_id );
109 do_action( 'action_scheduler/migrate_action_incomplete', $source_action_id, $destination_action_id, $this->source, $this->destination );
110 do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination );
111
112 return $destination_action_id;
113 }
114 }
115 }
116