Upgrades.php
13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
<?php
/**
* Upgrades Controller Class.
*
* @package ContentControl
*/
namespace ContentControl\Controllers\Admin;
use ContentControl\Base\Controller;
defined( 'ABSPATH' ) || exit;
use function __;
use function add_filter;
use function add_action;
use function esc_html_e;
use function esc_attr;
use function get_current_screen;
use function admin_url;
use function is_admin;
use function current_user_can;
use function wp_create_nonce;
use function wp_verify_nonce;
use function wp_send_json_error;
use function wp_unslash;
use function is_wp_error;
use function ContentControl\get_upgrade_name;
use function ContentControl\is_upgrade_complete;
use function ContentControl\mark_upgrade_complete;
/**
* Upgrades Controller.
*
* @package ContentControl\Admin
*/
class Upgrades extends Controller {
/**
* Initialize the settings page.
*/
public function init() {
add_action( 'admin_init', [ $this, 'hooks' ] );
add_action( 'wp_ajax_content_control_upgrades', [ $this, 'ajax_handler' ] );
add_filter( 'content_control/settings-page_localized_vars', [ $this, 'localize_vars' ] );
add_action( 'content_control/update_version', '\\ContentControl\\maybe_force_v2_migrations' );
}
/**
* Hook into relevant WP actions.
*
* @return void
*/
public function hooks() {
if ( is_admin() && current_user_can( 'manage_options' ) ) {
add_action( 'admin_notices', [ $this, 'admin_notices' ] );
add_action( 'network_admin_notices', [ $this, 'admin_notices' ] );
add_action( 'user_admin_notices', [ $this, 'admin_notices' ] );
}
}
/**
* Get a list of all upgrades.
*
* @return string[]
*/
public function all_upgrades() {
return [
// Version 2 upgrades.
'\ContentControl\Upgrades\Backup_2',
'\ContentControl\Upgrades\PluginMeta_2',
'\ContentControl\Upgrades\Settings_2',
'\ContentControl\Upgrades\UserMeta_2',
'\ContentControl\Upgrades\Restrictions_2',
];
}
/**
* Check if there are any upgrades to run.
*
* @return boolean
*/
public function has_upgrades() {
return (bool) count( $this->get_required_upgrades() );
}
/**
* Get a list of required upgrades.
*
* Uses a cached list of done upgrades to prevent extra processing.
*
* @return \ContentControl\Base\Upgrade[]
*/
public function get_required_upgrades() {
$required_upgrades = [];
$all_upgrades = $this->all_upgrades();
$required_upgrades = [];
foreach ( $all_upgrades as $upgrade_class ) {
if ( ! class_exists( $upgrade_class ) ) {
continue;
}
/**
* Upgrade class instance.
*
* @var \ContentControl\Base\Upgrade $upgrade
*/
$upgrade = new $upgrade_class();
// Check if required, and if so, add it to the list.
if ( is_upgrade_complete( $upgrade ) ) {
continue;
} elseif ( ! $upgrade->is_required() ) {
// If its not required, mark it as done.
mark_upgrade_complete( $upgrade );
continue;
}
$required_upgrades[] = $upgrade;
}
// Sort the required upgrades based on prerequisites.
$required_upgrades = $this->sort_upgrades_by_prerequisites( $required_upgrades );
return $required_upgrades;
}
/**
* Sort upgrades based on prerequisites using a graph-based approach.
*
* @param \ContentControl\Base\Upgrade[] $upgrades List of upgrades to sort.
*
* @return \ContentControl\Base\Upgrade[]
*/
private function sort_upgrades_by_prerequisites( $upgrades ) {
// Build the graph of upgrades and their dependencies.
$graph = [];
// Build a lookup table of upgrades by name.
$upgrade_by_name = [];
// Build the graph & lookup tables.
foreach ( $upgrades as $upgrade ) {
$updgrade_name = get_upgrade_name( $upgrade );
// Add the upgrade to the graph with its dependencies.
$graph[ $updgrade_name ] = $upgrade->get_dependencies();
// Add the upgrade to the lookup table.
$upgrade_by_name[ $updgrade_name ] = $upgrade;
}
// Perform a topological sort on the graph.
$sorted = $this->topological_sort( $graph );
$sorted_upgrades = [];
// Map the sorted ugprade list with the upgrade from the lookup table.
foreach ( $sorted as $upgrade_name ) {
$upgrade = isset( $upgrade_by_name[ $upgrade_name ] ) ? $upgrade_by_name[ $upgrade_name ] : null;
$sorted_upgrades[] = $upgrade;
}
// Remove null values, these are upgrades that have been marked as done.
$sorted_upgrades = array_filter( $sorted_upgrades );
// Return the sorted upgrades.
return $sorted_upgrades;
}
/**
* Perform a topological sort on a graph.
*
* @param array<string,array<string>> $graph Graph to sort.
*
* @return array<string>
*/
private function topological_sort( $graph ) {
$visited = [];
$sorted = [];
foreach ( $graph as $node => $dependencies ) {
$this->visit_node( $node, $graph, $visited, $sorted );
}
return $sorted;
}
/**
* Visit a node in the graph for topological sort.
*
* @param string $node Node to visit.
* @param array<string,array<string>> $graph Graph to sort.
* @param array<string,bool> $visited List of visited nodes.
* @param array<string> $sorted List of sorted nodes.
*
* @return void
*/
private function visit_node( $node, $graph, &$visited, &$sorted ) {
if ( isset( $visited[ $node ] ) ) {
// Node already visited, skip.
return;
}
$visited[ $node ] = true;
if ( isset( $graph[ $node ] ) && ! empty( $graph[ $node ] ) ) {
foreach ( $graph[ $node ] as $dependency ) {
$this->visit_node( $dependency, $graph, $visited, $sorted );
}
}
$sorted[] = $node;
}
/**
* AJAX Handler.
*
* @return void
*/
public function ajax_handler() {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( wp_unslash( $_REQUEST['nonce'] ), 'content_control_upgrades' ) ) {
wp_send_json_error( __( 'Invalid nonce.', 'content-control' ) );
}
if ( ! current_user_can( $this->container->get_permission( 'manage_settings' ) ) ) {
wp_send_json_error( __( 'You do not have permission to run upgrades.', 'content-control' ) );
}
try {
$upgrades = $this->get_required_upgrades();
$count = count( $upgrades );
$stream = new \ContentControl\Services\UpgradeStream( 'upgrades' );
$stream->start();
// First do/while loop starts the stream and breaks if connection aborted.
do {
$stream->start_upgrades( $count, __( 'Upgrades started', 'content-control' ) );
$failed_upgrades = [];
// This second while loop runs the upgrades.
while ( ! empty( $this->get_required_upgrades() ) ) {
$required_upgrades = $this->get_required_upgrades();
$upgrade = array_shift( $required_upgrades );
$upgrade_name = get_upgrade_name( $upgrade );
if ( ! isset( $failed_upgrades[ $upgrade_name ] ) ) {
$failed_upgrades[ $upgrade_name ] = 0;
} elseif ( $failed_upgrades[ $upgrade_name ] > 0 ) {
$stream->send_event(
'task:retry',
[
'message' => __( 'Retrying upgrade', 'content-control' ),
'data' => [
'key' => $upgrade_name,
'label' => $upgrade->label(),
],
]
);
}
if ( $failed_upgrades[ $upgrade_name ] > 2 ) {
$stream->send_error( [
'message' => __( 'Some upgrades failed to complete.', 'content-control' ),
] );
$stream->send_event( 'upgrades:error', [
'message' => __( 'Upgrade did not complete, see error logs above.', 'content-control' ),
'data' => $failed_upgrades,
] );
return;
}
$result = $upgrade->stream_run( $stream );
if ( is_wp_error( $result ) ) {
$stream->send_error( [
'message' => sprintf(
// translators: %s: error message.
__( 'Some upgrades failed to complete: %s', 'content-control' ),
$result->get_error_message()
),
'data' => $result,
] );
} elseif ( false !== $result ) {
mark_upgrade_complete( $upgrade );
} else {
// False means the upgrade failed.
++$failed_upgrades[ $upgrade_name ];
}
}
// Filter out any upgrades that have fail counts of 0.
$failed_upgrades = array_filter( $failed_upgrades );
if ( ! empty( $failed_upgrades ) ) {
$stream->send_error( [
'message' => __( 'Some upgrades failed to complete.', 'content-control' ),
'data' => $failed_upgrades,
] );
$stream->complete_upgrades( __( 'Upgrades complete with errors.', 'content-control' ) );
} else {
$stream->complete_upgrades( __( 'Upgrades complete!', 'content-control' ) );
}
} while ( ! $stream->should_abort() );
} catch ( \Exception $e ) {
if ( isset( $stream ) ) {
$stream->send_error( $e );
} else {
wp_send_json_error( $e );
}
}
}
/**
* AJAX Handler.
*
* @return void
*/
public function ajax_handler_demo() {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( wp_unslash( $_REQUEST['nonce'] ), 'content_control_upgrades' ) ) {
wp_send_json_error( __( 'Invalid nonce.', 'content-control' ) );
}
if ( ! current_user_can( $this->container->get_permission( 'manage_settings' ) ) ) {
wp_send_json_error( __( 'You do not have permission to run upgrades.', 'content-control' ) );
}
try {
$upgrades = $this->get_required_upgrades();
$count = count( $upgrades ) * 2;
$stream = new \ContentControl\Services\UpgradeStream( 'upgrades' );
$stream->start();
$count = wp_rand( 3, 10 );
do {
$stream->start_upgrades( $count, __( 'Upgrades started', 'content-control' ) );
// test loop of 1000 upgrades.
$test_delay = 60000;
for ( $i = 0; $i < $count; $i++ ) {
usleep( $test_delay );
$task_count = wp_rand( 5, 100 );
$stream->start_task(
__( 'Migrating restrictions', 'content-control' ),
$task_count
);
// test loop of 1000 upgrades.
for ( $i2 = 0; $i2 < $task_count; $i2++ ) {
usleep( $test_delay );
$stream->update_task_progress( $i2 + 1 );
}
usleep( $test_delay );
// translators: %d: number of restrictions migrated.
$stream->complete_task( sprintf( __( '%d restrictions migrated', 'content-control' ), $i2 ) );
}
usleep( $test_delay );
$stream->complete_upgrades( __( 'Upgrades complete!', 'content-control' ) );
} while ( ! $stream->should_abort() && ! empty( $upgrades ) );
} catch ( \Exception $e ) {
if ( isset( $stream ) ) {
$stream->send_error( [
'message' => $e->getMessage(),
'data' => $e,
] );
} else {
wp_send_json_error( $e );
}
}
}
/**
* Render admin notices if available.
*
* @return void
*/
public function admin_notices() {
if ( ! is_admin() || ! current_user_can( $this->container->get_permission( 'manage_settings' ) ) ) {
return;
}
if ( ! $this->has_upgrades() ) {
return;
}
$screen = get_current_screen();
if ( 'settings_page_content-control-settings' === $screen->id ) {
return;
}
?>
<style>
.content-control-notice {
display: flex;
align-items: center;
gap: 16px;
padding: 8px;
margin-top: 16px;
margin-bottom: 16px;
}
.content-control-notice .notice-logo {
flex: 0 0 60px;
max-width: 60px;
font-size: 60px;
}
.content-control-notice .notice-content {
flex-grow: 1;
}
.content-control-notice p {
margin-bottom: 0;
max-width: 800px;
}
.content-control-notice .notice-actions {
margin-top: 10px;
margin-bottom: 0;
padding-left: 0;
list-style: none;
display: flex;
gap: 16px;
align-items: center;
}
</style>
<div class="notice notice-info content-control-notice">
<div class="notice-logo">
<img class="logo" width="60" src="<?php echo esc_attr( $this->container->get_url( 'assets/images/illustration-check.svg' ) ); ?>" />
</div>
<div class="notice-content">
<p>
<strong><?php esc_html_e( 'Content Control has been updated and needs to run some database upgrades.', 'content-control' ); ?></strong>
</p>
<ul class="notice-actions">
<li>
<a class="content-control-go-to-settings button button-tertiary" href="<?php echo esc_attr( admin_url( 'options-general.php?page=content-control-settings' ) ); ?>" data-reason="am_now">
🚨 <?php esc_html_e( 'Upgrade Now', 'content-control' ); ?>
</a>
</li>
</ul>
</div>
</div>
<?php
}
/**
* Add localized vars to settings page if there are upgrades to run.
*
* @param array<string,mixed> $vars Localized vars.
*
* @return array<string,mixed>
*/
public function localize_vars( $vars ) {
if ( ! is_admin() || ! current_user_can( $this->container->get_permission( 'manage_settings' ) ) ) {
return $vars;
}
$vars['hasUpgrades'] = false;
if ( ! $this->has_upgrades() ) {
return $vars;
}
$vars['hasUpgrades'] = true;
$vars['upgradeNonce'] = wp_create_nonce( 'content_control_upgrades' );
$vars['upgradeUrl'] = admin_url( 'admin-ajax.php?action=content_control_upgrades' );
$vars['upgrades'] = [];
$upgrades = $this->get_required_upgrades();
foreach ( $upgrades as $upgrade ) {
$upgrade_name = get_upgrade_name( $upgrade );
switch ( $upgrade->get_type() ) {
case 'restrictions':
$vars['hasRestrictionUpgrades'] = true;
break;
case 'settings':
$vars['hasSettingsUpgrades'] = true;
break;
}
$vars['upgrades'][ $upgrade_name ] = [
'key' => $upgrade_name,
'label' => $upgrade->label(),
'description' => $upgrade->description(),
];
}
return $vars;
}
}