-
Notifications
You must be signed in to change notification settings - Fork 327
/
class-kirki-styles-frontend.php
199 lines (167 loc) · 5.31 KB
/
class-kirki-styles-frontend.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
* Generates the styles for the frontend.
* Handles the 'output' argument of fields.
* Usage instructions on https://github.com/aristath/kirki/wiki/output
*
* @package Kirki
* @category Core
* @author Aristeides Stathopoulos
* @copyright Copyright (c) 2016, Aristeides Stathopoulos
* @license http://opensource.org/licenses/https://opensource.org/licenses/MIT
* @since 1.0
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'Kirki_Styles_Frontend' ) ) {
/**
* Applies field-generated styles to the frontend.
*/
class Kirki_Styles_Frontend {
/**
* Whether we've already processed this or not.
*
* @access public
* @var bool
*/
public $processed = false;
/**
* The CSS array
*
* @access public
* @var array
*/
public static $css_array = array();
/**
* Set to true if you want to use the AJAX method.
*
* @access public
* @var bool
*/
public static $ajax = false;
/**
* Constructor
*
* @access public
*/
public function __construct() {
add_action( 'init', array( $this, 'init' ) );
}
/**
* Init.
*
* @access public
*/
public function init() {
Kirki_Fonts_Google::get_instance();
global $wp_customize;
$config = apply_filters( 'kirki/config', array() );
$priority = ( isset( $config['styles_priority'] ) ) ? intval( $config['styles_priority'] ) : 999;
// Allow completely disabling Kirki CSS output.
if ( ( defined( 'KIRKI_NO_OUTPUT' ) && KIRKI_NO_OUTPUT ) || ( isset( $config['disable_output'] ) && true !== $config['disable_output'] ) ) {
return;
}
// If we are in the customizer, load CSS using inline-styles.
// If we are in the frontend AND self::$ajax is true, then load dynamic CSS using AJAX.
if ( ! $wp_customize && ( ( true === self::$ajax ) || ( isset( $config['inline_css'] ) && false === $config['inline_css'] ) ) ) {
add_action( 'wp_enqueue_scripts', array( $this, 'frontend_styles' ), $priority );
add_action( 'wp_ajax_kirki_dynamic_css', array( $this, 'ajax_dynamic_css' ) );
add_action( 'wp_ajax_nopriv_kirki_dynamic_css', array( $this, 'ajax_dynamic_css' ) );
} else {
add_action( 'wp_enqueue_scripts', array( $this, 'inline_dynamic_css' ), $priority );
}
}
/**
* Adds inline styles.
*
* @access public
*/
public function inline_dynamic_css() {
$configs = Kirki::$config;
if ( ! $this->processed ) {
foreach ( $configs as $config_id => $args ) {
if ( true === $args['disable_output'] ) {
continue;
}
$styles = self::loop_controls( $config_id );
$styles = apply_filters( 'kirki/' . $config_id . '/dynamic_css', $styles );
if ( ! empty( $styles ) ) {
wp_enqueue_style( 'kirki-styles-' . $config_id, trailingslashit( Kirki::$url ) . 'assets/css/kirki-styles.css', null, null );
wp_add_inline_style( 'kirki-styles-' . $config_id, $styles );
}
}
$this->processed = true;
}
}
/**
* Get the dynamic-css.php file
*
* @access public
*/
public function ajax_dynamic_css() {
require( Kirki::$path . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'dynamic-css.php' );
exit;
}
/**
* Enqueues the ajax stylesheet.
*
* @access public
*/
public function frontend_styles() {
wp_enqueue_style( 'kirki-styles-php', admin_url( 'admin-ajax.php' ) . '?action=kirki_dynamic_css', null, null );
}
/**
* Loop through all fields and create an array of style definitions.
*
* @static
* @access public
* @param string $config_id The configuration ID.
*/
public static function loop_controls( $config_id ) {
// Get an instance of the Kirki_Styles_Output_CSS class.
// This will make sure google fonts and backup fonts are loaded.
Kirki_Styles_Output_CSS::get_instance();
$fields = Kirki::$fields;
$css = array();
// Early exit if no fields are found.
if ( empty( $fields ) ) {
return;
}
foreach ( $fields as $field ) {
// Only process fields that belong to $config_id.
if ( $config_id != $field['kirki_config'] ) {
continue;
}
// Only continue if field dependencies are met.
if ( ! empty( $field['required'] ) ) {
$valid = true;
foreach ( $field['required'] as $requirement ) {
if ( isset( $requirement['setting'] ) && isset( $requirement['value'] ) && isset( $requirement['operator'] ) ) {
$controller_value = Kirki_Values::get_value( $config_id, $requirement['setting'] );
if ( ! Kirki_Active_Callback::compare( $controller_value, $requirement['value'], $requirement['operator'] ) ) {
$valid = false;
}
}
}
if ( ! $valid ) {
continue;
}
}
// Only continue if $field['output'] is set.
if ( isset( $field['output'] ) && ! empty( $field['output'] ) && 'background' != $field['type'] ) {
$css = Kirki_Helper::array_replace_recursive( $css, Kirki_Styles_Output_CSS::css( $field ) );
// Add the globals.
if ( isset( self::$css_array[ $config_id ] ) && ! empty( self::$css_array[ $config_id ] ) ) {
Kirki_Helper::array_replace_recursive( $css, self::$css_array[ $config_id ] );
}
}
}
if ( is_array( $css ) ) {
return Kirki_Styles_Output_CSS::styles_parse( Kirki_Styles_Output_CSS::add_prefixes( $css ) );
}
return;
}
}
}