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 / Settings.php

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

169 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Persisted, administrator-editable retention settings.
4 *
5 * @package Templately
6 */
7
8 namespace Templately\Modules\Utilities\Cleanup;
9
10 use WP_Error;
11
12 /**
13 * Plain `get_option`/`update_option`, deliberately NOT `Templately\Utils\Options`:
14 * that helper scopes to USER options on multisite, so a value written in one
15 * scope can be read from another. Retention is a property of the site, and
16 * `$wpdb->options` is already per-site on multisite — which is exactly the
17 * scoping FR-036 requires, for free.
18 *
19 * Values are validated and REFUSED when out of range rather than silently
20 * clamped. Silently clamping an administrator's "0 days" to 1 tells them their
21 * setting was accepted when it was not.
22 */
23 final class Settings {
24
25 const OPTION = 'templately_cleanup_settings';
26
27 const DEFAULT_AGE_DAYS = 7;
28 const DEFAULT_CEILING_BYTES = 1073741824; // 1 GiB.
29 const DEFAULT_LOG_KEEP = 10;
30
31 /** Below a day risks deleting an import someone paused overnight. */
32 const MIN_AGE_DAYS = 1;
33 /** Past a year the size ceiling is doing all the work anyway. */
34 const MAX_AGE_DAYS = 365;
35
36 /**
37 * A ceiling under ~100 MiB would trip on a single ordinary template pack,
38 * making every run destructive.
39 */
40 const MIN_CEILING_BYTES = 104857600;
41
42 const MIN_LOG_KEEP = 1;
43 const MAX_LOG_KEEP = 1000;
44
45 public static function defaults(): array {
46 return [
47 'age_days' => self::DEFAULT_AGE_DAYS,
48 'ceiling_bytes' => self::DEFAULT_CEILING_BYTES,
49 'ceiling_enabled' => true,
50 'log_keep' => self::DEFAULT_LOG_KEEP,
51 'disabled' => false,
52 ];
53 }
54
55 public static function get(): array {
56 $stored = get_option( self::OPTION, [] );
57 if ( ! is_array( $stored ) ) {
58 $stored = [];
59 }
60
61 $settings = array_merge( self::defaults(), $stored );
62
63 // Cast defensively: an option written by an older version, or by hand,
64 // must not hand a string into arithmetic.
65 $settings['age_days'] = (int) $settings['age_days'];
66 $settings['ceiling_bytes'] = (int) $settings['ceiling_bytes'];
67 $settings['ceiling_enabled'] = (bool) $settings['ceiling_enabled'];
68 $settings['log_keep'] = (int) $settings['log_keep'];
69 $settings['disabled'] = (bool) $settings['disabled'];
70
71 return $settings;
72 }
73
74 /**
75 * Validate and persist a partial update.
76 *
77 * @param array $changes Only the keys present are written.
78 * @return array|WP_Error The stored settings, or the first validation failure.
79 */
80 public static function update( array $changes ) {
81 $current = self::get();
82
83 if ( array_key_exists( 'age_days', $changes ) ) {
84 $age = (int) $changes['age_days'];
85 if ( $age < self::MIN_AGE_DAYS || $age > self::MAX_AGE_DAYS ) {
86 return new WP_Error(
87 'invalid_retention',
88 sprintf(
89 /* translators: 1: minimum days, 2: maximum days. */
90 __( 'Retention must be between %1$d and %2$d days.', 'templately' ),
91 self::MIN_AGE_DAYS,
92 self::MAX_AGE_DAYS
93 ),
94 [ 'status' => 400, 'field' => 'age_days' ]
95 );
96 }
97 $current['age_days'] = $age;
98 }
99
100 if ( array_key_exists( 'ceiling_bytes', $changes ) ) {
101 $ceiling = (int) $changes['ceiling_bytes'];
102 if ( $ceiling < self::MIN_CEILING_BYTES ) {
103 return new WP_Error(
104 'invalid_ceiling',
105 sprintf(
106 /* translators: %s: minimum size, already formatted (e.g. "100 MB"). */
107 __( 'The storage limit must be at least %s — a smaller limit would be reached by a single template pack.', 'templately' ),
108 size_format( self::MIN_CEILING_BYTES )
109 ),
110 [ 'status' => 400, 'field' => 'ceiling_bytes' ]
111 );
112 }
113 $current['ceiling_bytes'] = $ceiling;
114 }
115
116 if ( array_key_exists( 'ceiling_enabled', $changes ) ) {
117 $current['ceiling_enabled'] = (bool) $changes['ceiling_enabled'];
118 }
119
120 if ( array_key_exists( 'log_keep', $changes ) ) {
121 $keep = (int) $changes['log_keep'];
122 if ( $keep < self::MIN_LOG_KEEP || $keep > self::MAX_LOG_KEEP ) {
123 return new WP_Error(
124 'invalid_log_keep',
125 sprintf(
126 /* translators: 1: minimum count, 2: maximum count. */
127 __( 'Keep between %1$d and %2$d import logs.', 'templately' ),
128 self::MIN_LOG_KEEP,
129 self::MAX_LOG_KEEP
130 ),
131 [ 'status' => 400, 'field' => 'log_keep' ]
132 );
133 }
134 $current['log_keep'] = $keep;
135 }
136
137 if ( array_key_exists( 'disabled', $changes ) ) {
138 $current['disabled'] = (bool) $changes['disabled'];
139 }
140
141 update_option( self::OPTION, $current, false );
142
143 return $current;
144 }
145
146 /**
147 * Whether cleanup is switched off entirely — by an administrator, or in code.
148 *
149 * When true no schedule is registered and no task runs.
150 */
151 public static function is_disabled(): bool {
152 $settings = self::get();
153
154 /**
155 * Disable all cleanup.
156 *
157 * @param bool $disabled
158 */
159 return (bool) apply_filters( 'templately_cleanup_disabled', $settings['disabled'] );
160 }
161
162 /**
163 * Test seam.
164 */
165 public static function reset(): void {
166 delete_option( self::OPTION );
167 }
168 }
169