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

Kirki_Control_Generic::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: kirki-generic.
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
 * A generic and pretty abstract control.
19
 * Allows for great manipulation using the field's "choices" argumnent.
20
 */
21
class Kirki_Control_Generic extends WP_Customize_Control {
22
23
	/**
24
	 * The control type.
25
	 *
26
	 * @access public
27
	 * @var string
28
	 */
29
	public $type = 'kirki-generic';
30
31
	/**
32
	 * Used to automatically generate all CSS output.
33
	 *
34
	 * @access public
35
	 * @var array
36
	 */
37
	public $output = array();
38
39
	/**
40
	 * Data type
41
	 *
42
	 * @access public
43
	 * @var string
44
	 */
45
	public $option_type = 'theme_mod';
46
47
	/**
48
	 * Enqueue control related scripts/styles.
49
	 *
50
	 * @access public
51
	 */
52
	public function enqueue() {
53
		wp_enqueue_script( 'kirki-generic', trailingslashit( Kirki::$url ) . 'controls/generic/generic.js', array( 'jquery', 'customize-base' ), false, true );
54
		wp_enqueue_style( 'kirki-generic-css', trailingslashit( Kirki::$url ) . 'controls/generic/generic.css', null );
55
	}
56
57
	/**
58
	 * Refresh the parameters passed to the JavaScript via JSON.
59
	 *
60
	 * @see WP_Customize_Control::to_json()
61
	 */
62
	public function to_json() {
63
		parent::to_json();
64
65
		$this->json['default'] = $this->setting->default;
66
		if ( isset( $this->default ) ) {
67
			$this->json['default'] = $this->default;
68
		}
69
		$this->json['output']  = $this->output;
70
		$this->json['value']   = $this->value();
71
		$this->json['choices'] = $this->choices;
72
		$this->json['link']    = $this->get_link();
73
		$this->json['id']      = $this->id;
74
75
		$this->json['inputAttrs'] = '';
76
		foreach ( $this->input_attrs as $attr => $value ) {
77
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
78
		}
79
80
	}
81
82
	/**
83
	 * An Underscore (JS) template for this control's content (but not its container).
84
	 *
85
	 * Class variables for this control class are available in the `data` JS object;
86
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
87
	 *
88
	 * @see WP_Customize_Control::print_template()
89
	 *
90
	 * @access protected
91
	 */
92
	protected function content_template() {
93
		?>
94
		<label>
95
			<# if ( data.label ) { #>
96
				<span class="customize-control-title">{{{ data.label }}}</span>
97
			<# } #>
98
			<# if ( data.description ) { #>
99
				<span class="description customize-control-description">{{{ data.description }}}</span>
100
			<# } #>
101
			<div class="customize-control-content">
102
				<# if ( 'textarea' == data.choices['element'] ) { #>
103
					<textarea {{{ data.inputAttrs }}} {{{ data.link }}} <# for ( key in data.choices ) { #> {{ key }}="{{ data.choices[ key ] }}"<# } #>>{{ data.value }}</textarea>
104
				<# } else { #>
105
					<# var element = ( data.choices.element ) ? data.choices.element : 'input'; #>
106
					<{{ element }}
107
						{{{ data.inputAttrs }}}
108
						value="{{ data.value }}"
109
						{{{ data.link }}}
110
						<# for ( key in data.choices ) { #>
111
							{{ key }}="{{ data.choices[ key ] }}"
112
						<# } #>
113
						<# if ( data.choices.content ) { #>
114
							>{{{ data.choices.content }}}</{{ element }}>
115
						<# } else { #>
116
							/>
117
						<# } #>
118
				<# } #>
119
			</div>
120
		</label>
121
		<?php
122
	}
123
}
124