Passed
Pull Request — master (#1889)
by Aristeides
03:44
created

Kirki_Modules_CSS_Vars::the_style()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 15
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
	}
48
49
	/**
50
	 * Gets an instance of this object.
51
	 * Prevents duplicate instances which avoid artefacts and improves performance.
52
	 *
53
	 * @static
54
	 * @access public
55
	 * @since 3.0.28
56
	 * @return object
57
	 */
58
	public static function get_instance() {
59
		if ( ! self::$instance ) {
60
			self::$instance = new self();
61
		}
62
		return self::$instance;
63
	}
64
65
	/**
66
	 * Add styles in <head>.
67
	 *
68
	 * @access public
69
	 * @since 3.0.28
70
	 * @return void
71
	 */
72
	public function the_style() {
73
		// Get an array of all fields.
74
		$fields = Kirki::$fields;
75
		echo '<style id="kirki-css-vars">';
76
		echo ':root{';
77
		foreach ( $fields as $id => $args ) {
78
			if ( isset( $args['css_var'] ) && ! empty( $args['css_var'] ) ) {
79
				if ( is_string( $args['css_var'] ) ) {
80
					$args['css_var'] = array( $args['css_var'], '$' );
81
				}
82
				echo esc_attr( $args['css_var'][0] ) . ':' . esc_attr( str_replace( '$', Kirki_Values::get_value( $args['kirki_config'], $id ), $args['css_var'][1] ) ) . ';';
83
			}
84
		}
85
		echo '}';
86
		echo '</style>';
87
	}
88
}
89