1 | <?php |
||
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() |
||
54 | |||
55 | /** |
||
56 | * Enqueue the PHP script used for compiling dynamic stylesheets that are |
||
57 | * loaded externally |
||
58 | */ |
||
59 | public function wp_enqueue_style() |
||
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() |
||
85 | |||
86 | /** |
||
87 | * Parse all styles in $this->stylesheets and print them if the flag 'print' |
||
88 | * is not set to true. Used for loading styles externally via an http request. |
||
89 | */ |
||
90 | public function compile_external_styles() |
||
99 | |||
100 | /** |
||
101 | * Add a style path to the pool of styles to be compiled |
||
102 | * |
||
103 | * @param string $handle The stylesheet's name/id |
||
104 | * @param string $path The absolute path to the dynamic style |
||
105 | * @param boolean $print Whether to print the compiled CSS to the document |
||
106 | * head, or include it as an external CSS file |
||
107 | * @param boolean $minify Whether to minify the CSS output |
||
108 | * @param boolean $cache Whether to store the compiled version of this |
||
109 | * stylesheet in cache to avoid compilation on every page load. |
||
110 | */ |
||
111 | public function enqueue_style( $handle, $path, $print, $minify, $cache ) |
||
121 | |||
122 | /** |
||
123 | * Register a value retrieval function and associate it with the given handle |
||
124 | * |
||
125 | * @param type $handle The stylesheet's name/id |
||
126 | * @param type $callback |
||
127 | */ |
||
128 | public function register_callback( $handle, $callback ) |
||
132 | |||
133 | /** |
||
134 | * Compile multiple dynamic stylesheets |
||
135 | * |
||
136 | * @param boolean $printed |
||
137 | * @return string Compiled CSS |
||
138 | */ |
||
139 | protected function get_compiled_styles( $printed ) |
||
151 | |||
152 | /** |
||
153 | * Get the compiled CSS for the given style. Skips compilation if the compiled |
||
154 | * version can be found in cache. |
||
155 | * |
||
156 | * @param array $style List of styles with the same structure as they are |
||
157 | * stored in $this->stylesheets |
||
158 | * @return type |
||
159 | */ |
||
160 | protected function get_compiled_style( $style ) |
||
179 | |||
180 | /** |
||
181 | * Minify a given CSS string by removing comments, whitespaces and newlines |
||
182 | * |
||
183 | * @see http://stackoverflow.com/a/6630103/1096470 |
||
184 | * @param string $css CSS style to minify |
||
185 | * @return string Minified CSS |
||
186 | */ |
||
187 | protected function minify_css( $css ) |
||
191 | |||
192 | /** |
||
193 | * This filter is used to return only the styles that are set to be printed |
||
194 | * in the document head |
||
195 | * |
||
196 | * @param array $style |
||
197 | * @return boolean |
||
198 | */ |
||
199 | protected function filter_print( $style ) |
||
203 | |||
204 | /** |
||
205 | * This filter is used to return only the styles that are set to be loaded |
||
206 | * externally |
||
207 | * |
||
208 | * @param array $style |
||
209 | * @return boolean |
||
210 | */ |
||
211 | protected function filter_external( $style ) |
||
215 | |||
216 | /** |
||
217 | * Parse the given CSS string by converting the variables to their |
||
218 | * corresponding values retrieved by applying the callback function |
||
219 | * |
||
220 | * @param callable $callback A function that replaces the variables with |
||
221 | * their values. The function accepts the variable's name as a parameter |
||
222 | * @param string $css A string containing dynamic CSS (pre-compiled CSS with |
||
223 | * variables) |
||
224 | * @return string The compiled CSS after converting the variables to their |
||
225 | * corresponding values |
||
226 | */ |
||
227 | protected function compile_css( $css, $callback ) |
||
238 | } |
||
239 |
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.