Completed
Pull Request — develop (#1603)
by Aristeides
02:18
created

Kirki_Control_Dimensions::to_json()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 10
nc 8
nop 0
dl 0
loc 18
rs 7.756
c 0
b 0
f 0
1
<?php
2
/**
3
 * Customizer Control: dimensions.
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.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 Kirki_Control_Base {
22
23
	/**
24
	 * The control type.
25
	 *
26
	 * @access public
27
	 * @var string
28
	 */
29
	public $type = 'kirki-dimensions';
30
31
	/**
32
	 * Refresh the parameters passed to the JavaScript via JSON.
33
	 *
34
	 * @see WP_Customize_Control::to_json()
35
	 */
36
	public function to_json() {
37
		parent::to_json();
38
39
		if ( is_array( $this->choices ) ) {
40
			foreach ( $this->choices as $choice => $value ) {
41
				if ( 'labels' !== $choice && true === $value ) {
42
					$this->json['choices'][ $choice ] = true;
43
				}
44
			}
45
		}
46
		if ( is_array( $this->json['default'] ) ) {
47
			foreach ( $this->json['default'] as $key => $value ) {
48
				if ( isset( $this->json['choices'][ $key ] ) && ! isset( $this->json['value'][ $key ] ) ) {
49
					$this->json['value'][ $key ] = $value;
50
				}
51
			}
52
		}
53
	}
54
55
	/**
56
	 * Enqueue control related scripts/styles.
57
	 *
58
	 * @access public
59
	 */
60
	public function enqueue() {
61
62
		wp_enqueue_style( 'kirki-styles', trailingslashit( Kirki::$url ) . 'controls/css/styles.css', null );
63
		wp_localize_script( 'kirki-script', 'dimensionskirkiL10n', $this->l10n() );
64
	}
65
66
	/**
67
	 * An Underscore (JS) template for this control's content (but not its container).
68
	 *
69
	 * Class variables for this control class are available in the `data` JS object;
70
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
71
	 *
72
	 * @see WP_Customize_Control::print_template()
73
	 *
74
	 * @access protected
75
	 */
76
	protected function content_template() {
77
		?>
78
		<label>
79
			<# if ( data.label ) { #><span class="customize-control-title">{{{ data.label }}}</span><# } #>
80
			<# if ( data.description ) { #><span class="description customize-control-description">{{{ data.description }}}</span><# } #>
81
			<div class="wrapper">
82
				<div class="control">
83
					<# for ( choiceKey in data.default ) { #>
84
						<div class="{{ choiceKey }}">
85
							<h5>
86
								<# if ( ! _.isUndefined( data.choices.labels ) && ! _.isUndefined( data.choices.labels[ choiceKey ] ) ) { #>
87
									{{ data.choices.labels[ choiceKey ] }}
88
								<# } else if ( ! _.isUndefined( data.l10n[ choiceKey ] ) ) { #>
89
									{{ data.l10n[ choiceKey ] }}
90
								<# } else { #>
91
									{{ choiceKey }}
92
								<# } #>
93
							</h5>
94
							<div class="{{ choiceKey }} input-wrapper">
95
								<# var val = ( ! _.isUndefined( data.value ) && ! _.isUndefined( data.value[ choiceKey ] ) ) ? data.value[ choiceKey ].replace( '%%', '%' ) : ''; #>
96
								<input {{{ data.inputAttrs }}} type="text" value="{{ val }}"/>
97
							</div>
98
						</div>
99
					<# } #>
100
				</div>
101
			</div>
102
		</label>
103
		<?php
104
	}
105
106
	/**
107
	 * Returns an array of translation strings.
108
	 *
109
	 * @access protected
110
	 * @since 3.0.0
111
	 * @return array
112
	 */
113
	protected function l10n() {
114
		return array(
115
			'left-top'              => esc_attr__( 'Left Top', 'kirki' ),
116
			'left-center'           => esc_attr__( 'Left Center', 'kirki' ),
117
			'left-bottom'           => esc_attr__( 'Left Bottom', 'kirki' ),
118
			'right-top'             => esc_attr__( 'Right Top', 'kirki' ),
119
			'right-center'          => esc_attr__( 'Right Center', 'kirki' ),
120
			'right-bottom'          => esc_attr__( 'Right Bottom', 'kirki' ),
121
			'center-top'            => esc_attr__( 'Center Top', 'kirki' ),
122
			'center-center'         => esc_attr__( 'Center Center', 'kirki' ),
123
			'center-bottom'         => esc_attr__( 'Center Bottom', 'kirki' ),
124
			'font-size'             => esc_attr__( 'Font Size', 'kirki' ),
125
			'font-weight'           => esc_attr__( 'Font Weight', 'kirki' ),
126
			'line-height'           => esc_attr__( 'Line Height', 'kirki' ),
127
			'font-style'            => esc_attr__( 'Font Style', 'kirki' ),
128
			'letter-spacing'        => esc_attr__( 'Letter Spacing', 'kirki' ),
129
			'word-spacing'          => esc_attr__( 'Word Spacing', 'kirki' ),
130
			'top'                   => esc_attr__( 'Top', 'kirki' ),
131
			'bottom'                => esc_attr__( 'Bottom', 'kirki' ),
132
			'left'                  => esc_attr__( 'Left', 'kirki' ),
133
			'right'                 => esc_attr__( 'Right', 'kirki' ),
134
			'center'                => esc_attr__( 'Center', 'kirki' ),
135
			'size'                  => esc_attr__( 'Size', 'kirki' ),
136
			'height'                => esc_attr__( 'Height', 'kirki' ),
137
			'spacing'               => esc_attr__( 'Spacing', 'kirki' ),
138
			'width'                 => esc_attr__( 'Width', 'kirki' ),
139
			'height'                => esc_attr__( 'Height', 'kirki' ),
140
			'invalid-value'         => esc_attr__( 'Invalid Value', 'kirki' ),
141
		);
142
	}
143
}
144