@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | /** |
91 | 91 | * Value retrieval function |
92 | 92 | */ |
93 | -function callback( $varname, $subscripts ) |
|
93 | +function callback($varname, $subscripts) |
|
94 | 94 | { |
95 | 95 | $values = array( |
96 | 96 | 'var1' => 'value1', |
@@ -106,9 +106,9 @@ discard block |
||
106 | 106 | ); |
107 | 107 | |
108 | 108 | $val = $values[$varname]; |
109 | - if( null !== $subscripts ) |
|
109 | + if (null !== $subscripts) |
|
110 | 110 | { |
111 | - foreach( $subscripts as $subscript ) |
|
111 | + foreach ($subscripts as $subscript) |
|
112 | 112 | { |
113 | 113 | $val = $val[$subscript]; |
114 | 114 | } |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | /** |
121 | 121 | * Simple string concat filter |
122 | 122 | */ |
123 | -function simple_filter_callback( $foo ) |
|
123 | +function simple_filter_callback($foo) |
|
124 | 124 | { |
125 | 125 | return $foo.'bar'; |
126 | 126 | } |
@@ -128,7 +128,7 @@ discard block |
||
128 | 128 | /** |
129 | 129 | * String concat filter with parameters |
130 | 130 | */ |
131 | -function complex_filter_callback( $value, $arg1 = '', $arg2 = '') |
|
131 | +function complex_filter_callback($value, $arg1 = '', $arg2 = '') |
|
132 | 132 | { |
133 | 133 | return $value.$arg1.$arg2; |
134 | 134 | } |
@@ -136,7 +136,7 @@ discard block |
||
136 | 136 | /** |
137 | 137 | * Number adding filter |
138 | 138 | */ |
139 | -function add_filter_callback( $value, $a, $b ) |
|
139 | +function add_filter_callback($value, $a, $b) |
|
140 | 140 | { |
141 | - return $value.($a+$b); |
|
141 | + return $value.($a + $b); |
|
142 | 142 | } |
143 | 143 | \ No newline at end of file |
@@ -20,14 +20,14 @@ discard block |
||
20 | 20 | * Domain Path: /languages |
21 | 21 | */ |
22 | 22 | |
23 | -defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); |
|
23 | +defined('ABSPATH') or die('No script kiddies please!'); |
|
24 | 24 | |
25 | 25 | |
26 | 26 | /** |
27 | 27 | * Prevent loading the library more than once |
28 | 28 | */ |
29 | -if( defined( 'WP_DYNAMIC_CSS' ) ) return; |
|
30 | -define( 'WP_DYNAMIC_CSS', true ); |
|
29 | +if (defined('WP_DYNAMIC_CSS')) return; |
|
30 | +define('WP_DYNAMIC_CSS', true); |
|
31 | 31 | |
32 | 32 | /** |
33 | 33 | * Load required files |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | * styles. |
44 | 44 | */ |
45 | 45 | $dcss = DynamicCSSCompiler::get_instance(); |
46 | -add_action( 'wp_print_styles', array( $dcss, 'print_styles' ), 100 ); |
|
47 | -add_action( 'wp_enqueue_scripts', array( $dcss, 'enqueue_styles' ), 100 ); |
|
48 | -add_action( 'wp_ajax_wp_dynamic_css', array( $dcss, 'ajax_callback' ) ); |
|
49 | -add_action( 'wp_ajax_nopriv_wp_dynamic_css', array( $dcss, 'ajax_callback' ) ); |
|
50 | 46 | \ No newline at end of file |
47 | +add_action('wp_print_styles', array($dcss, 'print_styles'), 100); |
|
48 | +add_action('wp_enqueue_scripts', array($dcss, 'enqueue_styles'), 100); |
|
49 | +add_action('wp_ajax_wp_dynamic_css', array($dcss, 'ajax_callback')); |
|
50 | +add_action('wp_ajax_nopriv_wp_dynamic_css', array($dcss, 'ajax_callback')); |
|
51 | 51 | \ No newline at end of file |
@@ -7,7 +7,7 @@ discard block |
||
7 | 7 | * @copyright 2016 Askupa Software |
8 | 8 | */ |
9 | 9 | |
10 | -if( !function_exists('wp_dynamic_css_enqueue') ) |
|
10 | +if (!function_exists('wp_dynamic_css_enqueue')) |
|
11 | 11 | { |
12 | 12 | /** |
13 | 13 | * Enqueue a dynamic stylesheet |
@@ -24,14 +24,14 @@ discard block |
||
24 | 24 | * @param boolean $cache Whether to store the compiled version of this |
25 | 25 | * stylesheet in cache to avoid compilation on every page load. |
26 | 26 | */ |
27 | - function wp_dynamic_css_enqueue( $handle, $path, $print = true, $minify = false, $cache = false ) |
|
27 | + function wp_dynamic_css_enqueue($handle, $path, $print = true, $minify = false, $cache = false) |
|
28 | 28 | { |
29 | 29 | $dcss = DynamicCSSCompiler::get_instance(); |
30 | - $dcss->enqueue_style( $handle, $path, $print, $minify, $cache ); |
|
30 | + $dcss->enqueue_style($handle, $path, $print, $minify, $cache); |
|
31 | 31 | } |
32 | 32 | } |
33 | 33 | |
34 | -if( !function_exists('wp_dynamic_css_set_callback') ) |
|
34 | +if (!function_exists('wp_dynamic_css_set_callback')) |
|
35 | 35 | { |
36 | 36 | /** |
37 | 37 | * Set the value retrieval callback function |
@@ -46,14 +46,14 @@ discard block |
||
46 | 46 | * can either be a reference to a function name or method within an |
47 | 47 | * class/object. |
48 | 48 | */ |
49 | - function wp_dynamic_css_set_callback( $handle, $callback ) |
|
49 | + function wp_dynamic_css_set_callback($handle, $callback) |
|
50 | 50 | { |
51 | 51 | $dcss = DynamicCSSCompiler::get_instance(); |
52 | - $dcss->register_callback( $handle, $callback ); |
|
52 | + $dcss->register_callback($handle, $callback); |
|
53 | 53 | } |
54 | 54 | } |
55 | 55 | |
56 | -if( !function_exists('wp_dynamic_css_clear_cache') ) |
|
56 | +if (!function_exists('wp_dynamic_css_clear_cache')) |
|
57 | 57 | { |
58 | 58 | /** |
59 | 59 | * Clear the cached compiled CSS for the given handle. |
@@ -65,14 +65,14 @@ discard block |
||
65 | 65 | * |
66 | 66 | * @param string $handle The name of the stylesheet to be cleared from cache |
67 | 67 | */ |
68 | - function wp_dynamic_css_clear_cache( $handle ) |
|
68 | + function wp_dynamic_css_clear_cache($handle) |
|
69 | 69 | { |
70 | 70 | $cache = DynamicCSSCache::get_instance(); |
71 | - $cache->clear( $handle ); |
|
71 | + $cache->clear($handle); |
|
72 | 72 | } |
73 | 73 | } |
74 | 74 | |
75 | -if( !function_exists('wp_dynamic_css_register_filter') ) |
|
75 | +if (!function_exists('wp_dynamic_css_register_filter')) |
|
76 | 76 | { |
77 | 77 | /** |
78 | 78 | * Register a filter function for a given stylesheet handle. |
@@ -109,9 +109,9 @@ discard block |
||
109 | 109 | * @param Callable $callback The actual filter function. Accepts the $value |
110 | 110 | * as an argument. Should return the filtered value. |
111 | 111 | */ |
112 | - function wp_dynamic_css_register_filter( $handle, $filter_name, $callback ) |
|
112 | + function wp_dynamic_css_register_filter($handle, $filter_name, $callback) |
|
113 | 113 | { |
114 | 114 | $dcss = DynamicCSSCompiler::get_instance(); |
115 | - $dcss->register_filter( $handle, $filter_name, $callback ); |
|
115 | + $dcss->register_filter($handle, $filter_name, $callback); |
|
116 | 116 | } |
117 | 117 | } |
@@ -62,16 +62,16 @@ discard block |
||
62 | 62 | */ |
63 | 63 | public function enqueue_styles() |
64 | 64 | { |
65 | - foreach( $this->stylesheets as $stylesheet ) |
|
65 | + foreach ($this->stylesheets as $stylesheet) |
|
66 | 66 | { |
67 | - if( !$stylesheet['print'] && $this->callback_exists( $stylesheet['handle'] ) ) |
|
67 | + if (!$stylesheet['print'] && $this->callback_exists($stylesheet['handle'])) |
|
68 | 68 | { |
69 | 69 | wp_enqueue_style( |
70 | 70 | 'wp-dynamic-css-'.$stylesheet['handle'], |
71 | - esc_url_raw( add_query_arg(array( |
|
71 | + esc_url_raw(add_query_arg(array( |
|
72 | 72 | 'action' => 'wp_dynamic_css', |
73 | 73 | 'handle' => $stylesheet['handle'] |
74 | - ), admin_url( 'admin-ajax.php'))) |
|
74 | + ), admin_url('admin-ajax.php'))) |
|
75 | 75 | ); |
76 | 76 | } |
77 | 77 | } |
@@ -82,11 +82,11 @@ discard block |
||
82 | 82 | */ |
83 | 83 | public function print_styles() |
84 | 84 | { |
85 | - foreach( $this->stylesheets as $stylesheet ) |
|
85 | + foreach ($this->stylesheets as $stylesheet) |
|
86 | 86 | { |
87 | - if( $stylesheet['print'] && $this->callback_exists( $stylesheet['handle'] ) ) |
|
87 | + if ($stylesheet['print'] && $this->callback_exists($stylesheet['handle'])) |
|
88 | 88 | { |
89 | - $compiled_css = $this->get_compiled_style( $stylesheet ); |
|
89 | + $compiled_css = $this->get_compiled_style($stylesheet); |
|
90 | 90 | |
91 | 91 | echo "<style id=\"wp-dynamic-css-".$stylesheet['handle']."\">\n"; |
92 | 92 | include 'style.phtml'; |
@@ -101,14 +101,14 @@ discard block |
||
101 | 101 | */ |
102 | 102 | public function ajax_callback() |
103 | 103 | { |
104 | - header( "Content-type: text/css; charset: UTF-8" ); |
|
105 | - $handle = filter_input( INPUT_GET, 'handle' ); |
|
104 | + header("Content-type: text/css; charset: UTF-8"); |
|
105 | + $handle = filter_input(INPUT_GET, 'handle'); |
|
106 | 106 | |
107 | - foreach( $this->stylesheets as $stylesheet ) |
|
107 | + foreach ($this->stylesheets as $stylesheet) |
|
108 | 108 | { |
109 | - if( $handle === $stylesheet['handle'] ) |
|
109 | + if ($handle === $stylesheet['handle']) |
|
110 | 110 | { |
111 | - $compiled_css = $this->get_compiled_style( $stylesheet ); |
|
111 | + $compiled_css = $this->get_compiled_style($stylesheet); |
|
112 | 112 | include 'style.phtml'; |
113 | 113 | } |
114 | 114 | } |
@@ -127,7 +127,7 @@ discard block |
||
127 | 127 | * @param boolean $cache Whether to store the compiled version of this |
128 | 128 | * stylesheet in cache to avoid compilation on every page load. |
129 | 129 | */ |
130 | - public function enqueue_style( $handle, $path, $print, $minify, $cache ) |
|
130 | + public function enqueue_style($handle, $path, $print, $minify, $cache) |
|
131 | 131 | { |
132 | 132 | $this->stylesheets[] = array( |
133 | 133 | 'handle'=> $handle, |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | * @param type $handle The stylesheet's name/id |
145 | 145 | * @param type $callback |
146 | 146 | */ |
147 | - public function register_callback( $handle, $callback ) |
|
147 | + public function register_callback($handle, $callback) |
|
148 | 148 | { |
149 | 149 | $this->callbacks[$handle] = $callback; |
150 | 150 | } |
@@ -152,9 +152,9 @@ discard block |
||
152 | 152 | /** |
153 | 153 | * Register a filter function for a given stylesheet handle. |
154 | 154 | */ |
155 | - public function register_filter( $handle, $filter_name, $callback ) |
|
155 | + public function register_filter($handle, $filter_name, $callback) |
|
156 | 156 | { |
157 | - if( !array_key_exists( $handle, $this->filters ) ) |
|
157 | + if (!array_key_exists($handle, $this->filters)) |
|
158 | 158 | { |
159 | 159 | $this->filters[$handle] = array(); |
160 | 160 | } |
@@ -169,31 +169,31 @@ discard block |
||
169 | 169 | * stored in $this->stylesheets |
170 | 170 | * @return type |
171 | 171 | */ |
172 | - protected function get_compiled_style( $style ) |
|
172 | + protected function get_compiled_style($style) |
|
173 | 173 | { |
174 | 174 | $cache = DynamicCSSCache::get_instance(); |
175 | 175 | |
176 | 176 | // Use cached compiled CSS if applicable |
177 | - if( $style['cache'] ) |
|
177 | + if ($style['cache']) |
|
178 | 178 | { |
179 | - $cached_css = $cache->get( $style['handle'] ); |
|
180 | - if( false !== $cached_css ) |
|
179 | + $cached_css = $cache->get($style['handle']); |
|
180 | + if (false !== $cached_css) |
|
181 | 181 | { |
182 | 182 | return $cached_css; |
183 | 183 | } |
184 | 184 | } |
185 | 185 | |
186 | - $css = file_get_contents( $style['path'] ); |
|
187 | - if( $style['minify'] ) $css = $this->minify_css( $css ); |
|
186 | + $css = file_get_contents($style['path']); |
|
187 | + if ($style['minify']) $css = $this->minify_css($css); |
|
188 | 188 | |
189 | 189 | // Compile the dynamic CSS |
190 | 190 | $compiled_css = $this->compile_css( |
191 | 191 | $css, |
192 | 192 | $this->callbacks[$style['handle']], |
193 | - key_exists( $style['handle'], $this->filters ) ? $this->filters[$style['handle']] : array() |
|
193 | + key_exists($style['handle'], $this->filters) ? $this->filters[$style['handle']] : array() |
|
194 | 194 | ); |
195 | 195 | |
196 | - $cache->update( $style['handle'], $compiled_css ); |
|
196 | + $cache->update($style['handle'], $compiled_css); |
|
197 | 197 | return $compiled_css; |
198 | 198 | } |
199 | 199 | |
@@ -204,9 +204,9 @@ discard block |
||
204 | 204 | * @param string $css CSS style to minify |
205 | 205 | * @return string Minified CSS |
206 | 206 | */ |
207 | - protected function minify_css( $css ) |
|
207 | + protected function minify_css($css) |
|
208 | 208 | { |
209 | - return preg_replace( '@({)\s+|(\;)\s+|/\*.+?\*\/|\R@is', '$1$2 ', $css ); |
|
209 | + return preg_replace('@({)\s+|(\;)\s+|/\*.+?\*\/|\R@is', '$1$2 ', $css); |
|
210 | 210 | } |
211 | 211 | |
212 | 212 | /** |
@@ -215,9 +215,9 @@ discard block |
||
215 | 215 | * @param string $handle |
216 | 216 | * @return boolean |
217 | 217 | */ |
218 | - protected function callback_exists( $handle ) |
|
218 | + protected function callback_exists($handle) |
|
219 | 219 | { |
220 | - if( array_key_exists( $handle, $this->callbacks ) ) |
|
220 | + if (array_key_exists($handle, $this->callbacks)) |
|
221 | 221 | { |
222 | 222 | return true; |
223 | 223 | } |
@@ -239,33 +239,33 @@ discard block |
||
239 | 239 | * @return string The compiled CSS after converting the variables to their |
240 | 240 | * corresponding values |
241 | 241 | */ |
242 | - protected function compile_css( $css, $callback, $filters ) |
|
242 | + protected function compile_css($css, $callback, $filters) |
|
243 | 243 | { |
244 | 244 | return preg_replace_callback( |
245 | 245 | |
246 | - "#". // Begin |
|
247 | - "\\$". // Must start with $ |
|
248 | - "([\\w-]+)". // Match alphanumeric characters and dashes |
|
249 | - "((?:\\['?[\\w-]+'?\\])*)". // Optionally match array subscripts i.e. $myVar['index'] |
|
250 | - "((?:". // Optionally match pipe filters i.e. $myVar|myFilter |
|
251 | - "\\|[\\w-]+". // Starting with the | character |
|
252 | - "(\([\w\.,']+\))?". // Filters can have strings and numbers i.e myFilter('string',1,2.5) |
|
253 | - ")*)". // Allow for 0 or more piped filters |
|
254 | - "#", // End |
|
246 | + "#".// Begin |
|
247 | + "\\$".// Must start with $ |
|
248 | + "([\\w-]+)".// Match alphanumeric characters and dashes |
|
249 | + "((?:\\['?[\\w-]+'?\\])*)".// Optionally match array subscripts i.e. $myVar['index'] |
|
250 | + "((?:".// Optionally match pipe filters i.e. $myVar|myFilter |
|
251 | + "\\|[\\w-]+".// Starting with the | character |
|
252 | + "(\([\w\.,']+\))?".// Filters can have strings and numbers i.e myFilter('string',1,2.5) |
|
253 | + ")*)".// Allow for 0 or more piped filters |
|
254 | + "#", // End |
|
255 | 255 | |
256 | - function( $matches ) use ( $callback, $filters ) { |
|
256 | + function($matches) use ($callback, $filters) { |
|
257 | 257 | // If this variable is an array, get the subscripts |
258 | - if( '' !== $matches[2] ) |
|
258 | + if ('' !== $matches[2]) |
|
259 | 259 | { |
260 | 260 | preg_match_all('/[\w-]+/i', $matches[2], $subscripts); |
261 | 261 | } |
262 | 262 | |
263 | - $val = call_user_func_array( $callback, array($matches[1],@$subscripts[0]) ); |
|
263 | + $val = call_user_func_array($callback, array($matches[1], @$subscripts[0])); |
|
264 | 264 | |
265 | 265 | // Apply custom filters |
266 | - if( '' !== $matches[3] ) |
|
266 | + if ('' !== $matches[3]) |
|
267 | 267 | { |
268 | - $val = $this->apply_filters( substr($matches[3], 1), $val, $filters ); |
|
268 | + $val = $this->apply_filters(substr($matches[3], 1), $val, $filters); |
|
269 | 269 | } |
270 | 270 | |
271 | 271 | return $val; |
@@ -280,24 +280,24 @@ discard block |
||
280 | 280 | * @param array $filters Array of callback functions |
281 | 281 | * @return string The value after all filters have been applied |
282 | 282 | */ |
283 | - protected function apply_filters( $filters_string, $value, $filters ) |
|
283 | + protected function apply_filters($filters_string, $value, $filters) |
|
284 | 284 | { |
285 | - foreach( explode( '|', $filters_string) as $filter ) |
|
285 | + foreach (explode('|', $filters_string) as $filter) |
|
286 | 286 | { |
287 | - $args = array( $value ); |
|
287 | + $args = array($value); |
|
288 | 288 | |
289 | - if( false !== strrpos( $filters_string, "(" ) ) |
|
289 | + if (false !== strrpos($filters_string, "(")) |
|
290 | 290 | { |
291 | - $pieces = explode( '(', $filter ); |
|
291 | + $pieces = explode('(', $filter); |
|
292 | 292 | $filter = $pieces[0]; |
293 | - $params = explode( ',', str_replace( ')', '', $pieces[1] ) ); |
|
294 | - array_walk( $params, array( $this, 'strtoval' ) ); // Convert string values to actual values |
|
295 | - $args = array_merge( $args, $params ); |
|
293 | + $params = explode(',', str_replace(')', '', $pieces[1])); |
|
294 | + array_walk($params, array($this, 'strtoval')); // Convert string values to actual values |
|
295 | + $args = array_merge($args, $params); |
|
296 | 296 | } |
297 | 297 | |
298 | - if( key_exists( $filter, $filters ) ) |
|
298 | + if (key_exists($filter, $filters)) |
|
299 | 299 | { |
300 | - $value = call_user_func_array( $filters[$filter], $args ); |
|
300 | + $value = call_user_func_array($filters[$filter], $args); |
|
301 | 301 | } |
302 | 302 | } |
303 | 303 | return $value; |
@@ -308,11 +308,11 @@ discard block |
||
308 | 308 | * |
309 | 309 | * @param string $str The string to be converted (passed by reference) |
310 | 310 | */ |
311 | - protected function strtoval( &$str ) |
|
311 | + protected function strtoval(&$str) |
|
312 | 312 | { |
313 | - if( 'false' === strtolower($str) ) $str = false; |
|
314 | - if( 'true' === strtolower($str) ) $str = true; |
|
315 | - if( false !== strrpos( $str, "'" ) ) $str = str_replace ( "'", "", $str ); |
|
316 | - if( is_numeric( $str ) ) $str = floatval( $str ); |
|
313 | + if ('false' === strtolower($str)) $str = false; |
|
314 | + if ('true' === strtolower($str)) $str = true; |
|
315 | + if (false !== strrpos($str, "'")) $str = str_replace("'", "", $str); |
|
316 | + if (is_numeric($str)) $str = floatval($str); |
|
317 | 317 | } |
318 | 318 | } |
@@ -184,7 +184,9 @@ discard block |
||
184 | 184 | } |
185 | 185 | |
186 | 186 | $css = file_get_contents( $style['path'] ); |
187 | - if( $style['minify'] ) $css = $this->minify_css( $css ); |
|
187 | + if( $style['minify'] ) { |
|
188 | + $css = $this->minify_css( $css ); |
|
189 | + } |
|
188 | 190 | |
189 | 191 | // Compile the dynamic CSS |
190 | 192 | $compiled_css = $this->compile_css( |
@@ -310,9 +312,17 @@ discard block |
||
310 | 312 | */ |
311 | 313 | protected function strtoval( &$str ) |
312 | 314 | { |
313 | - if( 'false' === strtolower($str) ) $str = false; |
|
314 | - if( 'true' === strtolower($str) ) $str = true; |
|
315 | - if( false !== strrpos( $str, "'" ) ) $str = str_replace ( "'", "", $str ); |
|
316 | - if( is_numeric( $str ) ) $str = floatval( $str ); |
|
315 | + if( 'false' === strtolower($str) ) { |
|
316 | + $str = false; |
|
317 | + } |
|
318 | + if( 'true' === strtolower($str) ) { |
|
319 | + $str = true; |
|
320 | + } |
|
321 | + if( false !== strrpos( $str, "'" ) ) { |
|
322 | + $str = str_replace ( "'", "", $str ); |
|
323 | + } |
|
324 | + if( is_numeric( $str ) ) { |
|
325 | + $str = floatval( $str ); |
|
326 | + } |
|
317 | 327 | } |
318 | 328 | } |