Passed
Push — main ( aaef5c...e4c121 )
by TARIQ
71:39
created
packages/action-scheduler/classes/ActionScheduler_NullLogEntry.php 1 patch
Indentation   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -4,8 +4,8 @@
 block discarded – undo
4 4
  * Class ActionScheduler_NullLogEntry
5 5
  */
6 6
 class ActionScheduler_NullLogEntry extends ActionScheduler_LogEntry {
7
-	public function __construct( $action_id = '', $message = '' ) {
8
-		// nothing to see here
9
-	}
7
+    public function __construct( $action_id = '', $message = '' ) {
8
+        // nothing to see here
9
+    }
10 10
 }
11
- 
12 11
\ No newline at end of file
12
+    
13 13
\ No newline at end of file
Please login to merge, or discard this patch.
packages/action-scheduler/classes/ActionScheduler_wcSystemStatus.php 1 patch
Indentation   +130 added lines, -130 removed lines patch added patch discarded remove patch
@@ -5,105 +5,105 @@  discard block
 block discarded – undo
5 5
  */
6 6
 class ActionScheduler_wcSystemStatus {
7 7
 
8
-	/**
9
-	 * The active data stores
10
-	 *
11
-	 * @var ActionScheduler_Store
12
-	 */
13
-	protected $store;
14
-
15
-	/**
16
-	 * Constructor method for ActionScheduler_wcSystemStatus.
17
-	 *
18
-	 * @param ActionScheduler_Store $store Active store object.
19
-	 *
20
-	 * @return void
21
-	 */
22
-	public function __construct( $store ) {
23
-		$this->store = $store;
24
-	}
25
-
26
-	/**
27
-	 * Display action data, including number of actions grouped by status and the oldest & newest action in each status.
28
-	 *
29
-	 * Helpful to identify issues, like a clogged queue.
30
-	 */
31
-	public function render() {
32
-		$action_counts     = $this->store->action_counts();
33
-		$status_labels     = $this->store->get_status_labels();
34
-		$oldest_and_newest = $this->get_oldest_and_newest( array_keys( $status_labels ) );
35
-
36
-		$this->get_template( $status_labels, $action_counts, $oldest_and_newest );
37
-	}
38
-
39
-	/**
40
-	 * Get oldest and newest scheduled dates for a given set of statuses.
41
-	 *
42
-	 * @param array $status_keys Set of statuses to find oldest & newest action for.
43
-	 * @return array
44
-	 */
45
-	protected function get_oldest_and_newest( $status_keys ) {
46
-
47
-		$oldest_and_newest = array();
48
-
49
-		foreach ( $status_keys as $status ) {
50
-			$oldest_and_newest[ $status ] = array(
51
-				'oldest' => '–',
52
-				'newest' => '–',
53
-			);
54
-
55
-			if ( 'in-progress' === $status ) {
56
-				continue;
57
-			}
58
-
59
-			$oldest_and_newest[ $status ]['oldest'] = $this->get_action_status_date( $status, 'oldest' );
60
-			$oldest_and_newest[ $status ]['newest'] = $this->get_action_status_date( $status, 'newest' );
61
-		}
62
-
63
-		return $oldest_and_newest;
64
-	}
65
-
66
-	/**
67
-	 * Get oldest or newest scheduled date for a given status.
68
-	 *
69
-	 * @param string $status Action status label/name string.
70
-	 * @param string $date_type Oldest or Newest.
71
-	 * @return DateTime
72
-	 */
73
-	protected function get_action_status_date( $status, $date_type = 'oldest' ) {
74
-
75
-		$order = 'oldest' === $date_type ? 'ASC' : 'DESC';
76
-
77
-		$action = $this->store->query_actions(
78
-			array(
79
-				'claimed'  => false,
80
-				'status'   => $status,
81
-				'per_page' => 1,
82
-				'order'    => $order,
83
-			)
84
-		);
85
-
86
-		if ( ! empty( $action ) ) {
87
-			$date_object = $this->store->get_date( $action[0] );
88
-			$action_date = $date_object->format( 'Y-m-d H:i:s O' );
89
-		} else {
90
-			$action_date = '–';
91
-		}
92
-
93
-		return $action_date;
94
-	}
95
-
96
-	/**
97
-	 * Get oldest or newest scheduled date for a given status.
98
-	 *
99
-	 * @param array $status_labels Set of statuses to find oldest & newest action for.
100
-	 * @param array $action_counts Number of actions grouped by status.
101
-	 * @param array $oldest_and_newest Date of the oldest and newest action with each status.
102
-	 */
103
-	protected function get_template( $status_labels, $action_counts, $oldest_and_newest ) {
104
-		$as_version   = ActionScheduler_Versions::instance()->latest_version();
105
-		$as_datastore = get_class( ActionScheduler_Store::instance() );
106
-		?>
8
+    /**
9
+     * The active data stores
10
+     *
11
+     * @var ActionScheduler_Store
12
+     */
13
+    protected $store;
14
+
15
+    /**
16
+     * Constructor method for ActionScheduler_wcSystemStatus.
17
+     *
18
+     * @param ActionScheduler_Store $store Active store object.
19
+     *
20
+     * @return void
21
+     */
22
+    public function __construct( $store ) {
23
+        $this->store = $store;
24
+    }
25
+
26
+    /**
27
+     * Display action data, including number of actions grouped by status and the oldest & newest action in each status.
28
+     *
29
+     * Helpful to identify issues, like a clogged queue.
30
+     */
31
+    public function render() {
32
+        $action_counts     = $this->store->action_counts();
33
+        $status_labels     = $this->store->get_status_labels();
34
+        $oldest_and_newest = $this->get_oldest_and_newest( array_keys( $status_labels ) );
35
+
36
+        $this->get_template( $status_labels, $action_counts, $oldest_and_newest );
37
+    }
38
+
39
+    /**
40
+     * Get oldest and newest scheduled dates for a given set of statuses.
41
+     *
42
+     * @param array $status_keys Set of statuses to find oldest & newest action for.
43
+     * @return array
44
+     */
45
+    protected function get_oldest_and_newest( $status_keys ) {
46
+
47
+        $oldest_and_newest = array();
48
+
49
+        foreach ( $status_keys as $status ) {
50
+            $oldest_and_newest[ $status ] = array(
51
+                'oldest' => '–',
52
+                'newest' => '–',
53
+            );
54
+
55
+            if ( 'in-progress' === $status ) {
56
+                continue;
57
+            }
58
+
59
+            $oldest_and_newest[ $status ]['oldest'] = $this->get_action_status_date( $status, 'oldest' );
60
+            $oldest_and_newest[ $status ]['newest'] = $this->get_action_status_date( $status, 'newest' );
61
+        }
62
+
63
+        return $oldest_and_newest;
64
+    }
65
+
66
+    /**
67
+     * Get oldest or newest scheduled date for a given status.
68
+     *
69
+     * @param string $status Action status label/name string.
70
+     * @param string $date_type Oldest or Newest.
71
+     * @return DateTime
72
+     */
73
+    protected function get_action_status_date( $status, $date_type = 'oldest' ) {
74
+
75
+        $order = 'oldest' === $date_type ? 'ASC' : 'DESC';
76
+
77
+        $action = $this->store->query_actions(
78
+            array(
79
+                'claimed'  => false,
80
+                'status'   => $status,
81
+                'per_page' => 1,
82
+                'order'    => $order,
83
+            )
84
+        );
85
+
86
+        if ( ! empty( $action ) ) {
87
+            $date_object = $this->store->get_date( $action[0] );
88
+            $action_date = $date_object->format( 'Y-m-d H:i:s O' );
89
+        } else {
90
+            $action_date = '–';
91
+        }
92
+
93
+        return $action_date;
94
+    }
95
+
96
+    /**
97
+     * Get oldest or newest scheduled date for a given status.
98
+     *
99
+     * @param array $status_labels Set of statuses to find oldest & newest action for.
100
+     * @param array $action_counts Number of actions grouped by status.
101
+     * @param array $oldest_and_newest Date of the oldest and newest action with each status.
102
+     */
103
+    protected function get_template( $status_labels, $action_counts, $oldest_and_newest ) {
104
+        $as_version   = ActionScheduler_Versions::instance()->latest_version();
105
+        $as_datastore = get_class( ActionScheduler_Store::instance() );
106
+        ?>
107 107
 
108 108
 		<table class="wc_status_table widefat" cellspacing="0">
109 109
 			<thead>
@@ -128,39 +128,39 @@  discard block
 block discarded – undo
128 128
 			</thead>
129 129
 			<tbody>
130 130
 				<?php
131
-				foreach ( $action_counts as $status => $count ) {
132
-					// WC uses the 3rd column for export, so we need to display more data in that (hidden when viewed as part of the table) and add an empty 2nd column.
133
-					printf(
134
-						'<tr><td>%1$s</td><td>&nbsp;</td><td>%2$s<span style="display: none;">, Oldest: %3$s, Newest: %4$s</span></td><td>%3$s</td><td>%4$s</td></tr>',
135
-						esc_html( $status_labels[ $status ] ),
136
-						esc_html( number_format_i18n( $count ) ),
137
-						esc_html( $oldest_and_newest[ $status ]['oldest'] ),
138
-						esc_html( $oldest_and_newest[ $status ]['newest'] )
139
-					);
140
-				}
141
-				?>
131
+                foreach ( $action_counts as $status => $count ) {
132
+                    // WC uses the 3rd column for export, so we need to display more data in that (hidden when viewed as part of the table) and add an empty 2nd column.
133
+                    printf(
134
+                        '<tr><td>%1$s</td><td>&nbsp;</td><td>%2$s<span style="display: none;">, Oldest: %3$s, Newest: %4$s</span></td><td>%3$s</td><td>%4$s</td></tr>',
135
+                        esc_html( $status_labels[ $status ] ),
136
+                        esc_html( number_format_i18n( $count ) ),
137
+                        esc_html( $oldest_and_newest[ $status ]['oldest'] ),
138
+                        esc_html( $oldest_and_newest[ $status ]['newest'] )
139
+                    );
140
+                }
141
+                ?>
142 142
 			</tbody>
143 143
 		</table>
144 144
 
145 145
 		<?php
146
-	}
147
-
148
-	/**
149
-	 * Is triggered when invoking inaccessible methods in an object context.
150
-	 *
151
-	 * @param string $name Name of method called.
152
-	 * @param array  $arguments Parameters to invoke the method with.
153
-	 *
154
-	 * @return mixed
155
-	 * @link https://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods
156
-	 */
157
-	public function __call( $name, $arguments ) {
158
-		switch ( $name ) {
159
-			case 'print':
160
-				_deprecated_function( __CLASS__ . '::print()', '2.2.4', __CLASS__ . '::render()' );
161
-				return call_user_func_array( array( $this, 'render' ), $arguments );
162
-		}
163
-
164
-		return null;
165
-	}
146
+    }
147
+
148
+    /**
149
+     * Is triggered when invoking inaccessible methods in an object context.
150
+     *
151
+     * @param string $name Name of method called.
152
+     * @param array  $arguments Parameters to invoke the method with.
153
+     *
154
+     * @return mixed
155
+     * @link https://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods
156
+     */
157
+    public function __call( $name, $arguments ) {
158
+        switch ( $name ) {
159
+            case 'print':
160
+                _deprecated_function( __CLASS__ . '::print()', '2.2.4', __CLASS__ . '::render()' );
161
+                return call_user_func_array( array( $this, 'render' ), $arguments );
162
+        }
163
+
164
+        return null;
165
+    }
166 166
 }
