Completed
Pull Request — develop (#1682)
by Aristeides
02:08
created

Kirki_Modules_CSS::loop_controls()   D

Complexity

Conditions 9
Paths 11

Size

Total Lines 38
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 16
nc 11
nop 1
dl 0
loc 38
rs 4.909
c 0
b 0
f 0
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     http://opensource.org/licenses/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
	 * Constructor
63
	 *
64
	 * @access protected
65
	 */
66
	protected function __construct() {
67
68
		$class_files = array(
69
			'Kirki_CSS_To_File'                         => '/class-kirki-css-to-file.php',
70
			'Kirki_Modules_CSS_Generator'               => '/class-kirki-modules-css-generator.php',
71
			'Kirki_Output'                              => '/class-kirki-output.php',
72
			'Kirki_Output_Field_Background'             => '/field/class-kirki-output-field-background.php',
73
			'Kirki_Output_Field_Image'                  => '/field/class-kirki-output-field-image.php',
74
			'Kirki_Output_Field_Multicolor'             => '/field/class-kirki-output-field-multicolor.php',
75
			'Kirki_Output_Field_Dimensions'             => '/field/class-kirki-output-field-dimensions.php',
76
			'Kirki_Output_Field_Typography'             => '/field/class-kirki-output-field-typography.php',
77
			'Kirki_Output_Property'                     => '/property/class-kirki-output-property.php',
78
			'Kirki_Output_Property_Background_Image'    => '/property/class-kirki-output-property-background-image.php',
79
			'Kirki_Output_Property_Background_Position' => '/property/class-kirki-output-property-background-position.php',
80
			'Kirki_Output_Property_Font_Family'         => '/property/class-kirki-output-property-font-family.php',
81
		);
82
83
		foreach ( $class_files as $class_name => $file ) {
84
			if ( ! class_exists( $class_name ) ) {
85
				include_once wp_normalize_path( dirname( __FILE__ ) . $file );
86
			}
87
		}
88
89
		add_action( 'init', array( $this, 'init' ) );
90
91
	}
92
93
	/**
94
	 * Gets an instance of this object.
95
	 * Prevents duplicate instances which avoid artefacts and improves performance.
96
	 *
97
	 * @static
98
	 * @access public
99
	 * @since 3.0.0
100
	 * @return object
101
	 */
102
	public static function get_instance() {
103
		if ( ! self::$instance ) {
104
			self::$instance = new self();
105
		}
106
		return self::$instance;
107
	}
108
109
	/**
110
	 * Init.
111
	 *
112
	 * @access public
113
	 */
114
	public function init() {
115
116
		Kirki_Modules_Webfonts::get_instance();
117
118
		global $wp_customize;
119
120
		$config   = apply_filters( 'kirki/config', array() );
121
		$priority = 999;
122
		if ( isset( $config['styles_priority'] ) ) {
123
			$priority = absint( $config['styles_priority'] );
124
		}
125
126
		// Allow completely disabling Kirki CSS output.
127
		if ( ( defined( 'KIRKI_NO_OUTPUT' ) && true === KIRKI_NO_OUTPUT ) || ( isset( $config['disable_output'] ) && true === $config['disable_output'] ) ) {
128
			return;
129
		}
130
131
		$method = apply_filters( 'kirki/dynamic_css/method', 'inline' );
132
		if ( $wp_customize ) {
133
			// If we're in the customizer, load inline no matter what.
134
			add_action( 'wp_enqueue_scripts', array( $this, 'inline_dynamic_css' ), $priority );
135
136
			// If we're using file method, on save write the new styles.
137
			if ( 'file' === $method ) {
138
				$this->css_to_file = new Kirki_CSS_To_File();
139
				add_action( 'customize_save_after', array( $this->css_to_file, 'write_file' ) );
140
			}
141
			return;
142
		}
143
144
		if ( 'file' === $method ) {
145
			// Attempt to write the CSS to file.
146
			$this->css_to_file = new Kirki_CSS_To_File();
147
			// If we succesd, load this file.
148
			$failed = get_transient( 'kirki_css_write_to_file_failed' );
149
			// If writing CSS to file hasn't failed, just enqueue this file.
150
			if ( ! $failed ) {
151
				add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_compiled_file' ), $priority );
152
				return;
153
			}
154
		}
155
156
		// If we are in the customizer, load CSS using inline-styles.
157
		// If we are in the frontend AND self::$ajax is true, then load dynamic CSS using AJAX.
158
		if ( ( true === self::$ajax ) || ( isset( $config['inline_css'] ) && false === $config['inline_css'] ) ) {
159
			add_action( 'wp_enqueue_scripts', array( $this, 'frontend_styles' ), $priority );
160
			add_action( 'wp_ajax_kirki_dynamic_css', array( $this, 'ajax_dynamic_css' ) );
161
			add_action( 'wp_ajax_nopriv_kirki_dynamic_css', array( $this, 'ajax_dynamic_css' ) );
162
			return;
163
		}
164
165
		// If we got this far then add styles inline.
166
		add_action( 'wp_enqueue_scripts', array( $this, 'inline_dynamic_css' ), $priority );
167
	}
168
169
	/**
170
	 * Enqueues compiled CSS file.
171
	 *
172
	 * @access public
173
	 * @since 3.0.0
174
	 */
175
	public function enqueue_compiled_file() {
176
177
		wp_enqueue_style( 'kirki-styles', $this->css_to_file->get_url(), array(), $this->css_to_file->get_timestamp() );
178
179
	}
180
	/**
181
	 * Adds inline styles.
182
	 *
183
	 * @access public
184
	 */
185
	public function inline_dynamic_css() {
186
		$configs = Kirki::$config;
187
		if ( ! $this->processed ) {
188
			foreach ( $configs as $config_id => $args ) {
189
				if ( isset( $args['disable_output'] ) && true === $args['disable_output'] ) {
190
					continue;
191
				}
192
				$styles = self::loop_controls( $config_id );
193
				$styles = apply_filters( "kirki/{$config_id}/dynamic_css", $styles );
194
				if ( ! empty( $styles ) ) {
195
					wp_enqueue_style( 'kirki-styles-' . $config_id, trailingslashit( Kirki::$url ) . 'assets/css/kirki-styles.css', array(), KIRKI_VERSION );
196
					wp_add_inline_style( 'kirki-styles-' . $config_id, $styles );
197
				}
198
			}
199
			$this->processed = true;
200
		}
201
	}
202
203
	/**
204
	 * Get the dynamic-css.php file
205
	 *
206
	 * @access public
207
	 */
208
	public function ajax_dynamic_css() {
209
		require wp_normalize_path( Kirki::$path . '/modules/css/dynamic-css.php' );
210
		exit;
211
	}
212
213
	/**
214
	 * Enqueues the ajax stylesheet.
215
	 *
216
	 * @access public
217
	 */
218
	public function frontend_styles() {
219
		wp_enqueue_style( 'kirki-styles-php', admin_url( 'admin-ajax.php' ) . '?action=kirki_dynamic_css', array(), KIRKI_VERSION );
220
	}
221
222
	/**
223
	 * Loop through all fields and create an array of style definitions.
224
	 *
225
	 * @static
226
	 * @access public
227
	 * @param string $config_id The configuration ID.
228
	 */
229
	public static function loop_controls( $config_id ) {
230
231
		// Get an instance of the Kirki_Modules_CSS_Generator class.
232
		// This will make sure google fonts and backup fonts are loaded.
233
		Kirki_Modules_CSS_Generator::get_instance();
234
235
		$fields = Kirki::$fields;
236
		$css    = array();
237
238
		// Early exit if no fields are found.
239
		if ( empty( $fields ) ) {
240
			return;
241
		}
242
243
		foreach ( $fields as $field ) {
244
245
			// Only process fields that belong to $config_id.
246
			if ( $config_id !== $field['kirki_config'] ) {
247
				continue;
248
			}
249
250
			// Only continue if $field['output'] is set.
251
			if ( isset( $field['output'] ) && ! empty( $field['output'] ) ) {
252
				$css = Kirki_Helper::array_replace_recursive( $css, Kirki_Modules_CSS_Generator::css( $field ) );
253
254
				// Add the globals.
255
				if ( isset( self::$css_array[ $config_id ] ) && ! empty( self::$css_array[ $config_id ] ) ) {
256
					Kirki_Helper::array_replace_recursive( $css, self::$css_array[ $config_id ] );
257
				}
258
			}
259
		}
260
261
		$css = apply_filters( "kirki/{$config_id}/styles", $css );
262
263
		if ( is_array( $css ) ) {
264
			return Kirki_Modules_CSS_Generator::styles_parse( Kirki_Modules_CSS_Generator::add_prefixes( $css ) );
265
		}
266
	}
267
}
268