Completed
Push — develop ( 25b4bc...d8be36 )
by Aristeides
02:18
created

Kirki_Control_Dimensions::l10n()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 31
nc 2
nop 1
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Customizer Control: dimensions.
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       2.1
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Dimensions control.
19
 * multiple fields with CSS units validation.
20
 */
21
class Kirki_Control_Dimensions extends WP_Customize_Control {
22
23
	/**
24
	 * The control type.
25
	 *
26
	 * @access public
27
	 * @var string
28
	 */
29
	public $type = 'kirki-dimensions';
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
	 * The kirki_config we're using for this control
49
	 *
50
	 * @access public
51
	 * @var string
52
	 */
53
	public $kirki_config = 'global';
54
55
	/**
56
	 * The translation strings.
57
	 *
58
	 * @access protected
59
	 * @since 2.3.5
60
	 * @var array
61
	 */
62
	protected $l10n = array();
63
64
	/**
65
	 * Refresh the parameters passed to the JavaScript via JSON.
66
	 *
67
	 * @see WP_Customize_Control::to_json()
68
	 */
69
	public function to_json() {
70
		parent::to_json();
71
72
		$this->json['default'] = $this->setting->default;
73
		if ( isset( $this->default ) ) {
74
			$this->json['default'] = $this->default;
75
		}
76
		$this->json['output']      = $this->output;
77
		$this->json['value']       = $this->value();
78
		$this->json['choices']     = $this->choices;
79
		$this->json['link']        = $this->get_link();
80
		$this->json['id']          = $this->id;
81
		$this->json['l10n']        = $this->l10n();
82
		$this->json['kirkiConfig'] = $this->kirki_config;
83
84
		if ( 'user_meta' === $this->option_type ) {
85
			$this->json['value'] = get_user_meta( get_current_user_id(), $this->id, true );
86
		}
87
88
		$this->json['inputAttrs'] = '';
89
		foreach ( $this->input_attrs as $attr => $value ) {
90
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
91
		}
92
93
		if ( is_array( $this->choices ) ) {
94
			foreach ( $this->choices as $choice => $value ) {
95
				if ( 'labels' !== $choice && true === $value ) {
96
					$this->json['choices'][ $choice ] = true;
97
				}
98
			}
99
		}
100
		if ( is_array( $this->json['default'] ) ) {
101
			foreach ( $this->json['default'] as $key => $value ) {
102
				if ( isset( $this->json['choices'][ $key ] ) && ! isset( $this->json['value'][ $key ] ) ) {
103
					$this->json['value'][ $key ] = $value;
104
				}
105
			}
106
		}
107
	}
108
109
	/**
110
	 * Enqueue control related scripts/styles.
111
	 *
112
	 * @access public
113
	 */
114
	public function enqueue() {
115
		wp_enqueue_script( 'kirki-dimensions', trailingslashit( Kirki::$url ) . 'controls/dimensions/dimensions.js', array( 'jquery', 'customize-base' ), false, true );
116
		wp_enqueue_style( 'kirki-dimensions-css', trailingslashit( Kirki::$url ) . 'controls/dimensions/dimensions.css', null );
117
	}
118
119
	/**
120
	 * An Underscore (JS) template for this control's content (but not its container).
121
	 *
122
	 * Class variables for this control class are available in the `data` JS object;
123
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
124
	 *
125
	 * @see WP_Customize_Control::print_template()
126
	 *
127
	 * @access protected
128
	 */
129
	protected function content_template() {
130
		?>
131
		<label>
132
			<# if ( data.label ) { #>
133
				<span class="customize-control-title">{{{ data.label }}}</span>
134
			<# } #>
135
			<# if ( data.description ) { #>
136
				<span class="description customize-control-description">{{{ data.description }}}</span>
137
			<# } #>
138
			<div class="wrapper">
139
				<div class="control">
140
					<# for ( choiceKey in data.default ) { #>
141
						<div class="{{ choiceKey }}">
142
							<h5>
143
								<# if ( 'undefined' !== typeof data.choices.labels && 'undefined' !== typeof data.choices.labels[ choiceKey ] ) { #>
144
									{{ data.choices.labels[ choiceKey ] }}
145
								<# } else if ( 'undefined' !== typeof data.l10n[ choiceKey ] ) { #>
146
									{{ data.l10n[ choiceKey ] }}
147
								<# } else { #>
148
									{{ choiceKey }}
149
								<# } #>
150
							</h5>
151
							<div class="{{ choiceKey }} input-wrapper">
152
								<input {{{ data.inputAttrs }}} type="text" value="{{ data.value[ choiceKey ] }}"/>
153
							</div>
154
						</div>
155
					<# } #>
156
				</div>
157
			</div>
158
		</label>
159
		<?php
160
	}
161
162
	/**
163
	 * Render the control's content.
164
	 *
165
	 * @see WP_Customize_Control::render_content()
166
	 */
167
	protected function render_content() {}
168
169
		/**
170
		 * Returns an array of translation strings.
171
		 *
172
		 * @access protected
173
		 * @since 2.4.0
174
		 * @param string|false $id The string-ID.
175
		 * @return string
176
		 */
177
		protected function l10n( $id = false ) {
178
			$translation_strings = array(
179
				'left-top'              => esc_attr__( 'Left Top', 'kirki' ),
180
				'left-center'           => esc_attr__( 'Left Center', 'kirki' ),
181
				'left-bottom'           => esc_attr__( 'Left Bottom', 'kirki' ),
182
				'right-top'             => esc_attr__( 'Right Top', 'kirki' ),
183
				'right-center'          => esc_attr__( 'Right Center', 'kirki' ),
184
				'right-bottom'          => esc_attr__( 'Right Bottom', 'kirki' ),
185
				'center-top'            => esc_attr__( 'Center Top', 'kirki' ),
186
				'center-center'         => esc_attr__( 'Center Center', 'kirki' ),
187
				'center-bottom'         => esc_attr__( 'Center Bottom', 'kirki' ),
188
				'font-size'             => esc_attr__( 'Font Size', 'kirki' ),
189
				'font-weight'           => esc_attr__( 'Font Weight', 'kirki' ),
190
				'line-height'           => esc_attr__( 'Line Height', 'kirki' ),
191
				'font-style'            => esc_attr__( 'Font Style', 'kirki' ),
192
				'letter-spacing'        => esc_attr__( 'Letter Spacing', 'kirki' ),
193
				'word-spacing'          => esc_attr__( 'Word Spacing', 'kirki' ),
194
				'top'                   => esc_attr__( 'Top', 'kirki' ),
195
				'bottom'                => esc_attr__( 'Bottom', 'kirki' ),
196
				'left'                  => esc_attr__( 'Left', 'kirki' ),
197
				'right'                 => esc_attr__( 'Right', 'kirki' ),
198
				'center'                => esc_attr__( 'Center', 'kirki' ),
199
				'size'                  => esc_attr__( 'Size', 'kirki' ),
200
				'height'                => esc_attr__( 'Height', 'kirki' ),
201
				'spacing'               => esc_attr__( 'Spacing', 'kirki' ),
202
				'width'                 => esc_attr__( 'Width', 'kirki' ),
203
				'height'                => esc_attr__( 'Height', 'kirki' ),
204
			);
205
			$translation_strings = apply_filters( 'kirki/' . $this->kirki_config . '/l10n', $translation_strings );
206
			if ( false === $id ) {
207
				return $translation_strings;
208
			}
209
			return $translation_strings[ $id ];
210
		}
211
}
212