PluginProbe
WP-Stateless – Google Cloud Storage / trunk
WP-Stateless – Google Cloud Storage vtrunk
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / classes / status / class-migrations.php

class-migrations.php in WP-Stateless – Google Cloud Storage trunk, at lib/classes/status/class-migrations.php

293 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Migrations status class
4 *
5 * @since 4.0.0
6 */
7
8 namespace wpCloud\StatelessMedia\Status;
9
10 use wpCloud\StatelessMedia\Singleton;
11 use wpCloud\StatelessMedia\Helper;
12 use wpCloud\StatelessMedia\Migrator;
13
14 class Migrations {
15 use Singleton;
16
17 /**
18 * Migrations data
19 *
20 * @var array
21 */
22 private $migrations = [];
23
24 /**
25 * Primary migration id (that need to run first)
26 *
27 * @var string
28 */
29 private $primary_migration_id = '';
30
31 /**
32 * Migration id that is currently in progress (running or paused)
33 *
34 * @var string
35 */
36 private $running_migration_id = '';
37
38 protected function __construct() {
39 $this->_init_hooks();
40 }
41
42 private function _init_hooks() {
43 add_action('wp_stateless_status_tab_content', [$this, 'tab_content'], 50);
44 add_filter('wp_stateless_batch_state', [$this, 'migrations_state'], 20, 2);
45 }
46
47 /**
48 * Detects the oldest migration that needs to be finished before the next one can run
49 */
50 private function _set_primary_migration_id() {
51 $this->primary_migration_id = '';
52
53 $migrations = array_reverse($this->migrations, true);
54
55 foreach ($migrations as $id => $migration) {
56 if ( in_array($migration['status'], [Migrator::STATUS_PENDING, Migrator::STATUS_RUNNING, Migrator::STATUS_FAILED]) ) {
57 $this->primary_migration_id = $id;
58 break;
59 }
60 }
61 }
62
63 /**
64 * Returns the id of the migration that is currently running (if any)
65 *
66 * @param array $state
67 * @return string|bool|null
68 */
69 private function _set_running_migration_id($state) {
70 $this->running_migration_id = '';
71
72 if ( !empty($state) ) {
73 if ( isset($state['is_migration']) && $state['is_migration'] ) {
74 $this->running_migration_id = $state['id']; // migration is running
75
76 return;
77 }
78 }
79 }
80
81 /**
82 * Generate the message for the migration, depending on the current state
83 *
84 * @param string $id
85 * @param array $migration
86 * @return string
87 */
88 private function _get_migration_message($id, $migration) {
89 $message = $migration['message'] ?? '';
90
91 switch ($migration['status']) {
92 case Migrator::STATUS_FINISHED:
93 $format = get_option('date_format') . ' ' . get_option( 'time_format' );
94 $date = wp_date($format, $migration['finished']);
95 $message = sprintf( __('Finished at %s', ud_get_stateless_media()->domain), $date );
96 break;
97 case Migrator::STATUS_SKIPPED:
98 $message = !empty($message) ? $message : __('Not required', ud_get_stateless_media()->domain);
99 break;
100 case Migrator::STATUS_FAILED:
101 $message = sprintf( __('Failed to finish: %s', ud_get_stateless_media()->domain), $message );
102 break;
103 case Migrator::STATUS_PENDING:
104 if ( $id == $this->primary_migration_id ) {
105 $message = sprintf( __('Ready to run', ud_get_stateless_media()->domain) );
106 } else {
107 $message = sprintf(
108 __('Requires <strong>%s</strong> to finish', ud_get_stateless_media()->domain),
109 $this->migrations[$this->primary_migration_id]['description']
110 );
111 }
112 break;
113 case Migrator::STATUS_RUNNING:
114 $message = __('In progress...', ud_get_stateless_media()->domain);
115 break;
116 }
117
118 return $message;
119 }
120
121 /**
122 * Generate the UI message for the migration, depending on the current state
123 *
124 * @param string $id
125 * @param array $migration
126 * @return string
127 */
128 private function _get_migration_ui_message($status, $state) {
129 $message = '';
130 $counter = false;
131
132 if ( isset( $state['id'] ) && isset( $state['queue'] ) && is_array( $state['queue'] ) && count( $state['queue'] ) > 1 ) {
133 $counter = sprintf( '%d/%d', array_search($state['id'], $state['queue']) + 1, count($state['queue']) );
134 }
135
136 switch ($status) {
137 case Migrator::STATUS_RUNNING:
138 $message = $counter
139 ? sprintf( __('In progress %s...', ud_get_stateless_media()->domain), $counter)
140 : __('In progress...', ud_get_stateless_media()->domain);
141 break;
142 case Migrator::STATUS_PAUSED:
143 $message = $counter
144 ? sprintf( __('Paused %s...', ud_get_stateless_media()->domain), $counter)
145 : __('Paused...', ud_get_stateless_media()->domain);
146 break;
147 }
148
149 return $message;
150 }
151
152 /**
153 * Returns the status of the migration
154 *
155 * @param string $status
156 * @return string
157 */
158 public static function get_status_text($status) {
159 switch ($status) {
160 case Migrator::STATUS_PENDING:
161 return __('Pending', ud_get_stateless_media()->domain);
162 case Migrator::STATUS_RUNNING:
163 return __('Running', ud_get_stateless_media()->domain);
164 case Migrator::STATUS_SKIPPED:
165 return __('Skipped', ud_get_stateless_media()->domain);
166 case Migrator::STATUS_FINISHED:
167 return __('Finished', ud_get_stateless_media()->domain);
168 case Migrator::STATUS_FAILED:
169 return __('Failed', ud_get_stateless_media()->domain);
170 }
171 }
172
173 /**
174 * Get migrations state for frontend display
175 *
176 * @return array
177 */
178 private function _get_migrations_state($state = null) {
179 $migrations = [];
180 $this->migrations = apply_filters('wp_stateless_get_migrations', []);
181
182 if ( !is_array($this->migrations) ) {
183 $this->migrations = [];
184 }
185
186 $defaults = [
187 'description' => '',
188 'status' => '',
189 'status_text' => '',
190 'message' => '',
191 'ui_message' => '',
192 'can_start' => false,
193 'can_pause' => false,
194 'can_resume' => false,
195 ];
196
197 $this->_set_primary_migration_id();
198 $batch_state = $state ? $state : apply_filters('wp_stateless_batch_state', [], []);
199 $this->_set_running_migration_id($batch_state);
200
201 foreach ($this->migrations as $id => $migration) {
202 $status = $migration['status'];
203 $can_start = $this->primary_migration_id == $id;
204 $can_pause = false;
205 $can_resume = false;
206
207 if ( $this->running_migration_id == $id ) {
208 $can_start = false;
209
210 if ( $batch_state['is_paused'] ) {
211 $status = Migrator::STATUS_PAUSED;
212 $can_resume = true;
213 } else {
214 $status = Migrator::STATUS_RUNNING;
215 $can_pause = true;
216 }
217 }
218
219 $classes = [
220 $status,
221 $can_start ? 'can-start' : '',
222 $can_pause ? 'can-pause' : '',
223 $can_resume ? 'can-resume' : '',
224 ];
225
226 $data = wp_parse_args([
227 'description' => $migration['description'],
228 'status' => $status,
229 'classes' => implode( ' ', array_filter($classes) ),
230 'status_text' => self::get_status_text($status),
231 'message' => $this->_get_migration_message($id, $migration),
232 'ui_message' => $this->_get_migration_ui_message($status, $batch_state),
233 'can_start' => $can_start,
234 'can_pause' => $can_pause,
235 'can_resume' => $can_resume,
236 ], $defaults);
237
238 $migrations[$id] = $data;
239 }
240
241 return $migrations;
242 }
243
244 /**
245 * Outputs 'Data Updates' section on the Status tab on the Settings page.
246 */
247 public function tab_content() {
248 if ( is_network_admin() ) {
249 return;
250 }
251
252 $migrations = $this->_get_migrations_state();
253 $migration_ids = [];
254
255 if ( !empty($migrations) ) {
256 foreach ( $migrations as $migration_id => $migration ) {
257 if ( $migration['status'] != Migrator::STATUS_FINISHED && $migration['status'] != Migrator::STATUS_SKIPPED ) {
258 $migration_ids[] = $migration_id;
259 }
260 }
261 }
262
263 if ( !empty($migration_ids) ) {
264 $migration_ids = array_reverse($migration_ids);
265 $migration_id = $migration_ids[0];
266 $migration = $migrations[ $migration_id ];
267
268 include ud_get_stateless_media()->path('static/views/status-sections/migrations.php', 'dir');
269 }
270 }
271
272 /**
273 * Get migration state
274 *
275 * @param array $state
276 * @return array
277 */
278 public function migrations_state($state, $params) {
279 $is_running = $state['is_running'] ?? false;
280 $is_migration = $state['is_migration'] ?? false;
281 $force_migrations = $params['force_migrations'] ?? false;
282
283 if ( !$is_running && !$is_migration && !$force_migrations ) {
284 return $state;
285 }
286
287 $state['migrations'] = $this->_get_migrations_state($state);
288 $state['migrations_notify'] = get_option(Migrator::MIGRATIONS_NOTIFY_KEY, false);
289
290 return $state;
291 }
292 }
293