PluginProbe ʕ •ᴥ•ʔ
10Web Booster – Website speed optimization, Cache & Page Speed optimizer / trunk
10Web Booster – Website speed optimization, Cache & Page Speed optimizer vtrunk
2.33.6 2.33.5 2.33.0 2.30.5 2.30.7 2.30.9 2.31.10 2.31.8 2.32.11 2.32.21 2.32.3 2.32.4 2.32.7 2.6.31 2.6.40 2.6.42 2.6.7 2.7.37 2.7.44 2.7.47 2.8.18 2.8.19 2.8.32 2.8.34 2.8.35 2.9.23 2.9.24 2.9.25 2.9.27 v2.27.4 trunk 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.17 2.0.18 2.0.21 2.0.22 2.0.25 2.0.26 2.0.27 2.0.3 2.0.7 2.0.9 2.10.46 2.10.65 2.10.66 2.10.68 2.11.41 2.11.42 2.11.43 2.12.15 2.12.21 2.12.22 2.12.23 2.12.26 2.13.37 2.13.40 2.13.41 2.13.42 2.13.44 2.13.45 2.13.47 2.14.49 2.14.50 2.15.18 2.17.21 2.17.23 2.18.17 2.19.44 2.19.45 2.19.46 2.19.49 2.2.12 2.2.15 2.2.16 2.2.18 2.2.8 2.20.31 2.20.32 2.20.33 2.21.11 2.21.12 2.21.16 2.21.25 2.22.32 2.23.13 2.23.15 2.23.16 2.23.18 2.24.12 2.24.14 2.24.18 2.25.14 2.26.6 2.28.10 2.28.13 2.28.14 2.28.7 2.29.1 2.29.2 2.29.3 2.3.0 2.3.1 2.3.2 2.3.3 2.30.18
tenweb-speed-optimizer / vendor / 10web-utils / 10web-wp-transients / src / TenWebWpTransients / OptimizerTransients.php
tenweb-speed-optimizer / vendor / 10web-utils / 10web-wp-transients / src / TenWebWpTransients Last commit date
OptimizerTransients.php 3 years ago
OptimizerTransients.php
446 lines
1 <?php
2
3 namespace TenWebWpTransients;
4
5 /**
6 * This class duplicates WP transients functionality with different keys.
7 * The persistent cache part was fully deleted to avoid different issues.
8 */
9 class OptimizerTransients
10 {
11 public const TRANSIENT_KEY = 'two_trans_';
12 public const SITE_TRANSIENT_KEY = 'two_site_trans_';
13 public const TRANSIENT_TIMEOUT_KEY = 'two_trans_timeout_';
14 public const SITE_TRANSIENT_TIMEOUT_KEY = 'two_site_trans_timeout_';
15 /**
16 * Sets/updates the value of a TWO transient.
17 *
18 * You do not need to serialize values. If the value needs to be serialized,
19 * then it will be serialized before it is set.
20 *
21 * @since 2.8.0
22 * @see set_transient()
23 * @param string $transient Transient name. Expected to not be SQL-escaped.
24 * Must be 172 characters or fewer in length.
25 * @param mixed $value Transient value. Must be serializable if non-scalar.
26 * Expected to not be SQL-escaped.
27 * @param int $expiration Optional. Time until expiration in seconds. Default 0 (no expiration).
28 * @return bool True if the value was set, false otherwise.
29 */
30 public static function set($transient, $value, $expiration = 0) {
31
32 $expiration = (int) $expiration;
33
34 /**
35 * Filters a specific TWO transient before its value is set.
36 *
37 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
38 *
39 * @since 3.0.0
40 * @since 4.2.0 The `$expiration` parameter was added.
41 * @since 4.4.0 The `$transient` parameter was added.
42 *
43 * @param mixed $value New value of transient.
44 * @param int $expiration Time until expiration in seconds.
45 * @param string $transient Transient name.
46 */
47 $value = apply_filters( "two_pre_set_transient_{$transient}", $value, $expiration, $transient );
48
49 /**
50 * Filters the expiration for a TWO transient before its value is set.
51 *
52 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
53 *
54 * @since 4.4.0
55 *
56 * @param int $expiration Time until expiration in seconds. Use 0 for no expiration.
57 * @param mixed $value New value of transient.
58 * @param string $transient Transient name.
59 */
60 $expiration = apply_filters( "two_expiration_of_transient_{$transient}", $expiration, $value, $transient );
61
62 $transient_timeout = self::TRANSIENT_TIMEOUT_KEY . $transient;
63 $transient_option = self::TRANSIENT_KEY . $transient;
64
65 if ( false === get_option( $transient_option ) ) {
66 $autoload = 'yes';
67 if ( $expiration ) {
68 $autoload = 'no';
69 add_option( $transient_timeout, time() + $expiration, '', 'no' );
70 }
71 $result = add_option( $transient_option, $value, '', $autoload );
72 } else {
73 // If expiration is requested, but the transient has no timeout option,
74 // delete, then re-create transient rather than update.
75 $update = true;
76
77 if ( $expiration ) {
78 if ( false === get_option( $transient_timeout ) ) {
79 delete_option( $transient_option );
80 add_option( $transient_timeout, time() + $expiration, '', 'no' );
81 $result = add_option( $transient_option, $value, '', 'no' );
82 $update = false;
83 } else {
84 update_option( $transient_timeout, time() + $expiration );
85 }
86 }
87
88 if ( $update ) {
89 $result = update_option( $transient_option, $value );
90 }
91 }
92
93 if ( $result ) {
94
95 /**
96 * Fires after the value for a specific transient has been set.
97 *
98 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
99 *
100 * @since 3.0.0
101 * @since 3.6.0 The `$value` and `$expiration` parameters were added.
102 * @since 4.4.0 The `$transient` parameter was added.
103 *
104 * @param mixed $value Transient value.
105 * @param int $expiration Time until expiration in seconds.
106 * @param string $transient The name of the transient.
107 */
108 do_action( "two_set_transient_{$transient}", $value, $expiration, $transient );
109
110 /**
111 * Fires after the value for a transient has been set.
112 *
113 * @since 3.0.0
114 * @since 3.6.0 The `$value` and `$expiration` parameters were added.
115 *
116 * @param string $transient The name of the transient.
117 * @param mixed $value Transient value.
118 * @param int $expiration Time until expiration in seconds.
119 */
120 do_action( 'two_setted_transient', $transient, $value, $expiration );
121 }
122
123 return $result;
124 }
125
126
127 /**
128 * Sets/updates the value of a TWO transient.
129 *
130 * You do not need to serialize values. If the value needs to be serialized,
131 * then it will be serialized before it is set.
132 *
133 * @since 2.8.0
134 * @see set_site_transient()
135 * @param string $transient Transient name. Expected to not be SQL-escaped.
136 * Must be 172 characters or fewer in length.
137 * @param mixed $value Transient value. Must be serializable if non-scalar.
138 * Expected to not be SQL-escaped.
139 * @param int $expiration Optional. Time until expiration in seconds. Default 0 (no expiration).
140 * @return bool True if the value was set, false otherwise.
141 */
142 public static function set_site($transient, $value, $expiration = 0) {
143
144 $expiration = (int) $expiration;
145
146 /**
147 * Filters a specific TWO transient before its value is set.
148 *
149 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
150 *
151 * @since 3.0.0
152 * @since 4.2.0 The `$expiration` parameter was added.
153 * @since 4.4.0 The `$transient` parameter was added.
154 *
155 * @param mixed $value New value of transient.
156 * @param int $expiration Time until expiration in seconds.
157 * @param string $transient Transient name.
158 */
159 $value = apply_filters( "two_pre_set_site_transient_{$transient}", $value, $expiration, $transient );
160
161 /**
162 * Filters the expiration for a TWO transient before its value is set.
163 *
164 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
165 *
166 * @since 4.4.0
167 *
168 * @param int $expiration Time until expiration in seconds. Use 0 for no expiration.
169 * @param mixed $value New value of transient.
170 * @param string $transient Transient name.
171 */
172 $expiration = apply_filters( "two_expiration_of_site_transient_{$transient}", $expiration, $value, $transient );
173
174 $transient_timeout = self::SITE_TRANSIENT_TIMEOUT_KEY . $transient;
175 $transient_option = self::SITE_TRANSIENT_KEY . $transient;
176
177
178 if ( false === get_site_option( $transient_option ) ) {
179 if ( $expiration ) {
180 add_site_option( $transient_timeout, time() + $expiration );
181 }
182 $result = add_site_option( $transient_option, $value );
183 } else {
184 if ( $expiration ) {
185 update_site_option( $transient_timeout, time() + $expiration );
186 }
187 $result = update_site_option( $transient_option, $value );
188 }
189
190 if ( $result ) {
191
192 /**
193 * Fires after the value for a specific transient has been set.
194 *
195 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
196 *
197 * @since 3.0.0
198 * @since 3.6.0 The `$value` and `$expiration` parameters were added.
199 * @since 4.4.0 The `$transient` parameter was added.
200 *
201 * @param mixed $value Transient value.
202 * @param int $expiration Time until expiration in seconds.
203 * @param string $transient The name of the transient.
204 */
205 do_action( "two_set_site_transient_{$transient}", $value, $expiration, $transient );
206
207 /**
208 * Fires after the value for a transient has been set.
209 *
210 * @since 3.0.0
211 * @since 3.6.0 The `$value` and `$expiration` parameters were added.
212 *
213 * @param string $transient The name of the transient.
214 * @param mixed $value Transient value.
215 * @param int $expiration Time until expiration in seconds.
216 */
217 do_action( 'two_setted_site_transient', $transient, $value, $expiration );
218 }
219
220 return $result;
221 }
222
223
224 /**
225 * Retrieves the value of a transient.
226 *
227 * If the transient does not exist, does not have a value, or has expired,
228 * then the return value will be false.
229 *
230 * @since 2.8.0
231 * @see get_transient()
232 * @param string $transient Transient name. Expected to not be SQL-escaped.
233 * @return mixed Value of transient.
234 */
235 public static function get( $transient ) {
236
237 /**
238 * Filters the value of an existing transient before it is retrieved.
239 *
240 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
241 *
242 * Returning a value other than false from the filter will short-circuit retrieval
243 * and return that value instead.
244 *
245 * @since 2.8.0
246 * @since 4.4.0 The `$transient` parameter was added
247 *
248 * @param mixed $pre_transient The default value to return if the transient does not exist.
249 * Any value other than false will short-circuit the retrieval
250 * of the transient, and return that value.
251 * @param string $transient Transient name.
252 */
253 $pre = apply_filters( "two_pre_transient_{$transient}", false, $transient );
254
255 if ( false !== $pre ) {
256 return $pre;
257 }
258
259 $transient_option = self::TRANSIENT_KEY . $transient;
260 if ( ! wp_installing() ) {
261 // If option is not in alloptions, it is not autoloaded and thus has a timeout.
262 $alloptions = wp_load_alloptions();
263 if ( ! isset( $alloptions[ $transient_option ] ) ) {
264 $transient_timeout = self::TRANSIENT_TIMEOUT_KEY . $transient;
265 $timeout = get_option( $transient_timeout );
266 if ( false !== $timeout && $timeout < time() ) {
267 delete_option( $transient_option );
268 delete_option( $transient_timeout );
269 $value = false;
270 }
271 }
272 }
273
274 if ( ! isset( $value ) ) {
275 $value = get_option( $transient_option );
276 }
277
278 /**
279 * Filters an existing transient's value.
280 *
281 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
282 *
283 * @since 2.8.0
284 * @since 4.4.0 The `$transient` parameter was added
285 *
286 * @param mixed $value Value of transient.
287 * @param string $transient Transient name.
288 */
289 return apply_filters( "two_transient_{$transient}", $value, $transient );
290 }
291
292
293 /**
294 * Retrieves the value of a transient.
295 *
296 * If the transient does not exist, does not have a value, or has expired,
297 * then the return value will be false.
298 *
299 * @since 2.8.0
300 * @see get_site_transient()
301 * @param string $transient Transient name. Expected to not be SQL-escaped.
302 * @return mixed Value of transient.
303 */
304 public static function get_site( $transient ) {
305
306 /**
307 * Filters the value of an existing transient before it is retrieved.
308 *
309 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
310 *
311 * Returning a value other than false from the filter will short-circuit retrieval
312 * and return that value instead.
313 *
314 * @since 2.8.0
315 * @since 4.4.0 The `$transient` parameter was added
316 *
317 * @param mixed $pre_transient The default value to return if the transient does not exist.
318 * Any value other than false will short-circuit the retrieval
319 * of the transient, and return that value.
320 * @param string $transient Transient name.
321 */
322 $pre = apply_filters( "two_pre_site_transient_{$transient}", false, $transient );
323
324 if ( false !== $pre ) {
325 return $pre;
326 }
327
328 $transient_option = self::SITE_TRANSIENT_KEY . $transient;
329 // Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
330 $no_timeout = array( 'update_core', 'update_plugins', 'update_themes' );
331 if ( ! in_array( $transient, $no_timeout, true ) ) {
332 $transient_timeout = self::SITE_TRANSIENT_TIMEOUT_KEY . $transient;
333 $timeout = get_site_option( $transient_timeout );
334 if ( false !== $timeout && $timeout < time() ) {
335 delete_site_option( $transient_option );
336 delete_site_option( $transient_timeout );
337 $value = false;
338 }
339 }
340
341 if ( ! isset( $value ) ) {
342 $value = get_site_option( $transient_option );
343 }
344
345 /**
346 * Filters an existing transient's value.
347 *
348 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
349 *
350 * @since 2.8.0
351 * @since 4.4.0 The `$transient` parameter was added
352 *
353 * @param mixed $value Value of transient.
354 * @param string $transient Transient name.
355 */
356 return apply_filters( "two_site_transient_{$transient}", $value, $transient );
357 }
358
359 /**
360 * Deletes a TWO transient.
361 *
362 * @since 2.8.0
363 * @see delete_transient()
364 * @param string $transient Transient name. Expected to not be SQL-escaped.
365 * @return bool True if the transient was deleted, false otherwise.
366 */
367 public static function delete( $transient ) {
368
369 /**
370 * Fires immediately before a specific transient is deleted.
371 *
372 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
373 *
374 * @since 3.0.0
375 *
376 * @param string $transient Transient name.
377 */
378 do_action( "two_delete_transient_{$transient}", $transient );
379
380 $option_timeout = self::TRANSIENT_TIMEOUT_KEY . $transient;
381 $option = self::TRANSIENT_KEY . $transient;
382 $result = delete_option( $option );
383
384 if ( $result ) {
385 delete_option( $option_timeout );
386 }
387
388 if ( $result ) {
389
390 /**
391 * Fires after a transient is deleted.
392 *
393 * @since 3.0.0
394 *
395 * @param string $transient Deleted transient name.
396 */
397 do_action( 'two_deleted_transient', $transient );
398 }
399
400 return $result;
401 }
402
403 /**
404 * Deletes a TWO transient.
405 *
406 * @since 2.8.0
407 * @see delete_site_transient()
408 * @param string $transient Transient name. Expected to not be SQL-escaped.
409 * @return bool True if the transient was deleted, false otherwise.
410 */
411 public static function delete_site( $transient ) {
412
413 /**
414 * Fires immediately before a specific transient is deleted.
415 *
416 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
417 *
418 * @since 3.0.0
419 *
420 * @param string $transient Transient name.
421 */
422 do_action( "two_delete_site_transient_{$transient}", $transient );
423
424 $option_timeout = self::SITE_TRANSIENT_TIMEOUT_KEY . $transient;
425 $option = self::SITE_TRANSIENT_KEY . $transient;
426 $result = delete_site_option( $option );
427
428 if ( $result ) {
429 delete_site_option( $option_timeout );
430 }
431
432 if ( $result ) {
433
434 /**
435 * Fires after a transient is deleted.
436 *
437 * @since 3.0.0
438 *
439 * @param string $transient Deleted transient name.
440 */
441 do_action( 'two_deleted_site_transient', $transient );
442 }
443
444 return $result;
445 }
446 }