Completed
Pull Request — develop (#2017)
by Tim
12:25
created

Kirki_Modules_CSS::get_enqueue_fa()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
/**
3
 * Handles the CSS Output of fields.
4
 *
5
 * @package     Kirki
6
 * @category    Modules
7
 * @author      Aristeides Stathopoulos
8
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
9
 * @license    https://opensource.org/licenses/MIT
10
 * @since       3.0.0
11
 */
12
13
/**
14
 * The Kirki_Modules_CSS object.
15
 */
16
class Kirki_Modules_CSS {
17
18
	/**
19
	 * The object instance.
20
	 *
21
	 * @static
22
	 * @access private
23
	 * @since 3.0.0
24
	 * @var object
25
	 */
26
	private static $instance;
27
28
	/**
29
	 * Whether we've already processed this or not.
30
	 *
31
	 * @access public
32
	 * @var bool
33
	 */
34
	public $processed = false;
35
36
	/**
37
	 * The CSS array
38
	 *
39
	 * @access public
40
	 * @var array
41
	 */
42
	public static $css_array = array();
43
44
	/**
45
	 * Set to true if you want to use the AJAX method.
46
	 *
47
	 * @access public
48
	 * @var bool
49
	 */
50
	public static $ajax = false;
51
52
	/**
53
	 * The Kirki_CSS_To_File object.
54
	 *
55
	 * @access protected
56
	 * @since 3.0.0
57
	 * @var object
58
	 */
59
	protected $css_to_file;
60
61
	/**
62
	 * Should we enqueue font-awesome?
63
	 *
64
	 * @static
65
	 * @access protected
66
	 * @since 3.0.26
67
	 * @var bool
68
	 */
69
	protected static $enqueue_fa = false;
70
71
	/**
72
	 * Constructor
73
	 *
74
	 * @access protected
75
	 */
76
	protected function __construct() {
77
		$class_files = array(
78
			'Kirki_CSS_To_File'                         => '/class-kirki-css-to-file.php',
79
			'Kirki_Modules_CSS_Generator'               => '/class-kirki-modules-css-generator.php',
80
			'Kirki_Output'                              => '/class-kirki-output.php',
81
			'Kirki_Output_Field_Background'             => '/field/class-kirki-output-field-background.php',
82
			'Kirki_Output_Field_Image'                  => '/field/class-kirki-output-field-image.php',
83
			'Kirki_Output_Field_Multicolor'             => '/field/class-kirki-output-field-multicolor.php',
84
			'Kirki_Output_Field_Dimensions'             => '/field/class-kirki-output-field-dimensions.php',
85
			'Kirki_Output_Field_Typography'             => '/field/class-kirki-output-field-typography.php',
86
			'Kirki_Output_Property'                     => '/property/class-kirki-output-property.php',
87
			'Kirki_Output_Property_Background_Image'    => '/property/class-kirki-output-property-background-image.php',
88
			'Kirki_Output_Property_Background_Position' => '/property/class-kirki-output-property-background-position.php',
89
			'Kirki_Output_Property_Font_Family'         => '/property/class-kirki-output-property-font-family.php',
90
		);
91
92
		foreach ( $class_files as $class_name => $file ) {
93
			if ( ! class_exists( $class_name ) ) {
94
				include_once wp_normalize_path( dirname( __FILE__ ) . $file );
95
			}
96
		}
97
		add_action( 'init', array( $this, 'init' ) );
98
	}
99
100
	/**
101
	 * Gets an instance of this object.
102
	 * Prevents duplicate instances which avoid artefacts and improves performance.
103
	 *
104
	 * @static
105
	 * @access public
106
	 * @since 3.0.0
107
	 * @return object
108
	 */
109
	public static function get_instance() {
110
		if ( ! self::$instance ) {
111
			self::$instance = new self();
112
		}
113
		return self::$instance;
114
	}
115
116
	/**
117
	 * Init.
118
	 *
119
	 * @access public
120
	 */
121
	public function init() {
122
		global $wp_customize;
123
124
		Kirki_Modules_Webfonts::get_instance();
125
126
		$config   = apply_filters( 'kirki_config', array() );
127
		$priority = 999;
128
		if ( isset( $config['styles_priority'] ) ) {
129
			$priority = absint( $config['styles_priority'] );
130
		}
131
132
		// Allow completely disabling Kirki CSS output.
133
		if ( ( defined( 'KIRKI_NO_OUTPUT' ) && true === KIRKI_NO_OUTPUT ) || ( isset( $config['disable_output'] ) && true === $config['disable_output'] ) ) {
134
			return;
135
		}
136
137
		$method = apply_filters( 'kirki_dynamic_css_method', 'inline' );
138
		if ( $wp_customize ) {
139
140
			// If we're in the customizer, load inline no matter what.
141
			add_action( 'wp_enqueue_scripts', array( $this, 'inline_dynamic_css' ), $priority );
142
143
			// If we're using file method, on save write the new styles.
144
			if ( 'file' === $method ) {
145
				$this->css_to_file = new Kirki_CSS_To_File();
146
				add_action( 'customize_save_after', array( $this->css_to_file, 'write_file' ) );
147
			}
148
			return;
149
		}
150
151
		if ( 'file' === $method ) {
152
153
			// Attempt to write the CSS to file.
154
			$this->css_to_file = new Kirki_CSS_To_File();
155
156
			// If we succesd, load this file.
157
			$failed = get_transient( 'kirki_css_write_to_file_failed' );
158
159
			// If writing CSS to file hasn't failed, just enqueue this file.
160
			if ( ! $failed ) {
161
				add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_compiled_file' ), $priority );
162
				return;
163
			}
164
		}
165
166
		// If we are in the customizer, load CSS using inline-styles.
167
		// If we are in the frontend AND self::$ajax is true, then load dynamic CSS using AJAX.
168
		if ( ( true === self::$ajax ) || ( isset( $config['inline_css'] ) && false === $config['inline_css'] ) ) {
169
			add_action( 'wp_enqueue_scripts', array( $this, 'frontend_styles' ), $priority );
170
			add_action( 'wp_ajax_kirki_dynamic_css', array( $this, 'ajax_dynamic_css' ) );
171
			add_action( 'wp_ajax_nopriv_kirki_dynamic_css', array( $this, 'ajax_dynamic_css' ) );
172
			return;
173
		}
174
175
		// If we got this far then add styles inline.
176
		add_action( 'wp_enqueue_scripts', array( $this, 'inline_dynamic_css' ), $priority );
177
	}
178
179
	/**
180
	 * Enqueues compiled CSS file.
181
	 *
182
	 * @access public
183
	 * @since 3.0.0
184
	 */
185
	public function enqueue_compiled_file() {
186
		wp_enqueue_style( 'kirki-styles', $this->css_to_file->get_url(), array(), $this->css_to_file->get_timestamp() );
187
	}
188
	/**
189
	 * Adds inline styles.
190
	 *
191
	 * @access public
192
	 */
193
	public function inline_dynamic_css() {
194
		$configs = Kirki::$config;
195
		if ( ! $this->processed ) {
196
			foreach ( $configs as $config_id => $args ) {
197
				if ( isset( $args['disable_output'] ) && true === $args['disable_output'] ) {
198
					continue;
199
				}
200
				$styles = self::loop_controls( $config_id );
201
				$styles = apply_filters( "kirki_{$config_id}_dynamic_css", $styles );
202
				if ( ! empty( $styles ) ) {
203
					$stylesheet = apply_filters( "kirki_{$config_id}_stylesheet", false );
204
					if ( $stylesheet ) {
205
						wp_add_inline_style( $stylesheet, $styles );
206
						continue;
207
					}
208
					wp_enqueue_style( 'kirki-styles-' . $config_id, trailingslashit( Kirki::$url ) . 'assets/css/kirki-styles.css', array(), KIRKI_VERSION );
209
					wp_add_inline_style( 'kirki-styles-' . $config_id, $styles );
210
				}
211
			}
212
			$this->processed = true;
213
		}
214
215
		if ( self::$enqueue_fa && apply_filters( 'kirki_load_fontawesome', true ) ) {
216
			wp_enqueue_script( 'kirki-fontawesome-font', 'https://use.fontawesome.com/30858dc40a.js', array(), '4.0.7', true );
217
		}
218
	}
219
220
	/**
221
	 * Get the dynamic-css.php file
222
	 *
223
	 * @access public
224
	 */
225
	public function ajax_dynamic_css() {
226
		require wp_normalize_path( Kirki::$path . '/modules/css/dynamic-css.php' );
227
		exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
228
	}
229
230
	/**
231
	 * Enqueues the ajax stylesheet.
232
	 *
233
	 * @access public
234
	 */
235
	public function frontend_styles() {
236
		wp_enqueue_style( 'kirki-styles-php', admin_url( 'admin-ajax.php' ) . '?action=kirki_dynamic_css', array(), KIRKI_VERSION );
237
	}
238
239
	/**
240
	 * Loop through all fields and create an array of style definitions.
241
	 *
242
	 * @static
243
	 * @access public
244
	 * @param string $config_id The configuration ID.
245
	 */
246
	public static function loop_controls( $config_id ) {
247
248
		// Get an instance of the Kirki_Modules_CSS_Generator class.
249
		// This will make sure google fonts and backup fonts are loaded.
250
		Kirki_Modules_CSS_Generator::get_instance();
251
252
		$fields = Kirki::$fields;
253
		$css    = array();
254
255
		// Early exit if no fields are found.
256
		if ( empty( $fields ) ) {
257
			return;
258
		}
259
260
		foreach ( $fields as $field ) {
261
262
			// Only process fields that belong to $config_id.
263
			if ( $config_id !== $field['kirki_config'] ) {
264
				continue;
265
			}
266
267
			if ( true === apply_filters( "kirki_{$config_id}_css_skip_hidden", true ) ) {
268
269
				// Only continue if field dependencies are met.
270
				if ( ! empty( $field['required'] ) ) {
271
					$valid = true;
272
273
					foreach ( $field['required'] as $requirement ) {
274
						if ( isset( $requirement['setting'] ) && isset( $requirement['value'] ) && isset( $requirement['operator'] ) ) {
275
							$controller_value = Kirki_Values::get_value( $config_id, $requirement['setting'] );
276
							if ( ! Kirki_Helper::compare_values( $controller_value, $requirement['value'], $requirement['operator'] ) ) {
277
								$valid = false;
278
							}
279
						}
280
					}
281
282
					if ( ! $valid ) {
283
						continue;
284
					}
285
				}
286
			}
287
288
			// Only continue if $field['output'] is set.
289
			if ( isset( $field['output'] ) && ! empty( $field['output'] ) ) {
290
				$css = Kirki_Helper::array_replace_recursive( $css, Kirki_Modules_CSS_Generator::css( $field ) );
291
292
				// Add the globals.
293
				if ( isset( self::$css_array[ $config_id ] ) && ! empty( self::$css_array[ $config_id ] ) ) {
294
					Kirki_Helper::array_replace_recursive( $css, self::$css_array[ $config_id ] );
295
				}
296
			}
297
		}
298
299
		$css = apply_filters( "kirki_{$config_id}_styles", $css );
300
301
		if ( is_array( $css ) ) {
302
			return Kirki_Modules_CSS_Generator::styles_parse( Kirki_Modules_CSS_Generator::add_prefixes( $css ) );
303
		}
304
	}
305
306
	/**
307
	 * Runs when we're adding a font-awesome field to allow enqueueing the
308
	 * fontawesome script on the frontend.
309
	 *
310
	 * @static
311
	 * @since 3.0.26
312
	 * @access public
313
	 * @return void
314
	 */
315
	public static function add_fontawesome_script() {
316
		self::$enqueue_fa = true;
317
	}
318
319
	/**
320
	 * Check if FontAwesome should be loaded.
321
	 *
322
	 * @static
323
	 * @since 3.0.26
324
	 * @access public
325
	 * @return void
0 ignored issues
show
Coding Style introduced by
Function return type is void, but function contains return statement
Loading history...
326
	 */
327
	public static function get_enqueue_fa() {
328
		return self::$enqueue_fa;
329
	}
330
}
331