| @@ -1,13 +1,22 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Mgleis\DiskUsageInsights; |
| 3 | 3 | |
| 4 | -use Mgleis\DiskUsageInsights\Frontend\ScanResults; | |
| 4 | +use Mgleis\DiskUsageInsights\Frontend\Controller\DeleteSnapshotController; | |
| 5 | +use Mgleis\DiskUsageInsights\Frontend\Controller\IndexController; | |
| 6 | +use Mgleis\DiskUsageInsights\Frontend\Controller\ScanController; | |
| 7 | +use Mgleis\DiskUsageInsights\Frontend\Controller\ScanStatusController; | |
| 8 | +use Mgleis\DiskUsageInsights\Frontend\Controller\ScanWorkerController; | |
| 9 | +use Mgleis\DiskUsageInsights\Frontend\Controller\ShowResultsController; | |
| 10 | +use Mgleis\DiskUsageInsights\Frontend\Controller\ShowResultsTableController; | |
| 11 | +use Mgleis\DiskUsageInsights\Frontend\Controller\ShowSnapshotsController; | |
| 5 | 12 | |
| 6 | 13 | class Plugin { |
| 7 | 14 | |
| 15 | + const TITLE = 'Disk Usage Insights'; | |
| 8 | 16 | const NONCE = 'disk_usage_insights'; |
| 9 | - private string $version = ''; | |
| 17 | + /** @var string */ | |
| 18 | + private $version = ''; | |
| 10 | 19 | |
| 11 | 20 | public function __construct(string $version) |
| 12 | 21 | { |
| 13 | 22 | $this->version = $version; |
| @@ -27,10 +36,10 @@ | ||
| 27 | 36 | // Add to Tools menue |
| 28 | 37 | add_action('admin_menu', function () { |
| 29 | 38 | add_submenu_page( |
| 30 | 39 | 'tools.php', |
| 31 | - 'Disk Usage Insights', | |
| 32 | - 'Disk Usage Insights', | |
| 40 | + self::TITLE, | |
| 41 | + self::TITLE, | |
| 33 | 42 | 'administrator', |
| 34 | 43 | 'disk-usage-insights', |
| 35 | 44 | [$this, 'index'] |
| 36 | 45 | ); |
| @@ -42,11 +51,31 @@ | ||
| 42 | 51 | // Add JavaScript libs + CSS |
| 43 | 52 | add_action('admin_enqueue_scripts', [$this, 'addScripts']); |
| 44 | 53 | |
| 45 | 54 | // Add AJAX |
| 46 | - add_action('wp_ajax_scan', [$this, 'scan']); | |
| 55 | + add_action('wp_ajax_dui_scan', $this->wrapErrors(function() { (new ScanController())->scan(); })); | |
| 56 | + add_action('wp_ajax_dui_worker', $this->wrapErrors(function() { (new ScanWorkerController())->worker(); })); | |
| 57 | + add_action('wp_ajax_dui_status', $this->wrapErrors(function() { (new ScanStatusController())->status(); })); | |
| 58 | + add_action('wp_ajax_dui_delete_snapshot', $this->wrapErrors(function() { (new DeleteSnapshotController())->delete(); })); | |
| 59 | + add_action('wp_ajax_dui_list_snapshots', $this->wrapErrors(function() { (new ShowSnapshotsController())->execute(); })); | |
| 60 | + add_action('wp_ajax_dui_results_table', $this->wrapErrors(function() { (new ShowResultsTableController())->execute(); })); | |
| 47 | 61 | } |
| 48 | 62 | |
| 63 | + public function wrapErrors(callable $fn) { | |
| 64 | + return function() use ($fn) { | |
| 65 | + try { | |
| 66 | + return $fn(); | |
| 67 | + } catch (\Throwable $e) { | |
| 68 | + status_header(500, 'Interner Fehler'); | |
| 69 | + //echo 'Fehler: Interner Serverfehler.'; | |
| 70 | + echo "\n\n"; | |
| 71 | + echo esc_html($e->getMessage()); | |
| 72 | + echo esc_html($e->getTraceAsString()); | |
| 73 | + wp_die(); | |
| 74 | + } | |
| 75 | + }; | |
| 76 | + } | |
| 77 | + | |
| 49 | 78 | public function addScripts($hook_suffix) { |
| 50 | 79 | if ($hook_suffix != 'tools_page_disk-usage-insights') { |
| 51 | 80 | return; |
| 52 | 81 | } |
| @@ -71,35 +100,25 @@ | ||
| 71 | 100 | ); |
| 72 | 101 | } |
| 73 | 102 | |
| 74 | 103 | public function admin_bar_item(\WP_Admin_Bar $admin_bar ) { |
| 75 | - $admin_bar->add_menu( array( | |
| 104 | + $admin_bar->add_menu(array( | |
| 76 | 105 | 'id' => 'menu-id', |
| 77 | 106 | 'parent' => null, |
| 78 | 107 | 'group' => null, |
| 79 | - 'title' => '<img src="' . WP_PLUGIN_URL . '/disk-usage-insights/res/pie.svg" style="margin-top:5px; width:20px" alt="Disk Usage Insights">', | |
| 108 | + 'title' => '<img src="' . WpHelper::getPluginUrl() . '/res/pie.svg" style="margin-top:5px; width:20px" alt="'. self::TITLE .'">', | |
| 80 | 109 | 'href' => admin_url('tools.php?page=disk-usage-insights'), |
| 81 | 110 | 'meta' => [ |
| 82 | - 'title' => 'Disk Usage Insights' | |
| 111 | + 'title' => self::TITLE | |
| 83 | 112 | ] |
| 84 | 113 | )); |
| 85 | 114 | } |
| 86 | 115 | |
| 87 | 116 | public function index() { |
| 88 | - $WP_PLUGIN_URL = plugin_dir_url(__DIR__); | |
| 89 | - $WP_NONCE = wp_create_nonce(self::NONCE); | |
| 90 | - $WP_ADMIN_AJAX_URL = admin_url('admin-ajax.php'); | |
| 91 | - include __DIR__ . '/../views/index.php'; | |
| 92 | - } | |
| 93 | - | |
| 94 | - public function scan() { | |
| 95 | - check_ajax_referer(self::NONCE); | |
| 96 | - | |
| 97 | - $scanResults = new ScanResults(); | |
| 98 | - $scanResults->execute(); | |
| 99 | - | |
| 100 | - sleep(1); | |
| 101 | - | |
| 102 | - wp_die(); // All ajax handlers should die when finished | |
| 117 | + if (!isset($_GET['snapshot'])) { | |
| 118 | + return (new IndexController())->execute(); | |
| 119 | + } else { | |
| 120 | + return (new ShowResultsController())->execute(); | |
| 121 | + } | |
| 103 | 122 | } |
| 104 | 123 | |
| 105 | 124 | } |