Completed
Push — develop ( 34fdaa...dae3ff )
by Aristeides
03:34
created

Kirki_Modules_CSS::__construct()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 3
nop 0
dl 0
loc 25
rs 8.8571
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) 2016, 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
	 * Whether we've already processed this or not.
20
	 *
21
	 * @access public
22
	 * @var bool
23
	 */
24
	public $processed = false;
25
26
	/**
27
	 * The CSS array
28
	 *
29
	 * @access public
30
	 * @var array
31
	 */
32
	public static $css_array = array();
33
34
	/**
35
	 * Set to true if you want to use the AJAX method.
36
	 *
37
	 * @access public
38
	 * @var bool
39
	 */
40
	public static $ajax = false;
41
42
	/**
43
	 * The Kirki_CSS_To_File object.
44
	 *
45
	 * @access protected
46
	 * @since 3.0.0
47
	 * @var object
48
	 */
49
	protected $css_to_file;
50
51
	/**
52
	 * Constructor
53
	 *
54
	 * @access public
55
	 */
56
	public function __construct() {
57
58
		$class_files = array(
59
			'Kirki_CSS_To_File'                         => '/class-kirki-css-to-file.php',
60
			'Kirki_Modules_CSS_Generator'               => '/class-kirki-modules-css-generator.php',
61
			'Kirki_Output'                              => '/class-kirki-output.php',
62
			'Kirki_Output_Field_Background'             => '/field/class-kirki-output-field-background.php',
63
			'Kirki_Output_Field_Multicolor'             => '/field/class-kirki-output-field-multicolor.php',
64
			'Kirki_Output_Field_Dimensions'             => '/field/class-kirki-output-field-dimensions.php',
65
			'Kirki_Output_Field_Typography'             => '/field/class-kirki-output-field-typography.php',
66
			'Kirki_Output_Property'                     => '/property/class-kirki-output-property.php',
67
			'Kirki_Output_Property_Background_Image'    => '/property/class-kirki-output-property-background-image.php',
68
			'Kirki_Output_Property_Background_Position' => '/property/class-kirki-output-property-background-position.php',
69
			'Kirki_Output_Property_Font_Family'         => '/property/class-kirki-output-property-font-family.php',
70
		);
71
72
		foreach ( $class_files as $class_name => $file ) {
73
			if ( ! class_exists( $class_name ) ) {
74
				include_once wp_normalize_path( dirname( __FILE__ ) . $file );
75
			}
76
		}
77
78
		add_action( 'init', array( $this, 'init' ) );
79
80
	}
81
82
	/**
83
	 * Init.
84
	 *
85
	 * @access public
86
	 */
87
	public function init() {
88
89
		Kirki_Fonts_Google::get_instance();
90
91
		global $wp_customize;
92
93
		$config   = apply_filters( 'kirki/config', array() );
94
		$priority = 999;
95
		if ( isset( $config['styles_priority'] ) ) {
96
			$priority = absint( $config['styles_priority'] );
97
		}
98
99
		// Allow completely disabling Kirki CSS output.
100
		if ( ( defined( 'KIRKI_NO_OUTPUT' ) && KIRKI_NO_OUTPUT ) || ( isset( $config['disable_output'] ) && true !== $config['disable_output'] ) ) {
101
			return;
102
		}
103
104
		$this->css_to_file = new Kirki_CSS_To_File();
105
106
		// If we're in the customizer, load inline no matter what.
107
		if ( $wp_customize ) {
108
			add_action( 'wp_enqueue_scripts', array( $this, 'inline_dynamic_css' ), $priority );
109
			return;
110
		}
111
112
		// Attempt to write the CSS to file.
113
		// If we succesd, load this file.
114
		$failed = get_transient( 'kirki_css_write_to_file_failed' );
115
		// If writing CSS to file hasn't failed, just enqueue this file.
116
		if ( ! $failed ) {
117
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_compiled_file' ), $priority );
118
			return;
119
		}
120
121
		// If we are in the customizer, load CSS using inline-styles.
122
		// If we are in the frontend AND self::$ajax is true, then load dynamic CSS using AJAX.
123
		if ( ! $wp_customize && ( ( true === self::$ajax ) || ( isset( $config['inline_css'] ) && false === $config['inline_css'] ) ) ) {
124
			add_action( 'wp_enqueue_scripts', array( $this, 'frontend_styles' ), $priority );
125
			add_action( 'wp_ajax_kirki_dynamic_css', array( $this, 'ajax_dynamic_css' ) );
126
			add_action( 'wp_ajax_nopriv_kirki_dynamic_css', array( $this, 'ajax_dynamic_css' ) );
127
		} else {
128
			add_action( 'wp_enqueue_scripts', array( $this, 'inline_dynamic_css' ), $priority );
129
		}
130
	}
