PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.5.4
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.5.4
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / vendor / woocommerce / action-scheduler / classes / migration / ActionMigrator.php

ActionMigrator.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.5.4, at vendor/woocommerce/action-scheduler/classes/migration/ActionMigrator.php

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