| 1 |
<?php |
| 2 |
namespace Mgleis\DiskUsageInsights\Frontend\Controller; |
| 3 |
|
| 4 |
use Mgleis\DiskUsageInsights\Domain\DatabaseRepository; |
| 5 |
use Mgleis\DiskUsageInsights\Domain\Jobs\PhaseCoordinatorJob; |
| 6 |
use Mgleis\DiskUsageInsights\Plugin; |
| 7 |
|
| 8 |
class ScanStatusController { |
| 9 |
|
| 10 |
public function status() { |
| 11 |
check_ajax_referer(Plugin::NONCE); |
| 12 |
|
| 13 |
// TODO validate value: ensure file exists in data directory |
| 14 |
$snapshotName = sanitize_file_name(wp_unslash($_POST['snapshot'] ?? '')); |
| 15 |
|
| 16 |
$database = (new DatabaseRepository())->loadDatabase($snapshotName); |
| 17 |
|
| 18 |
// if process finished = stop reloading |
| 19 |
$_snapshot = $database->snapshotRepository->load(); |
| 20 |
if ($_snapshot->collectPhaseFinished === 1) { |
| 21 |
http_response_code(286); // htmx stops timer |
| 22 |
header('HX-Redirect: ' . admin_url('tools.php?page=disk-usage-insights&snapshot=' . $snapshotName)); |
| 23 |
exit; |
| 24 |
} |
| 25 |
$currentPhase = $_snapshot->phase; |
| 26 |
|
| 27 |
echo "<br>\n"; |
| 28 |
foreach(PhaseCoordinatorJob::PHASES as $idx => $phaseText) { |
| 29 |
|
| 30 |
$phaseStatus = $currentPhase > $idx ? '✓ ' : ''; |
| 31 |
$taskDescription = ''; |
| 32 |
|
| 33 |
if ($currentPhase == $idx) { |
| 34 |
|
| 35 |
$job = $database->q->peek(); |
| 36 |
if ($job !== null) { |
| 37 |
$reflect = new \ReflectionClass($job->payload['type']); |
| 38 |
$instance = $reflect->newInstanceArgs($job->payload['args']); |
| 39 |
|
| 40 |
$taskDescription = ' -- ' . $instance->toDescription(); |
| 41 |
} |
| 42 |
|
| 43 |
} |
| 44 |
if ($idx == $currentPhase) { |
| 45 |
echo "<b>"; |
| 46 |
} |
| 47 |
echo sprintf('%s%s%s', esc_html($phaseStatus), esc_html($phaseText), esc_html($taskDescription)); |
| 48 |
if ($idx == $currentPhase) { |
| 49 |
echo "</b>"; |
| 50 |
} |
| 51 |
echo "<br>\n"; |
| 52 |
} |
| 53 |
|
| 54 |
// debug counter |
| 55 |
$counter = (int) sanitize_text_field(wp_unslash($_POST['counter'] ?? 0)); |
| 56 |
$counter++; |
| 57 |
echo '<input type="hidden" name="counter" value="' . esc_attr($counter) . '">'; |
| 58 |
echo "<br>\nRunning for " . esc_html($counter) . " seconds...<br>\n"; |
| 59 |
|
| 60 |
wp_die(); // All ajax handlers should die when finished |
| 61 |
} |
| 62 |
|
| 63 |
} |
| 64 |
|