# disk-usage-insights/1.0/src/Plugin.php

Disk Usage Insights, version 1.0. 106 lines.

- Page: https://pluginprobe.com/plugins/disk-usage-insights/1.0/code/src/Plugin.php
- Raw: https://pluginprobe.com/plugins/disk-usage-insights/1.0/raw/src/Plugin.php
- Modified: 2024-08-20T16:21:52+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/disk-usage-insights/1.0/code/src/Plugin.php#L10-L20`.

```php
<?php
namespace Mgleis\DiskUsageInsights;

use Mgleis\DiskUsageInsights\Frontend\ScanResults;

class Plugin {

    const NONCE = 'disk_usage_insights';
    private string $version = '';

    public function __construct(string $version)
    {
        $this->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',
                'Disk Usage Insights',
                'Disk Usage Insights',
                '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_scan', [$this, 'scan']);
    }

    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' => '<img src="' . WP_PLUGIN_URL . '/disk-usage-insights/res/pie.svg" style="margin-top:5px; width:20px" alt="Disk Usage Insights">',
            'href'  => admin_url('tools.php?page=disk-usage-insights'),
            'meta' => [
                'title' => 'Disk Usage Insights'
            ]
        ));
    }

    public function index() {
        $WP_PLUGIN_URL = plugin_dir_url(__DIR__);
        $WP_NONCE = wp_create_nonce(self::NONCE);
        $WP_ADMIN_AJAX_URL = admin_url('admin-ajax.php');
        include __DIR__ . '/../views/index.php';
    }

    public function scan() {
        check_ajax_referer(self::NONCE);

        $scanResults = new ScanResults();
        $scanResults->execute();

        sleep(1);

        wp_die(); // All ajax handlers should die when finished
    }

}

```
