| 1 |
<?php |
| 2 |
/** |
| 3 |
* What the plugin is using on disk, and how much room is left. |
| 4 |
* |
| 5 |
* @package Templately |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Templately\Modules\Utilities\Cleanup; |
| 9 |
|
| 10 |
/** |
| 11 |
* A thin, UI-shaped view over the scanner. |
| 12 |
* |
| 13 |
* The `partial` flag is load-bearing, not decoration. A scan that hit its time |
| 14 |
* budget must be reported AS partial — presenting a partial figure as a total |
| 15 |
* tells a user with 8 GB of leftovers that they have 300 MB, and "nothing much |
| 16 |
* to clean" is the one wrong answer this screen can give. |
| 17 |
*/ |
| 18 |
final class UsageReport { |
| 19 |
|
| 20 |
/** |
| 21 |
* @param bool $refresh Bypass the 5-minute cache. |
| 22 |
* @param string|null $continuation Resume a scan that ran out of budget. |
| 23 |
*/ |
| 24 |
public static function build( bool $refresh = false, ?string $continuation = null ): array { |
| 25 |
$payload = Scanner::get_usage( |
| 26 |
[ |
| 27 |
'refresh' => $refresh, |
| 28 |
'continue' => (bool) $continuation, |
| 29 |
] |
| 30 |
); |
| 31 |
|
| 32 |
$areas = []; |
| 33 |
foreach ( (array) ( $payload['subdirs'] ?? [] ) as $entry ) { |
| 34 |
$name = isset( $entry['name'] ) ? (string) $entry['name'] : ''; |
| 35 |
$areas[ $name ] = [ |
| 36 |
'bytes' => (int) ( $entry['bytes'] ?? 0 ), |
| 37 |
'items' => (int) ( $entry['files'] ?? 0 ), |
| 38 |
'label' => self::label_for( $name ), |
| 39 |
]; |
| 40 |
} |
| 41 |
|
| 42 |
$loose_bytes = (int) ( $payload['loose_bytes'] ?? 0 ); |
| 43 |
if ( $loose_bytes > 0 ) { |
| 44 |
$areas['_loose'] = [ |
| 45 |
'bytes' => $loose_bytes, |
| 46 |
'items' => (int) ( $payload['loose_files'] ?? 0 ), |
| 47 |
'label' => __( 'Other files', 'templately' ), |
| 48 |
]; |
| 49 |
} |
| 50 |
|
| 51 |
$done = ! empty( $payload['done'] ); |
| 52 |
|
| 53 |
return [ |
| 54 |
'exists' => ! empty( $payload['exists'] ), |
| 55 |
'areas' => $areas, |
| 56 |
'total_bytes' => (int) ( $payload['total_bytes'] ?? 0 ), |
| 57 |
'total_items' => (int) ( $payload['total_files'] ?? 0 ), |
| 58 |
// A caller must be able to tell "we measured everything and it is |
| 59 |
// small" from "we ran out of time and this is what we got so far". |
| 60 |
'partial' => ! $done, |
| 61 |
'continuation' => $done ? null : 'resume', |
| 62 |
'free_bytes' => self::free_bytes(), |
| 63 |
'cached' => ! empty( $payload['cached'] ), |
| 64 |
]; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Human-readable name for a top-level directory. |
| 69 |
* |
| 70 |
* Falls back to the raw directory name rather than hiding an area the UI |
| 71 |
* does not recognise — an unexplained 4 GB is still worth showing. |
| 72 |
*/ |
| 73 |
private static function label_for( string $name ): string { |
| 74 |
switch ( $name ) { |
| 75 |
case 'tmp': |
| 76 |
return __( 'Import working files', 'templately' ); |
| 77 |
case 'preview': |
| 78 |
return __( 'AI preview files', 'templately' ); |
| 79 |
case 'log': |
| 80 |
return __( 'Logs', 'templately' ); |
| 81 |
case 'patterns': |
| 82 |
return __( 'Cached patterns', 'templately' ); |
| 83 |
default: |
| 84 |
return $name; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Free space on the volume holding the uploads directory. |
| 90 |
* |
| 91 |
* Null where the host disables the check — reported as unknown rather than |
| 92 |
* as zero, which would read as "the disk is full". |
| 93 |
*/ |
| 94 |
private static function free_bytes(): ?int { |
| 95 |
$base = Scanner::get_base_dir(); |
| 96 |
$dir = is_dir( $base ) ? $base : dirname( $base ); |
| 97 |
|
| 98 |
$free = @disk_free_space( $dir ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 99 |
|
| 100 |
return ( false === $free || null === $free ) ? null : (int) $free; |
| 101 |
} |
| 102 |
} |
| 103 |
|