Kirki_Control_Dimensions   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 66
dl 0
loc 118
rs 10
c 1
b 0
f 0
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
B to_json() 0 14 9
A content_template() 0 3 1
A l10n() 0 27 1
A enqueue() 0 3 1
1
<?php
2
/**
3
 * Customizer Control: dimensions.
4
 *
5
 * @package     Kirki
6
 * @subpackage  Controls
7
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
8
 * @license    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
		wp_enqueue_style( 'kirki-styles', trailingslashit( Kirki::$url ) . 'controls/css/styles.css', array(), KIRKI_VERSION );
62
		wp_localize_script( 'kirki-script', 'dimensionskirkiL10n', $this->l10n() );
63
	}
64
65
	/**
66
	 * An Underscore (JS) template for this control's content (but not its container).
67
	 *
68
	 * Class variables for this control class are available in the `data` JS object;
69
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
70
	 *
71
	 * @see WP_Customize_Control::print_template()
72
	 *
73
	 * @access protected
74
	 */
75
	protected function content_template() {
76
		?>
77
		<label>
78
			<# if ( data.label ) { #><span class="customize-control-title">{{{ data.label }}}</span><# } #>
79
			<# if ( data.description ) { #><span class="description customize-control-description">{{{ data.description }}}</span><# } #>
80
			<div class="wrapper">
81
				<div class="control">
82
					<# for ( choiceKey in data.default ) { #>
83
						<div class="{{ choiceKey }}">
84
							<h5>
85
								<# if ( ! _.isUndefined( data.choices.labels ) && ! _.isUndefined( data.choices.labels[ choiceKey ] ) ) { #>
86
									{{ data.choices.labels[ choiceKey ] }}
87
								<# } else if ( ! _.isUndefined( data.l10n[ choiceKey ] ) ) { #>
88
									{{ data.l10n[ choiceKey ] }}
89
								<# } else { #>
90
									{{ choiceKey }}
91
								<# } #>
92
							</h5>
93
							<div class="{{ choiceKey }} input-wrapper">
94
								<# var val = ( ! _.isUndefined( data.value ) && ! _.isUndefined( data.value[ choiceKey ] ) ) ? data.value[ choiceKey ].toString().replace( '%%', '%' ) : ''; #>
95
								<input {{{ data.inputAttrs }}} type="text" data-choice="{{ choiceKey }}" value="{{ val }}"/>
96
							</div>
97
						</div>
98
					<# } #>
99
				</div>
100
			</div>
101
		</label>
102
		<?php
103
	}
104
105
	/**
106
	 * Returns an array of translation strings.
107
	 *
108
	 * @access protected
109
	 * @since 3.0.0
110
	 * @return array
111
	 */
112
	protected function l10n() {
113
		return array(
114
			'left-top'       => esc_html__( 'Left Top', 'kirki' ),
115
			'left-center'    => esc_html__( 'Left Center', 'kirki' ),
116
			'left-bottom'    => esc_html__( 'Left Bottom', 'kirki' ),
117
			'right-top'      => esc_html__( 'Right Top', 'kirki' ),
118
			'right-center'   => esc_html__( 'Right Center', 'kirki' ),
119
			'right-bottom'   => esc_html__( 'Right Bottom', 'kirki' ),
120
			'center-top'     => esc_html__( 'Center Top', 'kirki' ),
121
			'center-center'  => esc_html__( 'Center Center', 'kirki' ),
122
			'center-bottom'  => esc_html__( 'Center Bottom', 'kirki' ),
123
			'font-size'      => esc_html__( 'Font Size', 'kirki' ),
124
			'font-weight'    => esc_html__( 'Font Weight', 'kirki' ),
125
			'line-height'    => esc_html__( 'Line Height', 'kirki' ),
126
			'font-style'     => esc_html__( 'Font Style', 'kirki' ),
127
			'letter-spacing' => esc_html__( 'Letter Spacing', 'kirki' ),
128
			'word-spacing'   => esc_html__( 'Word Spacing', 'kirki' ),
129
			'top'            => esc_html__( 'Top', 'kirki' ),
130
			'bottom'         => esc_html__( 'Bottom', 'kirki' ),
131
			'left'           => esc_html__( 'Left', 'kirki' ),
132
			'right'          => esc_html__( 'Right', 'kirki' ),
133
			'center'         => esc_html__( 'Center', 'kirki' ),
134
			'size'           => esc_html__( 'Size', 'kirki' ),
135
			'spacing'        => esc_html__( 'Spacing', 'kirki' ),
136
			'width'          => esc_html__( 'Width', 'kirki' ),
137
			'height'         => esc_html__( 'Height', 'kirki' ),
138
			'invalid-value'  => esc_html__( 'Invalid Value', 'kirki' ),
139
		);
140
	}
141
}
142