1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package WordPress Dynamic CSS |
4
|
|
|
* @version 1.0.3 |
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 Compiler Utility Class |
12
|
|
|
* |
13
|
|
|
* |
14
|
|
|
* Dynamic CSS Syntax |
15
|
|
|
* ------------------ |
16
|
|
|
* <pre> |
17
|
|
|
* body {color: $body_color;} |
18
|
|
|
* </pre> |
19
|
|
|
* In the above example, the variable $body_color is replaced by a value |
20
|
|
|
* retrieved by the value callback function. The function is passed the variable |
21
|
|
|
* name without the dollar sign, which can be used with get_option() or |
22
|
|
|
* get_theme_mod() etc. |
23
|
|
|
*/ |
24
|
|
|
class DynamicCSSCompiler |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var Singleton The reference to *Singleton* instance of this class |
28
|
|
|
*/ |
29
|
|
|
private static $instance; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array The list of dynamic styles paths to compile |
33
|
|
|
*/ |
34
|
|
|
private $stylesheets = array(); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $callbacks = array(); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Returns the *Singleton* instance of this class. |
43
|
|
|
* |
44
|
|
|
* @return Singleton The *Singleton* instance. |
45
|
|
|
*/ |
46
|
|
|
public static function get_instance() |
47
|
|
|
{ |
48
|
|
|
if (null === static::$instance) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
static::$instance = new static(); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
return static::$instance; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Enqueue the PHP script used for compiling dynamic stylesheets that are |
57
|
|
|
* loaded externally |
58
|
|
|
*/ |
59
|
|
|
public function wp_enqueue_style() |
60
|
|
|
{ |
61
|
|
|
// Only enqueue if there is at least one dynamic stylesheet that is |
62
|
|
|
// set to be loaded externally |
63
|
|
|
if( 0 < count( array_filter($this->stylesheets, array( $this, 'filter_external' ) ) ) ) |
64
|
|
|
{ |
65
|
|
|
wp_enqueue_style( 'wp-dynamic-css', admin_url( 'admin-ajax.php?action=wp_dynamic_css' ) ); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Parse all styles in $this->stylesheets and print them if the flag 'print' |
71
|
|
|
* is set to true. Used for printing styles to the document head. |
72
|
|
|
*/ |
73
|
|
|
public function compile_printed_styles() |
74
|
|
|
{ |
75
|
|
|
$styles = array_filter($this->stylesheets, array( $this, 'filter_print' ) ); |
76
|
|
|
|
77
|
|
|
// Bail if there are no styles to be printed |
78
|
|
|
if( count( $styles ) === 0 ) return; |
79
|
|
|
|
80
|
|
|
$compiled_css = $this->compile_styles( $styles ); |
81
|
|
|
|
82
|
|
|
echo "<style id=\"wp-dynamic-css\">\n"; |
83
|
|
|
include 'style.phtml'; |
84
|
|
|
echo "</style>"; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Parse all styles in $this->stylesheets and print them if the flag 'print' |
89
|
|
|
* is not set to true. Used for loading styles externally via an http request. |
90
|
|
|
*/ |
91
|
|
|
public function compile_external_styles() |
92
|
|
|
{ |
93
|
|
|
header( "Content-type: text/css; charset: UTF-8" ); |
94
|
|
|
header( "Cache-Control: no-cache, must-revalidate" ); //set headers to NOT cache a page so that changes to options are reflected immediately |
95
|
|
|
|
96
|
|
|
$compiled_css = $this->compile_styles( array_filter($this->stylesheets, array( $this, 'filter_external' ) ) ); |
97
|
|
|
|
98
|
|
|
include 'style.phtml'; |
99
|
|
|
wp_die(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Add a style path to the pool of styles to be compiled |
104
|
|
|
* |
105
|
|
|
* @param string $handle The stylesheet's name/id |
106
|
|
|
* @param string $path The absolute path to the dynamic style |
107
|
|
|
* @param boolean $print Whether to print the compiled CSS to the document |
108
|
|
|
* head, or include it as an external CSS file |
109
|
|
|
*/ |
110
|
|
|
public function enqueue_style( $handle, $path, $print ) |
111
|
|
|
{ |
112
|
|
|
$this->stylesheets[] = array( |
113
|
|
|
'handle'=> $handle, |
114
|
|
|
'path' => $path, |
115
|
|
|
'print' => $print |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Register a value retrieval function and associate it with the given handle |
121
|
|
|
* |
122
|
|
|
* @param type $handle The stylesheet's name/id |
123
|
|
|
* @param type $callback |
124
|
|
|
*/ |
125
|
|
|
public function register_callback( $handle, $callback ) |
126
|
|
|
{ |
127
|
|
|
$this->callbacks[$handle] = $callback; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Compile multiple dynamic stylesheets |
132
|
|
|
* |
133
|
|
|
* @param array $styles List of styles with the same structure as they are |
134
|
|
|
* stored in $this->stylesheets |
135
|
|
|
* @return string Compiled CSS |
136
|
|
|
*/ |
137
|
|
|
protected function compile_styles( $styles ) |
138
|
|
|
{ |
139
|
|
|
$compiled_css = ''; |
140
|
|
|
foreach( $styles as $style ) |
141
|
|
|
{ |
142
|
|
|
$css = file_get_contents( $style['path'] ); |
143
|
|
|
$compiled_css .= $this->compile_css( $css, $this->callbacks[$style['handle']] )."\n"; |
144
|
|
|
} |
145
|
|
|
return $compiled_css; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* This filter is used to return only the styles that are set to be printed |
150
|
|
|
* in the document head |
151
|
|
|
* |
152
|
|
|
* @param array $style |
153
|
|
|
* @return boolean |
154
|
|
|
*/ |
155
|
|
|
protected function filter_print( $style ) |
156
|
|
|
{ |
157
|
|
|
return true === $style['print']; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* This filter is used to return only the styles that are set to be loaded |
162
|
|
|
* externally |
163
|
|
|
* |
164
|
|
|
* @param array $style |
165
|
|
|
* @return boolean |
166
|
|
|
*/ |
167
|
|
|
protected function filter_external( $style ) |
168
|
|
|
{ |
169
|
|
|
return true !== $style['print']; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Parse the given CSS string by converting the variables to their |
174
|
|
|
* corresponding values retrieved by applying the callback function |
175
|
|
|
* |
176
|
|
|
* @param callable $callback A function that replaces the variables with |
177
|
|
|
* their values. The function accepts the variable's name as a parameter |
178
|
|
|
* @param string $css A string containing dynamic CSS (pre-compiled CSS with |
179
|
|
|
* variables) |
180
|
|
|
* @return string The compiled CSS after converting the variables to their |
181
|
|
|
* corresponding values |
182
|
|
|
*/ |
183
|
|
|
protected function compile_css( $css, $callback ) |
184
|
|
|
{ |
185
|
|
|
return preg_replace_callback( "#\\$([\\w-]+)((?:\\['?[\\w-]+'?\\])*)#", function( $matches ) use ( $callback ) { |
186
|
|
|
// If this variable is an array, get the subscripts |
187
|
|
|
if( '' !== $matches[2] ) |
188
|
|
|
{ |
189
|
|
|
preg_match_all('/[\w-]+/i', $matches[2], $subscripts); |
190
|
|
|
} |
191
|
|
|
return call_user_func_array( $callback, array($matches[1],@$subscripts[0]) ); |
|
|
|
|
192
|
|
|
}, $css); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
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.