askupasoftware /
wp-dynamic-css
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.1 |
||
| 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 | * 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) |
||
|
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.
}
}
Loading history...
|
|||
| 44 | { |
||
| 45 | 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.
}
}
Loading history...
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.. Loading history...
|
|||
| 46 | } |
||
| 47 | 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.
}
}
Loading history...
|
|||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Enqueue the PHP script used for compiling dynamic stylesheets that are |
||
| 52 | * loaded externally |
||
| 53 | */ |
||
| 54 | public function wp_enqueue_style() |
||
| 55 | { |
||
| 56 | // Only enqueue if there is at least one dynamic stylesheet that is |
||
| 57 | // set to be loaded externally |
||
| 58 | if( 0 < count( array_filter($this->stylesheets, array( $this, 'filter_external' ) ) ) ) |
||
| 59 | { |
||
| 60 | wp_enqueue_style( 'wp-dynamic-css', admin_url( 'admin-ajax.php?action=wp_dynamic_css' ) ); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Parse all styles in $this->stylesheets and print them if the flag 'print' |
||
| 66 | * is set to true. Used for printing styles to the document head. |
||
| 67 | */ |
||
| 68 | public function compile_printed_styles() |
||
| 69 | { |
||
| 70 | $precompiled_css = ''; |
||
| 71 | $styles = array_filter($this->stylesheets, array( $this, 'filter_print' ) ); |
||
| 72 | |||
| 73 | // Bail if there are no styles to be printed |
||
| 74 | if( count( $styles ) === 0 ) return; |
||
| 75 | |||
| 76 | foreach( $styles as $style ) |
||
| 77 | { |
||
| 78 | $precompiled_css .= $this->get_file_contents( $style['path'] )."\n"; |
||
| 79 | } |
||
| 80 | $css = $this->compile_css( $precompiled_css ); |
||
| 81 | echo "<style id=\"wp-dynamic-css\">\n"; |
||
| 82 | include 'style.phtml'; |
||
| 83 | echo "</style>"; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Parse all styles in $this->stylesheets and print them if the flag 'print' |
||
| 88 | * is not set to true. Used for loading styles externally via an http request. |
||
| 89 | */ |
||
| 90 | public function compile_external_styles() |
||
| 91 | { |
||
| 92 | header( "Content-type: text/css; charset: UTF-8" ); |
||
| 93 | $precompiled_css = ''; |
||
| 94 | $styles = array_filter($this->stylesheets, array( $this, 'filter_external' ) ); |
||
| 95 | |||
| 96 | foreach( $styles as $style ) |
||
| 97 | { |
||
| 98 | $precompiled_css .= $this->get_file_contents( $style['path'] )."\n"; |
||
| 99 | } |
||
| 100 | $css = $this->compile_css( $precompiled_css ); |
||
| 101 | include 'style.phtml'; |
||
| 102 | wp_die(); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Add a style path to the pool of styles to be compiled |
||
| 107 | * |
||
| 108 | * @param string $path The absolute path to the dynamic style |
||
| 109 | * @param boolean $print Whether to print the compiled CSS to the document |
||
| 110 | * head, or include it as an external CSS file |
||
| 111 | */ |
||
| 112 | public function enqueue_style( $path, $print ) |
||
| 113 | { |
||
| 114 | $this->stylesheets[] = array( |
||
| 115 | 'path' => $path, |
||
| 116 | 'print' => $print |
||
| 117 | ); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * This filter is used to return only the styles that are set to be printed |
||
| 122 | * in the document head |
||
| 123 | * |
||
| 124 | * @param array $style |
||
| 125 | * @return boolean |
||
| 126 | */ |
||
| 127 | protected function filter_print( $style ) |
||
| 128 | { |
||
| 129 | return true === $style['print']; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * This filter is used to return only the styles that are set to be loaded |
||
| 134 | * externally |
||
| 135 | * |
||
| 136 | * @param array $style |
||
| 137 | * @return boolean |
||
| 138 | */ |
||
| 139 | protected function filter_external( $style ) |
||
| 140 | { |
||
| 141 | return true !== $style['print']; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get the contents of a given file |
||
| 146 | * |
||
| 147 | * @param string $path The absolute path to the file |
||
| 148 | * @return string The file contents |
||
| 149 | */ |
||
| 150 | protected function get_file_contents( $path ) |
||
| 151 | { |
||
| 152 | ob_start(); |
||
| 153 | include $path; |
||
| 154 | return ob_get_clean(); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Parse the given CSS string by converting the variables to their |
||
| 159 | * corresponding values retrieved by applying the filter |
||
| 160 | * wp_dynamic_css_get_variable_value. |
||
| 161 | * |
||
| 162 | * @param string $css A string containing dynamic CSS (pre-compiled CSS with |
||
| 163 | * variables) |
||
| 164 | * @uses wp_dynamic_css_get_variable_value filter |
||
| 165 | * @return string The compiled CSS after converting the variables to their |
||
| 166 | * corresponding values |
||
| 167 | */ |
||
| 168 | protected function compile_css( $css ) |
||
| 169 | { |
||
| 170 | return preg_replace_callback( '#\$([\w]+)#', function( $matches ) { |
||
| 171 | return apply_filters( 'wp_dynamic_css_get_variable_value', $matches[1] ); |
||
| 172 | }, $css); |
||
| 173 | } |
||
| 174 | } |
||
| 175 |
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.