1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Handles the CSS-variables of fields. |
4
|
|
|
* |
5
|
|
|
* @package Kirki |
6
|
|
|
* @category Modules |
7
|
|
|
* @author Aristeides Stathopoulos |
8
|
|
|
* @copyright Copyright (c) 2017, Aristeides Stathopoulos |
9
|
|
|
* @license https://opensource.org/licenses/MIT |
10
|
|
|
* @since 3.0.28 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The Kirki_Modules_CSS_Vars object. |
15
|
|
|
* |
16
|
|
|
* @since 3.0.28 |
17
|
|
|
*/ |
18
|
|
|
class Kirki_Modules_CSS_Vars { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The object instance. |
22
|
|
|
* |
23
|
|
|
* @static |
24
|
|
|
* @access private |
25
|
|
|
* @since 3.0.28 |
26
|
|
|
* @var object |
27
|
|
|
*/ |
28
|
|
|
private static $instance; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Fields with variables. |
32
|
|
|
* |
33
|
|
|
* @access private |
34
|
|
|
* @since 3.0.28 |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $fields = array(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* CSS-variables array [var=>val]. |
41
|
|
|
* |
42
|
|
|
* @access private |
43
|
|
|
* @since 3.0.35 |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private $vars = array(); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Constructor |
50
|
|
|
* |
51
|
|
|
* @access protected |
52
|
|
|
* @since 3.0.28 |
53
|
|
|
*/ |
54
|
|
|
protected function __construct() { |
55
|
|
|
add_action( 'wp', array( $this, 'populate_vars' ) ); |
56
|
|
|
add_action( 'wp_head', array( $this, 'the_style' ), 0 ); |
57
|
|
|
add_action( 'admin_head', array( $this, 'the_style' ), 0 ); |
58
|
|
|
add_action( 'customize_preview_init', array( $this, 'postmessage' ) ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Gets an instance of this object. |
63
|
|
|
* Prevents duplicate instances which avoid artefacts and improves performance. |
64
|
|
|
* |
65
|
|
|
* @static |
66
|
|
|
* @access public |
67
|
|
|
* @since 3.0.28 |
68
|
|
|
* @return object |
69
|
|
|
*/ |
70
|
|
|
public static function get_instance() { |
71
|
|
|
if ( ! self::$instance ) { |
72
|
|
|
self::$instance = new self(); |
73
|
|
|
} |
74
|
|
|
return self::$instance; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Populates the $vars property of this object. |
79
|
|
|
* |
80
|
|
|
* @access public |
81
|
|
|
* @since 3.0.35 |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
public function populate_vars() { |
85
|
|
|
|
86
|
|
|
// Get an array of all fields. |
87
|
|
|
$fields = Kirki::$fields; |
88
|
|
|
foreach ( $fields as $id => $args ) { |
89
|
|
|
if ( ! isset( $args['css_vars'] ) || empty( $args['css_vars'] ) ) { |
90
|
|
|
continue; |
91
|
|
|
} |
92
|
|
|
$val = Kirki_Values::get_value( $args['kirki_config'], $id ); |
93
|
|
|
foreach ( $args['css_vars'] as $css_var ) { |
94
|
|
|
if ( isset( $css_var[2] ) && is_array( $val ) && isset( $val[ $css_var[2] ] ) ) { |
95
|
|
|
$this->vars[ $css_var[0] ] = str_replace( '$', $val[ $css_var[2] ], $css_var[1] ); |
96
|
|
|
} else { |
97
|
|
|
$this->vars[ $css_var[0] ] = str_replace( '$', $val, $css_var[1] ); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Add styles in <head>. |
105
|
|
|
* |
106
|
|
|
* @access public |
107
|
|
|
* @since 3.0.28 |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
|
|
public function the_style() { |
111
|
|
|
|
112
|
|
|
if ( empty( $this->vars ) ) { |
113
|
|
|
return; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
echo '<style id="kirki-css-vars">'; |
117
|
|
|
echo ':root{'; |
118
|
|
|
foreach ( $this->vars as $var => $val ) { |
119
|
|
|
echo esc_html( $var ) . ':' . esc_html( $val ) . ';'; |
120
|
|
|
} |
121
|
|
|
echo '}'; |
122
|
|
|
echo '</style>'; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get an array of all the variables. |
127
|
|
|
* |
128
|
|
|
* @access public |
129
|
|
|
* @since 3.0.35 |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
public function get_vars() { |
133
|
|
|
return $this->vars; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Enqueues the script that handles postMessage |
138
|
|
|
* and adds variables to it using the wp_localize_script function. |
139
|
|
|
* The rest is handled via JS. |
140
|
|
|
* |
141
|
|
|
* @access public |
142
|
|
|
* @since 3.0.28 |
143
|
|
|
* @return void |
144
|
|
|
*/ |
145
|
|
|
public function postmessage() { |
146
|
|
|
wp_enqueue_script( 'kirki_auto_css_vars', trailingslashit( Kirki::$url ) . 'modules/css-vars/script.js', array( 'jquery', 'customize-preview' ), KIRKI_VERSION, true ); |
147
|
|
|
$fields = Kirki::$fields; |
148
|
|
|
$data = array(); |
149
|
|
|
foreach ( $fields as $field ) { |
150
|
|
|
if ( isset( $field['transport'] ) && 'postMessage' === $field['transport'] && isset( $field['css_vars'] ) && ! empty( $field['css_vars'] ) ) { |
151
|
|
|
$data[] = $field; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
wp_localize_script( 'kirki_auto_css_vars', 'kirkiCssVarFields', $data ); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|