Passed
Push — develop ( fe986b...95ca01 )
by Aristeides
04:19
created

Kirki_Modules_CSS_Generator::add_prefixes()   B

Complexity

Conditions 9
Paths 2

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 2
nop 1
dl 0
loc 42
rs 7.6924
c 0
b 0
f 0
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     http://opensource.org/licenses/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 ) ) {
0 ignored issues
show
introduced by
The condition is_null(self::google_fonts) is always false.
Loading history...
119
			self::$google_fonts = Kirki_Fonts::get_google_fonts();
120
		}
121
		if ( is_null( self::$backup_fonts ) ) {
0 ignored issues
show
introduced by
The condition is_null(self::backup_fonts) is always false.
Loading history...
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();
0 ignored issues
show
Documentation Bug introduced by
It seems like new self() of type Kirki_Modules_CSS_Generator is incompatible with the declared type object|null 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...
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
		$field_output_classes = apply_filters( "kirki_{$field['kirki_config']}_output_control_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
		if ( array_key_exists( self::$field_type, $field_output_classes ) ) {
175
			$classname = $field_output_classes[ self::$field_type ];
176
		}
177
		$obj = new $classname( $field['kirki_config'], self::$output, self::$value, $field );
178
		return $obj->get_styles();
179
180
	}
181
182
	/**
183
	 * Gets the array of generated styles and creates the minimized, inline CSS.
184
	 *
185
	 * @static
186
	 * @access public
187
	 * @param array $css The CSS definitions array.
188
	 * @return string    The generated CSS.
189
	 */
190
	public static function styles_parse( $css = array() ) {
191
192
		// Pass our styles from the kirki_styles_array filter.
193
		$css = apply_filters( 'kirki_styles_array', $css );
194
195
		// Process the array of CSS properties and produce the final CSS.
196
		$final_css = '';
197
		if ( ! is_array( $css ) || empty( $css ) ) {
198
			return '';
199
		}
200
		foreach ( $css as $media_query => $styles ) {
201
			$final_css .= ( 'global' !== $media_query ) ? $media_query . '{' : '';
202
			foreach ( $styles as $style => $style_array ) {
203
				$css_for_style = '';
204
205
				foreach ( $style_array as $property => $value ) {
206
					if ( is_string( $value ) && '' !== $value ) {
207
						$css_for_style .= $property . ':' . $value . ';';
208
					} elseif ( is_array( $value ) ) {
209
						foreach ( $value as $subvalue ) {
210
							if ( is_string( $subvalue ) && '' !== $subvalue ) {
211
								$css_for_style .= $property . ':' . $subvalue . ';';
212
							}
213
						}
214
					}
215
					$value = ( is_string( $value ) ) ? $value : '';
0 ignored issues
show
Unused Code introduced by
The assignment to $value is dead and can be removed.
Loading history...
216
				}
217
				if ( '' !== $css_for_style ) {
218
					$final_css .= $style . '{' . $css_for_style . '}';
219
				}
220
			}
221
			$final_css .= ( 'global' !== $media_query ) ? '}' : '';
222
		}
223
		return $final_css;
224
	}
225
226
	/**
227
	 * Add prefixes if necessary.
228
	 *
229
	 * @param  array $css The CSS definitions array.
230
	 * @return array
231
	 */
232
	public static function add_prefixes( $css ) {
233
234
		if ( is_array( $css ) ) {
0 ignored issues
show
introduced by
The condition is_array($css) is always true.
Loading history...
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( $property, array(
241
							'border-radius',
242
							'box-shadow',
243
							'box-sizing',
244
							'text-shadow',
245
							'transform',
246
							'background-size',
247
							'transition',
248
							'transition-property',
249
						), true ) ) {
250
							unset( $css[ $media_query ][ $element ][ $property ] );
251
							$css[ $media_query ][ $element ][ '-webkit-' . $property ] = $value;
252
							$css[ $media_query ][ $element ][ '-moz-' . $property ]    = $value;
253
							$css[ $media_query ][ $element ][ $property ]              = $value;
254
						}
255
256
						// Add -ms-* and -o-*.
257
						if ( is_string( $property ) && in_array( $property, array(
258
							'transform',
259
							'background-size',
260
							'transition',
261
							'transition-property',
262
						), true ) ) {
263
							unset( $css[ $media_query ][ $element ][ $property ] );
264
							$css[ $media_query ][ $element ][ '-ms-' . $property ] = $value;
265
							$css[ $media_query ][ $element ][ '-o-' . $property ]  = $value;
266
							$css[ $media_query ][ $element ][ $property ]          = $value;
267
						}
268
					}
269
				}
270
			} // End foreach().
271
		} // End if().
272
273
		return $css;
274
275
	}
276
}
277