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 http://opensource.org/licenses/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
|
|
|
* Constructor |
41
|
|
|
* |
42
|
|
|
* @access protected |
43
|
|
|
* @since 3.0.28 |
44
|
|
|
*/ |
45
|
|
|
protected function __construct() { |
46
|
|
|
add_action( 'wp_head', array( $this, 'the_style' ), 0 ); |
47
|
|
|
add_action( 'customize_preview_init', array( $this, 'postmessage' ) ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Gets an instance of this object. |
52
|
|
|
* Prevents duplicate instances which avoid artefacts and improves performance. |
53
|
|
|
* |
54
|
|
|
* @static |
55
|
|
|
* @access public |
56
|
|
|
* @since 3.0.28 |
57
|
|
|
* @return object |
58
|
|
|
*/ |
59
|
|
|
public static function get_instance() { |
60
|
|
|
if ( ! self::$instance ) { |
61
|
|
|
self::$instance = new self(); |
62
|
|
|
} |
63
|
|
|
return self::$instance; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Add styles in <head>. |
68
|
|
|
* |
69
|
|
|
* @access public |
70
|
|
|
* @since 3.0.28 |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
public function the_style() { |
74
|
|
|
// Get an array of all fields. |
75
|
|
|
$fields = Kirki::$fields; |
76
|
|
|
echo '<style id="kirki-css-vars">'; |
77
|
|
|
echo ':root{'; |
78
|
|
|
foreach ( $fields as $id => $args ) { |
79
|
|
|
if ( ! isset( $args['css_var'] ) || empty( $args['css_var'] ) ) { |
80
|
|
|
continue; |
81
|
|
|
} |
82
|
|
|
if ( is_string( $args['css_var'] ) ) { |
83
|
|
|
$args['css_var'] = array( $args['css_var'], '$' ); |
84
|
|
|
} |
85
|
|
|
$val = Kirki_Values::get_value( $args['kirki_config'], $id ); |
86
|
|
|
if ( isset( $args['css_var'][2] ) && is_array( $val ) && isset( $val[ $args['css_var'][2] ] ) ) { |
87
|
|
|
$val = $val[ $args['css_var'][2] ]; |
88
|
|
|
} |
89
|
|
|
echo esc_attr( $args['css_var'][0] ) . ':' . esc_attr( str_replace( '$', $val, $args['css_var'][1] ) ) . ';'; |
90
|
|
|
} |
91
|
|
|
echo '}'; |
92
|
|
|
echo '</style>'; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Enqueues the script that handles postMessage |
97
|
|
|
* and adds variables to it using the wp_localize_script function. |
98
|
|
|
* The rest is handled via JS. |
99
|
|
|
* |
100
|
|
|
* @access public |
101
|
|
|
* @since 3.0.28 |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
public function postmessage() { |
105
|
|
|
wp_enqueue_script( 'kirki_auto_css_vars', trailingslashit( Kirki::$url ) . 'modules/css-vars/script.js', array( 'jquery', 'customize-preview' ), KIRKI_VERSION, true ); |
106
|
|
|
$fields = Kirki::$fields; |
107
|
|
|
$data = array(); |
108
|
|
|
foreach ( $fields as $field ) { |
109
|
|
|
if ( isset( $field['transport'] ) && 'postMessage' === $field['transport'] && isset( $field['css_var'] ) && ! empty( $field['css_var'] ) ) { |
110
|
|
|
$data[] = $field; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
wp_localize_script( 'kirki_auto_css_vars', 'kirkiCssVarFields', $data ); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|