PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.4.1
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.4.1
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.4.1, at vendor/woocommerce/action-scheduler/classes/migration/ActionMigrator.php

111 lines 3.6 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 */
45 public function migrate( $source_action_id ) {
46 try {
47 $action = $this->source->fetch_action( $source_action_id );
48 $status = $this->source->get_status( $source_action_id );
49 } catch ( \Exception $e ) {
50 $action = null;
51 $status = '';
52 }
53
54 if ( is_null( $action ) || empty( $status ) || ! $action->get_schedule()->get_date() ) {
55 // null action or empty status means the fetch operation failed or the action didn't exist
56 // null schedule means it's missing vital data
57 // delete it and move on
58 try {
59 $this->source->delete_action( $source_action_id );
60 } catch ( \Exception $e ) {
61 // nothing to do, it didn't exist in the first place
62 }
63 do_action( 'action_scheduler/no_action_to_migrate', $source_action_id, $this->source, $this->destination );
64
65 return 0;
66 }
67
68 try {
69
70 // Make sure the last attempt date is set correctly for completed and failed actions
71 $last_attempt_date = ( $status !== \ActionScheduler_Store::STATUS_PENDING ) ? $this->source->get_date( $source_action_id ) : null;
72
73 $destination_action_id = $this->destination->save_action( $action, null, $last_attempt_date );
74 } catch ( \Exception $e ) {
75 do_action( 'action_scheduler/migrate_action_failed', $source_action_id, $this->source, $this->destination );
76
77 return 0; // could not save the action in the new store
78 }
79
80 try {
81 switch ( $status ) {
82 case \ActionScheduler_Store::STATUS_FAILED :
83 $this->destination->mark_failure( $destination_action_id );
84 break;
85 case \ActionScheduler_Store::STATUS_CANCELED :
86 $this->destination->cancel_action( $destination_action_id );
87 break;
88 }
89
90 $this->log_migrator->migrate( $source_action_id, $destination_action_id );
91 $this->source->delete_action( $source_action_id );
92
93 $test_action = $this->source->fetch_action( $source_action_id );
94 if ( ! is_a( $test_action, 'ActionScheduler_NullAction' ) ) {
95 // translators: %s is an action ID.
96 throw new \RuntimeException( sprintf( __( 'Unable to remove source migrated action %s', 'action-scheduler' ), $source_action_id ) );
97 }
98 do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination );
99
100 return $destination_action_id;
101 } catch ( \Exception $e ) {
102 // could not delete from the old store
103 $this->source->mark_migrated( $source_action_id );
104 do_action( 'action_scheduler/migrate_action_incomplete', $source_action_id, $destination_action_id, $this->source, $this->destination );
105 do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination );
106
107 return $destination_action_id;
108 }
109 }
110 }
111