Please login to merge, or discard this patch.
plugins/woocommerce/packages/action-scheduler/classes/migration/Runner.php 1 patch
Indentation   +120 added lines, -120 removed lines patch added patch discarded remove patch
@@ -13,124 +13,124 @@
 block discarded – undo
13 13
  * @codeCoverageIgnore
14 14
  */
15 15
 class Runner {
16
-	/** @var ActionScheduler_Store */
17
-	private $source_store;
18
-
19
-	/** @var ActionScheduler_Store */
20
-	private $destination_store;
21
-
22
-	/** @var ActionScheduler_Logger */
23
-	private $source_logger;
24
-
25
-	/** @var ActionScheduler_Logger */
26
-	private $destination_logger;
27
-
28
-	/** @var BatchFetcher */
29
-	private $batch_fetcher;
30
-
31
-	/** @var ActionMigrator */
32
-	private $action_migrator;
33
-
34
-	/** @var LogMigrator */
35
-	private $log_migrator;
36
-
37
-	/** @var ProgressBar */
38
-	private $progress_bar;
39
-
40
-	/**
41
-	 * Runner constructor.
42
-	 *
43
-	 * @param Config $config Migration configuration object.
44
-	 */
45
-	public function __construct( Config $config ) {
46
-		$this->source_store       = $config->get_source_store();
47
-		$this->destination_store  = $config->get_destination_store();
48
-		$this->source_logger      = $config->get_source_logger();
49
-		$this->destination_logger = $config->get_destination_logger();
50
-
51
-		$this->batch_fetcher = new BatchFetcher( $this->source_store );
52
-		if ( $config->get_dry_run() ) {
53
-			$this->log_migrator    = new DryRun_LogMigrator( $this->source_logger, $this->destination_logger );
54
-			$this->action_migrator = new DryRun_ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator );
55
-		} else {
56
-			$this->log_migrator    = new LogMigrator( $this->source_logger, $this->destination_logger );
57
-			$this->action_migrator = new ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator );
58
-		}
59
-
60
-		if ( defined( 'WP_CLI' ) && WP_CLI ) {
61
-			$this->progress_bar = $config->get_progress_bar();
62
-		}
63
-	}
64
-
65
-	/**
66
-	 * Run migration batch.
67
-	 *
68
-	 * @param int $batch_size Optional batch size. Default 10.
69
-	 *
70
-	 * @return int Size of batch processed.
71
-	 */
72
-	public function run( $batch_size = 10 ) {
73
-		$batch = $this->batch_fetcher->fetch( $batch_size );
74
-		$batch_size = count( $batch );
75
-
76
-		if ( ! $batch_size ) {
77
-			return 0;
78
-		}
79
-
80
-		if ( $this->progress_bar ) {
81
-			/* translators: %d: amount of actions */
82
-			$this->progress_bar->set_message( sprintf( _n( 'Migrating %d action', 'Migrating %d actions', $batch_size, 'woocommerce' ), number_format_i18n( $batch_size ) ) );
83
-			$this->progress_bar->set_count( $batch_size );
84
-		}
85
-
86
-		$this->migrate_actions( $batch );
87
-
88
-		return $batch_size;
89
-	}
90
-
91
-	/**
92
-	 * Migration a batch of actions.
93
-	 *
94
-	 * @param array $action_ids List of action IDs to migrate.
95
-	 */
96
-	public function migrate_actions( array $action_ids ) {
97
-		do_action( 'action_scheduler/migration_batch_starting', $action_ids );
98
-
99
-		\ActionScheduler::logger()->unhook_stored_action();
100
-		$this->destination_logger->unhook_stored_action();
101
-
102
-		foreach ( $action_ids as $source_action_id ) {
103
-			$destination_action_id = $this->action_migrator->migrate( $source_action_id );
104
-			if ( $destination_action_id ) {
105
-				$this->destination_logger->log( $destination_action_id, sprintf(
106
-					/* translators: 1: source action ID 2: source store class 3: destination action ID 4: destination store class */
107
-					__( 'Migrated action with ID %1$d in %2$s to ID %3$d in %4$s', 'woocommerce' ),
108
-					$source_action_id,
109
-					get_class( $this->source_store ),
110
-					$destination_action_id,
111
-					get_class( $this->destination_store )
112
-				) );
113
-			}
114
-
115
-			if ( $this->progress_bar ) {
116
-				$this->progress_bar->tick();
117
-			}
118
-		}
119
-
120
-		if ( $this->progress_bar ) {
121
-			$this->progress_bar->finish();
122
-		}
123
-
124
-		\ActionScheduler::logger()->hook_stored_action();
125
-
126
-		do_action( 'action_scheduler/migration_batch_complete', $action_ids );
127
-	}
128
-
129
-	/**
130
-	 * Initialize destination store and logger.
131
-	 */
132
-	public function init_destination() {
133
-		$this->destination_store->init();
134
-		$this->destination_logger->init();
135
-	}
16
+    /** @var ActionScheduler_Store */
17
+    private $source_store;
18
+
19
+    /** @var ActionScheduler_Store */
20
+    private $destination_store;
21
+
22
+    /** @var ActionScheduler_Logger */
23
+    private $source_logger;
24
+
25
+    /** @var ActionScheduler_Logger */
26
+    private $destination_logger;
27
+
28
+    /** @var BatchFetcher */
29
+    private $batch_fetcher;
30
+
31
+    /** @var ActionMigrator */
32
+    private $action_migrator;
33
+
34
+    /** @var LogMigrator */
35
+    private $log_migrator;
36
+
37
+    /** @var ProgressBar */
38
+    private $progress_bar;
39
+
40
+    /**
41
+     * Runner constructor.
42
+     *
43
+     * @param Config $config Migration configuration object.
44
+     */
45
+    public function __construct( Config $config ) {
46
+        $this->source_store       = $config->get_source_store();
47
+        $this->destination_store  = $config->get_destination_store();
48
+        $this->source_logger      = $config->get_source_logger();
49
+        $this->destination_logger = $config->get_destination_logger();
50
+
51
+        $this->batch_fetcher = new BatchFetcher( $this->source_store );
52
+        if ( $config->get_dry_run() ) {
53
+            $this->log_migrator    = new DryRun_LogMigrator( $this->source_logger, $this->destination_logger );
54
+            $this->action_migrator = new DryRun_ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator );
55
+        } else {
56
+            $this->log_migrator    = new LogMigrator( $this->source_logger, $this->destination_logger );
57
+            $this->action_migrator = new ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator );
58
+        }
59
+
60
+        if ( defined( 'WP_CLI' ) && WP_CLI ) {
61
+            $this->progress_bar = $config->get_progress_bar();
62
+        }
63
+    }
64
+
65
+    /**
66
+     * Run migration batch.
67
+     *
68
+     * @param int $batch_size Optional batch size. Default 10.
69
+     *
70
+     * @return int Size of batch processed.
71
+     */
72
+    public function run( $batch_size = 10 ) {
73
+        $batch = $this->batch_fetcher->fetch( $batch_size );
74
+        $batch_size = count( $batch );
75
+
76
+        if ( ! $batch_size ) {
77
+            return 0;
78
+        }
79
+
80
+        if ( $this->progress_bar ) {
81
+            /* translators: %d: amount of actions */
82
+            $this->progress_bar->set_message( sprintf( _n( 'Migrating %d action', 'Migrating %d actions', $batch_size, 'woocommerce' ), number_format_i18n( $batch_size ) ) );
83
+            $this->progress_bar->set_count( $batch_size );
84
+        }
85
+
86
+        $this->migrate_actions( $batch );
87
+
88
+        return $batch_size;
89
+    }
90
+
91
+    /**
92
+     * Migration a batch of actions.
93
+     *
94
+     * @param array $action_ids List of action IDs to migrate.
95
+     */
96
+    public function migrate_actions( array $action_ids ) {
97
+        do_action( 'action_scheduler/migration_batch_starting', $action_ids );
98
+
99
+        \ActionScheduler::logger()->unhook_stored_action();
100
+        $this->destination_logger->unhook_stored_action();
101
+
102
+        foreach ( $action_ids as $source_action_id ) {
103
+            $destination_action_id = $this->action_migrator->migrate( $source_action_id );
104
+            if ( $destination_action_id ) {
105
+                $this->destination_logger->log( $destination_action_id, sprintf(
106
+                    /* translators: 1: source action ID 2: source store class 3: destination action ID 4: destination store class */
107
+                    __( 'Migrated action with ID %1$d in %2$s to ID %3$d in %4$s', 'woocommerce' ),
108
+                    $source_action_id,
109
+                    get_class( $this->source_store ),
110
+                    $destination_action_id,
111
+                    get_class( $this->destination_store )
112
+                ) );
113
+            }
114
+
115
+            if ( $this->progress_bar ) {
116
+                $this->progress_bar->tick();
117
+            }
118
+        }
119
+
120
+        if ( $this->progress_bar ) {
121
+            $this->progress_bar->finish();
122
+        }
123
+
124
+        \ActionScheduler::logger()->hook_stored_action();
125
+
126
+        do_action( 'action_scheduler/migration_batch_complete', $action_ids );
127
+    }
128
+
129
+    /**
130
+     * Initialize destination store and logger.
131
+     */
132
+    public function init_destination() {
133
+        $this->destination_store->init();
134
+        $this->destination_logger->init();
135
+    }
136 136
 }
