| 1 |
<?php |
| 2 |
namespace Mgleis\DiskUsageInsights; |
| 3 |
|
| 4 |
use Mgleis\DiskUsageInsights\Frontend\ScanResults; |
| 5 |
|
| 6 |
class Plugin { |
| 7 |
|
| 8 |
const NONCE = 'disk_usage_insights'; |
| 9 |
private string $version = ''; |
| 10 |
|
| 11 |
public function __construct(string $version) |
| 12 |
{ |
| 13 |
$this->version = $version; |
| 14 |
} |
| 15 |
|
| 16 |
public function init() { |
| 17 |
// Ensure someone is logged in |
| 18 |
if (!is_user_logged_in()) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
// Ensure the user is an administrator |
| 23 |
if (!in_array('administrator', wp_get_current_user()->roles)) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
// Add to Tools menue |
| 28 |
add_action('admin_menu', function () { |
| 29 |
add_submenu_page( |
| 30 |
'tools.php', |
| 31 |
'Disk Usage Insights', |
| 32 |
'Disk Usage Insights', |
| 33 |
'administrator', |
| 34 |
'disk-usage-insights', |
| 35 |
[$this, 'index'] |
| 36 |
); |
| 37 |
}); |
| 38 |
|
| 39 |
// Add to admin bar |
| 40 |
add_action('admin_bar_menu', [$this, 'admin_bar_item'], 500); |
| 41 |
|
| 42 |
// Add JavaScript libs + CSS |
| 43 |
add_action('admin_enqueue_scripts', [$this, 'addScripts']); |
| 44 |
|
| 45 |
// Add AJAX |
| 46 |
add_action('wp_ajax_scan', [$this, 'scan']); |
| 47 |
} |
| 48 |
|
| 49 |
public function addScripts($hook_suffix) { |
| 50 |
if ($hook_suffix != 'tools_page_disk-usage-insights') { |
| 51 |
return; |
| 52 |
} |
| 53 |
wp_enqueue_script( |
| 54 |
'htmx.min.js', |
| 55 |
plugins_url('/res/js/htmx-1.9.12.min.js', __DIR__), |
| 56 |
[], |
| 57 |
$this->version, |
| 58 |
['in_footer' => true] |
| 59 |
); |
| 60 |
wp_enqueue_script( |
| 61 |
'htmx-custom-error-handler.js', |
| 62 |
plugins_url('/res/js/htmx-custom-error-handler.js', __DIR__), |
| 63 |
[], |
| 64 |
$this->version, |
| 65 |
['in_footer' => true] |
| 66 |
); |
| 67 |
wp_enqueue_style('styles', |
| 68 |
plugins_url('/res/css/styles.css', __DIR__), |
| 69 |
[], |
| 70 |
$this->version |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
public function admin_bar_item(\WP_Admin_Bar $admin_bar ) { |
| 75 |
$admin_bar->add_menu( array( |
| 76 |
'id' => 'menu-id', |
| 77 |
'parent' => null, |
| 78 |
'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">', |
| 80 |
'href' => admin_url('tools.php?page=disk-usage-insights'), |
| 81 |
'meta' => [ |
| 82 |
'title' => 'Disk Usage Insights' |
| 83 |
] |
| 84 |
)); |
| 85 |
} |
| 86 |
|
| 87 |
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 |
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
|