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() |
||
52 | { |
||
53 | if (null === static::$instance) |
||
54 | { |
||
55 | static::$instance = new static(); |
||
56 | } |
||
57 | return static::$instance; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Enqueue all registered stylesheets. |
||
62 | */ |
||
63 | public function enqueue_styles() |
||
64 | { |
||
65 | foreach( $this->stylesheets as $stylesheet ) |
||
66 | { |
||
67 | if( $this->callback_exists( $stylesheet['handle'] ) ) |
||
68 | { |
||
69 | $this->enqueue_style( $stylesheet ); |
||
70 | } |
||
71 | } |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Enqueue a single registered stylesheet. |
||
76 | * |
||
77 | * @param array $stylesheet |
||
78 | */ |
||
79 | public function enqueue_style( $stylesheet ) |
||
80 | { |
||
81 | $handle = 'wp-dynamic-css-'.$stylesheet['handle']; |
||
82 | $print = $stylesheet['print']; |
||
83 | |||
84 | wp_register_style( |
||
85 | $handle, |
||
86 | // Don't pass a URL if this style is to be printed |
||
87 | $print ? false : $this->get_ajax_callback_url( $stylesheet['handle'] ) |
||
88 | ); |
||
89 | |||
90 | wp_enqueue_style( $handle ); |
||
91 | |||
92 | // Add inline styles for styles that are set to be printed |
||
93 | if( $print ) |
||
94 | { |
||
95 | // Inline styles only work if the handle has already been registered and enqueued |
||
96 | wp_add_inline_style( $handle, $this->get_compiled_style( $stylesheet ) ); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * This is the AJAX callback used for loading styles externally via an http |
||
102 | * request. |
||
103 | */ |
||
104 | public function ajax_callback() |
||
105 | { |
||
106 | header( "Content-type: text/css; charset: UTF-8" ); |
||
107 | $handle = filter_input( INPUT_GET, 'handle' ); |
||
108 | |||
109 | foreach( $this->stylesheets as $stylesheet ) |
||
110 | { |
||
111 | if( $handle === $stylesheet['handle'] ) |
||
112 | { |
||
113 | echo $this->get_compiled_style( $stylesheet ); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | wp_die(); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Add a style path to the pool of styles to be compiled |
||
122 | * |
||
123 | * @param string $handle The stylesheet's name/id |
||
124 | * @param string $path The absolute path to the dynamic style |
||
125 | * @param boolean $print Whether to print the compiled CSS to the document |
||
126 | * head, or include it as an external CSS file |
||
127 | * @param boolean $minify Whether to minify the CSS output |
||
128 | * @param boolean $cache Whether to store the compiled version of this |
||
129 | * stylesheet in cache to avoid compilation on every page load. |
||
130 | */ |
||
131 | public function register_style( $handle, $path, $print, $minify, $cache ) |
||
141 | |||
142 | /** |
||
143 | * Register a value retrieval function and associate it with the given handle |
||
144 | * |
||
145 | * @param string $handle The stylesheet's name/id |
||
146 | * @param callable $callback |
||
147 | */ |
||
148 | public function register_callback( $handle, $callback ) |
||
152 | |||
153 | /** |
||
154 | * Register a filter function for a given stylesheet handle. |
||
155 | */ |
||
156 | public function register_filter( $handle, $filter_name, $callback ) |
||
164 | |||
165 | /** |
||
166 | * Get the compiled CSS for the given style. Skips compilation if the compiled |
||
167 | * version can be found in cache. |
||
168 | * |
||
169 | * @param array $style List of styles with the same structure as they are |
||
170 | * stored in $this->stylesheets |
||
171 | * @return string The compiled CSS for this stylesheet |
||
172 | */ |
||
173 | protected function get_compiled_style( $style ) |
||
200 | |||
201 | /** |
||
202 | * Add meta information to the compiled CSS |
||
203 | * |
||
204 | * @param string $compiled_css The compiled CSS |
||
205 | * @return string The compiled CSS with the meta information added to it |
||
206 | */ |
||
207 | protected function add_meta_info( $compiled_css ) |
||
215 | |||
216 | /** |
||
217 | * Get the callback URL for enqueuing the stylesheet extrnally |
||
218 | * |
||
219 | * @param string $handle The stylesheet's handle |
||
220 | * @return string The URL for the given handle |
||
221 | */ |
||
222 | protected function get_ajax_callback_url( $handle ) |
||
232 | |||
233 | /** |
||
234 | * Minify a given CSS string by removing comments, whitespaces and newlines |
||
235 | * |
||
236 | * @see http://stackoverflow.com/a/6630103/1096470 |
||
237 | * @param string $css CSS style to minify |
||
238 | * @return string Minified CSS |
||
239 | */ |
||
240 | protected function minify_css( $css ) |
||
244 | |||
245 | /** |
||
246 | * Check if a callback function has been register for the given handle. |
||
247 | * |
||
248 | * @param string $handle |
||
249 | * @return boolean |
||
250 | */ |
||
251 | protected function callback_exists( $handle ) |
||
263 | |||
264 | /** |
||
265 | * Parse the given CSS string by converting the variables to their |
||
266 | * corresponding values retrieved by applying the callback function |
||
267 | * |
||
268 | * @param callable $callback A function that replaces the variables with |
||
269 | * their values. The function accepts the variable's name as a parameter |
||
270 | * @param string $css A string containing dynamic CSS (pre-compiled CSS with |
||
271 | * variables) |
||
272 | * @return string The compiled CSS after converting the variables to their |
||
273 | * corresponding values |
||
274 | */ |
||
275 | protected function compile_css( $css, $callback, $filters ) |
||
311 | |||
312 | /** |
||
313 | * Apply the filters specified in $filters_string to the given $value. |
||
314 | * |
||
315 | * @param string $filters_string |
||
316 | * @param string $value |
||
317 | * @param array $filters Array of callback functions |
||
318 | * @return string The value after all filters have been applied |
||
319 | */ |
||
320 | protected function apply_filters( $filters_string, $value, $filters = array() ) |
||
342 | |||
343 | /** |
||
344 | * Convert the given string to its actual value. |
||
345 | * |
||
346 | * @param string $str The string to be converted (passed by reference) |
||
347 | */ |
||
348 | protected function strtoval( &$str ) |
||
355 | } |
||
356 |
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.