Please login to merge, or discard this patch.
packages/action-scheduler/classes/migration/DryRun_ActionMigrator.php 1 patch
Indentation   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -13,16 +13,16 @@
 block discarded – undo
13 13
  * @codeCoverageIgnore
14 14
  */
15 15
 class DryRun_ActionMigrator extends ActionMigrator {
16
-	/**
17
-	 * Simulate migrating an action.
18
-	 *
19
-	 * @param int $source_action_id Action ID.
20
-	 *
21
-	 * @return int
22
-	 */
23
-	public function migrate( $source_action_id ) {
24
-		do_action( 'action_scheduler/migrate_action_dry_run', $source_action_id );
16
+    /**
17
+     * Simulate migrating an action.
18
+     *
19
+     * @param int $source_action_id Action ID.
20
+     *
21
+     * @return int
22
+     */
23
+    public function migrate( $source_action_id ) {
24
+        do_action( 'action_scheduler/migrate_action_dry_run', $source_action_id );
25 25
 
26
-		return 0;
27
-	}
26
+        return 0;
27
+    }
28 28
 }
Please login to merge, or discard this patch.
woocommerce/packages/action-scheduler/classes/migration/ActionMigrator.php 1 patch
Indentation   +93 added lines, -93 removed lines patch added patch discarded remove patch
@@ -13,97 +13,97 @@
 block discarded – undo
13 13
  * @codeCoverageIgnore
14 14
  */
