Passed
Pull Request — master (#1889)
by Aristeides
04:14
created

Kirki_Modules_CSS_Vars::postmessage()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 7
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 10
rs 8.8571
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_vars'] ) || empty( $args['css_vars'] ) ) {
80
				continue;
81
			}
82
			$val = Kirki_Values::get_value( $args['kirki_config'], $id );
83
			foreach ( $args['css_vars'] as $css_var ) {
84
			if ( isset( $css_var[2] ) && is_array( $val ) && isset( $val[ $css_var[2] ] ) ) {
85
				$val = $val[ $css_var[2] ];
86
			}
87
			echo esc_attr( $css_var[0] ) . ':' . esc_attr( str_replace( '$', $val, $css_var[1] ) ) . ';';
88
		}
89
		}
90
		echo '}';
91
		echo '</style>';
92
	}
93
94
	/**
95
	 * Enqueues the script that handles postMessage
96
	 * and adds variables to it using the wp_localize_script function.
97
	 * The rest is handled via JS.
98
	 *
99
	 * @access public
100
	 * @since 3.0.28
101
	 * @return void
102
	 */
103
	public function postmessage() {
104
		wp_enqueue_script( 'kirki_auto_css_vars', trailingslashit( Kirki::$url ) . 'modules/css-vars/script.js', array( 'jquery', 'customize-preview' ), KIRKI_VERSION, true );
105
		$fields = Kirki::$fields;
106
		$data   = array();
107
		foreach ( $fields as $field ) {
108
			if ( isset( $field['transport'] ) && 'postMessage' === $field['transport'] && isset( $field['css_vars'] ) && ! empty( $field['css_vars'] ) ) {
109
				$data[] = $field;
110
			}
111
		}
112
		wp_localize_script( 'kirki_auto_css_vars', 'kirkiCssVarFields', $data );
113
	}
114
}
115