1 | <?php |
||
2 | /** |
||
3 | * Generates the styles for the frontend. |
||
4 | * Handles the 'output' argument of fields |
||
5 | * |
||
6 | * @package Kirki |
||
7 | * @category Core |
||
8 | * @author Aristeides Stathopoulos |
||
9 | * @copyright Copyright (c) 2017, Aristeides Stathopoulos |
||
10 | * @license https://opensource.org/licenses/MIT |
||
11 | * @since 1.0 |
||
12 | */ |
||
13 | |||
14 | // Exit if accessed directly. |
||
15 | if ( ! defined( 'ABSPATH' ) ) { |
||
16 | exit; |
||
17 | } |
||
18 | |||
19 | /** |
||
20 | * Handles CSS output. |
||
21 | */ |
||
22 | final class Kirki_Modules_CSS_Generator { |
||
23 | |||
24 | /** |
||
25 | * The instance of this class (singleton pattern). |
||
26 | * |
||
27 | * @static |
||
28 | * @access public |
||
29 | * @var null|object |
||
30 | */ |
||
31 | public static $instance = null; |
||
32 | |||
33 | /** |
||
34 | * Settings. |
||
35 | * |
||
36 | * @static |
||
37 | * @access public |
||
38 | * @var null|string|array |
||
39 | */ |
||
40 | public static $settings = null; |
||
41 | |||
42 | /** |
||
43 | * Output. |
||
44 | * |
||
45 | * @static |
||
46 | * @access public |
||
47 | * @var array |
||
48 | */ |
||
49 | public static $output = array(); |
||
50 | |||
51 | /** |
||
52 | * Callback. |
||
53 | * |
||
54 | * @static |
||
55 | * @access public |
||
56 | * @var null|string|array |
||
57 | */ |
||
58 | public static $callback = null; |
||
59 | |||
60 | /** |
||
61 | * Option Name. |
||
62 | * |
||
63 | * @static |
||
64 | * @access public |
||
65 | * @var null|string |
||
66 | */ |
||
67 | public static $option_name = null; |
||
68 | |||
69 | /** |
||
70 | * Field Type. |
||
71 | * |
||
72 | * @static |
||
73 | * @access public |
||
74 | * @var string |
||
75 | */ |
||
76 | public static $field_type = null; |
||
77 | |||
78 | /** |
||
79 | * Google Fonts |
||
80 | * |
||
81 | * @static |
||
82 | * @access public |
||
83 | * @var array |
||
84 | */ |
||
85 | public static $google_fonts = null; |
||
86 | |||
87 | /** |
||
88 | * Standard Fonts |
||
89 | * |
||
90 | * @static |
||
91 | * @access public |
||
92 | * @var array |
||
93 | */ |
||
94 | public static $backup_fonts = null; |
||
95 | |||
96 | /** |
||
97 | * CSS |
||
98 | * |
||
99 | * @static |
||
100 | * @access public |
||
101 | * @var string |
||
102 | */ |
||
103 | public static $css; |
||
104 | |||
105 | /** |
||
106 | * Value |
||
107 | * |
||
108 | * @static |
||
109 | * @access public |
||
110 | * @var mixed |
||
111 | */ |
||
112 | public static $value = null; |
||
113 | |||
114 | /** |
||
115 | * The class constructor. |
||
116 | */ |
||
117 | private function __construct() { |
||
118 | if ( is_null( self::$google_fonts ) ) { |
||
119 | self::$google_fonts = Kirki_Fonts::get_google_fonts(); |
||
120 | } |
||
121 | if ( is_null( self::$backup_fonts ) ) { |
||
122 | self::$backup_fonts = Kirki_Fonts::get_backup_fonts(); |
||
123 | } |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Get a single instance of this class |
||
128 | * |
||
129 | * @return object |
||
130 | */ |
||
131 | public static function get_instance() { |
||
132 | if ( null === self::$instance ) { |
||
133 | self::$instance = new self(); |
||
134 | } |
||
135 | return self::$instance; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Get the CSS for a field. |
||
140 | * |
||
141 | * @static |
||
142 | * @access public |
||
143 | * @param array $field The field. |
||
144 | * @return array |
||
145 | */ |
||
146 | public static function css( $field ) { |
||
147 | |||
148 | // Set class vars. |
||
149 | self::$settings = $field['settings']; |
||
150 | self::$callback = $field['sanitize_callback']; |
||
151 | self::$field_type = $field['type']; |
||
152 | self::$output = $field['output']; |
||
153 | if ( ! is_array( self::$output ) ) { |
||
154 | self::$output = array( |
||
155 | array( |
||
156 | 'element' => self::$output, |
||
157 | 'sanitize_callback' => null, |
||
158 | ), |
||
159 | ); |
||
160 | } |
||
161 | |||
162 | // Get the value of this field. |
||
163 | self::$value = Kirki_Values::get_sanitized_field_value( $field ); |
||
164 | |||
165 | // Find the class that will handle the outpout for this field. |
||
166 | $classname = 'Kirki_Output'; |
||
167 | $default_classnames = array( |
||
168 | 'kirki-background' => 'Kirki_Output_Field_Background', |
||
169 | 'kirki-dimensions' => 'Kirki_Output_Field_Dimensions', |
||
170 | 'kirki-image' => 'Kirki_Output_Field_Image', |
||
171 | 'kirki-typography' => 'Kirki_Output_Field_Typography', |
||
172 | 'kirki-multicolor' => 'Kirki_Output_Field_Multicolor', |
||
173 | ); |
||
174 | $field_output_classes = apply_filters( "kirki_output_control_classnames", $default_classnames ); |
||
0 ignored issues
–
show
|
|||
175 | $field_output_classes = apply_filters( "kirki_{$field['kirki_config']}_output_control_classnames", $field_output_classes ); |
||
176 | if ( array_key_exists( self::$field_type, $field_output_classes ) ) { |
||
177 | $classname = $field_output_classes[ self::$field_type ]; |
||
178 | } |
||
179 | $obj = new $classname( $field['kirki_config'], self::$output, self::$value, $field ); |
||
180 | return $obj->get_styles(); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Gets the array of generated styles and creates the minimized, inline CSS. |
||
185 | * |
||
186 | * @static |
||
187 | * @access public |
||
188 | * @param array $css The CSS definitions array. |
||
189 | * @return string The generated CSS. |
||
190 | */ |
||
191 | public static function styles_parse( $css = array() ) { |
||
192 | |||
193 | // Pass our styles from the kirki_styles_array filter. |
||
194 | $css = apply_filters( 'kirki_styles_array', $css ); |
||
195 | |||
196 | // Process the array of CSS properties and produce the final CSS. |
||
197 | $final_css = ''; |
||
198 | if ( ! is_array( $css ) || empty( $css ) ) { |
||
199 | return ''; |
||
200 | } |
||
201 | foreach ( $css as $media_query => $styles ) { |
||
202 | $final_css .= ( 'global' !== $media_query ) ? $media_query . '{' : ''; |
||
203 | foreach ( $styles as $style => $style_array ) { |
||
204 | $css_for_style = ''; |
||
205 | |||
206 | foreach ( $style_array as $property => $value ) { |
||
207 | if ( is_string( $value ) && '' !== $value ) { |
||
208 | $css_for_style .= $property . ':' . $value . ';'; |
||
209 | } elseif ( is_array( $value ) ) { |
||
210 | foreach ( $value as $subvalue ) { |
||
211 | if ( is_string( $subvalue ) && '' !== $subvalue ) { |
||
212 | $css_for_style .= $property . ':' . $subvalue . ';'; |
||
213 | } |
||
214 | } |
||
215 | } |
||
216 | $value = ( is_string( $value ) ) ? $value : ''; |
||
217 | } |
||
218 | if ( '' !== $css_for_style ) { |
||
219 | $final_css .= $style . '{' . $css_for_style . '}'; |
||
220 | } |
||
221 | } |
||
222 | $final_css .= ( 'global' !== $media_query ) ? '}' : ''; |
||
223 | } |
||
224 | return $final_css; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Add prefixes if necessary. |
||
229 | * |
||
230 | * @param array $css The CSS definitions array. |
||
231 | * @return array |
||
232 | */ |
||
233 | public static function add_prefixes( $css ) { |
||
234 | if ( is_array( $css ) ) { |
||
235 | foreach ( $css as $media_query => $elements ) { |
||
236 | foreach ( $elements as $element => $style_array ) { |
||
237 | foreach ( $style_array as $property => $value ) { |
||
238 | |||
239 | // Add -webkit-* and -moz-*. |
||
240 | if ( is_string( $property ) && in_array( |
||
241 | $property, array( |
||
242 | 'border-radius', |
||
243 | 'box-shadow', |
||
244 | 'box-sizing', |
||
245 | 'text-shadow', |
||
246 | 'transform', |
||
247 | 'background-size', |
||
248 | 'transition', |
||
249 | 'transition-property', |
||
250 | ), true |
||
251 | ) ) { |
||
252 | unset( $css[ $media_query ][ $element ][ $property ] ); |
||
253 | $css[ $media_query ][ $element ][ '-webkit-' . $property ] = $value; |
||
254 | $css[ $media_query ][ $element ][ '-moz-' . $property ] = $value; |
||
255 | $css[ $media_query ][ $element ][ $property ] = $value; |
||
256 | } |
||
257 | |||
258 | // Add -ms-* and -o-*. |
||
259 | if ( is_string( $property ) && in_array( |
||
260 | $property, array( |
||
261 | 'transform', |
||
262 | 'background-size', |
||
263 | 'transition', |
||
264 | 'transition-property', |
||
265 | ), true |
||
266 | ) ) { |
||
267 | unset( $css[ $media_query ][ $element ][ $property ] ); |
||
268 | $css[ $media_query ][ $element ][ '-ms-' . $property ] = $value; |
||
269 | $css[ $media_query ][ $element ][ '-o-' . $property ] = $value; |
||
270 | $css[ $media_query ][ $element ][ $property ] = $value; |
||
271 | } |
||
272 | } |
||
273 | } |
||
274 | } |
||
275 | } |
||
276 | return $css; |
||
277 | } |
||
278 | } |
||
279 |
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.