Completed
Push — develop ( 656b88...85b7b9 )
by Aristeides
06:46
created

Kirki_Control_Radio_Buttonset::enqueue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Customizer Control: radio-buttonset.
4
 *
5
 * @package     Kirki
6
 * @subpackage  Controls
7
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
8
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Radio Buttonset control (modified radio)
19
 */
20
class Kirki_Control_Radio_Buttonset extends WP_Customize_Control {
21
22
	/**
23
	 * The control type.
24
	 *
25
	 * @access public
26
	 * @var string
27
	 */
28
	public $type = 'kirki-radio-buttonset';
29
30
	/**
31
	 * Used to automatically generate all CSS output.
32
	 *
33
	 * @access public
34
	 * @var array
35
	 */
36
	public $output = array();
37
38
	/**
39
	 * Data type
40
	 *
41
	 * @access public
42
	 * @var string
43
	 */
44
	public $option_type = 'theme_mod';
45
46
	/**
47
	 * Enqueue control related scripts/styles.
48
	 *
49
	 * @access public
50
	 */
51
	public function enqueue() {
52
53
		wp_enqueue_script( 'kirki-dynamic-control', trailingslashit( Kirki::$url ) . 'assets/js/dynamic-control.js', array( 'jquery', 'customize-base' ), false, true );
54
		wp_enqueue_script( 'kirki-radio-buttonset', trailingslashit( Kirki::$url ) . 'controls/radio-buttonset/radio-buttonset.js', array( 'jquery', 'customize-base', 'kirki-dynamic-control' ), false, true );
55
		wp_enqueue_style( 'kirki-radio-buttonset-css', trailingslashit( Kirki::$url ) . 'controls/radio-buttonset/radio-buttonset.css', null );
56
	}
57
58
	/**
59
	 * Refresh the parameters passed to the JavaScript via JSON.
60
	 *
61
	 * @see WP_Customize_Control::to_json()
62
	 */
63
	public function to_json() {
64
		parent::to_json();
65
66
		$this->json['default'] = ( isset( $this->default ) ) ? $this->default : $this->setting->default;
67
		$this->json['output']  = $this->output;
68
		$this->json['value']   = $this->value();
69
		$this->json['choices'] = $this->choices;
70
		$this->json['link']    = $this->get_link();
71
		$this->json['id']      = $this->id;
72
73
		$this->json['inputAttrs'] = '';
74
		foreach ( $this->input_attrs as $attr => $value ) {
75
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
76
		}
77
78
	}
79
80
	/**
81
	 * An Underscore (JS) template for this control's content (but not its container).
82
	 *
83
	 * Class variables for this control class are available in the `data` JS object;
84
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
85
	 *
86
	 * @see WP_Customize_Control::print_template()
87
	 *
88
	 * @access protected
89
	 */
90
	protected function content_template() {
91
		?>
92
		<# if ( data.label ) { #><span class="customize-control-title">{{{ data.label }}}</span><# } #>
93
		<# if ( data.description ) { #><span class="description customize-control-description">{{{ data.description }}}</span><# } #>
94
		<div id="input_{{ data.id }}" class="buttonset">
95
			<# for ( key in data.choices ) { #>
96
				<input {{{ data.inputAttrs }}} class="switch-input screen-reader-text" type="radio" value="{{ key }}" name="_customize-radio-{{{ data.id }}}" id="{{ data.id }}{{ key }}" {{{ data.link }}}<# if ( key === data.value ) { #> checked="checked" <# } #>>
97
					<label class="switch-label switch-label-<# if ( key === data.value ) { #>on <# } else { #>off<# } #>" for="{{ data.id }}{{ key }}">{{ data.choices[ key ] }}</label>
98
				</input>
99
			<# } #>
100
		</div>
101
		<?php
102
	}
103
}
104