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 |
||
0 ignored issues
–
show
|
|||
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) |
||
0 ignored issues
–
show
Since
$instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self , or increasing the visibility of $instance to at least protected.
Let’s assume you have a class which uses late-static binding: class YourClass
{
private static $someVariable;
public static function getSomeVariable()
{
return static::$someVariable;
}
}
The code above will run fine in your PHP runtime. However, if you now create a
sub-class and call the class YourSubClass extends YourClass { }
YourSubClass::getSomeVariable(); // Will cause an access error.
In the case above, it makes sense to update class SomeClass
{
private static $someVariable;
public static function getSomeVariable()
{
return self::$someVariable; // self works fine with private.
}
}
![]() |
|||
49 | { |
||
50 | static::$instance = new static(); |
||
0 ignored issues
–
show
Since
$instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self , or increasing the visibility of $instance to at least protected.
Let’s assume you have a class which uses late-static binding: class YourClass
{
private static $someVariable;
public static function getSomeVariable()
{
return static::$someVariable;
}
}
The code above will run fine in your PHP runtime. However, if you now create a
sub-class and call the class YourSubClass extends YourClass { }
YourSubClass::getSomeVariable(); // Will cause an access error.
In the case above, it makes sense to update class SomeClass
{
private static $someVariable;
public static function getSomeVariable()
{
return self::$someVariable; // self works fine with private.
}
}
![]() It seems like
new static() of type this<DynamicCSSCompiler> is incompatible with the declared type object<Singleton> of property $instance .
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. ![]() |
|||
51 | } |
||
52 | return static::$instance; |
||
0 ignored issues
–
show
Since
$instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self , or increasing the visibility of $instance to at least protected.
Let’s assume you have a class which uses late-static binding: class YourClass
{
private static $someVariable;
public static function getSomeVariable()
{
return static::$someVariable;
}
}
The code above will run fine in your PHP runtime. However, if you now create a
sub-class and call the class YourSubClass extends YourClass { }
YourSubClass::getSomeVariable(); // Will cause an access error.
In the case above, it makes sense to update class SomeClass
{
private static $someVariable;
public static function getSomeVariable()
{
return self::$someVariable; // self works fine with private.
}
}
![]() |
|||
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 | $styles = array_filter($this->stylesheets, array( $this, 'filter_print' ) ); |
||
76 | |||
77 | // Bail if there are no styles to be printed |
||
78 | if( count( $styles ) === 0 ) return; |
||
79 | |||
80 | $compiled_css = $this->compile_styles( $styles ); |
||
81 | |||
82 | echo "<style id=\"wp-dynamic-css\">\n"; |
||
83 | include 'style.phtml'; |
||
84 | echo "</style>"; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Parse all styles in $this->stylesheets and print them if the flag 'print' |
||
89 | * is not set to true. Used for loading styles externally via an http request. |
||
90 | */ |
||
91 | public function compile_external_styles() |
||
92 | { |
||
93 | header( "Content-type: text/css; charset: UTF-8" ); |
||
94 | header( "Cache-Control: no-cache, must-revalidate" ); //set headers to NOT cache a page so that changes to options are reflected immediately |
||
95 | |||
96 | $compiled_css = $this->compile_styles( array_filter($this->stylesheets, array( $this, 'filter_external' ) ) ); |
||
97 | |||
98 | include 'style.phtml'; |
||
99 | wp_die(); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Add a style path to the pool of styles to be compiled |
||
104 | * |
||
105 | * @param string $handle The stylesheet's name/id |
||
106 | * @param string $path The absolute path to the dynamic style |
||
107 | * @param boolean $print Whether to print the compiled CSS to the document |
||
108 | * head, or include it as an external CSS file |
||
109 | */ |
||
110 | public function enqueue_style( $handle, $path, $print ) |
||
111 | { |
||
112 | $this->stylesheets[] = array( |
||
113 | 'handle'=> $handle, |
||
114 | 'path' => $path, |
||
115 | 'print' => $print |
||
116 | ); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Register a value retrieval function and associate it with the given handle |
||
121 | * |
||
122 | * @param type $handle The stylesheet's name/id |
||
123 | * @param type $callback |
||
124 | */ |
||
125 | public function register_callback( $handle, $callback ) |
||
126 | { |
||
127 | $this->callbacks[$handle] = $callback; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Compile multiple dynamic stylesheets |
||
132 | * |
||
133 | * @param array $styles List of styles with the same structure as they are |
||
134 | * stored in $this->stylesheets |
||
135 | * @return string Compiled CSS |
||
136 | */ |
||
137 | protected function compile_styles( $styles ) |
||
138 | { |
||
139 | $compiled_css = ''; |
||
140 | foreach( $styles as $style ) |
||
141 | { |
||
142 | $css = file_get_contents( $style['path'] ); |
||
143 | $compiled_css .= $this->compile_css( $css, $this->callbacks[$style['handle']] )."\n"; |
||
144 | } |
||
145 | return $compiled_css; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * This filter is used to return only the styles that are set to be printed |
||
150 | * in the document head |
||
151 | * |
||
152 | * @param array $style |
||
153 | * @return boolean |
||
154 | */ |
||
155 | protected function filter_print( $style ) |
||
156 | { |
||
157 | return true === $style['print']; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * This filter is used to return only the styles that are set to be loaded |
||
162 | * externally |
||
163 | * |
||
164 | * @param array $style |
||
165 | * @return boolean |
||
166 | */ |
||
167 | protected function filter_external( $style ) |
||
168 | { |
||
169 | return true !== $style['print']; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Parse the given CSS string by converting the variables to their |
||
174 | * corresponding values retrieved by applying the callback function |
||
175 | * |
||
176 | * @param callable $callback A function that replaces the variables with |
||
177 | * their values. The function accepts the variable's name as a parameter |
||
178 | * @param string $css A string containing dynamic CSS (pre-compiled CSS with |
||
179 | * variables) |
||
180 | * @return string The compiled CSS after converting the variables to their |
||
181 | * corresponding values |
||
182 | */ |
||
183 | protected function compile_css( $css, $callback ) |
||
184 | { |
||
185 | return preg_replace_callback( "#\\$([\\w-]+)((?:\\['?[\\w-]+'?\\])*)#", function( $matches ) use ( $callback ) { |
||
186 | // If this variable is an array, get the subscripts |
||
187 | if( '' !== $matches[2] ) |
||
188 | { |
||
189 | preg_match_all('/[\w-]+/i', $matches[2], $subscripts); |
||
190 | } |
||
191 | return call_user_func_array( $callback, array($matches[1],@$subscripts[0]) ); |
||
0 ignored issues
–
show
The variable
$subscripts does not seem to be defined for all execution paths leading up to this point.
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: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
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
![]() |
|||
192 | }, $css); |
||
193 | } |
||
194 | } |
||
195 |
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.