15 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
-				throw new \RuntimeException( sprintf( __( 'Unable to remove source migrated action %s', 'woocommerce' ), $source_action_id ) );
96
-			}
97
-			do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination );
98
-
99
-			return $destination_action_id;
100
-		} catch ( \Exception $e ) {
101
-			// could not delete from the old store
102
-			$this->source->mark_migrated( $source_action_id );
103
-			do_action( 'action_scheduler/migrate_action_incomplete', $source_action_id, $destination_action_id, $this->source, $this->destination );
104
-			do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination );
105
-
106
-			return $destination_action_id;
107
-		}
108
-	}
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
+                throw new \RuntimeException( sprintf( __( 'Unable to remove source migrated action %s', 'woocommerce' ), $source_action_id ) );
96
+            }
97
+            do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination );
98
+
99
+            return $destination_action_id;
100
+        } catch ( \Exception $e ) {
101
+            // could not delete from the old store
102
+            $this->source->mark_migrated( $source_action_id );
103
+            do_action( 'action_scheduler/migrate_action_incomplete', $source_action_id, $destination_action_id, $this->source, $this->destination );
104
+            do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination );
105
+
106
+            return $destination_action_id;
107
+        }
108
+    }
109 109
 }
Please login to merge, or discard this patch.
woocommerce/packages/action-scheduler/classes/migration/Scheduler.php 1 patch
Indentation   +111 added lines, -111 removed lines patch added patch discarded remove patch
@@ -13,116 +13,116 @@
 block discarded – undo
13 13
  * @codeCoverageIgnore
14 14
  */
15 15
 class Scheduler {
16
-	/** Migration action hook. */
17
-	const HOOK            = 'action_scheduler/migration_hook';
18
-
19
-	/** Migration action group. */
20
-	const GROUP           = 'action-scheduler-migration';
21
-
22
-	/**
23
-	 * Set up the callback for the scheduled job.
24
-	 */
25
-	public function hook() {
26
-		add_action( self::HOOK, array( $this, 'run_migration' ), 10, 0 );
27
-	}
28
-
29
-	/**
30
-	 * Remove the callback for the scheduled job.
31
-	 */
32
-	public function unhook() {
33
-		remove_action( self::HOOK, array( $this, 'run_migration' ), 10 );
34
-	}
35
-
36
-	/**
37
-	 * The migration callback.
38
-	 */
39
-	public function run_migration() {
40
-		$migration_runner = $this->get_migration_runner();
41
-		$count            = $migration_runner->run( $this->get_batch_size() );
42
-
43
-		if ( $count === 0 ) {
44
-			$this->mark_complete();
45
-		} else {
46
-			$this->schedule_migration( time() + $this->get_schedule_interval() );
47
-		}
48
-	}
49
-
50
-	/**
51
-	 * Mark the migration complete.
52
-	 */
53
-	public function mark_complete() {
54
-		$this->unschedule_migration();
55
-
56
-		\ActionScheduler_DataController::mark_migration_complete();
57
-		do_action( 'action_scheduler/migration_complete' );
58
-	}
59
-
60
-	/**
61
-	 * Get a flag indicating whether the migration is scheduled.
62
-	 *
63
-	 * @return bool Whether there is a pending action in the store to handle the migration
64
-	 */
65
-	public function is_migration_scheduled() {
66
-		$next = as_next_scheduled_action( self::HOOK );
67
-
68
-		return ! empty( $next );
69
-	}
70
-
71
-	/**
72
-	 * Schedule the migration.
73
-	 *
74
-	 * @param int $when Optional timestamp to run the next migration batch. Defaults to now.
75
-	 *
76
-	 * @return string The action ID
77
-	 */
78
-	public function schedule_migration( $when = 0 ) {
79
-		$next = as_next_scheduled_action( self::HOOK );
80
-
81
-		if ( ! empty( $next ) ) {
82
-			return $next;
83
-		}
84
-
85
-		if ( empty( $when ) ) {
86
-			$when = time() + MINUTE_IN_SECONDS;
87
-		}
88
-
89
-		return as_schedule_single_action( $when, self::HOOK, array(), self::GROUP );
90
-	}
91
-
92
-	/**
93
-	 * Remove the scheduled migration action.
94
-	 */
95
-	public function unschedule_migration() {
96
-		as_unschedule_action( self::HOOK, null, self::GROUP );
97
-	}
98
-
99
-	/**
100
-	 * Get migration batch schedule interval.
101
-	 *
102
-	 * @return int Seconds between migration runs. Defaults to 0 seconds to allow chaining migration via Async Runners.
103
-	 */
104
-	private function get_schedule_interval() {
105
-		return (int) apply_filters( 'action_scheduler/migration_interval', 0 );
106
-	}
107
-
108
-	/**
109
-	 * Get migration batch size.
110
-	 *
111
-	 * @return int Number of actions to migrate in each batch. Defaults to 250.
112
-	 */
113
-	private function get_batch_size() {
114
-		return (int) apply_filters( 'action_scheduler/migration_batch_size', 250 );
115
-	}
116
-
117
-	/**
118
-	 * Get migration runner object.
119
-	 *
120
-	 * @return Runner
121
-	 */
122
-	private function get_migration_runner() {
123
-		$config = Controller::instance()->get_migration_config_object();
124
-
125
-		return new Runner( $config );
126
-	}
16
+    /** Migration action hook. */
17
+    const HOOK            = 'action_scheduler/migration_hook';
18
+
19
+    /** Migration action group. */
20
+    const GROUP           = 'action-scheduler-migration';
21
+
22
+    /**
23
+     * Set up the callback for the scheduled job.
24
+     */
25
+    public function hook() {
26
+        add_action( self::HOOK, array( $this, 'run_migration' ), 10, 0 );
27
+    }
28
+
29
+    /**
30
+     * Remove the callback for the scheduled job.
31
+     */
32
+    public function unhook() {
33
+        remove_action( self::HOOK, array( $this, 'run_migration' ), 10 );
34
+    }
35
+
36
+    /**
37
+     * The migration callback.
38
+     */
39
+    public function run_migration() {
40
+        $migration_runner = $this->get_migration_runner();
41
+        $count            = $migration_runner->run( $this->get_batch_size() );
42
+
43
+        if ( $count === 0 ) {
44
+            $this->mark_complete();
45
+        } else {
46
+            $this->schedule_migration( time() + $this->get_schedule_interval() );
47
+        }
48
+    }
49
+
50
+    /**
51
+     * Mark the migration complete.
52
+     */
53
+    public function mark_complete() {
54
+        $this->unschedule_migration();
55
+
56
+        \ActionScheduler_DataController::mark_migration_complete();
57
+        do_action( 'action_scheduler/migration_complete' );
58
+    }
59
+
60
+    /**
61
+     * Get a flag indicating whether the migration is scheduled.
62
+     *
63
+     * @return bool Whether there is a pending action in the store to handle the migration
64
+     */
65
+    public function is_migration_scheduled() {
66
+        $next = as_next_scheduled_action( self::HOOK );
67
+
68
+        return ! empty( $next );
69
+    }
70
+
71
+    /**
72
+     * Schedule the migration.
73
+     *
74
+     * @param int $when Optional timestamp to run the next migration batch. Defaults to now.
75
+     *
76
+     * @return string The action ID
77
+     */
78
+    public function schedule_migration( $when = 0 ) {
79
+        $next = as_next_scheduled_action( self::HOOK );
80
+
81
+        if ( ! empty( $next ) ) {
82
+            return $next;
83
+        }
84
+
85
+        if ( empty( $when ) ) {
86
+            $when = time() + MINUTE_IN_SECONDS;
87
+        }
88
+
89
+        return as_schedule_single_action( $when, self::HOOK, array(), self::GROUP );
90
+    }
91
+
92
+    /**
93
+     * Remove the scheduled migration action.
94
+     */
95
+    public function unschedule_migration() {
96
+        as_unschedule_action( self::HOOK, null, self::GROUP );
97
+    }
98
+
99
+    /**
100
+     * Get migration batch schedule interval.
101
+     *
102
+     * @return int Seconds between migration runs. Defaults to 0 seconds to allow chaining migration via Async Runners.
103
+     */
104
+    private function get_schedule_interval() {
105
+        return (int) apply_filters( 'action_scheduler/migration_interval', 0 );
106
+    }
107
+
108
+    /**
109
+     * Get migration batch size.
110
+     *
111
+     * @return int Number of actions to migrate in each batch. Defaults to 250.
112
+     */
113
+    private function get_batch_size() {
114
+        return (int) apply_filters( 'action_scheduler/migration_batch_size', 250 );
115
+    }
116
+
117
+    /**
118
+     * Get migration runner object.
119
+     *
120
+     * @return Runner
121
+     */
122
+    private function get_migration_runner() {
123
+        $config = Controller::instance()->get_migration_config_object();
124
+
125
+        return new Runner( $config );
126
+    }
127 127
 
128 128
 }
