| 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\ResultsController; |
| 7 |
use Mgleis\DiskUsageInsights\Frontend\Controller\ScanController; |
| 8 |
use Mgleis\DiskUsageInsights\Frontend\Controller\ScanStatusController; |
| 9 |
use Mgleis\DiskUsageInsights\Frontend\Controller\ScanWorkerController; |
| 10 |
use Mgleis\DiskUsageInsights\Frontend\Controller\ShowSnapshotsController; |
| 11 |
|
| 12 |
class Plugin { |
| 13 |
|
| 14 |
const NONCE = 'disk_usage_insights'; |
| 15 |
/** @var string */ |
| 16 |
private $version = ''; |
| 17 |
|
| 18 |
public function __construct(string $version) |
| 19 |
{ |
| 20 |
$this->version = $version; |
| 21 |
} |
| 22 |
|
| 23 |
public function init() { |
| 24 |
// Ensure someone is logged in |
| 25 |
if (!is_user_logged_in()) { |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
// Ensure the user is an administrator |
| 30 |
if (!in_array('administrator', wp_get_current_user()->roles)) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
// Add to Tools menue |
| 35 |
add_action('admin_menu', function () { |
| 36 |
add_submenu_page( |
| 37 |
'tools.php', |
| 38 |
'Disk Usage Insights', |
| 39 |
'Disk Usage Insights', |
| 40 |
'administrator', |
| 41 |
'disk-usage-insights', |
| 42 |
[$this, 'index'] |
| 43 |
); |
| 44 |
}); |
| 45 |
|
| 46 |
// Add to admin bar |
| 47 |
add_action('admin_bar_menu', [$this, 'admin_bar_item'], 500); |
| 48 |
|
| 49 |
// Add JavaScript libs + CSS |
| 50 |
add_action('admin_enqueue_scripts', [$this, 'addScripts']); |
| 51 |
|
| 52 |
// Add AJAX |
| 53 |
add_action('wp_ajax_dui_scan', function() { (new ScanController())->scan(); }); |
| 54 |
add_action('wp_ajax_dui_worker', function() { (new ScanWorkerController())->worker(); }); |
| 55 |
add_action('wp_ajax_dui_status', function() { (new ScanStatusController())->status(); }); |
| 56 |
add_action('wp_ajax_dui_delete_snapshot', function() { (new DeleteSnapshotController())->delete(); }); |
| 57 |
add_action('wp_ajax_dui_list_snapshots', function() { (new ShowSnapshotsController())->execute(); }); |
| 58 |
} |
| 59 |
|
| 60 |
public function addScripts($hook_suffix) { |
| 61 |
if ($hook_suffix != 'tools_page_disk-usage-insights') { |
| 62 |
return; |
| 63 |
} |
| 64 |
wp_enqueue_script( |
| 65 |
'htmx.min.js', |
| 66 |
plugins_url('/res/js/htmx-1.9.12.min.js', __DIR__), |
| 67 |
[], |
| 68 |
$this->version, |
| 69 |
['in_footer' => true] |
| 70 |
); |
| 71 |
wp_enqueue_script( |
| 72 |
'htmx-custom-error-handler.js', |
| 73 |
plugins_url('/res/js/htmx-custom-error-handler.js', __DIR__), |
| 74 |
[], |
| 75 |
$this->version, |
| 76 |
['in_footer' => true] |
| 77 |
); |
| 78 |
wp_enqueue_style('styles', |
| 79 |
plugins_url('/res/css/styles.css', __DIR__), |
| 80 |
[], |
| 81 |
$this->version |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
public function admin_bar_item(\WP_Admin_Bar $admin_bar ) { |
| 86 |
$admin_bar->add_menu(array( |
| 87 |
'id' => 'menu-id', |
| 88 |
'parent' => null, |
| 89 |
'group' => null, |
| 90 |
'title' => '<img src="' . WpHelper::getPluginUrl() . '/res/pie.svg" style="margin-top:5px; width:20px" alt="Disk Usage Insights">', |
| 91 |
'href' => admin_url('tools.php?page=disk-usage-insights'), |
| 92 |
'meta' => [ |
| 93 |
'title' => 'Disk Usage Insights' |
| 94 |
] |
| 95 |
)); |
| 96 |
} |
| 97 |
|
| 98 |
public function index() { |
| 99 |
if (!isset($_GET['snapshot'])) { |
| 100 |
return (new IndexController())->execute(); |
| 101 |
} else { |
| 102 |
return (new ResultsController())->execute(); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
} |
| 107 |
|