Completed
Push — develop ( 6bc4f7...cab9c0 )
by Aristeides
04:02
created

Kirki_Control_MultiCheck::to_json()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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