Please login to merge, or discard this patch.
plugins/woocommerce/packages/action-scheduler/classes/migration/Config.php 1 patch
Indentation   +148 added lines, -148 removed lines patch added patch discarded remove patch
@@ -17,152 +17,152 @@
 block discarded – undo
17 17
  * A config builder for the ActionScheduler\Migration\Runner class
18 18
  */
19 19
 class Config {
20
-	/** @var ActionScheduler_Store */
21
-	private $source_store;
22
-
23
-	/** @var ActionScheduler_Logger */
24
-	private $source_logger;
25
-
26
-	/** @var ActionScheduler_Store */
27
-	private $destination_store;
28
-
29
-	/** @var ActionScheduler_Logger */
30
-	private $destination_logger;
31
-
32
-	/** @var Progress bar */
33
-	private $progress_bar;
34
-
35
-	/** @var bool */
36
-	private $dry_run = false;
37
-
38
-	/**
39
-	 * Config constructor.
40
-	 */
41
-	public function __construct() {
42
-
43
-	}
44
-
45
-	/**
46
-	 * Get the configured source store.
47
-	 *
48
-	 * @return ActionScheduler_Store
49
-	 */
50
-	public function get_source_store() {
51
-		if ( empty( $this->source_store ) ) {
52
-			throw new \RuntimeException( __( 'Source store must be configured before running a migration', 'woocommerce' ) );
53
-		}
54
-
55
-		return $this->source_store;
56
-	}
57
-
58
-	/**
59
-	 * Set the configured source store.
60
-	 *
61
-	 * @param ActionScheduler_Store $store Source store object.
62
-	 */
63
-	public function set_source_store( Store $store ) {
64
-		$this->source_store = $store;
65
-	}
66
-
67
-	/**
68
-	 * Get the configured source loger.
69
-	 *
70
-	 * @return ActionScheduler_Logger
71
-	 */
72
-	public function get_source_logger() {
73
-		if ( empty( $this->source_logger ) ) {
74
-			throw new \RuntimeException( __( 'Source logger must be configured before running a migration', 'woocommerce' ) );
75
-		}
76
-
77
-		return $this->source_logger;
78
-	}
79
-
80
-	/**
81
-	 * Set the configured source logger.
82
-	 *
83
-	 * @param ActionScheduler_Logger $logger
84
-	 */
85
-	public function set_source_logger( Logger $logger ) {
86
-		$this->source_logger = $logger;
87
-	}
88
-
89
-	/**
90
-	 * Get the configured destination store.
91
-	 *
92
-	 * @return ActionScheduler_Store
93
-	 */
94
-	public function get_destination_store() {
95
-		if ( empty( $this->destination_store ) ) {
96
-			throw new \RuntimeException( __( 'Destination store must be configured before running a migration', 'woocommerce' ) );
97
-		}
98
-
99
-		return $this->destination_store;
100
-	}
101
-
102
-	/**
103
-	 * Set the configured destination store.
104
-	 *
105
-	 * @param ActionScheduler_Store $store
106
-	 */
107
-	public function set_destination_store( Store $store ) {
108
-		$this->destination_store = $store;
109
-	}
110
-
111
-	/**
112
-	 * Get the configured destination logger.
113
-	 *
114
-	 * @return ActionScheduler_Logger
115
-	 */
116
-	public function get_destination_logger() {
117
-		if ( empty( $this->destination_logger ) ) {
118
-			throw new \RuntimeException( __( 'Destination logger must be configured before running a migration', 'woocommerce' ) );
119
-		}
120
-
121
-		return $this->destination_logger;
122
-	}
123
-
124
-	/**
125
-	 * Set the configured destination logger.
126
-	 *
127
-	 * @param ActionScheduler_Logger $logger
128
-	 */
129
-	public function set_destination_logger( Logger $logger ) {
130
-		$this->destination_logger = $logger;
131
-	}
132
-
133
-	/**
134
-	 * Get flag indicating whether it's a dry run.
135
-	 *
136
-	 * @return bool
137
-	 */
138
-	public function get_dry_run() {
139
-		return $this->dry_run;
140
-	}
141
-
142
-	/**
143
-	 * Set flag indicating whether it's a dry run.
144
-	 *
145
-	 * @param bool $dry_run
146
-	 */
147
-	public function set_dry_run( $dry_run ) {
148
-		$this->dry_run = (bool) $dry_run;
149
-	}
150
-
151
-	/**
152
-	 * Get progress bar object.
153
-	 *
154
-	 * @return ActionScheduler\WPCLI\ProgressBar
155
-	 */
156
-	public function get_progress_bar() {
157
-		return $this->progress_bar;
158
-	}
159
-
160
-	/**
161
-	 * Set progress bar object.
162
-	 *
163
-	 * @param ActionScheduler\WPCLI\ProgressBar $progress_bar
164
-	 */
165
-	public function set_progress_bar( ProgressBar $progress_bar ) {
166
-		$this->progress_bar = $progress_bar;
167
-	}
20
+    /** @var ActionScheduler_Store */
21
+    private $source_store;
22
+
23
+    /** @var ActionScheduler_Logger */
24
+    private $source_logger;
25
+
26
+    /** @var ActionScheduler_Store */
27
+    private $destination_store;
28
+
29
+    /** @var ActionScheduler_Logger */
30
+    private $destination_logger;
31
+
32
+    /** @var Progress bar */
33
+    private $progress_bar;
34
+
35
+    /** @var bool */
36
+    private $dry_run = false;
37
+
38
+    /**
39
+     * Config constructor.
40
+     */
41
+    public function __construct() {
42
+
43
+    }
44
+
45
+    /**
46
+     * Get the configured source store.
47
+     *
48
+     * @return ActionScheduler_Store
49
+     */
50
+    public function get_source_store() {
51
+        if ( empty( $this->source_store ) ) {
52
+            throw new \RuntimeException( __( 'Source store must be configured before running a migration', 'woocommerce' ) );
53
+        }
54
+
55
+        return $this->source_store;
56
+    }
57
+
58
+    /**
59
+     * Set the configured source store.
60
+     *
61
+     * @param ActionScheduler_Store $store Source store object.
62
+     */
63
+    public function set_source_store( Store $store ) {
64
+        $this->source_store = $store;
65
+    }
66
+
67
+    /**
68
+     * Get the configured source loger.
69
+     *
70
+     * @return ActionScheduler_Logger
71
+     */
72
+    public function get_source_logger() {
73
+        if ( empty( $this->source_logger ) ) {
74
+            throw new \RuntimeException( __( 'Source logger must be configured before running a migration', 'woocommerce' ) );
75
+        }
76
+
77
+        return $this->source_logger;
78
+    }
79
+
80
+    /**
81
+     * Set the configured source logger.
82
+     *
83
+     * @param ActionScheduler_Logger $logger
84
+     */
85
+    public function set_source_logger( Logger $logger ) {
86
+        $this->source_logger = $logger;
87
+    }
88
+
89
+    /**
90
+     * Get the configured destination store.
91
+     *
92
+     * @return ActionScheduler_Store
93
+     */
94
+    public function get_destination_store() {
95
+        if ( empty( $this->destination_store ) ) {
96
+            throw new \RuntimeException( __( 'Destination store must be configured before running a migration', 'woocommerce' ) );
97
+        }
98
+
99
+        return $this->destination_store;
100
+    }
101
+
102
+    /**
103
+     * Set the configured destination store.
104
+     *
105
+     * @param ActionScheduler_Store $store
106
+     */
107
+    public function set_destination_store( Store $store ) {
108
+        $this->destination_store = $store;
109
+    }
110
+
111
+    /**
112
+     * Get the configured destination logger.
113
+     *
114
+     * @return ActionScheduler_Logger
115
+     */
116
+    public function get_destination_logger() {
117
+        if ( empty( $this->destination_logger ) ) {
118
+            throw new \RuntimeException( __( 'Destination logger must be configured before running a migration', 'woocommerce' ) );
119
+        }
120
+
121
+        return $this->destination_logger;
122
+    }
123
+
124
+    /**
125
+     * Set the configured destination logger.
126
+     *
127
+     * @param ActionScheduler_Logger $logger
128
+     */
129
+    public function set_destination_logger( Logger $logger ) {
130
+        $this->destination_logger = $logger;
131
+    }
132
+
133
+    /**
134
+     * Get flag indicating whether it's a dry run.
135
+     *
136
+     * @return bool
137
+     */
138
+    public function get_dry_run() {
139
+        return $this->dry_run;
140
+    }
141
+
142
+    /**
143
+     * Set flag indicating whether it's a dry run.
144
+     *
145
+     * @param bool $dry_run
146
+     */
147
+    public function set_dry_run( $dry_run ) {
148
+        $this->dry_run = (bool) $dry_run;
149
+    }
150
+
151
+    /**
152
+     * Get progress bar object.
153
+     *
154
+     * @return ActionScheduler\WPCLI\ProgressBar
155
+     */
156
+    public function get_progress_bar() {
157
+        return $this->progress_bar;
158
+    }
159
+
160
+    /**
161
+     * Set progress bar object.
162
+     *
163
+     * @param ActionScheduler\WPCLI\ProgressBar $progress_bar
164
+     */
165
+    public function set_progress_bar( ProgressBar $progress_bar ) {
166
+        $this->progress_bar = $progress_bar;
167
+    }
168 168
 }
