Completed
Push — develop ( 5d9bd3...5122ce )
by Aristeides
08:08
created

Kirki_Modules_CSS   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 198
rs 8.6
c 0
b 0
f 0
wmc 37
lcom 2
cbo 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 24 3
D init() 0 27 10
B inline_dynamic_css() 0 17 6
A ajax_dynamic_css() 0 4 1
A frontend_styles() 0 3 1
C loop_controls() 0 59 16
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
	 * Constructor
44
	 *
45
	 * @access public
46
	 */
47
	public function __construct() {
48
49
		$class_files = array(
50
			'Kirki_Modules_CSS_Generator'               => '/class-kirki-modules-css-generator.php',
51
			'Kirki_Output'                              => '/class-kirki-output.php',
52
			'Kirki_Output_Field_Background'             => '/field/class-kirki-output-field-background.php',
53
			'Kirki_Output_Field_Multicolor'             => '/field/class-kirki-output-field-multicolor.php',
54
			'Kirki_Output_Field_Dimensions'             => '/field/class-kirki-output-field-dimensions.php',
55
			'Kirki_Output_Field_Typography'             => '/field/class-kirki-output-field-typography.php',
56
			'Kirki_Output_Property'                     => '/property/class-kirki-output-property.php',
57
			'Kirki_Output_Property_Background_Image'    => '/property/class-kirki-output-property-background-image.php',
58
			'Kirki_Output_Property_Background_Position' => '/property/class-kirki-output-property-background-position.php',
59
			'Kirki_Output_Property_Font_Family'         => '/property/class-kirki-output-property-font-family.php',
60
		);
61
62
		foreach ( $class_files as $class_name => $file ) {
63
			if ( ! class_exists( $class_name ) ) {
64
				include_once wp_normalize_path( dirname( __FILE__ ) . $file );
65
			}
66
		}
67
68
		add_action( 'init', array( $this, 'init' ) );
69
70
	}
71
72
	/**
73
	 * Init.
74
	 *
75
	 * @access public
76
	 */
77
	public function init() {
78
79
		Kirki_Fonts_Google::get_instance();
80
81
		global $wp_customize;
82
83
		$config   = apply_filters( 'kirki/config', array() );
84
		$priority = 999;
85
		if ( isset( $config['styles_priority'] ) ) {
86
			$priority = absint( $config['styles_priority'] );
87
		}
88
89
		// Allow completely disabling Kirki CSS output.
90
		if ( ( defined( 'KIRKI_NO_OUTPUT' ) && KIRKI_NO_OUTPUT ) || ( isset( $config['disable_output'] ) && true !== $config['disable_output'] ) ) {
91
			return;
92
		}
93
94
		// If we are in the customizer, load CSS using inline-styles.
95
		// If we are in the frontend AND self::$ajax is true, then load dynamic CSS using AJAX.
96
		if ( ! $wp_customize && ( ( true === self::$ajax ) || ( isset( $config['inline_css'] ) && false === $config['inline_css'] ) ) ) {
97
			add_action( 'wp_enqueue_scripts', array( $this, 'frontend_styles' ), $priority );
98
			add_action( 'wp_ajax_kirki_dynamic_css', array( $this, 'ajax_dynamic_css' ) );
99
			add_action( 'wp_ajax_nopriv_kirki_dynamic_css', array( $this, 'ajax_dynamic_css' ) );
100
		} else {
101
			add_action( 'wp_enqueue_scripts', array( $this, 'inline_dynamic_css' ), $priority );
102
		}
103
	}
104
105
	/**
106
	 * Adds inline styles.
107
	 *
108
	 * @access public
109
	 */
110
	public function inline_dynamic_css() {
111
		$configs = Kirki::$config;
112
		if ( ! $this->processed ) {
113
			foreach ( $configs as $config_id => $args ) {
114
				if ( isset( $args['disable_output'] ) && true === $args['disable_output'] ) {
115
					continue;
116
				}
117
				$styles = self::loop_controls( $config_id );
118
				$styles = apply_filters( 'kirki/' . $config_id . '/dynamic_css', $styles );
119
				if ( ! empty( $styles ) ) {
120
					wp_enqueue_style( 'kirki-styles-' . $config_id, trailingslashit( Kirki::$url ) . 'assets/css/kirki-styles.css', null, null );
121
					wp_add_inline_style( 'kirki-styles-' . $config_id, $styles );
122
				}
123
			}
124
			$this->processed = true;
125
		}
126
	}
127
128
	/**
129
	 * Get the dynamic-css.php file
130
	 *
131
	 * @access public
132
	 */
133
	public function ajax_dynamic_css() {
134
		require wp_normalize_path( Kirki::$path . '/modules/css/dynamic-css.php' );
135
		exit;
136
	}
137
138
	/**
139
	 * Enqueues the ajax stylesheet.
140
	 *
141
	 * @access public
142
	 */
143
	public function frontend_styles() {
144
		wp_enqueue_style( 'kirki-styles-php', admin_url( 'admin-ajax.php' ) . '?action=kirki_dynamic_css', null, null );
145
	}
146
147
	/**
148
	 * Loop through all fields and create an array of style definitions.
149
	 *
150
	 * @static
151
	 * @access public
152
	 * @param string $config_id The configuration ID.
153
	 */
154
	public static function loop_controls( $config_id ) {
155
156
		// Get an instance of the Kirki_Modules_CSS_Generator class.
157
		// This will make sure google fonts and backup fonts are loaded.
158
		Kirki_Modules_CSS_Generator::get_instance();
159
160
		$fields = Kirki::$fields;
161
		$css    = array();
162
163
		// Early exit if no fields are found.
164
		if ( empty( $fields ) ) {
165
			return;
166
		}
167
168
		foreach ( $fields as $field ) {
169
170
			// Only process fields that belong to $config_id.
171
			if ( $config_id != $field['kirki_config'] ) {
172
				continue;
173
			}
174
175
			// Only continue if field dependencies are met.
176
			if ( ! empty( $field['required'] ) ) {
177
				$valid = true;
178
179
				foreach ( $field['required'] as $requirement ) {
180
					if ( isset( $requirement['setting'] ) && isset( $requirement['value'] ) && isset( $requirement['operator'] ) ) {
181
						$controller_value = Kirki_Values::get_value( $config_id, $requirement['setting'] );
182
						if ( ! Kirki_Active_Callback::compare( $controller_value, $requirement['value'], $requirement['operator'] ) ) {
183
							$valid = false;
184
						}
185
					}
186
				}
187
188
				if ( ! $valid ) {
189
					continue;
190
				}
191
			}
192
193
			// Only continue if $field['output'] is set.
194
			if ( isset( $field['output'] ) && ! empty( $field['output'] ) ) {
195
				$css  = Kirki_Helper::array_replace_recursive( $css, Kirki_Modules_CSS_Generator::css( $field ) );
196
197
				// Add the globals.
198
				if ( isset( self::$css_array[ $config_id ] ) && ! empty( self::$css_array[ $config_id ] ) ) {
199
					Kirki_Helper::array_replace_recursive( $css, self::$css_array[ $config_id ] );
200
				}
201
			}
202
		}
203
204
		$css = apply_filters( 'kirki/' . $config_id . '/styles', $css );
205
206
		if ( is_array( $css ) ) {
207
			return Kirki_Modules_CSS_Generator::styles_parse( Kirki_Modules_CSS_Generator::add_prefixes( $css ) );
208
		}
209
210
		return;
211
212
	}
213
}
214