Completed
Pull Request — develop (#1282)
by Aristeides
03:07
created

Kirki_Control_Select::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: kirki-select.
4
 *
5
 * @package     Kirki
6
 * @subpackage  Controls
7
 * @copyright   Copyright (c) 2016, 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
 * Select control.
19
 */
20
class Kirki_Control_Select extends WP_Customize_Control {
21
22
	/**
23
	 * The control type.
24
	 *
25
	 * @access public
26
	 * @var string
27
	 */
28
	public $type = 'kirki-select';
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
	 * Maximum number of options the user will be able to select.
48
	 * Set to 1 for single-select.
49
	 *
50
	 * @access public
51
	 * @var int
52
	 */
53
	public $multiple = 1;
54
55
	/**
56
	 * Enqueue control related scripts/styles.
57
	 *
58
	 * @access public
59
	 */
60
	public function enqueue() {
61
		wp_enqueue_script( 'select2', trailingslashit( Kirki::$url ) . 'controls/select/select2/js/select2.full.js', array( 'jquery' ), false, true );
62
		wp_enqueue_style( 'select2', trailingslashit( Kirki::$url ) . 'controls/select/select2/css/select2.min.css', null );
63
		wp_enqueue_script( 'kirki-select', trailingslashit( Kirki::$url ) . 'controls/select/select.js', array( 'jquery', 'customize-base', 'select2', 'jquery-ui-sortable' ), false, true );
64
		wp_enqueue_style( 'kirki-select-css', trailingslashit( Kirki::$url ) . 'controls/select/select.css', null );
65
	}
66
67
	/**
68
	 * Refresh the parameters passed to the JavaScript via JSON.
69
	 *
70
	 * @see WP_Customize_Control::to_json()
71
	 */
72
	public function to_json() {
73
		parent::to_json();
74
75
		$this->json['default'] = $this->setting->default;
76
		if ( isset( $this->default ) ) {
77
			$this->json['default'] = $this->default;
78
		}
79
		$this->json['output']  = $this->output;
80
		$this->json['value']   = $this->value();
81
		$this->json['choices'] = $this->choices;
82
		$this->json['link']    = $this->get_link();
83
		$this->json['id']      = $this->id;
84
85
		if ( 'user_meta' === $this->option_type ) {
86
			$this->json['value'] = get_user_meta( get_current_user_id(), $this->id, true );
87
		}
88
89
		$this->json['inputAttrs'] = '';
90
		foreach ( $this->input_attrs as $attr => $value ) {
91
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
92
		}
93
94
		$this->json['multiple'] = $this->multiple;
95
	}
96
97
98
	/**
99
	 * An Underscore (JS) template for this control's content (but not its container).
100
	 *
101
	 * Class variables for this control class are available in the `data` JS object;
102
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
103
	 *
104
	 * @see WP_Customize_Control::print_template()
105
	 *
106
	 * @access protected
107
	 */
108
	protected function content_template() {
109
		?>
110
		<# if ( ! data.choices ) {
111
			return;
112
		}
113
		if ( 1 < data.multiple && data.value && 'string' === typeof data.value ) {
114
			data.value = [ data.value ];
115
		}
116
		#>
117
		<label>
118
			<# if ( data.label ) { #>
119
				<span class="customize-control-title">{{ data.label }}</span>
120
			<# } #>
121
			<# if ( data.description ) { #>
122
				<span class="description customize-control-description">{{{ data.description }}}</span>
123
			<# } #>
124
			<select {{{ data.inputAttrs }}} {{{ data.link }}}<# if ( 1 < data.multiple ) { #> data-multiple="{{ data.multiple }}" multiple="multiple"<# } #>>
125
				<# for ( key in data.choices ) { #>
126
					<#
127
					selected = ( data.value === key );
128
					if ( 1 < data.multiple && data.value ) {
129
						_.each( data.value, function( value ) {
130
							if ( key === value ) {
131
								selected = true;
132
							}
133
						});
134
					}
135
					#>
136
					<option value="{{ key }}"<# if ( selected ) { #> selected <# } #>>{{ data.choices[ key ] }}</option>
137
				<# } #>
138
			</select>
139
		</label>
140
		<?php
141
	}
142
143
144
	/**
145
	 * Render the control's content.
146
	 *
147
	 * Allows the content to be overridden without having to rewrite the wrapper in `$this::render()`.
148
	 *
149
	 * Supports basic input types `text`, `checkbox`, `textarea`, `radio`, `select` and `dropdown-pages`.
150
	 * Additional input types such as `email`, `url`, `number`, `hidden` and `date` are supported implicitly.
151
	 *
152
	 * Control content can alternately be rendered in JS. See WP_Customize_Control::print_template().
153
	 *
154
	 * @since 3.0.0
155
	 */
156
	protected function render_content() {}
157
}
158