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 DynamicCSSCache 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 DynamicCSSCache The *Singleton* instance. |
29
|
|
|
*/ |
30
|
|
|
public static function get_instance() { |
31
|
|
|
|
32
|
|
|
if ( null === static::$instance ) { |
|
|
|
|
33
|
|
|
static::$instance = new static(); |
|
|
|
|
34
|
|
|
self::load_cache(); |
35
|
|
|
} |
36
|
|
|
return static::$instance; |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns the compiled CSS for the given handle if it exists in the cache. |
41
|
|
|
* Returns false otherwise. |
42
|
|
|
* |
43
|
|
|
* @param string $handle The handle of the stylesheet |
44
|
|
|
* @return boolean|string |
45
|
|
|
*/ |
46
|
|
|
public function get( $handle ) { |
47
|
|
|
|
48
|
|
|
if ( array_key_exists( $handle, self::$cache ) ) { |
49
|
|
|
return self::$cache[ $handle ]; |
50
|
|
|
} |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Update the compiled CSS for the given handle. |
56
|
|
|
* |
57
|
|
|
* @param string $handle The handle of the stylesheet |
58
|
|
|
* @param string $compiled_css |
59
|
|
|
*/ |
60
|
|
|
public function update( $handle, $compiled_css, $expiration ) { |
61
|
|
|
|
62
|
|
|
self::$cache[ $handle ] = $compiled_css; |
63
|
|
|
$this->update_option( $expiration ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Clear the compiled CSS from cache for the given handle. |
68
|
|
|
* |
69
|
|
|
* @param string $handle The handle of the stylesheet |
70
|
|
|
*/ |
71
|
|
|
public function clear( $handle ) { |
72
|
|
|
|
73
|
|
|
unset( self::$cache[ $handle ] ); |
74
|
|
|
delete_transient( 'wp-dynamic-css-cache' ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Load the cache value from the database or create an empty array if the |
79
|
|
|
* cache is empty. |
80
|
|
|
*/ |
81
|
|
|
private static function load_cache() { |
82
|
|
|
|
83
|
|
|
if ( false === ( self::$cache = get_transient( 'wp-dynamic-css-cache' ) ) ) { |
84
|
|
|
self::$cache = []; |
85
|
|
|
} |
86
|
|
|
// self::$cache = get_option('wp-dynamic-css-cache'); |
|
|
|
|
87
|
|
|
// |
88
|
|
|
// if( false === self::$cache ) |
|
|
|
|
89
|
|
|
// { |
90
|
|
|
// self::$cache = array(); |
|
|
|
|
91
|
|
|
// } |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Update the database option with the local cache value. |
96
|
|
|
*/ |
97
|
|
|
private function update_option( $expiration ) { |
98
|
|
|
|
99
|
|
|
// update_option('wp-dynamic-css-cache', self::$cache); |
|
|
|
|
100
|
|
|
set_transient( 'wp-dynamic-css-cache', self::$cache, $expiration ); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
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.