Completed
Push — develop ( 656b88...85b7b9 )
by Aristeides
06:46
created

Kirki_Control_Dimension   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A enqueue() 0 9 1
A to_json() 0 19 3
A content_template() 0 11 1
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-dynamic-control', trailingslashit( Kirki::$url ) . 'assets/js/dynamic-control.js', array( 'jquery', 'customize-base' ), false, true );
62
		wp_enqueue_script( 'kirki-dimension', trailingslashit( Kirki::$url ) . 'controls/dimension/dimension.js', array( 'jquery', 'customize-base', 'kirki-dynamic-control' ), false, true );
63
		wp_enqueue_style( 'kirki-dimension-css', trailingslashit( Kirki::$url ) . 'controls/dimension/dimension.css', null );
64
		wp_localize_script( 'kirki-dimension', 'dimensionkirkiL10n', array(
65
			'invalid-value' => esc_attr__( 'Invalid Value', 'kirki' ),
66
		) );
67
	}
68
69
	/**
70
	 * Refresh the parameters passed to the JavaScript via JSON.
71
	 *
72
	 * @see WP_Customize_Control::to_json()
73
	 */
74
	public function to_json() {
75
		parent::to_json();
76
77
		$this->json['default'] = $this->setting->default;
78
		if ( isset( $this->default ) ) {
79
			$this->json['default'] = $this->default;
80
		}
81
		$this->json['output']  = $this->output;
82
		$this->json['value']   = $this->value();
83
		$this->json['choices'] = $this->choices;
84
		$this->json['link']    = $this->get_link();
85
		$this->json['id']      = $this->id;
86
87
		$this->json['inputAttrs'] = '';
88
		foreach ( $this->input_attrs as $attr => $value ) {
89
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
90
		}
91
92
	}
93
94
	/**
95
	 * An Underscore (JS) template for this control's content (but not its container).
96
	 *
97
	 * Class variables for this control class are available in the `data` JS object;
98
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
99
	 *
100
	 * @see WP_Customize_Control::print_template()
101
	 *
102
	 * @access protected
103
	 */
104
	protected function content_template() {
105
		?>
106
		<label class="customizer-text">
107
			<# if ( data.label ) { #><span class="customize-control-title">{{{ data.label }}}</span><# } #>
108
			<# if ( data.description ) { #><span class="description customize-control-description">{{{ data.description }}}</span><# } #>
109
			<div class="input-wrapper">
110
				<input {{{ data.inputAttrs }}} type="text" value="{{ data.value.replace( '%%', '%' ) }}"/>
111
			</div>
112
		</label>
113
		<?php
114
	}
115
}
116