| 1 |
<?php |
| 2 |
namespace Mgleis\DiskUsageInsights; |
| 3 |
|
| 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; |
| 12 |
|
| 13 |
class Plugin { |
| 14 |
|
| 15 |
const TITLE = 'Disk Usage Insights'; |
| 16 |
const NONCE = 'disk_usage_insights'; |
| 17 |
/** @var string */ |
| 18 |
private $version = ''; |
| 19 |
|
| 20 |
public function __construct(string $version) |
| 21 |
{ |
| 22 |
$this->version = $version; |
| 23 |
} |
| 24 |
|
| 25 |
public function init() { |
| 26 |
// Ensure someone is logged in |
| 27 |
if (!is_user_logged_in()) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
// Ensure the user is an administrator |
| 32 |
if (!in_array('administrator', wp_get_current_user()->roles)) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
// Add to Tools menue |
| 37 |
add_action('admin_menu', function () { |
| 38 |
add_submenu_page( |
| 39 |
'tools.php', |
| 40 |
self::TITLE, |
| 41 |
self::TITLE, |
| 42 |
'administrator', |
| 43 |
'disk-usage-insights', |
| 44 |
[$this, 'index'] |
| 45 |
); |
| 46 |
}); |
| 47 |
|
| 48 |
// Add to admin bar |
| 49 |
add_action('admin_bar_menu', [$this, 'admin_bar_item'], 500); |
| 50 |
|
| 51 |
// Add JavaScript libs + CSS |
| 52 |
add_action('admin_enqueue_scripts', [$this, 'addScripts']); |
| 53 |
|
| 54 |
// Add AJAX |
| 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(); })); |
| 61 |
} |
| 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 |
|
| 78 |
public function addScripts($hook_suffix) { |
| 79 |
if ($hook_suffix != 'tools_page_disk-usage-insights') { |
| 80 |
return; |
| 81 |
} |
| 82 |
wp_enqueue_script( |
| 83 |
'htmx.min.js', |
| 84 |
plugins_url('/res/js/htmx-1.9.12.min.js', __DIR__), |
| 85 |
[], |
| 86 |
$this->version, |
| 87 |
['in_footer' => true] |
| 88 |
); |
| 89 |
wp_enqueue_script( |
| 90 |
'htmx-custom-error-handler.js', |
| 91 |
plugins_url('/res/js/htmx-custom-error-handler.js', __DIR__), |
| 92 |
[], |
| 93 |
$this->version, |
| 94 |
['in_footer' => true] |
| 95 |
); |
| 96 |
wp_enqueue_style('styles', |
| 97 |
plugins_url('/res/css/styles.css', __DIR__), |
| 98 |
[], |
| 99 |
$this->version |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
public function admin_bar_item(\WP_Admin_Bar $admin_bar ) { |
| 104 |
$admin_bar->add_menu(array( |
| 105 |
'id' => 'menu-id', |
| 106 |
'parent' => null, |
| 107 |
'group' => null, |
| 108 |
'title' => '<img src="' . WpHelper::getPluginUrl() . '/res/pie.svg" style="margin-top:5px; width:20px" alt="'. self::TITLE .'">', |
| 109 |
'href' => admin_url('tools.php?page=disk-usage-insights'), |
| 110 |
'meta' => [ |
| 111 |
'title' => self::TITLE |
| 112 |
] |
| 113 |
)); |
| 114 |
} |
| 115 |
|
| 116 |
public function index() { |
| 117 |
if (!isset($_GET['snapshot'])) { |
| 118 |
return (new IndexController())->execute(); |
| 119 |
} else { |
| 120 |
return (new ShowResultsController())->execute(); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
} |
| 125 |
|