These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * @package WordPress Dynamic CSS |
||
4 | * @version 1.0.0 |
||
5 | * @author Askupa Software <[email protected]> |
||
6 | * @link https://github.com/askupasoftware/wp-dynamic-css |
||
7 | * @copyright 2016 Askupa Software |
||
8 | */ |
||
9 | |||
10 | /** |
||
11 | * Dynamic CSS Compiler Utility Class |
||
12 | * |
||
13 | * |
||
14 | * Dynamic CSS Syntax |
||
15 | * ------------------ |
||
16 | * <pre> |
||
17 | * body {color: $body_color;} |
||
18 | * </pre> |
||
19 | * In the above example, the variable $body_color is replaced by a value |
||
20 | * that is retrieved by the filter wp_dynamic_css_get_variable_value. The filter |
||
21 | * is passed the variable name without the dollar sign, which can be used with |
||
22 | * get_option() or get_theme_mod() etc. |
||
23 | */ |
||
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 $styles = array(); |
||
35 | |||
36 | /** |
||
37 | * Returns the *Singleton* instance of this class. |
||
38 | * |
||
39 | * @return Singleton The *Singleton* instance. |
||
40 | */ |
||
41 | public static function get_instance() |
||
42 | { |
||
43 | if (null === static::$instance) |
||
44 | { |
||
45 | static::$instance = new static(); |
||
46 | static::$instance->init(); |
||
0 ignored issues
–
show
|
|||
47 | } |
||
48 | |||
49 | return static::$instance; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Initiate the compiler by hooking to wp_print_styles |
||
54 | */ |
||
55 | public function init() |
||
56 | { |
||
57 | add_action('wp_print_styles', array($this, 'print_compiled_style')); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Add a style path to the pool of styles to be compiled |
||
62 | * |
||
63 | * @param type $path The absolute path to the dynamic style |
||
64 | */ |
||
65 | public function enqueue_style($path) |
||
66 | { |
||
67 | $this->styles[] = $path; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Parse all styles in $this->styles and print them |
||
72 | */ |
||
73 | public function print_compiled_style() |
||
74 | { |
||
75 | ob_start(); |
||
76 | foreach ($this->styles as $style) |
||
77 | { |
||
78 | include $style; |
||
79 | echo "\n"; |
||
80 | } |
||
81 | $css = $this->parse_css(ob_get_clean()); |
||
82 | include 'style.phtml'; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Parse the given CSS string by converting the variables to their |
||
87 | * corresponding values retrieved by applying the filter |
||
88 | * wp_dynamic_css_get_variable_value. |
||
89 | * |
||
90 | * @param string $css A string containing dynamic CSS (pre-compiled CSS with |
||
91 | * variables) |
||
92 | * @uses wp_dynamic_css_get_variable_value filter |
||
93 | * @return string The compiled CSS after converting the variables to their |
||
94 | * corresponding values |
||
95 | */ |
||
96 | public function parse_css($css) |
||
97 | { |
||
98 | return preg_replace_callback('#\$([\w]+)#', function($matches) { |
||
99 | return apply_filters('wp_dynamic_css_get_variable_value', $matches[1]); |
||
100 | }, $css); |
||
101 | } |
||
102 | } |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: