Completed
Push — develop ( 00d08a...f55376 )
by Aristeides
03:14
created

Kirki_Control_MultiCheck   A

Complexity

Total Complexity 7

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 7
lcom 1
cbo 2

4 Methods

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