Completed
Push — develop ( 8a2f67...52f1e8 )
by Aristeides
02:16
created

Kirki_Control_MultiCheck::render_content()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 1
rs 10
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) 2016, 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
	 * The kirki_config we're using for this control
51
	 *
52
	 * @access public
53
	 * @var string
54
	 */
55
	public $kirki_config = 'global';
56
57
	/**
58
	 * The translation strings.
59
	 *
60
	 * @access protected
61
	 * @since 2.3.5
62
	 * @var array
63
	 */
64
	protected $l10n = array();
65
66
	/**
67
	 * Enqueue control related scripts/styles.
68
	 *
69
	 * @access public
70
	 */
71
	public function enqueue() {
72
		wp_enqueue_script( 'kirki-multicheck', trailingslashit( Kirki::$url ) . 'controls/multicheck/multicheck.js', array( 'jquery', 'customize-base' ), false, true );
73
		wp_enqueue_style( 'kirki-multicheck-css', trailingslashit( Kirki::$url ) . 'controls/multicheck/multicheck.css', null );
74
	}
75
76
	/**
77
	 * Refresh the parameters passed to the JavaScript via JSON.
78
	 *
79
	 * @see WP_Customize_Control::to_json()
80
	 */
81
	public function to_json() {
82
		parent::to_json();
83
84
		$this->json['default'] = $this->setting->default;
85
		if ( isset( $this->default ) ) {
86
			$this->json['default'] = $this->default;
87
		}
88
		$this->json['output']      = $this->output;
89
		$this->json['value']       = $this->value();
90
		$this->json['choices']     = $this->choices;
91
		$this->json['link']        = $this->get_link();
92
		$this->json['id']          = $this->id;
93
		$this->json['l10n']        = $this->l10n;
94
		$this->json['kirkiConfig'] = $this->kirki_config;
95
96
		if ( 'user_meta' === $this->option_type ) {
97
			$this->json['value'] = get_user_meta( get_current_user_id(), $this->id, true );
98
		}
99
100
		$this->json['inputAttrs'] = '';
101
		foreach ( $this->input_attrs as $attr => $value ) {
102
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
103
		}
104
105
	}
106
107
	/**
108
	 * An Underscore (JS) template for this control's content (but not its container).
109
	 *
110
	 * Class variables for this control class are available in the `data` JS object;
111
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
112
	 *
113
	 * @see WP_Customize_Control::print_template()
114
	 *
115
	 * @access protected
116
	 */
117
	protected function content_template() {
118
		?>
119
120
		<# if ( ! data.choices ) { return; } #>
121
122
		<# if ( data.label ) { #>
123
			<span class="customize-control-title">{{ data.label }}</span>
124
		<# } #>
125
126
		<# if ( data.description ) { #>
127
			<span class="description customize-control-description">{{{ data.description }}}</span>
128
		<# } #>
129
130
		<ul>
131
			<# for ( key in data.choices ) { #>
132
				<li>
133
					<label>
134
						<input {{{ data.inputAttrs }}} type="checkbox" value="{{ key }}"<# if ( _.contains( data.value, key ) ) { #> checked<# } #> />
135
						{{ data.choices[ key ] }}
136
					</label>
137
				</li>
138
			<# } #>
139
		</ul>
140
		<?php
141
	}
142
143
	/**
144
	 * Render the control's content.
145
	 *
146
	 * @see WP_Customize_Control::render_content()
147
	 */
148
	protected function render_content() {}
149
}
150