Completed
Push — develop ( da7e54...51af65 )
by Aristeides
03:36
created

Kirki_Control_Switch   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 100
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A enqueue() 0 10 2
B to_json() 0 24 5
A content_template() 0 17 1
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
53
		Kirki_Custom_Build::register_dependency( 'jquery' );
54
		Kirki_Custom_Build::register_dependency( 'customize-base' );
55
56
		if ( ! Kirki_Custom_Build::is_custom_build() ) {
57
			wp_enqueue_script( 'kirki-switch', trailingslashit( Kirki::$url ) . 'controls/switch/switch.js', array( 'jquery', 'customize-base' ), false, true );
58
			wp_enqueue_style( 'kirki-switch-css', trailingslashit( Kirki::$url ) . 'controls/switch/switch.css', null );
59
		}
60
	}
61
62
	/**
63
	 * Refresh the parameters passed to the JavaScript via JSON.
64
	 *
65
	 * @see WP_Customize_Control::to_json()
66
	 */
67
	public function to_json() {
68
		parent::to_json();
69
70
		$this->json['default'] = $this->setting->default;
71
		if ( isset( $this->default ) ) {
72
			$this->json['default'] = $this->default;
73
		}
74
		$this->json['output']  = $this->output;
75
		$this->json['value']   = $this->value();
76
		if ( '1' === $this->json['value'] ) {
77
			$this->json['value'] = true;
78
		} elseif ( '0' === $this->json['value'] ) {
79
			$this->json['value'] = false;
80
		}
81
		$this->json['choices'] = $this->choices;
82
		$this->json['link']    = $this->get_link();
83
		$this->json['id']      = $this->id;
84
85
		$this->json['inputAttrs'] = '';
86
		foreach ( $this->input_attrs as $attr => $value ) {
87
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
88
		}
89
90
	}
91
92
	/**
93
	 * An Underscore (JS) template for this control's content (but not its container).
94
	 *
95
	 * Class variables for this control class are available in the `data` JS object;
96
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
97
	 *
98
	 * @see WP_Customize_Control::print_template()
99
	 *
100
	 * @access protected
101
	 */
102
	protected function content_template() {
103
		?>
104
		<div class="switch<# if ( data.choices['round'] ) { #> round<# } #>">
105
			<span class="customize-control-title">
106
				{{{ data.label }}}
107
			</span>
108
			<# if ( data.description ) { #>
109
				<span class="description customize-control-description">{{{ data.description }}}</span>
110
			<# } #>
111
			<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<# } #> />
112
			<label class="switch-label" for="switch_{{ data.id }}">
113
				<span class="switch-on">{{ data.choices['on'] }}</span>
114
				<span class="switch-off">{{ data.choices['off'] }}</span>
115
			</label>
116
		</div>
117
		<?php
118
	}
119
}
120