| 1 |
<?php |
| 2 |
namespace Mgleis\DiskUsageInsights\Frontend\Controller; |
| 3 |
|
| 4 |
use Mgleis\DiskUsageInsights\Domain\Database; |
| 5 |
use Mgleis\DiskUsageInsights\Domain\DatabaseRepository; |
| 6 |
use Mgleis\DiskUsageInsights\Plugin; |
| 7 |
use Mgleis\DiskUsageInsights\WpHelper; |
| 8 |
|
| 9 |
class ShowResultsController { |
| 10 |
|
| 11 |
private Database $database; |
| 12 |
|
| 13 |
public function execute() { |
| 14 |
$snapshot = sanitize_file_name(wp_unslash($_GET['snapshot'] ?? '')); |
| 15 |
|
| 16 |
$this->database = (new DatabaseRepository())->loadDatabase($snapshot); |
| 17 |
$sn = $this->database->snapshotRepository->load(); |
| 18 |
if ($sn->collectPhaseFinished !== 1) { |
| 19 |
echo "Cannot read database because it is corrupt/incomplete. Please create a new snapshot."; |
| 20 |
exit; |
| 21 |
} |
| 22 |
$root = $sn->root; |
| 23 |
|
| 24 |
$WP_PLUGIN_URL = WpHelper::getPluginUrl(); |
| 25 |
$WP_NONCE = wp_create_nonce(Plugin::NONCE); |
| 26 |
$WP_ADMIN_AJAX_URL = admin_url('admin-ajax.php'); |
| 27 |
$WP_SNAPSHOT_FILE = $snapshot; |
| 28 |
|
| 29 |
$totalSize = $this->selectInt("SELECT SUM(size) FROM fileentries;"); |
| 30 |
$barChart = $this->createBarchart($totalSize); |
| 31 |
|
| 32 |
include_once __DIR__ . '/../../../views/results.php'; |
| 33 |
} |
| 34 |
|
| 35 |
private function createBarchart(int $totalSize) { |
| 36 |
// Echo WordPress directories |
| 37 |
// echo "WP Content Dir: " . WP_CONTENT_DIR . "\n"; |
| 38 |
// echo "Uploads Dir: " . wp_upload_dir()['basedir'] . "\n"; |
| 39 |
// echo "Themes Dir: " . get_theme_root() . "\n"; |
| 40 |
// echo "Plugins Dir: " . WP_PLUGIN_DIR . "\n"; |
| 41 |
|
| 42 |
// Bar Chart |
| 43 |
$wpCoreSize = $this->selectInt("SELECT SUM(size) FROM fileentries WHERE is_wp_core_file = 1;"); |
| 44 |
$uploadsSize = $this->selectInt("SELECT dir_recursive_size FROM fileentries WHERE name = 'uploads';"); // TODO wrong |
| 45 |
$themesSize = $this->selectInt("SELECT dir_recursive_size FROM fileentries WHERE name = 'themes';"); // TODO wrong |
| 46 |
$pluginsSize = $this->selectInt("SELECT dir_recursive_size FROM fileentries WHERE name = 'plugins';"); // TODO wrong |
| 47 |
$wpContentSize = $this->selectInt("SELECT dir_recursive_size FROM fileentries WHERE name = 'wp-content';") // TODO wrong |
| 48 |
- $themesSize - $pluginsSize - $uploadsSize; |
| 49 |
$barChart = [ |
| 50 |
['label' => 'WP Core', 'percent' => round(100 * $wpCoreSize / $totalSize), 'mb' => round($wpCoreSize / 1_000_000, 1)], |
| 51 |
['label' => 'Uploads', 'percent' => round(100 * $uploadsSize / $totalSize), 'mb' => round($uploadsSize / 1_000_000, 1)], |
| 52 |
['label' => 'Themes', 'percent' => round(100 * $themesSize / $totalSize), 'mb' => round($themesSize / 1_000_000, 1)], |
| 53 |
['label' => 'Plugins', 'percent' => round(100 * $pluginsSize / $totalSize), 'mb' => round($pluginsSize / 1_000_000, 1)], |
| 54 |
['label' => 'wp-content', 'percent' => round(100 * $wpContentSize / $totalSize), 'mb' => round($wpContentSize / 1_000_000, 1)], |
| 55 |
]; |
| 56 |
|
| 57 |
return $barChart; |
| 58 |
} |
| 59 |
|
| 60 |
private function selectInt(string $sql) { |
| 61 |
$stmt = $this->database->q->db->prepare($sql); |
| 62 |
$stmt->execute(); |
| 63 |
$result = $stmt->fetch(); |
| 64 |
|
| 65 |
return $result[0]; |
| 66 |
} |
| 67 |
|
| 68 |
} |
| 69 |
|