PluginProbe ʕ •ᴥ•ʔ
ShortPixel Image Optimizer – Optimize Images, Convert WebP & AVIF / 6.5.3
ShortPixel Image Optimizer – Optimize Images, Convert WebP & AVIF v6.5.3
6.5.3 6.5.2 6.5.1 trunk 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.6 1.2.0 1.3.1 1.3.5 1.4.0 1.4.1 1.5.0 1.5.3 1.6.0 1.6.1 1.6.10 1.6.2 1.6.3 1.6.4 1.6.6 1.6.7 1.6.8 1.6.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.10 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.2 3.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 4.0.0 4.0.1 4.0.2 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.1.5 4.1.7 4.10.0 4.10.1 4.10.2 4.10.3 4.10.4 4.10.5 4.11.0 4.11.1 4.11.2 4.11.3 4.12.0 4.12.1 4.12.2 4.12.3 4.12.4 4.12.5 4.12.6 4.12.7 4.12.8 4.13.0 4.13.1 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.15.0 4.15.1 4.15.2 4.15.3 4.15.4 4.16.0 4.16.1 4.16.2 4.16.3 4.16.4 4.17.0 4.17.1 4.17.2 4.17.3 4.17.4 4.18.0 4.18.1 4.19.0 4.19.1 4.19.2 4.19.3 4.2.0 4.2.1 4.2.2 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.20.0 4.20.1 4.20.2 4.21.0 4.21.1 4.21.2 4.22.0 4.22.1 4.22.10 4.22.2 4.22.3 4.22.4 4.22.5 4.22.6 4.22.7 4.22.8 4.22.9 4.3.0 4.3.1 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.5.3 4.5.5 4.6.0 4.7.0 4.7.1 4.7.2 4.8.0 4.8.1 4.8.2 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.8.9 4.9.0 4.9.1 5.0.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.0.9 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.2.0 5.2.1 5.2.2 5.2.3 5.3.0 5.4.0 5.4.1 5.4.2 5.4.3 5.5.0 5.5.1 5.5.2 5.5.3 5.5.4 5.5.5 5.6.0 5.6.1 5.6.2 5.6.3 5.6.4 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.1.2 6.1.3 6.1.4 6.2.0 6.2.1 6.2.2 6.3.0 6.3.1 6.3.2 6.3.3 6.3.4 6.3.5 6.4.0 6.4.1 6.4.2 6.4.3 6.4.4 6.5.0
shortpixel-image-optimiser / class / Model / CacheModel.php
shortpixel-image-optimiser / class / Model Last commit date
AdminNotices 3 weeks ago Backup 3 weeks ago Converter 3 weeks ago File 1 month ago Image 6 days ago Queue 6 days ago AccessModel.php 3 weeks ago AdminNoticeModel.php 3 weeks ago AiDataModel.php 3 weeks ago ApiKeyModel.php 3 weeks ago CacheModel.php 3 weeks ago EnvironmentModel.php 1 month ago FrontImage.php 10 months ago MultiSettingsModel.php 1 year ago ResponseModel.php 7 months ago SettingsModel.php 1 month ago StatsModel.php 1 year ago
CacheModel.php
175 lines
1 <?php
2 namespace ShortPixel\Model;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit; // Exit if accessed directly.
6 }
7
8 use ShortPixel\ShortPixelLogger\ShortPixelLogger as Log;
9
10
11 /**
12 * Model for storing cached data via WordPress transients.
13 *
14 * Use this in conjunction with CacheController — do not instantiate stand-alone.
15 * Wraps get_transient/set_transient/delete_transient with additional expiration
16 * sanity checks to guard against persistent (non-expiring) transients.
17 *
18 * @package ShortPixel\Model
19 */
20 class CacheModel
21 {
22
23 /**
24 * Transient key used for storage and retrieval.
25 *
26 * @var string
27 */
28 protected $name;
29
30 /**
31 * The cached value.
32 *
33 * @var mixed
34 */
35 protected $value;
36
37 /**
38 * Expiration time in seconds applied when save() is called.
39 *
40 * This is the default TTL for new items; it does NOT represent the remaining
41 * TTL of an already-loaded transient.
42 *
43 * @var int
44 */
45 protected $expires = HOUR_IN_SECONDS; // This is the expires, when saved without SetExpires! This value is not a representation of any expire time when loading something cache!
46
47 /**
48 * Whether a non-expired transient was found during load().
49 *
50 * @var bool
51 */
52 protected $exists = false;
53
54
55 /**
56 * Load the transient identified by $name.
57 *
58 * @param string $name Transient key to load.
59 */
60 public function __construct($name)
61 {
62 $this->name = $name;
63 $this->load();
64 }
65
66 /** Set the expiration of this item. In seconds
67 * @param int $time Expiration in seconds.
68 * @return void
69 */
70 public function setExpires($time)
71 {
72 $this->expires = $time;
73 }
74
75 public function setValue($value)
76 {
77 $this->value = $value;
78 }
79
80 public function exists()
81 {
82 return $this->exists;
83 }
84
85 public function getValue()
86 {
87 return $this->value;
88 }
89
90 public function getName()
91 {
92 return $this->name;
93 }
94
95
96 /**
97 * Persist the current value as a WordPress transient.
98 *
99 * Skips saving if the configured expiration is zero or negative, since
100 * transients without a positive TTL become persistent and can cause issues.
101 *
102 * @return void
103 */
104 public function save()
105 {
106 if ($this->expires <= 0)
107 {
108 return; // don't save transients without expiration
109 }
110 $this->exists = set_transient($this->name, $this->value, $this->expires);
111
112 }
113
114 /**
115 * Delete the transient from WordPress and mark this item as non-existent.
116 *
117 * @return void
118 */
119 public function delete()
120 {
121 delete_transient($this->name);
122 $this->exists = false;
123 }
124
125 /**
126 * Load the transient value from WordPress, if it exists and has a valid expiration.
127 *
128 * Calls checkExpiration() to detect and remove transients whose timeout option
129 * has been lost (a known WordPress edge case that creates persistent transients).
130 *
131 * @return void
132 */
133 protected function load()
134 {
135 $item = get_transient($this->name);
136 if ($item !== false)
137 {
138 $this->value = $item;
139 $this->exists = true;
140 $this->checkExpiration($this->name);
141 }
142 }
143
144 /** It has been shown that sometimes the expire of the transient is lost, creating a persistent transient. This can be harmful, especially in the case of bulk-secret which can create a situation were no client will optimize due to the hanging transient.
145 *
146 * Skips the check when an external object cache is in use, since transient
147 * timeout options are not stored in that case.
148 *
149 * @param string $name Transient key whose timeout option should be verified.
150 * @return bool True when the expiration is intact or an external cache is used.
151 */
152 private function checkExpiration($name)
153 {
154 $option = get_option('_transient_timeout_' . $name);
155
156 if (false !== $option && is_numeric($option))
157 {
158 return true; // ok
159 }
160 else {
161
162 // Via object cache the expire info can't be retrieved. Customer is on it's own with this.
163 if (wp_using_ext_object_cache())
164 {
165 return true;
166 }
167
168 $this->value = '';
169 $this->delete();
170 Log::addError('Found hanging transient with no expiration! ' . $name, $option);
171 }
172 }
173
174 }
175