PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
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 trunk, at vendor/averta/wordpress/src/Cache/WPCache.php

261 lines 5.6 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', 'clearCache'], $requestArgs['url'] );
113
114 // exclude flush params from hash request key
115 unset( $requestArgs['flush'] );
116 unset( $requestArgs['clearCache'] );
117
118 $requestArgs = $this->beforeHashRequest( $requestArgs, $request );
119
120 return Sanitize::textfield( md5( serialize( $requestArgs ) ) );
121 }
122
123 /**
124 * Prepares request args for hashing
125 * Override this method to change requestArgs before hash
126 *
127 * @param array $requestArgs
128 * @param RequestInterface $request
129 *
130 * @return array
131 */
132 protected function beforeHashRequest( array $requestArgs, RequestInterface $request ){
133 return $requestArgs;
134 }
135
136 public function has( $key ): bool {
137 return $this->get( $key, false ) !== false;
138 }
139
140
141 public function getMultiple( $keys, $default = null ) {
142 $result = [];
143
144 foreach ( $keys as $key ) {
145 $result[ $key ] = $this->get( $key, $default );
146 }
147
148 return $result;
149 }
150
151
152 public function setMultiple( $values, $ttl = null ): bool {
153 foreach ( $values as $key => $value ) {
154 if ( $this->set( $key, $value, $ttl ) ) {
155 continue;
156 }
157 return false;
158 }
159
160 return true;
161 }
162
163
164 public function deleteMultiple( $keys ): bool {
165
166 foreach ( $keys as $key ) {
167 if ( $this->delete( $key ) ) {
168 continue;
169 }
170 return false;
171 }
172
173 return true;
174 }
175
176
177 public function clear(): bool {
178
179 if( ! empty( $this->keyPrefix ) ) {
180 global $wpdb;
181 $wpdb->query($wpdb->prepare(
182 "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s",
183 "_transient_{$this->keyPrefix}%",
184 "_transient_timeout_{$this->keyPrefix}%"
185 ));
186 }
187
188 return $this->deleteMultiple( $this->inUseKeys() );
189 }
190
191 /**
192 * @param DateInterval $ttl
193 *
194 * @return int
195 */
196 private function convertDateIntervalToInteger( DateInterval $ttl ) : int {
197
198 return ( new DateTime() )
199 ->setTimestamp(0)
200 ->add( $ttl )
201 ->getTimestamp();
202 }
203
204 /**
205 * Adds a key to cache key list
206 *
207 * @param string $key
208 */
209 private function addToKeysList( $key ): void {
210 $this->inUseKeys[ $key ] = $key;
211 }
212
213 /**
214 * Removes a key from cache key list
215 *
216 * @param string $key
217 */
218 private function deleteFromKeyList( $key ): void {
219 unset( $this->inUseKeys[ $key ] );
220 }
221
222
223 private function inUseKeys(): array {
224 return $this->inUseKeys;
225 }
226
227 protected function validateKey( $key ){
228 if ( strpos( $key, $this->keyPrefix ) === 0 ) {
229 $key = substr( $key, strlen( $this->keyPrefix ) );
230 }
231 return $this->keyPrefix . $key;
232 }
233
234
235 public function prevent(){
236 if ( ! defined( 'DONOTCACHEPAGE' ) ) {
237 define( 'DONOTCACHEPAGE', true );
238 }
239
240 if ( ! defined( 'DONOTCACHEDB' ) ) {
241 define( 'DONOTCACHEDB', true );
242 }
243
244 if ( ! defined( 'DONOTMINIFY' ) ) {
245 define( 'DONOTMINIFY', true );
246 }
247
248 if ( ! defined( 'DONOTCDN' ) ) {
249 define( 'DONOTCDN', true );
250 }
251
252 if ( ! defined( 'DONOTCACHCEOBJECT' ) ) {
253 define( 'DONOTCACHCEOBJECT', true );
254 }
255
256 // prevent caching.
257 nocache_headers();
258 }
259
260 }
261