| 1 |
<?php |
| 2 |
/** |
| 3 |
* Purge the plugin's own expired cache entries. |
| 4 |
* |
| 5 |
* @package Templately |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Templately\Modules\Utilities\Cleanup\Tasks; |
| 9 |
|
| 10 |
use Templately\Modules\Utilities\Cleanup\CleanupTask; |
| 11 |
use Templately\Modules\Utilities\Cleanup\Context; |
| 12 |
use Templately\Modules\Utilities\Cleanup\RetentionKind; |
| 13 |
use Templately\Modules\Utilities\Cleanup\Tasks\Support\Transients; |
| 14 |
use Templately\Modules\Utilities\Cleanup\TaskResult; |
| 15 |
|
| 16 |
/** |
| 17 |
* Shipped sites had NO transient purge at all before this: the cleaner existed |
| 18 |
* only in `modules/developer/`, which `.distignore` strips from the zip. |
| 19 |
* |
| 20 |
* Two modes, and the split matters: |
| 21 |
* |
| 22 |
* - SCHEDULED removes only entries WordPress already considers expired. Safe |
| 23 |
* to run unattended because nothing live is touched. |
| 24 |
* - MANUAL (the developer panel's "delete all") removes unexpired ones too. |
| 25 |
* Deliberately not schedulable: the cached pattern library is a transient, |
| 26 |
* and dropping it unexpired costs real cloud requests to rebuild. |
| 27 |
* |
| 28 |
* INTRINSIC retention: each entry carries the expiry its writer chose. The |
| 29 |
* shared age window must not apply — a 7-day rule would delete cache the plugin |
| 30 |
* still considers live. |
| 31 |
*/ |
| 32 |
class TransientsTask implements CleanupTask { |
| 33 |
|
| 34 |
public function descriptor(): array { |
| 35 |
return [ |
| 36 |
'id' => 'plugin-transients', |
| 37 |
'label' => __( 'Expired cached data', 'templately' ), |
| 38 |
'group' => 'cache', |
| 39 |
'scope' => 'records', |
| 40 |
'retention_kind' => RetentionKind::INTRINSIC, |
| 41 |
'destructive' => true, |
| 42 |
'schedulable' => true, |
| 43 |
]; |
| 44 |
} |
| 45 |
|
| 46 |
public function estimate( Context $context ): TaskResult { |
| 47 |
return TaskResult::empty()->add( count( $this->expired_keys() ), 0 ); |
| 48 |
} |
| 49 |
|
| 50 |
public function run( Context $context ): TaskResult { |
| 51 |
$expired = $this->expired_keys(); |
| 52 |
|
| 53 |
if ( $context->is_dry_run() ) { |
| 54 |
return TaskResult::empty()->add( count( $expired ), 0 ); |
| 55 |
} |
| 56 |
|
| 57 |
$result = TaskResult::empty(); |
| 58 |
|
| 59 |
foreach ( $expired as $key ) { |
| 60 |
if ( delete_transient( $key ) ) { |
| 61 |
$result->add( 1, 0 ); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return $result; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Keys whose stored timeout has already passed. |
| 70 |
* |
| 71 |
* Scoped to the plugin's own `_templately_` entries — reclaiming another |
| 72 |
* plugin's cache is explicitly out of scope, and WordPress's own |
| 73 |
* `delete_expired_transients()` is site-wide. |
| 74 |
* |
| 75 |
* @return string[] |
| 76 |
*/ |
| 77 |
private function expired_keys(): array { |
| 78 |
$expired = []; |
| 79 |
$now = time(); |
| 80 |
|
| 81 |
foreach ( Transients::get_templately_transients() as $row ) { |
| 82 |
$key = Transients::option_name_to_transient_key( $row->option_name ); |
| 83 |
|
| 84 |
$timeout = get_option( '_transient_timeout_' . $key, false ); |
| 85 |
if ( false === $timeout ) { |
| 86 |
// No timeout row = never expires. Not ours to reclaim on a schedule. |
| 87 |
continue; |
| 88 |
} |
| 89 |
|
| 90 |
if ( (int) $timeout < $now ) { |
| 91 |
$expired[] = $key; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
return $expired; |
| 96 |
} |
| 97 |
} |
| 98 |
|