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 = array(); |
||
35 | |||
36 | /** |
||
37 | * @var array The list of registered callbacks |
||
38 | */ |
||
39 | private $callbacks = array(); |
||
40 | |||
41 | /** |
||
42 | * @var aray The list of registered filters |
||
43 | */ |
||
44 | private $filters = array(); |
||
45 | |||
46 | /** |
||
47 | * Returns the *Singleton* instance of this class. |
||
48 | * |
||
49 | * @return DynamicCSSCompiler The *Singleton* instance. |
||
50 | */ |
||
51 | public static function get_instance() |
||
59 | |||
60 | /** |
||
61 | * Enqueue the stylesheets that are registered to be loaded externally |
||
62 | */ |
||
63 | public function enqueue_styles() |
||
73 | |||
74 | /** |
||
75 | * |
||
76 | * @param type $stylesheet |
||
77 | */ |
||
78 | public function enqueue_style( $stylesheet ) |
||
97 | |||
98 | /** |
||
99 | * This is the AJAX callback used for loading styles externally via an http |
||
100 | * request. |
||
101 | */ |
||
102 | public function ajax_callback() |
||
117 | |||
118 | /** |
||
119 | * Add a style path to the pool of styles to be compiled |
||
120 | * |
||
121 | * @param string $handle The stylesheet's name/id |
||
122 | * @param string $path The absolute path to the dynamic style |
||
123 | * @param boolean $print Whether to print the compiled CSS to the document |
||
124 | * head, or include it as an external CSS file |
||
125 | * @param boolean $minify Whether to minify the CSS output |
||
126 | * @param boolean $cache Whether to store the compiled version of this |
||
127 | * stylesheet in cache to avoid compilation on every page load. |
||
128 | */ |
||
129 | public function register_style( $handle, $path, $print, $minify, $cache ) |
||
139 | |||
140 | /** |
||
141 | * Register a value retrieval function and associate it with the given handle |
||
142 | * |
||
143 | * @param type $handle The stylesheet's name/id |
||
144 | * @param type $callback |
||
145 | */ |
||
146 | public function register_callback( $handle, $callback ) |
||
150 | |||
151 | /** |
||
152 | * Register a filter function for a given stylesheet handle. |
||
153 | */ |
||
154 | public function register_filter( $handle, $filter_name, $callback ) |
||
162 | |||
163 | /** |
||
164 | * Get the compiled CSS for the given style. Skips compilation if the compiled |
||
165 | * version can be found in cache. |
||
166 | * |
||
167 | * @param array $style List of styles with the same structure as they are |
||
168 | * stored in $this->stylesheets |
||
169 | * @return string The compiled CSS for this stylesheet |
||
170 | */ |
||
171 | protected function get_compiled_style( $style ) |
||
198 | |||
199 | /** |
||
200 | * Add meta information to the compiled CSS |
||
201 | * |
||
202 | * @param string $compiled_css The compiled CSS |
||
203 | * @return string The compiled CSS with the meta information added to it |
||
204 | */ |
||
205 | protected function add_meta_info( $compiled_css ) |
||
213 | |||
214 | /** |
||
215 | * Get the callback URL for enqueuing the stylesheet extrnally |
||
216 | * |
||
217 | * @param string $handle The stylesheet's handle |
||
218 | * @return string The URL for the given handle |
||
219 | */ |
||
220 | protected function get_ajax_callback_url( $handle ) |
||
230 | |||
231 | /** |
||
232 | * Minify a given CSS string by removing comments, whitespaces and newlines |
||
233 | * |
||
234 | * @see http://stackoverflow.com/a/6630103/1096470 |
||
235 | * @param string $css CSS style to minify |
||
236 | * @return string Minified CSS |
||
237 | */ |
||
238 | protected function minify_css( $css ) |
||
242 | |||
243 | /** |
||
244 | * Check if a callback function has been register for the given handle. |
||
245 | * |
||
246 | * @param string $handle |
||
247 | * @return boolean |
||
248 | */ |
||
249 | protected function callback_exists( $handle ) |
||
261 | |||
262 | /** |
||
263 | * Parse the given CSS string by converting the variables to their |
||
264 | * corresponding values retrieved by applying the callback function |
||
265 | * |
||
266 | * @param callable $callback A function that replaces the variables with |
||
267 | * their values. The function accepts the variable's name as a parameter |
||
268 | * @param string $css A string containing dynamic CSS (pre-compiled CSS with |
||
269 | * variables) |
||
270 | * @return string The compiled CSS after converting the variables to their |
||
271 | * corresponding values |
||
272 | */ |
||
273 | protected function compile_css( $css, $callback, $filters ) |
||
309 | |||
310 | /** |
||
311 | * Apply the filters specified in $filters_string to the given $value. |
||
312 | * |
||
313 | * @param string $filters_string |
||
314 | * @param string $value |
||
315 | * @param array $filters Array of callback functions |
||
316 | * @return string The value after all filters have been applied |
||
317 | */ |
||
318 | protected function apply_filters( $filters_string, $value, $filters ) |
||
340 | |||
341 | /** |
||
342 | * Convert the given string to its actual value. |
||
343 | * |
||
344 | * @param string $str The string to be converted (passed by reference) |
||
345 | */ |
||
346 | protected function strtoval( &$str ) |
||
353 | } |
||
354 |
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.