PluginProbe
Depicter — Popup & Slider Builder / 1.3.2
Depicter — Popup & Slider Builder v1.3.2
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / vendor / averta / wordpress / src / Cache / WPCache.php

WPCache.php in Depicter — Popup & Slider Builder 1.3.2, at vendor/averta/wordpress/src/Cache/WPCache.php

244 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Averta\WordPress\Cache;
3
4 use Averta\WordPress\Utility\Sanitize;
5 use DateInterval;
6 use Psr\Http\Message\RequestInterface;
7 use Psr\SimpleCache\CacheInterface;
8
9 class WPCache implements CacheInterface{
10
11 /**
12 * The list of transient keys
13 *
14 * @var array
15 */
16 private $inUseKeys = [];
17
18 /**
19 * A prefix for all transient keys
20 *
21 * @var string
22 */
23 protected $keyPrefix = '';
24
25
26 /**
27 * Cache constructor.
28 *
29 * @param null $cachePrefix
30 */
31 public function __construct( $cachePrefix = null )
32 {
33 if( ! is_null( $cachePrefix ) ){
34 $this->keyPrefix = $cachePrefix;
35 }
36 }
37
38 /**
39 * Get the value of a transient.
40 *
41 * If the transient does not exist, does not have a value, or has expired,
42 * then the return value will be false.
43 *
44 * @param string $key Cache key. Expected to not be SQL-escaped.
45 *
46 * @param bool $default Default cache value
47 *
48 * @return mixed Value of transient.
49 */
50 public function get( $key, $default = false ) {
51 $key = $this->validateKey( $key );
52
53 $value = get_transient( $key );
54 if ( false === $value ) {
55 $value = $default;
56 }
57
58 return $value;
59 }
60
61 /**
62 * Set/update the value of a transient.
63 *
64 * You do not need to serialize values. If the value needs to be serialized, then
65 * it will be serialized before it is set.
66 *
67 *
68 * @param string $key Cache key. Expected to not be SQL-escaped. Must be
69 * 172 characters or fewer in length.
70 * @param mixed $value Transient value. Must be serializable if non-scalar.
71 * Expected to not be SQL-escaped.
72 * @param int $ttl Optional. Time until expiration in seconds. Default 0 (no expiration).
73 *
74 * @return bool False if value was not set and true if value was set.
75 */
76 public function set( $key, $value, $ttl = null ): bool {
77 $key = $this->validateKey( $key );
78 $this->addToKeysList( $key );
79
80 if ( $ttl instanceof DateInterval ) {
81 $ttl = $this->convertDateIntervalToInteger( $ttl );
82 }
83
84 return set_transient( $key, $value, intval($ttl) );
85 }
86
87 /**
88 * Delete a transient.
89 *
90 * @param string $key Cache key. Expected to not be SQL-escaped.
91 *
92 * @return bool true if successful, false otherwise
93 */
94 public function delete( $key ): bool {
95 $key = $this->validateKey( $key );
96 $this->deleteFromKeyList( $key );
97
98 return delete_transient( $key );
99 }
100
101 /**
102 * Convert a request object to a unique key
103 *
104 * @param RequestInterface $request
105 *
106 * @return string
107 */
108 public function hashRequest( RequestInterface $request ) {
109 $requestArgs = $request->getQueryParams();
110
111 $requestArgs['url'] = $request->getRequestTarget();
112 $requestArgs['url'] = remove_query_arg( 'flush', $requestArgs['url'] );
113 $requestArgs['url'] = remove_query_arg( 'clearCache', $requestArgs['url'] );
114
115 // exclude flush params from hash request key
116 unset( $requestArgs['flush'] );
117 unset( $requestArgs['clearCache'] );
118
119 return Sanitize::textfield( md5( serialize( $requestArgs ) ) );
120 }
121
122 public function has( $key ): bool {
123 return $this->get( $key, false ) !== false;
124 }
125
126
127 public function getMultiple( $keys, $default = null ) {
128 $result = [];
129
130 foreach ( $keys as $key ) {
131 $result[ $key ] = $this->get( $key, $default );
132 }
133
134 return $result;
135 }
136
137
138 public function setMultiple( $values, $ttl = null ): bool {
139 foreach ( $values as $key => $value ) {
140 if ( $this->set( $key, $value, $ttl ) ) {
141 continue;
142 }
143 return false;
144 }
145
146 return true;
147 }
148
149
150 public function deleteMultiple( $keys ): bool {
151
152 foreach ( $keys as $key ) {
153 if ( $this->delete( $key ) ) {
154 continue;
155 }
156 return false;
157 }
158
159 return true;
160 }
161
162
163 public function clear(): bool {
164
165 if( ! empty( $this->keyPrefix ) ) {
166 global $wpdb;
167 $wpdb->query($wpdb->prepare(
168 "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s",
169 "_transient_{$this->keyPrefix}%",
170 "_transient_timeout_{$this->keyPrefix}%"
171 ));
172 }
173
174 return $this->deleteMultiple( $this->inUseKeys() );
175 }
176
177 /**
178 * @param DateInterval $ttl
179 *
180 * @return int
181 */
182 private function convertDateIntervalToInteger( DateInterval $ttl ) : int {
183
184 return ( new DateTime() )
185 ->setTimestamp(0)
186 ->add( $ttl )
187 ->getTimestamp();
188 }
189
190 /**
191 * Adds a key to cache key list
192 *
193 * @param string $key
194 */
195 private function addToKeysList( $key ): void {
196 $this->inUseKeys[ $key ] = $key;
197 }
198
199 /**
200 * Removes a key from cache key list
201 *
202 * @param string $key
203 */
204 private function deleteFromKeyList( $key ): void {
205 unset( $this->inUseKeys[ $key ] );
206 }
207
208
209 private function inUseKeys(): array {
210 return $this->inUseKeys;
211 }
212
213 protected function validateKey( $key ){
214 return $this->keyPrefix . ltrim( $key, $this->keyPrefix );
215 }
216
217
218 public function prevent(){
219 if ( ! defined( 'DONOTCACHEPAGE' ) ) {
220 define( 'DONOTCACHEPAGE', true );
221 }
222
223 if ( ! defined( 'DONOTCACHEDB' ) ) {
224 define( 'DONOTCACHEDB', true );
225 }
226
227 if ( ! defined( 'DONOTMINIFY' ) ) {
228 define( 'DONOTMINIFY', true );
229 }
230
231 if ( ! defined( 'DONOTCDN' ) ) {
232 define( 'DONOTCDN', true );
233 }
234
235 if ( ! defined( 'DONOTCACHCEOBJECT' ) ) {
236 define( 'DONOTCACHCEOBJECT', true );
237 }
238
239 // prevent caching.
240 nocache_headers();
241 }
242
243 }
244