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

Kirki_Control_Dimension::to_json()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 4
nop 0
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Customizer Control: dimension
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 text control with validation for CSS units.
19
 */
20
class Kirki_Control_Dimension extends WP_Customize_Control {
21
22
	/**
23
	 * The control type.
24
	 *
25
	 * @access public
26
	 * @var string
27
	 */
28
	public $type = 'kirki-dimension';
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
	 * The kirki_config we're using for this control
48
	 *
49
	 * @access public
50
	 * @var string
51
	 */
52
	public $kirki_config = 'global';
53
54
	/**
55
	 * Enqueue control related scripts/styles.
56
	 *
57
	 * @access public
58
	 */
59
	public function enqueue() {
60
61
		wp_enqueue_script( 'kirki-dimension', trailingslashit( Kirki::$url ) . 'controls/dimension/dimension.js', array( 'jquery', 'customize-base' ), false, true );
62
		wp_localize_script( 'kirki-dimension', 'dimensionkirkiL10n', $this->l10n() );
63
		wp_enqueue_style( 'kirki-dimension-css', trailingslashit( Kirki::$url ) . 'controls/dimension/dimension.css', null );
64
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
		$this->json['l10n']    = $this->l10n();
85
86
		$this->json['inputAttrs'] = '';
87
		foreach ( $this->input_attrs as $attr => $value ) {
88
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
89
		}
90
91
	}
92
93
	/**
94
	 * An Underscore (JS) template for this control's content (but not its container).
95
	 *
96
	 * Class variables for this control class are available in the `data` JS object;
97
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
98
	 *
99
	 * @see WP_Customize_Control::print_template()
100
	 *
101
	 * @access protected
102
	 */
103
	protected function content_template() {
104
		?>
105
		<label class="customizer-text">
106
			<# if ( data.label ) { #>
107
				<span class="customize-control-title">{{{ data.label }}}</span>
108
			<# } #>
109
			<# if ( data.description ) { #>
110
				<span class="description customize-control-description">{{{ data.description }}}</span>
111
			<# } #>
112
			<div class="input-wrapper">
113
				<input {{{ data.inputAttrs }}} type="text" value="{{ data.value }}"/>
114
			</div>
115
		</label>
116
		<?php
117
	}
118
119
	/**
120
	 * Returns an array of translation strings.
121
	 *
122
	 * @access protected
123
	 * @since 3.0.0
124
	 * @return string
125
	 */
126
	protected function l10n() {
127
		$translation_strings = array(
128
			'invalid-value' => esc_attr__( 'Invalid Value', 'kirki' ),
129
		);
130
		return apply_filters( "kirki/{$this->kirki_config}/l10n", $translation_strings );
131
	}
132
}
133