Completed
Push — develop ( 9f3e22...fba03d )
by Aristeides
02:33
created

Kirki_Control_Number::to_json()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 8
nop 0
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
1
<?php
2
/**
3
 * Customizer Control: number.
4
 *
5
 * @package     Kirki
6
 * @subpackage  Controls
7
 * @copyright   Copyright (c) 2015, Aristeides Stathopoulos
8
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Create a simple number control
19
 */
20
class Kirki_Control_Number extends WP_Customize_Control {
21
22
	/**
23
	 * The control type.
24
	 *
25
	 * @access public
26
	 * @var string
27
	 */
28
	public $type = 'kirki-number';
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
		wp_enqueue_script( 'kirki-number', trailingslashit( Kirki::$url ) . 'controls/number/number.js', array( 'jquery', 'customize-base', 'jquery-ui-button', 'jquery-ui-spinner' ), false, true );
61
		wp_localize_script( 'kirki-number', 'kirkiL10n', $this->l10n() );
62
		wp_enqueue_style( 'kirki-number-css', trailingslashit( Kirki::$url ) . 'controls/number/number.css', null );
63
	}
64
65
	/**
66
	 * Refresh the parameters passed to the JavaScript via JSON.
67
	 *
68
	 * @see WP_Customize_Control::to_json()
69
	 */
70
	public function to_json() {
71
		parent::to_json();
72
73
		$this->json['default'] = $this->setting->default;
74
		if ( isset( $this->default ) ) {
75
			$this->json['default'] = $this->default;
76
		}
77
		$this->json['output']  = $this->output;
78
		$this->json['value']   = $this->value();
79
		$this->json['choices'] = $this->choices;
80
		$this->json['link']    = $this->get_link();
81
		$this->json['id']      = $this->id;
82
		$this->json['l10n']    = $this->l10n();
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
	}
94
95
	/**
96
	 * An Underscore (JS) template for this control's content (but not its container).
97
	 *
98
	 * Class variables for this control class are available in the `data` JS object;
99
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
100
	 *
101
	 * @see WP_Customize_Control::print_template()
102
	 *
103
	 * @access protected
104
	 */
105
	protected function content_template() {
106
		?>
107
		<label>
108
			<# if ( data.label ) { #>
109
				<span class="customize-control-title">{{{ data.label }}}</span>
110
			<# } #>
111
			<# if ( data.description ) { #>
112
				<span class="description customize-control-description">{{{ data.description }}}</span>
113
			<# } #>
114
			<div class="customize-control-content">
115
				<input {{{ data.inputAttrs }}} type="text" {{{ data.link }}} value="{{ data.value }}" />
116
			</div>
117
		</label>
118
		<?php
119
	}
120
121
	/**
122
	 * Returns an array of translation strings.
123
	 *
124
	 * @access protected
125
	 * @since 3.0.0
126
	 * @param string|false $id The string-ID.
127
	 * @return string
128
	 */
129
	protected function l10n( $id = false ) {
130
		$translation_strings = array(
131
			'min-error'  => esc_attr__( 'Value lower than allowed minimum', 'kirki' ),
132
			'max-error'  => esc_attr__( 'Value higher than allowed maximum', 'kirki' ),
133
			'step-error' => esc_attr__( 'Invalid Value', 'kirki' ),
134
		);
135
		return apply_filters( 'kirki/' . $this->kirki_config . '/l10n', $translation_strings );
136
	}
137
}
138