| 1 |
<?php |
| 2 |
/** |
| 3 |
* The retention rules in force for one run. |
| 4 |
* |
| 5 |
* @package Templately |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Templately\Modules\Utilities\Cleanup; |
| 9 |
|
| 10 |
/** |
| 11 |
* Resolved ONCE per run and threaded through, so two tasks in the same sweep |
| 12 |
* cannot see different values because an administrator saved settings midway. |
| 13 |
* |
| 14 |
* The age window is SHARED: every age-kind task reads this one number. They all |
| 15 |
* used 7 days already, so unifying them changes no behaviour — it removes the |
| 16 |
* possibility of drift. A per-task override exists for the case a global value |
| 17 |
* genuinely cannot serve, and is unused by default. |
| 18 |
*/ |
| 19 |
final class RetentionPolicy { |
| 20 |
|
| 21 |
/** @var int */ |
| 22 |
private $age_days; |
| 23 |
|
| 24 |
/** @var int */ |
| 25 |
private $ceiling_bytes; |
| 26 |
|
| 27 |
/** @var bool */ |
| 28 |
private $ceiling_enabled; |
| 29 |
|
| 30 |
/** @var int */ |
| 31 |
private $log_keep; |
| 32 |
|
| 33 |
private function __construct( int $age_days, int $ceiling_bytes, bool $ceiling_enabled, int $log_keep ) { |
| 34 |
$this->age_days = $age_days; |
| 35 |
$this->ceiling_bytes = $ceiling_bytes; |
| 36 |
$this->ceiling_enabled = $ceiling_enabled; |
| 37 |
$this->log_keep = $log_keep; |
| 38 |
} |
| 39 |
|
| 40 |
public static function from_settings(): self { |
| 41 |
$settings = Settings::get(); |
| 42 |
|
| 43 |
return new self( |
| 44 |
$settings['age_days'], |
| 45 |
$settings['ceiling_bytes'], |
| 46 |
$settings['ceiling_enabled'], |
| 47 |
$settings['log_keep'] |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Explicit construction, for tests and for callers that need a fixed policy. |
| 53 |
*/ |
| 54 |
public static function create( int $age_days, int $ceiling_bytes, bool $ceiling_enabled, int $log_keep ): self { |
| 55 |
return new self( $age_days, $ceiling_bytes, $ceiling_enabled, $log_keep ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* The shared age window, in days, for a given task. |
| 60 |
* |
| 61 |
* @param string $task_id Task the window is being read for; passed to the filter. |
| 62 |
*/ |
| 63 |
public function age_days( string $task_id = '' ): int { |
| 64 |
/** |
| 65 |
* Override the retention window for one task. |
| 66 |
* |
| 67 |
* @param int $days |
| 68 |
* @param string $task_id |
| 69 |
*/ |
| 70 |
return (int) apply_filters( 'templately_cleanup_retention_days', $this->age_days, $task_id ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Unix timestamp before which age-kind data is expired. |
| 75 |
*/ |
| 76 |
public function age_threshold( string $task_id = '' ): int { |
| 77 |
return time() - ( $this->age_days( $task_id ) * DAY_IN_SECONDS ); |
| 78 |
} |
| 79 |
|
| 80 |
public function ceiling_enabled(): bool { |
| 81 |
return $this->ceiling_enabled; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* The size ceiling in bytes, or 0 when disabled. |
| 86 |
*/ |
| 87 |
public function ceiling_bytes( string $task_id = '' ): int { |
| 88 |
if ( ! $this->ceiling_enabled ) { |
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Override the storage ceiling for one task. |
| 94 |
* |
| 95 |
* @param int $bytes |
| 96 |
* @param string $task_id |
| 97 |
*/ |
| 98 |
return (int) apply_filters( 'templately_cleanup_size_ceiling', $this->ceiling_bytes, $task_id ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* How many count-kind items to retain. |
| 103 |
*/ |
| 104 |
public function keep_count(): int { |
| 105 |
return $this->log_keep; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Whether the shared age window applies to a descriptor's retention kind. |
| 110 |
* |
| 111 |
* Count-kind and intrinsic-kind tasks MUST ignore it — see RetentionKind. |
| 112 |
*/ |
| 113 |
public function applies_to( TaskDescriptor $descriptor ): bool { |
| 114 |
return RetentionKind::uses_age( $descriptor->retention_kind ); |
| 115 |
} |
| 116 |
|
| 117 |
public function to_array(): array { |
| 118 |
return [ |
| 119 |
'age_days' => $this->age_days, |
| 120 |
'ceiling_bytes' => $this->ceiling_bytes, |
| 121 |
'ceiling_enabled' => $this->ceiling_enabled, |
| 122 |
'log_keep' => $this->log_keep, |
| 123 |
]; |
| 124 |
} |
| 125 |
} |
| 126 |
|