1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package WordPress Dynamic CSS |
4
|
|
|
* @version 1.0.5 |
5
|
|
|
* @author Askupa Software <[email protected]> |
6
|
|
|
* @link https://github.com/askupasoftware/wp-dynamic-css |
7
|
|
|
* @copyright 2016 Askupa Software |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Dynamic CSS Cache Handler |
12
|
|
|
*/ |
13
|
|
|
class DynamicCSSCache |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Singleton The reference to *Singleton* instance of this class |
17
|
|
|
*/ |
18
|
|
|
private static $instance; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array The reference to the cached compiled CSS that is stored in the database |
22
|
|
|
*/ |
23
|
|
|
private static $cache; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Returns the *Singleton* instance of this class. |
27
|
|
|
* |
28
|
|
|
* @return Singleton The *Singleton* instance. |
29
|
|
|
*/ |
30
|
|
|
public static function get_instance() |
31
|
|
|
{ |
32
|
|
|
if (null === static::$instance) |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
static::$instance = new static(); |
|
|
|
|
35
|
|
|
self::load_cache(); |
36
|
|
|
} |
37
|
|
|
return static::$instance; |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns the compiled CSS for the given handle if it exists in the cache. |
42
|
|
|
* Returns false otherwise. |
43
|
|
|
* |
44
|
|
|
* @param string $handle The handle of the stylesheet |
45
|
|
|
* @return boolean|string |
46
|
|
|
*/ |
47
|
|
|
public function get( $handle ) |
48
|
|
|
{ |
49
|
|
|
if( array_key_exists( $handle, self::$cache ) ) |
50
|
|
|
{ |
51
|
|
|
return self::$cache[$handle]; |
52
|
|
|
} |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Update the compiled CSS for the given handle. |
58
|
|
|
* |
59
|
|
|
* @param string $handle The handle of the stylesheet |
60
|
|
|
* @param type $compiled_css |
61
|
|
|
*/ |
62
|
|
|
public function update( $handle, $compiled_css ) |
63
|
|
|
{ |
64
|
|
|
self::$cache[$handle] = $compiled_css; |
65
|
|
|
$this->update_option(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Clear the compiled CSS from cache for the given handle. |
70
|
|
|
* |
71
|
|
|
* @param string $handle The handle of the stylesheet |
72
|
|
|
*/ |
73
|
|
|
public function clear( $handle ) |
74
|
|
|
{ |
75
|
|
|
unset(self::$cache[$handle]); |
76
|
|
|
$this->update_option(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Load the cache value from the database or create an empty array if the |
81
|
|
|
* cache is empty. |
82
|
|
|
*/ |
83
|
|
|
private static function load_cache() |
84
|
|
|
{ |
85
|
|
|
self::$cache = get_option('wp-dynamic-css-cache'); |
86
|
|
|
|
87
|
|
|
if( false === self::$cache ) |
88
|
|
|
{ |
89
|
|
|
self::$cache = array(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Update the database option with the local cache value. |
95
|
|
|
*/ |
96
|
|
|
private function update_option() |
97
|
|
|
{ |
98
|
|
|
update_option('wp-dynamic-css-cache', self::$cache); |
99
|
|
|
} |
100
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.