PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.8.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.8.0
3.8.0 3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 All 112 releases
templately / modules / utilities / Cleanup / RunJournal.php

RunJournal.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.8.0, at modules/utilities/Cleanup/RunJournal.php

100 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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