| 1 |
<?php |
| 2 |
/** |
| 3 |
* A rolling record of recent cleanup runs. |
| 4 |
* |
| 5 |
* @package Templately |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Templately\Modules\Utilities\Cleanup; |
| 9 |
|
| 10 |
/** |
| 11 |
* Enough for support to see recent behaviour; not an audit log. Bounded at |
| 12 |
* {@see self::DEPTH} entries so the option stays small — and non-autoloaded, so |
| 13 |
* a record nobody is reading costs nothing on an ordinary page load. |
| 14 |
* |
| 15 |
* Plain `update_option`, not `Templately\Utils\Options`: that helper switches to |
| 16 |
* user options on multisite, and this is per-site state. |
| 17 |
*/ |
| 18 |
final class RunJournal { |
| 19 |
|
| 20 |
const OPTION = 'templately_cleanup_journal'; |
| 21 |
|
| 22 |
/** Newest-first, capped. */ |
| 23 |
const DEPTH = 20; |
| 24 |
|
| 25 |
/** |
| 26 |
* @return array[] Newest first. |
| 27 |
*/ |
| 28 |
public static function all(): array { |
| 29 |
$stored = get_option( self::OPTION, [] ); |
| 30 |
|
| 31 |
return is_array( $stored ) ? $stored : []; |
| 32 |
} |
| 33 |
|
| 34 |
public static function latest(): ?array { |
| 35 |
$all = self::all(); |
| 36 |
|
| 37 |
return $all[0] ?? null; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Record one run. |
| 42 |
* |
| 43 |
* @param string $trigger schedule | manual | post_import |
| 44 |
* @param array<string,TaskResult> $results Per task. |
| 45 |
* @param bool $dry_run |
| 46 |
* @param int $duration_ms |
| 47 |
* @return array The stored entry. |
| 48 |
*/ |
| 49 |
public static function record( string $trigger, array $results, bool $dry_run, int $duration_ms ): array { |
| 50 |
$tasks = []; |
| 51 |
$bytes_total = 0; |
| 52 |
$items_total = 0; |
| 53 |
|
| 54 |
foreach ( $results as $id => $result ) { |
| 55 |
$tasks[ $id ] = $result->to_array(); |
| 56 |
$bytes_total += $result->bytes; |
| 57 |
$items_total += $result->items; |
| 58 |
} |
| 59 |
|
| 60 |
$entry = [ |
| 61 |
'time' => time(), |
| 62 |
'trigger' => $trigger, |
| 63 |
'dry_run' => $dry_run, |
| 64 |
'tasks' => $tasks, |
| 65 |
'bytes_total' => $bytes_total, |
| 66 |
'items_total' => $items_total, |
| 67 |
'duration_ms' => $duration_ms, |
| 68 |
]; |
| 69 |
|
| 70 |
$journal = self::all(); |
| 71 |
array_unshift( $journal, $entry ); |
| 72 |
$journal = array_slice( $journal, 0, self::DEPTH ); |
| 73 |
|
| 74 |
update_option( self::OPTION, $journal, false ); |
| 75 |
|
| 76 |
return $entry; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* The most recent dry-run estimate of what could be reclaimed. |
| 81 |
* |
| 82 |
* This is what the activation prompt shows — an unactivated site journals |
| 83 |
* dry runs, so there is always a current figure to state without scanning |
| 84 |
* the disk again on an admin page load. |
| 85 |
*/ |
| 86 |
public static function last_estimate_bytes(): int { |
| 87 |
foreach ( self::all() as $entry ) { |
| 88 |
if ( ! empty( $entry['dry_run'] ) ) { |
| 89 |
return (int) ( $entry['bytes_total'] ?? 0 ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
public static function reset(): void { |
| 97 |
delete_option( self::OPTION ); |
| 98 |
} |
| 99 |
} |
| 100 |
|