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 / Tasks / TransientsTask.php

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

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