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 |