Please login to merge, or discard this patch.
woocommerce/packages/action-scheduler/classes/migration/BatchFetcher.php 1 patch
Indentation   +61 added lines, -61 removed lines patch added patch discarded remove patch
@@ -16,71 +16,71 @@
 block discarded – undo
16 16
  * @codeCoverageIgnore
17 17
  */
18 18
 class BatchFetcher {
19
-	/** var ActionScheduler_Store */
20
-	private $store;
19
+    /** var ActionScheduler_Store */
20
+    private $store;
21 21
 
22
-	/**
23
-	 * BatchFetcher constructor.
24
-	 *
25
-	 * @param ActionScheduler_Store $source_store Source store object.
26
-	 */
27
-	public function __construct( Store $source_store ) {
28
-		$this->store = $source_store;
29
-	}
22
+    /**
23
+     * BatchFetcher constructor.
24
+     *
25
+     * @param ActionScheduler_Store $source_store Source store object.
26
+     */
27
+    public function __construct( Store $source_store ) {
28
+        $this->store = $source_store;
29
+    }
30 30
 
31
-	/**
32
-	 * Retrieve a list of actions.
33
-	 *
34
-	 * @param int $count The number of actions to retrieve
35
-	 *
36
-	 * @return int[] A list of action IDs
37
-	 */
38
-	public function fetch( $count = 10 ) {
39
-		foreach ( $this->get_query_strategies( $count ) as $query ) {
40
-			$action_ids = $this->store->query_actions( $query );
41
-			if ( ! empty( $action_ids ) ) {
42
-				return $action_ids;
43
-			}
44
-		}
31
+    /**
32
+     * Retrieve a list of actions.
33
+     *
34
+     * @param int $count The number of actions to retrieve
35
+     *
36
+     * @return int[] A list of action IDs
37
+     */
38
+    public function fetch( $count = 10 ) {
39
+        foreach ( $this->get_query_strategies( $count ) as $query ) {
40
+            $action_ids = $this->store->query_actions( $query );
41
+            if ( ! empty( $action_ids ) ) {
42
+                return $action_ids;
43
+            }
44
+        }
45 45
 
46
-		return [];
47
-	}
46
+        return [];
47
+    }
48 48
 
49
-	/**
50
-	 * Generate a list of prioritized of action search parameters.
51
-	 *
52
-	 * @param int $count Number of actions to find.
53
-	 *
54
-	 * @return array
55
-	 */
56
-	private function get_query_strategies( $count ) {
57
-		$now  = as_get_datetime_object();
58
-		$args = [
59
-			'date'     => $now,
60
-			'per_page' => $count,
61
-			'offset'   => 0,
62
-			'orderby'  => 'date',
63
-			'order'    => 'ASC',
64
-		];
49
+    /**
50
+     * Generate a list of prioritized of action search parameters.
51
+     *
52
+     * @param int $count Number of actions to find.
53
+     *
54
+     * @return array
55
+     */
56
+    private function get_query_strategies( $count ) {
57
+        $now  = as_get_datetime_object();
58
+        $args = [
59
+            'date'     => $now,
60
+            'per_page' => $count,
61
+            'offset'   => 0,
62
+            'orderby'  => 'date',
63
+            'order'    => 'ASC',
64
+        ];
65 65
 
66
-		$priorities = [
67
-			Store::STATUS_PENDING,
68
-			Store::STATUS_FAILED,
69
-			Store::STATUS_CANCELED,
70
-			Store::STATUS_COMPLETE,
71
-			Store::STATUS_RUNNING,
72
-			'', // any other unanticipated status
73
-		];
66
+        $priorities = [
67
+            Store::STATUS_PENDING,
68
+            Store::STATUS_FAILED,
69
+            Store::STATUS_CANCELED,
70
+            Store::STATUS_COMPLETE,
71
+            Store::STATUS_RUNNING,
72
+            '', // any other unanticipated status
73
+        ];
74 74
 
75
-		foreach ( $priorities as $status ) {
76
-			yield wp_parse_args( [
77
-				'status'       => $status,
78
-				'date_compare' => '<=',
79
-			], $args );
80
-			yield wp_parse_args( [
81
-				'status'       => $status,
82
-				'date_compare' => '>=',
83
-			], $args );
84
-		}
85
-	}
75
+        foreach ( $priorities as $status ) {
76
+            yield wp_parse_args( [
77
+                'status'       => $status,
78
+                'date_compare' => '<=',
79
+            ], $args );
80
+            yield wp_parse_args( [
81
+                'status'       => $status,
82
+                'date_compare' => '>=',
83
+            ], $args );
84
+        }
85
+    }
86 86
 }
