Completed
Push — develop ( a03475...8ba485 )
by Aristeides
02:49
created

Kirki_Modules_CSS::ajax_dynamic_css()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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