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

Kirki_Control_Preset   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 95
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A enqueue() 0 7 1
A to_json() 0 19 3
A content_template() 0 20 1
1
<?php
2
/**
3
 * Customizer Control: preset.
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       2.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Preset control (modified select).
19
 */
20
class Kirki_Control_Preset extends WP_Customize_Control {
21
22
	/**
23
	 * The control type.
24
	 *
25
	 * @access public
26
	 * @var string
27
	 */
28
	public $type = 'kirki-preset';
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
		wp_register_script( 'kirki-set-setting-value', trailingslashit( Kirki::$url ) . 'controls/preset/set-setting-value.js' );
54
		wp_enqueue_script( 'kirki-preset', trailingslashit( Kirki::$url ) . 'controls/preset/preset.js', array( 'jquery', 'customize-base', 'kirki-set-setting-value' ), false, true );
55
		wp_enqueue_style( 'kirki-preset-css', trailingslashit( Kirki::$url ) . 'controls/preset/preset.css', null );
56
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
		<# if ( ! data.choices ) return; #>
97
		<label>
98
			<# if ( data.label ) { #>
99
				<span class="customize-control-title">{{ data.label }}</span>
100
			<# } #>
101
			<# if ( data.description ) { #>
102
				<span class="description customize-control-description">{{{ data.description }}}</span>
103
			<# } #>
104
			<select {{{ data.inputAttrs }}} {{{ data.link }}} data-multiple="1">
105
				<# for ( key in data.choices ) { #>
106
					<option value="{{ key }}"<# if ( key === data.value ) { #>selected<# } #>>
107
						{{ data.choices[ key ]['label'] }}
108
					</option>
109
				<# } #>
110
			</select>
111
		</label>
112
		<?php
113
	}
114
}
115