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.3 |
||
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 | * retrieved by the value callback function. The function is passed the variable |
||
21 | * name without the dollar sign, which can be used with get_option() or |
||
22 | * 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 $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() |
||
47 | { |
||
48 | if (null === static::$instance) |
||
49 | { |
||
50 | static::$instance = new static(); |
||
51 | } |
||
52 | return static::$instance; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Enqueue the PHP script used for compiling dynamic stylesheets that are |
||
57 | * loaded externally |
||
58 | */ |
||
59 | public function wp_enqueue_style() |
||
60 | { |
||
61 | // Only enqueue if there is at least one dynamic stylesheet that is |
||
62 | // set to be loaded externally |
||
63 | if( 0 < count( array_filter($this->stylesheets, array( $this, 'filter_external' ) ) ) ) |
||
64 | { |
||
65 | wp_enqueue_style( 'wp-dynamic-css', admin_url( 'admin-ajax.php?action=wp_dynamic_css' ) ); |
||
66 | } |
||
67 | } |
||
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() |
||
74 | { |
||
75 | $compiled_css = ''; |
||
76 | $styles = array_filter($this->stylesheets, array( $this, 'filter_print' ) ); |
||
77 | |||
78 | // Bail if there are no styles to be printed |
||
79 | if( count( $styles ) === 0 ) return; |
||
80 | |||
81 | View Code Duplication | foreach( $styles as $style ) |
|
82 | { |
||
83 | $css = file_get_contents( $style['path'] ); |
||
84 | $compiled_css .= $this->compile_css( $css, $this->callbacks[$style['handle']] )."\n"; |
||
85 | } |
||
86 | |||
87 | echo "<style id=\"wp-dynamic-css\">\n"; |
||
88 | include 'style.phtml'; |
||
89 | echo "</style>"; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Parse all styles in $this->stylesheets and print them if the flag 'print' |
||
94 | * is not set to true. Used for loading styles externally via an http request. |
||
95 | */ |
||
96 | public function compile_external_styles() |
||
97 | { |
||
98 | header( "Content-type: text/css; charset: UTF-8" ); |
||
99 | $compiled_css = ''; |
||
100 | $styles = array_filter($this->stylesheets, array( $this, 'filter_external' ) ); |
||
101 | |||
102 | View Code Duplication | foreach( $styles as $style ) |
|
103 | { |
||
104 | $css = file_get_contents( $style['path'] ); |
||
105 | $compiled_css .= $this->compile_css( $css, $this->callbacks[$style['handle']] )."\n"; |
||
106 | } |
||
107 | |||
108 | include 'style.phtml'; |
||
109 | wp_die(); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Add a style path to the pool of styles to be compiled |
||
114 | * |
||
115 | * @param string $handle The stylesheet's name/id |
||
116 | * @param string $path The absolute path to the dynamic style |
||
117 | * @param boolean $print Whether to print the compiled CSS to the document |
||
118 | * head, or include it as an external CSS file |
||
119 | */ |
||
120 | public function enqueue_style( $handle, $path, $print ) |
||
121 | { |
||
122 | $this->stylesheets[] = array( |
||
123 | 'handle'=> $handle, |
||
124 | 'path' => $path, |
||
125 | 'print' => $print |
||
126 | ); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Register a value retrieval function and associate it with the given handle |
||
131 | * |
||
132 | * @param type $handle The stylesheet's name/id |
||
133 | * @param type $callback |
||
134 | */ |
||
135 | public function register_callback( $handle, $callback ) |
||
136 | { |
||
137 | $this->callbacks[$handle] = $callback; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * This filter is used to return only the styles that are set to be printed |
||
142 | * in the document head |
||
143 | * |
||
144 | * @param array $style |
||
145 | * @return boolean |
||
146 | */ |
||
147 | protected function filter_print( $style ) |
||
148 | { |
||
149 | return true === $style['print']; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * This filter is used to return only the styles that are set to be loaded |
||
154 | * externally |
||
155 | * |
||
156 | * @param array $style |
||
157 | * @return boolean |
||
158 | */ |
||
159 | protected function filter_external( $style ) |
||
160 | { |
||
161 | return true !== $style['print']; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Parse the given CSS string by converting the variables to their |
||
166 | * corresponding values retrieved by applying the callback function |
||
167 | * |
||
168 | * @param callable $callback A function that replaces the variables with |
||
169 | * their values. The function accepts the variable's name as a parameter |
||
170 | * @param string $css A string containing dynamic CSS (pre-compiled CSS with |
||
171 | * variables) |
||
172 | * @return string The compiled CSS after converting the variables to their |
||
173 | * corresponding values |
||
174 | */ |
||
175 | protected function compile_css( $css, $callback ) |
||
176 | { |
||
177 | return preg_replace_callback( "#\\$([\\w-]+)((?:\\['?[\\w-]+'?\\])*)#", function( $matches ) use ( $callback ) { |
||
178 | // If this variable is an array, get the subscripts |
||
179 | if( '' !== $matches[2] ) |
||
180 | { |
||
181 | preg_match_all('/[\w-]+/i', $matches[2], $subscripts); |
||
182 | } |
||
183 | return call_user_func_array( $callback, array($matches[1],@$subscripts[0]) ); |
||
0 ignored issues
–
show
|
|||
184 | }, $css); |
||
185 | } |
||
186 | } |
||
187 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: