1 | <?php |
||
24 | class DynamicCSSCompiler { |
||
|
|||
25 | |||
26 | /** |
||
27 | * @var DynamicCSSCompiler 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 = []; |
||
35 | |||
36 | /** |
||
37 | * @var array The list of registered callbacks |
||
38 | */ |
||
39 | private $callbacks = []; |
||
40 | |||
41 | /** |
||
42 | * @var aray The list of registered filters |
||
43 | */ |
||
44 | private $filters = []; |
||
45 | |||
46 | /** |
||
47 | * Returns the *Singleton* instance of this class. |
||
48 | * |
||
49 | * @return DynamicCSSCompiler The *Singleton* instance. |
||
50 | */ |
||
51 | public static function get_instance() { |
||
58 | |||
59 | /** |
||
60 | * Enqueue all registered stylesheets. |
||
61 | */ |
||
62 | public function enqueue_styles() { |
||
70 | |||
71 | /** |
||
72 | * Enqueue a single registered stylesheet. |
||
73 | * |
||
74 | * @param array $stylesheet |
||
75 | */ |
||
76 | public function enqueue_style( $stylesheet ) { |
||
96 | |||
97 | /** |
||
98 | * This is the AJAX callback used for loading styles externally via an http |
||
99 | * request. |
||
100 | */ |
||
101 | public function ajax_callback() { |
||
114 | |||
115 | /** |
||
116 | * Add a style path to the pool of styles to be compiled |
||
117 | * |
||
118 | * @param string $handle The stylesheet's name/id |
||
119 | * @param string $path The absolute path to the dynamic style |
||
120 | * @param boolean $print Whether to print the compiled CSS to the document |
||
121 | * head, or include it as an external CSS file |
||
122 | * @param boolean $minify Whether to minify the CSS output |
||
123 | * @param boolean $cache Whether to store the compiled version of this |
||
124 | * stylesheet in cache to avoid compilation on every page load. |
||
125 | */ |
||
126 | public function register_style( $handle, $path, $deps, $print, $minify, $cache, $expiration ) { |
||
138 | |||
139 | /** |
||
140 | * Register a value retrieval function and associate it with the given handle |
||
141 | * |
||
142 | * @param string $handle The stylesheet's name/id |
||
143 | * @param callable $callback |
||
144 | */ |
||
145 | public function register_callback( $handle, $callback ) { |
||
149 | |||
150 | /** |
||
151 | * Register a filter function for a given stylesheet handle. |
||
152 | */ |
||
153 | public function register_filter( $handle, $filter_name, $callback ) { |
||
160 | |||
161 | /** |
||
162 | * Get the compiled CSS for the given style. Skips compilation if the compiled |
||
163 | * version can be found in cache. |
||
164 | * |
||
165 | * @param array $style List of styles with the same structure as they are |
||
166 | * stored in $this->stylesheets |
||
167 | * @return string The compiled CSS for this stylesheet |
||
168 | */ |
||
169 | protected function get_compiled_style( $style ) { |
||
196 | |||
197 | /** |
||
198 | * Add meta information to the compiled CSS |
||
199 | * |
||
200 | * @param string $compiled_css The compiled CSS |
||
201 | * @return string The compiled CSS with the meta information added to it |
||
202 | */ |
||
203 | protected function add_meta_info( $compiled_css ) { |
||
211 | |||
212 | /** |
||
213 | * Get the callback URL for enqueuing the stylesheet extrnally |
||
214 | * |
||
215 | * @param string $handle The stylesheet's handle |
||
216 | * @return string The URL for the given handle |
||
217 | */ |
||
218 | protected function get_ajax_callback_url( $handle ) { |
||
227 | |||
228 | /** |
||
229 | * Minify a given CSS string by removing comments, whitespaces and newlines |
||
230 | * |
||
231 | * @see http://stackoverflow.com/a/6630103/1096470 |
||
232 | * @param string $css CSS style to minify |
||
233 | * @return string Minified CSS |
||
234 | */ |
||
235 | protected function minify_css( $css ) { |
||
239 | |||
240 | /** |
||
241 | * Check if a callback function has been register for the given handle. |
||
242 | * |
||
243 | * @param string $handle |
||
244 | * @return boolean |
||
245 | */ |
||
246 | protected function callback_exists( $handle ) { |
||
257 | |||
258 | /** |
||
259 | * Parse the given CSS string by converting the variables to their |
||
260 | * corresponding values retrieved by applying the callback function |
||
261 | * |
||
262 | * @param callable $callback A function that replaces the variables with |
||
263 | * their values. The function accepts the variable's name as a parameter |
||
264 | * @param string $css A string containing dynamic CSS (pre-compiled CSS with |
||
265 | * variables) |
||
266 | * @return string The compiled CSS after converting the variables to their |
||
267 | * corresponding values |
||
268 | */ |
||
269 | protected function compile_css( $css, $callback, $filters ) { |
||
301 | |||
302 | /** |
||
303 | * Apply the filters specified in $filters_string to the given $value. |
||
304 | * |
||
305 | * @param string $filters_string |
||
306 | * @param string $value |
||
307 | * @param array $filters Array of callback functions |
||
308 | * @return string The value after all filters have been applied |
||
309 | */ |
||
310 | protected function apply_filters( $filters_string, $value, $filters = [] ) { |
||
329 | |||
330 | /** |
||
331 | * Convert the given string to its actual value. |
||
332 | * |
||
333 | * @param string $str The string to be converted (passed by reference) |
||
334 | */ |
||
335 | protected function strtoval( &$str ) { |
||
350 | } |
||
351 |
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.