PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / trunk
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses vtrunk
4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 4.2.1.1 All 137 releases
learnpress / inc / cache.php

cache.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses trunk, at inc/cache.php

601 lines 14.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class LP_Object_Cache
4 */
5
6 class LP_Object_Cache {
7
8 /**
9 * Holds the cached objects.
10 *
11 * @since 2.0.0
12 * @var array
13 */
14 private $cache = array();
15
16 /**
17 * The amount of times the cache data was already stored in the cache.
18 *
19 * @since 2.5.0
20 * @var int
21 */
22 public $cache_hits = 0;
23
24 /**
25 * Amount of times the cache did not have the request in cache.
26 *
27 * @since 2.0.0
28 * @var int
29 */
30 public $cache_misses = 0;
31
32 /**
33 * List of global cache groups.
34 *
35 * @since 3.0.0
36 * @var array
37 */
38 protected $global_groups = array();
39
40 /**
41 * The blog prefix to prepend to keys in non-global groups.
42 *
43 * @since 3.5.0
44 * @var int
45 */
46 private $blog_prefix;
47
48 /**
49 * Holds the value of is_multisite().
50 *
51 * @since 3.5.0
52 * @var bool
53 */
54 private $multisite;
55
56 /**
57 * @var bool
58 */
59 protected static $_use_core = false;
60
61 /**
62 * @var LP_Object_Cache
63 */
64 protected static $instance = null;
65
66 /**
67 * @return LP_Object_Cache
68 */
69 public static function instance() {
70 if ( ! self::$instance ) {
71 self::$instance = new self();
72 }
73
74 return self::$instance;
75 }
76
77 public static function init() {
78 // add_action( 'shutdown', array( __CLASS__, 'wp_cache_flush' ), 9999 );
79 }
80
81 public static function wp_cache_flush() {
82
83 }
84
85 public static function get_group( $group ) {
86 if ( self::$_use_core ) {
87 if ( false === strpos( $group, 'learn-press/' ) ) {
88 return "learn-press/{$group}";
89 }
90 }
91
92 return $group;
93 }
94
95 /**
96 * Makes private properties readable for backward compatibility.
97 *
98 * @param string $name Property to get.
99 *
100 * @return mixed Property.
101 * @since 4.0.0
102 *
103 */
104 public function __get( $name ) {
105 return $this->$name;
106 }
107
108 /**
109 * Makes private properties settable for backward compatibility.
110 *
111 * @param string $name Property to set.
112 * @param mixed $value Property value.
113 *
114 * @return mixed Newly-set property.
115 * @since 4.0.0
116 *
117 */
118 public function __set( $name, $value ) {
119 return $this->$name = $value;
120 }
121
122 /**
123 * Makes private properties checkable for backward compatibility.
124 *
125 * @param string $name Property to check if set.
126 *
127 * @return bool Whether the property is set.
128 * @since 4.0.0
129 *
130 */
131 public function __isset( $name ) {
132 return isset( $this->$name );
133 }
134
135 /**
136 * Makes private properties un-settable for backward compatibility.
137 *
138 * @param string $name Property to unset.
139 *
140 * @since 4.0.0
141 *
142 */
143 public function __unset( $name ) {
144 unset( $this->$name );
145 }
146
147 /**
148 * Adds data to the cache if it doesn't already exist.
149 *
150 * @param int|string $key What to call the contents in the cache.
151 * @param mixed $data The contents to store in the cache.
152 * @param string $group Optional. Where to group the cache contents. Default 'default'.
153 * @param int $expire Optional. When to expire the cache contents. Default 0 (no expiration).
154 *
155 * @return bool False if cache key and group already exist, true on success
156 * @uses WP_Object_Cache::set() Sets the data after the checking the cache
157 * contents existence.
158 *
159 * @since 2.0.0
160 *
161 * @uses WP_Object_Cache::_exists() Checks to see if the cache already has data.
162 */
163 public static function add( $key, $data, $group = 'default', $expire = 0 ) {
164
165 $group = self::get_group( $group );
166
167 if ( self::$_use_core ) {
168 return wp_cache_add( $key, $data, $group, $expire );
169 }
170
171 if ( wp_suspend_cache_addition() ) {
172 return false;
173 }
174
175 if ( empty( $group ) ) {
176 $group = 'default';
177 }
178
179 $self = self::instance();
180
181 $id = $key;
182 if ( $self->multisite && ! isset( $self->global_groups[ $group ] ) ) {
183 $id = $self->blog_prefix . $key;
184 }
185
186 if ( self::_exists( $id, $group ) ) {
187 return false;
188 }
189
190 return self::set( $key, $data, $group, (int) $expire );
191 }
192
193 /**
194 * Sets the list of global cache groups.
195 *
196 * @param array $groups List of groups that are global.
197 *
198 * @since 3.0.0
199 *
200 */
201 public static function add_global_groups( $groups ) {
202 $groups = (array) $groups;
203 $self = self::instance();
204
205 $groups = array_fill_keys( $groups, true );
206 $self->global_groups = array_merge( $self->global_groups, $groups );
207 }
208
209 /**
210 * Decrements numeric cache item's value.
211 *
212 * @param int|string $key The cache key to decrement.
213 * @param int $offset Optional. The amount by which to decrement the item's value. Default 1.
214 * @param string $group Optional. The group the key is in. Default 'default'.
215 *
216 * @return false|int False on failure, the item's new value on success.
217 * @since 3.3.0
218 *
219 */
220 public static function decr( $key, $offset = 1, $group = 'default' ) {
221 if ( empty( $group ) ) {
222 $group = 'default';
223 }
224
225 $self = self::instance();
226
227 if ( $self->multisite && ! isset( $self->global_groups[ $group ] ) ) {
228 $key = $self->blog_prefix . $key;
229 }
230
231 if ( ! self::_exists( $key, $group ) ) {
232 return false;
233 }
234
235 if ( ! is_numeric( $self->cache[ $group ][ $key ] ) ) {
236 $self->cache[ $group ][ $key ] = 0;
237 }
238
239 $offset = (int) $offset;
240
241 $self->cache[ $group ][ $key ] -= $offset;
242
243 if ( $self->cache[ $group ][ $key ] < 0 ) {
244 $self->cache[ $group ][ $key ] = 0;
245 }
246
247 return $self->cache[ $group ][ $key ];
248 }
249
250 /**
251 * Removes the contents of the cache key in the group.
252 *
253 * If the cache key does not exist in the group, then nothing will happen.
254 *
255 * @param int|string $key What the contents in the cache are called.
256 * @param string $group Optional. Where the cache contents are grouped. Default 'default'.
257 * @param bool $deprecated Optional. Unused. Default false.
258 *
259 * @return bool False if the contents weren't deleted and true on success.
260 * @since 2.0.0
261 *
262 */
263 public static function delete( $key, $group = 'default', $deprecated = false ) {
264
265 $group = self::get_group( $group );
266
267 if ( self::$_use_core ) {
268 return wp_cache_delete( $key, $group );
269 }
270
271 if ( empty( $group ) ) {
272 $group = 'default';
273 }
274
275 $self = self::instance();
276
277 if ( $self->multisite && ! isset( $self->global_groups[ $group ] ) ) {
278 $key = $self->blog_prefix . $key;
279 }
280
281 if ( ! self::_exists( $key, $group ) ) {
282 return false;
283 }
284
285 unset( $self->cache[ $group ][ $key ] );
286
287 return true;
288 }
289
290 /**
291 * Clears the object cache of all data.
292 *
293 * @return true Always returns true.
294 * @since 2.0.0
295 *
296 */
297 public static function flush() {
298
299 if ( self::$_use_core ) {
300 // return wp_cache_flush();
301 }
302
303 $self = self::instance();
304
305 $self->cache = array();
306
307 return true;
308 }
309
310 /**
311 * Retrieves the cache contents, if it exists.
312 *
313 * The contents will be first attempted to be retrieved by searching by the
314 * key in the cache group. If the cache is hit (success) then the contents
315 * are returned.
316 *
317 * On failure, the number of cache misses will be incremented.
318 *
319 * @param int|string $key What the contents in the cache are called.
320 * @param string $group Optional. Where the cache contents are grouped. Default 'default'.
321 * @param string $force Optional. Unused. Whether to force a refetch rather than relying on the local
322 * cache. Default false.
323 * @param bool $found Optional. Whether the key was found in the cache (passed by reference).
324 * Disambiguates a return of false, a storable value. Default null.
325 *
326 * @return false|mixed False on failure to retrieve contents or the cache contents on success.
327 * @since 2.0.0
328 *
329 */
330 public static function get( $key, $group = 'default', $force = false, &$found = null ) {
331 $group = self::get_group( $group );
332
333 if ( self::$_use_core ) {
334 return wp_cache_get( $key, $group, $force, $found );
335 }
336
337 if ( empty( $group ) ) {
338 $group = 'default';
339 }
340
341 $self = self::instance();
342
343 if ( $self->multisite && ! isset( $self->global_groups[ $group ] ) ) {
344 $key = $self->blog_prefix . $key;
345 }
346
347 if ( self::_exists( $key, $group ) ) {
348 $found = true;
349 $self->cache_hits += 1;
350 if ( is_object( $self->cache[ $group ][ $key ] ) ) {
351 return clone $self->cache[ $group ][ $key ];
352 } else {
353 return $self->cache[ $group ][ $key ];
354 }
355 }
356
357 $found = false;
358 $self->cache_misses += 1;
359
360 return false;
361 }
362
363 /**
364 * Increments numeric cache item's value.
365 *
366 * @param int|string $key The cache key to increment
367 * @param int $offset Optional. The amount by which to increment the item's value. Default 1.
368 * @param string $group Optional. The group the key is in. Default 'default'.
369 *
370 * @return false|int False on failure, the item's new value on success.
371 * @since 3.3.0
372 *
373 */
374 public static function incr( $key, $offset = 1, $group = 'default' ) {
375 if ( empty( $group ) ) {
376 $group = 'default';
377 }
378 $self = self::instance();
379
380 if ( $self->multisite && ! isset( $self->global_groups[ $group ] ) ) {
381 $key = $self->blog_prefix . $key;
382 }
383
384 if ( ! self::_exists( $key, $group ) ) {
385 return false;
386 }
387
388 if ( ! is_numeric( $self->cache[ $group ][ $key ] ) ) {
389 $self->cache[ $group ][ $key ] = 0;
390 }
391
392 $offset = (int) $offset;
393
394 $self->cache[ $group ][ $key ] += $offset;
395
396 if ( $self->cache[ $group ][ $key ] < 0 ) {
397 $self->cache[ $group ][ $key ] = 0;
398 }
399
400 return $self->cache[ $group ][ $key ];
401 }
402
403 /**
404 * Replaces the contents in the cache, if contents already exist.
405 *
406 * @param int|string $key What to call the contents in the cache.
407 * @param mixed $data The contents to store in the cache.
408 * @param string $group Optional. Where to group the cache contents. Default 'default'.
409 * @param int $expire Optional. When to expire the cache contents. Default 0 (no expiration).
410 *
411 * @return bool False if not exists, true if contents were replaced.
412 * @see WP_Object_Cache::set()
413 *
414 * @since 2.0.0
415 *
416 */
417 public static function replace( $key, $data, $group = 'default', $expire = 0 ) {
418
419 $group = self::get_group( $group );
420
421 if ( self::$_use_core ) {
422 return wp_cache_replace( $key, $data, $group, $expire );
423 }
424
425 if ( empty( $group ) ) {
426 $group = 'default';
427 }
428
429 $self = self::instance();
430
431 $id = $key;
432 if ( $self->multisite && ! isset( $self->global_groups[ $group ] ) ) {
433 $id = $self->blog_prefix . $key;
434 }
435
436 if ( ! self::_exists( $id, $group ) ) {
437 return false;
438 }
439
440 return self::set( $key, $data, $group, (int) $expire );
441 }
442
443 /**
444 * Resets cache keys.
445 *
446 * @since 3.0.0
447 *
448 * @deprecated 3.5.0 Use switch_to_blog()
449 * @see switch_to_blog()
450 */
451 public static function reset() {
452
453 $self = self::instance();
454
455 _deprecated_function( __FUNCTION__, '3.5.0', 'switch_to_blog()' );
456
457 // Clear out non-global caches since the blog ID has changed.
458 foreach ( array_keys( $self->cache ) as $group ) {
459 if ( ! isset( $self->global_groups[ $group ] ) ) {
460 unset( $self->cache[ $group ] );
461 }
462 }
463 }
464
465 /**
466 * Sets the data contents into the cache.
467 *
468 * The cache contents is grouped by the $group parameter followed by the
469 * $key. This allows for duplicate ids in unique groups. Therefore, naming of
470 * the group should be used with care and should follow normal function
471 * naming guidelines outside of core WordPress usage.
472 *
473 * The $expire parameter is not used, because the cache will automatically
474 * expire for each time a page is accessed and PHP finishes. The method is
475 * more for cache plugins which use files.
476 *
477 * @param int|string $key What to call the contents in the cache.
478 * @param mixed $data The contents to store in the cache.
479 * @param string $group Optional. Where to group the cache contents. Default 'default'.
480 * @param int $expire Not Used.
481 *
482 * @return true Always returns true.
483 * @since 2.0.0
484 *
485 */
486 public static function set( $key, $data, $group = 'default', $expire = 0 ) {
487 $group = self::get_group( $group );
488
489 if ( self::$_use_core ) {
490 return wp_cache_set( $key, $data, $group, $expire );
491 }
492
493 if ( empty( $group ) ) {
494 $group = 'default';
495 }
496
497 $self = self::instance();
498
499 if ( $self->multisite && ! isset( $self->global_groups[ $group ] ) ) {
500 $key = $self->blog_prefix . $key;
501 }
502
503 if ( is_object( $data ) ) {
504 $data = clone $data;
505 }
506
507 $self->cache[ $group ][ $key ] = $data;
508
509 return true;
510 }
511
512 /**
513 * Echoes the stats of the caching.
514 *
515 * Gives the cache hits, and cache misses. Also prints every cached group,
516 * key and the data.
517 *
518 * @since 2.0.0
519 */
520 /*public static function stats() {
521 $self = self::instance();
522 echo '<p>';
523 echo "<strong>Cache Hits:</strong> {$self->cache_hits}<br />";
524 echo "<strong>Cache Misses:</strong> {$self->cache_misses}<br />";
525 echo '</p>';
526 echo '<ul>';
527 foreach ( $self->cache as $group => $cache ) {
528 echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES,
529 2 ) . 'k )</li>';
530 }
531 echo '</ul>';
532 }*/
533
534 /**
535 * Switches the internal blog ID.
536 *
537 * This changes the blog ID used to create keys in blog specific groups.
538 *
539 * @param int $blog_id Blog ID.
540 *
541 * @since 3.5.0
542 *
543 */
544 public static function switch_to_blog( $blog_id ) {
545 $self = self::instance();
546
547 $blog_id = (int) $blog_id;
548 $self->blog_prefix = $self->multisite ? $blog_id . ':' : '';
549 }
550
551 /**
552 * Serves as a utility function to determine whether a key exists in the cache.
553 *
554 * @param int|string $key Cache key to check for existence.
555 * @param string $group Cache group for the key existence check.
556 *
557 * @return bool Whether the key exists in the cache for the given group.
558 * @since 3.4.0
559 *
560 */
561 protected static function _exists( $key, $group ) {
562 $self = self::instance();
563
564 return isset( $self->cache[ $group ] ) && ( isset( $self->cache[ $group ][ $key ] ) || array_key_exists(
565 $key,
566 $self->cache[ $group ]
567 ) );
568 }
569
570 /**
571 * Sets up object properties; PHP 5 style constructor.
572 *
573 * @since 2.0.8
574 */
575 public function __construct() {
576 $this->multisite = is_multisite();
577 $this->blog_prefix = $this->multisite ? get_current_blog_id() . ':' : '';
578
579 /**
580 * @todo This should be moved to the PHP4 style constructor, PHP5
581 * already calls __destruct()
582 */
583 register_shutdown_function( array( $this, '__destruct' ) );
584 }
585
586 /**
587 * Saves the object cache before object is completely destroyed.
588 *
589 * Called upon object destruction, which should be when PHP ends.
590 *
591 * @return true Always returns true.
592 * @since 2.0.8
593 *
594 */
595 public function __destruct() {
596 return true;
597 }
598 }
599
600 LP_Object_Cache::init();
601