Completed
Push — develop ( 051c9b...a1bb86 )
by Aristeides
03:41
created

Kirki_Control_Switch::to_json()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 12
nop 0
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
1
<?php
2
/**
3
 * Customizer Control: switch.
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
 * Switch control (modified checkbox).
19
 */
20
class Kirki_Control_Switch extends WP_Customize_Control {
21
22
	/**
23
	 * The control type.
24
	 *
25
	 * @access public
26
	 * @var string
27
	 */
28
	public $type = 'kirki-switch';
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
		wp_enqueue_script( 'kirki-switch', trailingslashit( Kirki::$url ) . 'controls/switch/switch.js', array( 'jquery', 'customize-base' ), false, true );
53
		wp_enqueue_style( 'kirki-switch-css', trailingslashit( Kirki::$url ) . 'controls/switch/switch.css', null );
54
	}
55
56
	/**
57
	 * Refresh the parameters passed to the JavaScript via JSON.
58
	 *
59
	 * @see WP_Customize_Control::to_json()
60
	 */
61
	public function to_json() {
62
		parent::to_json();
63
64
		$this->json['default'] = $this->setting->default;
65
		if ( isset( $this->default ) ) {
66
			$this->json['default'] = $this->default;
67
		}
68
		$this->json['output']  = $this->output;
69
		$this->json['value']   = $this->value();
70
		if ( '1' === $this->json['value'] ) {
71
			$this->json['value'] = true;
72
		} elseif ( '0' === $this->json['value'] ) {
73
			$this->json['value'] = false;
74
		}
75
		$this->json['choices'] = $this->choices;
76
		$this->json['link']    = $this->get_link();
77
		$this->json['id']      = $this->id;
78
79
		$this->json['inputAttrs'] = '';
80
		foreach ( $this->input_attrs as $attr => $value ) {
81
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
82
		}
83
84
	}
85
86
	/**
87
	 * An Underscore (JS) template for this control's content (but not its container).
88
	 *
89
	 * Class variables for this control class are available in the `data` JS object;
90
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
91
	 *
92
	 * @see WP_Customize_Control::print_template()
93
	 *
94
	 * @access protected
95
	 */
96
	protected function content_template() {
97
		?>
98
		<style>
99
		<# var cssID = data.id.replace( '[', '-' ).replace( ']', '' ); #>
100
		#customize-control-{{ cssID }} .switch label {
101
			width: calc({{ data.choices['on'].length }}ch + {{ data.choices['off'].length }}ch + 40px);
102
		}
103
		#customize-control-{{ cssID }} .switch label:after {
104
			width: calc({{ data.choices['on'].length }}ch + 10px);
105
		}
106
		#customize-control-{{ cssID }} .switch input:checked + label:after {
107
			left: calc({{ data.choices['on'].length }}ch + 25px);
108
			width: calc({{ data.choices['off'].length }}ch + 10px);
109
		}
110
		</style>
111
		<div class="switch<# if ( data.choices['round'] ) { #> round<# } #>">
112
			<span class="customize-control-title">
113
				{{{ data.label }}}
114
			</span>
115
			<# if ( data.description ) { #>
116
				<span class="description customize-control-description">{{{ data.description }}}</span>
117
			<# } #>
118
			<input class="screen-reader-text" {{{ data.inputAttrs }}} name="switch_{{ data.id }}" id="switch_{{ data.id }}" type="checkbox" value="{{ data.value }}" {{{ data.link }}}<# if ( '1' == data.value ) { #> checked<# } #> />
119
			<label class="switch-label" for="switch_{{ data.id }}">
120
				<span class="switch-on">{{ data.choices['on'] }}</span>
121
				<span class="switch-off">{{ data.choices['off'] }}</span>
122
			</label>
123
		</div>
124
		<?php
125
	}
126
}
127