87 87
\ No newline at end of file
Please login to merge, or discard this patch.
woocommerce/packages/action-scheduler/classes/migration/Controller.php 1 patch
Indentation   +195 added lines, -195 removed lines patch added patch discarded remove patch
@@ -19,85 +19,85 @@  discard block
 block discarded – undo
19 19
  * @codeCoverageIgnore
20 20
  */
21 21
 class Controller {
22
-	private static $instance;
23
-
24
-	/** @var Action_Scheduler\Migration\Scheduler */
25
-	private $migration_scheduler;
26
-
27
-	/** @var string */
28
-	private $store_classname;
29
-
30
-	/** @var string */
31
-	private $logger_classname;
32
-
33
-	/** @var bool */
34
-	private $migrate_custom_store;
35
-
36
-	/**
37
-	 * Controller constructor.
38
-	 *
39
-	 * @param Scheduler $migration_scheduler Migration scheduler object.
40
-	 */
41
-	protected function __construct( Scheduler $migration_scheduler ) {
42
-		$this->migration_scheduler = $migration_scheduler;
43
-		$this->store_classname     = '';
44
-	}
45
-
46
-	/**
47
-	 * Set the action store class name.
48
-	 *
49
-	 * @param string $class Classname of the store class.
50
-	 *
51
-	 * @return string
52
-	 */
53
-	public function get_store_class( $class ) {
54
-		if ( \ActionScheduler_DataController::is_migration_complete() ) {
55
-			return \ActionScheduler_DataController::DATASTORE_CLASS;
56
-		} elseif ( \ActionScheduler_Store::DEFAULT_CLASS !== $class ) {
57
-			$this->store_classname = $class;
58
-			return $class;
59
-		} else {
60
-			return 'ActionScheduler_HybridStore';
61
-		}
62
-	}
63
-
64
-	/**
65
-	 * Set the action logger class name.
66
-	 *
67
-	 * @param string $class Classname of the logger class.
68
-	 *
69
-	 * @return string
70
-	 */
71
-	public function get_logger_class( $class ) {
72
-		\ActionScheduler_Store::instance();
73
-
74
-		if ( $this->has_custom_datastore() ) {
75
-			$this->logger_classname = $class;
76
-			return $class;
77
-		} else {
78
-			return \ActionScheduler_DataController::LOGGER_CLASS;
79
-		}
80
-	}
81
-
82
-	/**
83
-	 * Get flag indicating whether a custom datastore is in use.
84
-	 *
85
-	 * @return bool
86
-	 */
87
-	public function has_custom_datastore() {
88
-		return (bool) $this->store_classname;
89
-	}
90
-
91
-	/**
92
-	 * Set up the background migration process.
93
-	 *
94
-	 * @return void
95
-	 */
96
-	public function schedule_migration() {
97
-		$logging_tables = new ActionScheduler_LoggerSchema();
98
-		$store_tables   = new ActionScheduler_StoreSchema();
99
-
100
-		/*
22
+    private static $instance;
23
+
24
+    /** @var Action_Scheduler\Migration\Scheduler */
25
+    private $migration_scheduler;
26
+
27
+    /** @var string */
28
+    private $store_classname;
29
+
30
+    /** @var string */
31
+    private $logger_classname;
32
+
33
+    /** @var bool */
34
+    private $migrate_custom_store;
35
+
36
+    /**
37
+     * Controller constructor.
38
+     *
39
+     * @param Scheduler $migration_scheduler Migration scheduler object.
40
+     */
41
+    protected function __construct( Scheduler $migration_scheduler ) {
42
+        $this->migration_scheduler = $migration_scheduler;
43
+        $this->store_classname     = '';
44
+    }
45
+
46
+    /**
47
+     * Set the action store class name.
48
+     *
49
+     * @param string $class Classname of the store class.
50
+     *
51
+     * @return string
52
+     */
53
+    public function get_store_class( $class ) {
54
+        if ( \ActionScheduler_DataController::is_migration_complete() ) {
55
+            return \ActionScheduler_DataController::DATASTORE_CLASS;
56
+        } elseif ( \ActionScheduler_Store::DEFAULT_CLASS !== $class ) {
57
+            $this->store_classname = $class;
58
+            return $class;
59
+        } else {
60
+            return 'ActionScheduler_HybridStore';
61
+        }
62
+    }
63
+
64
+    /**
65
+     * Set the action logger class name.
66
+     *
67
+     * @param string $class Classname of the logger class.
68
+     *
69
+     * @return string
70
+     */
71
+    public function get_logger_class( $class ) {
72
+        \ActionScheduler_Store::instance();
73
+
74
+        if ( $this->has_custom_datastore() ) {
75
+            $this->logger_classname = $class;
76
+            return $class;
77
+        } else {
78
+            return \ActionScheduler_DataController::LOGGER_CLASS;
79
+        }
80
+    }
81
+
82
+    /**
83
+     * Get flag indicating whether a custom datastore is in use.
84
+     *
85
+     * @return bool
86
+     */
87
+    public function has_custom_datastore() {
88
+        return (bool) $this->store_classname;
89
+    }
90
+
91
+    /**
92
+     * Set up the background migration process.
93
+     *
94
+     * @return void
95
+     */
96
+    public function schedule_migration() {
97
+        $logging_tables = new ActionScheduler_LoggerSchema();
98
+        $store_tables   = new ActionScheduler_StoreSchema();
99
+
100
+        /*
101 101
 		 * In some unusual cases, the expected tables may not have been created. In such cases
102 102
 		 * we do not schedule a migration as doing so will lead to fatal error conditions.
103 103
 		 *
@@ -107,120 +107,120 @@  discard block
 block discarded – undo
107 107
 		 *
108 108
 		 * @see https://github.com/woocommerce/action-scheduler/issues/653
109 109
 		 */
110
-		if (
111
-			ActionScheduler_DataController::is_migration_complete()
112
-			|| $this->migration_scheduler->is_migration_scheduled()
113
-			|| ! $store_tables->tables_exist()
114
-			|| ! $logging_tables->tables_exist()
115
-		) {
116
-			return;
117
-		}
118
-
119
-		$this->migration_scheduler->schedule_migration();
120
-	}
121
-
122
-	/**
123
-	 * Get the default migration config object
124
-	 *
125
-	 * @return ActionScheduler\Migration\Config
126
-	 */
127
-	public function get_migration_config_object() {
128
-		static $config = null;
129
-
130
-		if ( ! $config ) {
131
-			$source_store  = $this->store_classname ? new $this->store_classname() : new \ActionScheduler_wpPostStore();
132
-			$source_logger = $this->logger_classname ? new $this->logger_classname() : new \ActionScheduler_wpCommentLogger();
133
-
134
-			$config = new Config();
135
-			$config->set_source_store( $source_store );
136
-			$config->set_source_logger( $source_logger );
137
-			$config->set_destination_store( new \ActionScheduler_DBStoreMigrator() );
138
-			$config->set_destination_logger( new \ActionScheduler_DBLogger() );
139
-
140
-			if ( defined( 'WP_CLI' ) && WP_CLI ) {
141
-				$config->set_progress_bar( new ProgressBar( '', 0 ) );
142
-			}
143
-		}
144
-
145
-		return apply_filters( 'action_scheduler/migration_config', $config );
146
-	}
147
-
148
-	/**
149
-	 * Hook dashboard migration notice.
150
-	 */
151
-	public function hook_admin_notices() {
152
-		if ( ! $this->allow_migration() || \ActionScheduler_DataController::is_migration_complete() ) {
153
-			return;
154
-		}
155
-		add_action( 'admin_notices', array( $this, 'display_migration_notice' ), 10, 0 );
156
-	}
157
-
158
-	/**
159
-	 * Show a dashboard notice that migration is in progress.
160
-	 */
161
-	public function display_migration_notice() {
162
-		printf( '<div class="notice notice-warning"><p>%s</p></div>', esc_html__( 'Action Scheduler migration in progress. The list of scheduled actions may be incomplete.', 'woocommerce' ) );
163
-	}
164
-
165
-	/**
166
-	 * Add store classes. Hook migration.
167
-	 */
168
-	private function hook() {
169
-		add_filter( 'action_scheduler_store_class', array( $this, 'get_store_class' ), 100, 1 );
170
-		add_filter( 'action_scheduler_logger_class', array( $this, 'get_logger_class' ), 100, 1 );
171
-		add_action( 'init', array( $this, 'maybe_hook_migration' ) );
172
-		add_action( 'wp_loaded', array( $this, 'schedule_migration' ) );
173
-
174
-		// Action Scheduler may be displayed as a Tools screen or WooCommerce > Status administration screen
175
-		add_action( 'load-tools_page_action-scheduler', array( $this, 'hook_admin_notices' ), 10, 0 );
176
-		add_action( 'load-woocommerce_page_wc-status', array( $this, 'hook_admin_notices' ), 10, 0 );
177
-	}
178
-
179
-	/**
180
-	 * Possibly hook the migration scheduler action.
181
-	 *
182
-	 * @author Jeremy Pry
183
-	 */
184
-	public function maybe_hook_migration() {
185
-		if ( ! $this->allow_migration() || \ActionScheduler_DataController::is_migration_complete() ) {
186
-			return;
187
-		}
188
-
189
-		$this->migration_scheduler->hook();
190
-	}
191
-
192
-	/**
193
-	 * Allow datastores to enable migration to AS tables.
194
-	 */
195
-	public function allow_migration() {
196
-		if ( ! \ActionScheduler_DataController::dependencies_met() ) {
197
-			return false;
198
-		}
199
-
200
-		if ( null === $this->migrate_custom_store ) {
201
-			$this->migrate_custom_store = apply_filters( 'action_scheduler_migrate_data_store', false );
202
-		}
203
-
204
-		return ( ! $this->has_custom_datastore() ) || $this->migrate_custom_store;
205
-	}
206
-
207
-	/**
208
-	 * Proceed with the migration if the dependencies have been met.
209
-	 */
210
-	public static function init() {
211
-		if ( \ActionScheduler_DataController::dependencies_met() ) {
212
-			self::instance()->hook();
213
-		}
214
-	}
215
-
216
-	/**
217
-	 * Singleton factory.
218
-	 */
219
-	public static function instance() {
220
-		if ( ! isset( self::$instance ) ) {
221
-			self::$instance = new static( new Scheduler() );
222
-		}
223
-
224
-		return self::$instance;
225
-	}
110
+        if (
111
+            ActionScheduler_DataController::is_migration_complete()
112
+            || $this->migration_scheduler->is_migration_scheduled()
113
+            || ! $store_tables->tables_exist()
114
+            || ! $logging_tables->tables_exist()
115
+        ) {
116
+            return;
117
+        }
118
+
119
+        $this->migration_scheduler->schedule_migration();
120
+    }
121
+
122
+    /**
123
+     * Get the default migration config object
124
+     *
125
+     * @return ActionScheduler\Migration\Config
126
+     */
127
+    public function get_migration_config_object() {
128
+        static $config = null;
129
+
130
+        if ( ! $config ) {
131
+            $source_store  = $this->store_classname ? new $this->store_classname() : new \ActionScheduler_wpPostStore();
132
+            $source_logger = $this->logger_classname ? new $this->logger_classname() : new \ActionScheduler_wpCommentLogger();
133
+
134
+            $config = new Config();
135
+            $config->set_source_store( $source_store );
136
+            $config->set_source_logger( $source_logger );
137
+            $config->set_destination_store( new \ActionScheduler_DBStoreMigrator() );
138
+            $config->set_destination_logger( new \ActionScheduler_DBLogger() );
139
+
140
+            if ( defined( 'WP_CLI' ) && WP_CLI ) {
141
+                $config->set_progress_bar( new ProgressBar( '', 0 ) );
142
+            }
143
+        }
144
+
145
+        return apply_filters( 'action_scheduler/migration_config', $config );
146
+    }
147
+
148
+    /**
149
+     * Hook dashboard migration notice.
150
+     */
151
+    public function hook_admin_notices() {
152
+        if ( ! $this->allow_migration() || \ActionScheduler_DataController::is_migration_complete() ) {
153
+            return;
154
+        }
155
+        add_action( 'admin_notices', array( $this, 'display_migration_notice' ), 10, 0 );
156
+    }
157
+
158
+    /**
159
+     * Show a dashboard notice that migration is in progress.
160
+     */
161
+    public function display_migration_notice() {
162
+        printf( '<div class="notice notice-warning"><p>%s</p></div>', esc_html__( 'Action Scheduler migration in progress. The list of scheduled actions may be incomplete.', 'woocommerce' ) );
163
+    }
164
+
165
+    /**
166
+     * Add store classes. Hook migration.
167
+     */
168
+    private function hook() {
169
+        add_filter( 'action_scheduler_store_class', array( $this, 'get_store_class' ), 100, 1 );
170
+        add_filter( 'action_scheduler_logger_class', array( $this, 'get_logger_class' ), 100, 1 );
171
+        add_action( 'init', array( $this, 'maybe_hook_migration' ) );
172
+        add_action( 'wp_loaded', array( $this, 'schedule_migration' ) );
173
+
174
+        // Action Scheduler may be displayed as a Tools screen or WooCommerce > Status administration screen
175
+        add_action( 'load-tools_page_action-scheduler', array( $this, 'hook_admin_notices' ), 10, 0 );
176
+        add_action( 'load-woocommerce_page_wc-status', array( $this, 'hook_admin_notices' ), 10, 0 );
177
+    }
178
+
179
+    /**
180
+     * Possibly hook the migration scheduler action.
181
+     *
182
+     * @author Jeremy Pry
183
+     */
184
+    public function maybe_hook_migration() {
185
+        if ( ! $this->allow_migration() || \ActionScheduler_DataController::is_migration_complete() ) {
186
+            return;
187
+        }
188
+
189
+        $this->migration_scheduler->hook();
190
+    }
191
+
192
+    /**
193
+     * Allow datastores to enable migration to AS tables.
194
+     */
195
+    public function allow_migration() {
196
+        if ( ! \ActionScheduler_DataController::dependencies_met() ) {
197
+            return false;
198
+        }
199
+
200
+        if ( null === $this->migrate_custom_store ) {
201
+            $this->migrate_custom_store = apply_filters( 'action_scheduler_migrate_data_store', false );
202
+        }
203
+
204
+        return ( ! $this->has_custom_datastore() ) || $this->migrate_custom_store;
205
+    }
206
+
207
+    /**
208
+     * Proceed with the migration if the dependencies have been met.
209
+     */
210
+    public static function init() {
211
+        if ( \ActionScheduler_DataController::dependencies_met() ) {
212
+            self::instance()->hook();
213
+        }
214
+    }
215
+
216
+    /**
217
+     * Singleton factory.
218
+     */
219
+    public static function instance() {
220
+        if ( ! isset( self::$instance ) ) {
221
+            self::$instance = new static( new Scheduler() );
222
+        }
223
+
224
+        return self::$instance;
225
+    }
226 226
 }
Please login to merge, or discard this patch.