version = $version; } public function init() { // Ensure someone is logged in if (!is_user_logged_in()) { return; } // Ensure the user is an administrator if (!in_array('administrator', wp_get_current_user()->roles)) { return; } // Add to Tools menue add_action('admin_menu', function () { add_submenu_page( 'tools.php', self::TITLE, self::TITLE, 'administrator', 'disk-usage-insights', [$this, 'index'] ); }); // Add to admin bar add_action('admin_bar_menu', [$this, 'admin_bar_item'], 500); // Add JavaScript libs + CSS add_action('admin_enqueue_scripts', [$this, 'addScripts']); // Add AJAX add_action('wp_ajax_dui_scan', $this->wrapErrors(function() { (new ScanController())->scan(); })); add_action('wp_ajax_dui_worker', $this->wrapErrors(function() { (new ScanWorkerController())->worker(); })); add_action('wp_ajax_dui_status', $this->wrapErrors(function() { (new ScanStatusController())->status(); })); add_action('wp_ajax_dui_delete_snapshot', $this->wrapErrors(function() { (new DeleteSnapshotController())->delete(); })); add_action('wp_ajax_dui_list_snapshots', $this->wrapErrors(function() { (new ShowSnapshotsController())->execute(); })); add_action('wp_ajax_dui_results_table', $this->wrapErrors(function() { (new ShowResultsTableController())->execute(); })); } public function wrapErrors(callable $fn) { return function() use ($fn) { try { return $fn(); } catch (\Throwable $e) { status_header(500, 'Interner Fehler'); //echo 'Fehler: Interner Serverfehler.'; echo "\n\n"; echo $e->getMessage(); echo $e->getTraceAsString(); wp_die(); } }; } public function addScripts($hook_suffix) { if ($hook_suffix != 'tools_page_disk-usage-insights') { return; } wp_enqueue_script( 'htmx.min.js', plugins_url('/res/js/htmx-1.9.12.min.js', __DIR__), [], $this->version, ['in_footer' => true] ); wp_enqueue_script( 'htmx-custom-error-handler.js', plugins_url('/res/js/htmx-custom-error-handler.js', __DIR__), [], $this->version, ['in_footer' => true] ); wp_enqueue_style('styles', plugins_url('/res/css/styles.css', __DIR__), [], $this->version ); } public function admin_bar_item(\WP_Admin_Bar $admin_bar ) { $admin_bar->add_menu(array( 'id' => 'menu-id', 'parent' => null, 'group' => null, 'title' => ''. self::TITLE .'', 'href' => admin_url('tools.php?page=disk-usage-insights'), 'meta' => [ 'title' => self::TITLE ] )); } public function index() { if (!isset($_GET['snapshot'])) { return (new IndexController())->execute(); } else { return (new ShowResultsController())->execute(); } } }