131
132
	/**
133
	 * Enqueues compiled CSS file.
134
	 *
135
	 * @access public
136
	 * @since 3.0.0
137
	 */
138
	public function enqueue_compiled_file() {
139
140
		wp_enqueue_style( 'kirki-styles', $this->css_to_file->get_url() );
141
142
	}
143
	/**
144
	 * Adds inline styles.
145
	 *
146
	 * @access public
147
	 */
148
	public function inline_dynamic_css() {
149
		$configs = Kirki::$config;
150
		if ( ! $this->processed ) {
151
			foreach ( $configs as $config_id => $args ) {
152
				if ( isset( $args['disable_output'] ) && true === $args['disable_output'] ) {
153
					continue;
154
				}
155
				$styles = self::loop_controls( $config_id );
156
				$styles = apply_filters( 'kirki/' . $config_id . '/dynamic_css', $styles );
157
				if ( ! empty( $styles ) ) {
158
					wp_enqueue_style( 'kirki-styles-' . $config_id, trailingslashit( Kirki::$url ) . 'assets/css/kirki-styles.css', null, null );
159
					wp_add_inline_style( 'kirki-styles-' . $config_id, $styles );
160
				}
161
			}
162
			$this->processed = true;
163
		}
164
	}
165
166
	/**
167
	 * Get the dynamic-css.php file
168
	 *
169
	 * @access public
170
	 */
171
	public function ajax_dynamic_css() {
172
		require wp_normalize_path( Kirki::$path . '/modules/css/dynamic-css.php' );
173
		exit;
174
	}
175
176
	/**
177
	 * Enqueues the ajax stylesheet.
178
	 *
179
	 * @access public
180
	 */
181
	public function frontend_styles() {
182
		wp_enqueue_style( 'kirki-styles-php', admin_url( 'admin-ajax.php' ) . '?action=kirki_dynamic_css', null, null );
183
	}
184
185
	/**
186
	 * Loop through all fields and create an array of style definitions.
187
	 *
188
	 * @static
189
	 * @access public
190
	 * @param string $config_id The configuration ID.
191
	 */
192
	public static function loop_controls( $config_id ) {
193
194
		// Get an instance of the Kirki_Modules_CSS_Generator class.
195
		// This will make sure google fonts and backup fonts are loaded.
196
		Kirki_Modules_CSS_Generator::get_instance();
197
198
		$fields = Kirki::$fields;
199
		$css    = array();
200
201
		// Early exit if no fields are found.
202
		if ( empty( $fields ) ) {
203
			return;
204
		}
205
206
		foreach ( $fields as $field ) {
207
208
			// Only process fields that belong to $config_id.
209
			if ( $config_id != $field['kirki_config'] ) {
210
				continue;
211
			}
212
213
			// Only continue if field dependencies are met.
214
			if ( ! empty( $field['required'] ) ) {
215
				$valid = true;
216
217
				foreach ( $field['required'] as $requirement ) {
218
					if ( isset( $requirement['setting'] ) && isset( $requirement['value'] ) && isset( $requirement['operator'] ) ) {
219
						$controller_value = Kirki_Values::get_value( $config_id, $requirement['setting'] );
220
						if ( ! Kirki_Active_Callback::compare( $controller_value, $requirement['value'], $requirement['operator'] ) ) {
221
							$valid = false;
222
						}
223
					}
224
				}
225
226
				if ( ! $valid ) {
227
					continue;
228
				}
229
			}
230
231
			// Only continue if $field['output'] is set.
232
			if ( isset( $field['output'] ) && ! empty( $field['output'] ) ) {
233
				$css  = Kirki_Helper::array_replace_recursive( $css, Kirki_Modules_CSS_Generator::css( $field ) );
234
235
				// Add the globals.
236
				if ( isset( self::$css_array[ $config_id ] ) && ! empty( self::$css_array[ $config_id ] ) ) {
237
					Kirki_Helper::array_replace_recursive( $css, self::$css_array[ $config_id ] );
238
				}
239
			}
240
		}
241
242
		$css = apply_filters( 'kirki/' . $config_id . '/styles', $css );
243
244
		if ( is_array( $css ) ) {
245
			return Kirki_Modules_CSS_Generator::styles_parse( Kirki_Modules_CSS_Generator::add_prefixes( $css ) );
246
		}
247
248
		return;
249
250
	}
251
}
252