Completed
Pull Request — develop (#1283)
by Aristeides
02:38
created

Kirki_Control_Background::enqueue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Customizer Control: background.
4
 *
5
 * Creates a new custom control.
6
 * Custom controls contains all background-related options.
7
 *
8
 * @package     Kirki
9
 * @subpackage  Controls
10
 * @copyright   Copyright (c) 2016, Aristeides Stathopoulos
11
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
12
 * @since       1.0
13
 */
14
15
/**
16
 * Adds multiple input fiels that combined make up the background control.
17
 */
18
class Kirki_Control_Background extends WP_Customize_Control {
19
20
	/**
21
	 * The control type.
22
	 *
23
	 * @access public
24
	 * @var string
25
	 */
26
	public $type = 'kirki-background';
27
28
	/**
29
	 * Used to automatically generate all CSS output.
30
	 *
31
	 * @access public
32
	 * @var array
33
	 */
34
	public $output = array();
35
36
	/**
37
	 * Data type
38
	 *
39
	 * @access public
40
	 * @var string
41
	 */
42
	public $option_type = 'theme_mod';
43
44
	/**
45
	 * Enqueue control related scripts/styles.
46
	 *
47
	 * @access public
48
	 */
49
	public function enqueue() {
50
		wp_enqueue_script( 'wp-color-picker-alpha', trailingslashit( Kirki::$url ) . 'controls/background/wp-color-picker-alpha.js', array( 'wp-color-picker' ), '1.2', true );
51
		wp_enqueue_style( 'wp-color-picker' );
52
53
		wp_enqueue_script( 'kirki-background', trailingslashit( Kirki::$url ) . 'controls/background/background.js', array( 'jquery', 'wp-color-picker-alpha' ) );
54
		wp_enqueue_style( 'kirki-background', trailingslashit( Kirki::$url ) . 'controls/background/background.css', null );
55
	}
56
57
	/**
58
	 * Refresh the parameters passed to the JavaScript via JSON.
59
	 *
60
	 * @access public
61
	 */
62
	public function to_json() {
63
		parent::to_json();
64
65
		$this->json['default'] = $this->setting->default;
66
		if ( isset( $this->default ) ) {
67
			$this->json['default'] = $this->default;
68
		}
69
		$this->json['output']  = $this->output;
70
		$this->json['value']   = $this->value();
71
		$this->json['choices'] = $this->choices;
72
		$this->json['link']    = $this->get_link();
73
		$this->json['id']      = $this->id;
74
75
		if ( 'user_meta' === $this->option_type ) {
76
			$this->json['value'] = get_user_meta( get_current_user_id(), $this->id, true );
77
		}
78
79
		$this->json['inputAttrs'] = '';
80
		foreach ( $this->input_attrs as $attr => $value ) {
81
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
82
		}
83
		$config_id = 'global';
84
		$this->json['l10n'] = $this->l10n( $config_id );
85
	}
86
87
	/**
88
	 * An Underscore (JS) template for this control's content (but not its container).
89
	 *
90
	 * Class variables for this control class are available in the `data` JS object;
91
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
92
	 *
93
	 * @see WP_Customize_Control::print_template()
94
	 *
95
	 * @access protected
96
	 */
97
	protected function content_template() {
98
		?>
99
		<label>
100
			<span class="customize-control-title">
101
				{{{ data.label }}}
102
			</span>
103
			<# if ( data.description ) { #>
104
				<span class="description customize-control-description">{{{ data.description }}}</span>
105
			<# } #>
106
		</label>
107
		<div class="background-wrapper">
108
109
			<!-- background-color -->
110
			<div class="background-color">
111
				<h4>{{ data.l10n['background-color'] }}</h4>
112
				<input type="text" data-default-color="{{ data.default['background-color'] }}" value="{{ data.value['background-color'] }}" class="kirki-color-control color-picker" {{{ data.link }}} />
113
			</div>
114
115
			<!-- background-image -->
116
			<div class="background-image">
117
				<h4>{{ data.l10n['background-image'] }}</h4>
118
				<div class="attachment-media-view background-image-upload">
119
					<# if ( data.value['background-image'] ) { #>
120
						<div class="thumbnail thumbnail-image">
121
							<img src="{{ data.value['background-image'] }}" alt="" />
122
						</div>
123
					<# } else { #>
124
						<div class="placeholder">
125
							{{ data.l10n['no-file-selected'] }}
126
						</div>
127
					<# } #>
128
					<div class="actions">
129
						<button class="button background-image-upload-remove-button<# if ( ! data.value['background-image'] ) { #> hidden <# } #>">
130
							{{ data.l10n['remove'] }}
131
						</button>
132
						<button type="button" class="button background-image-upload-button">
133
							{{ data.l10n['select-file'] }}
134
						</button>
135
					</div>
136
				</div>
137
			</div>
138
139
			<!-- background-repeat -->
140
			<div class="background-repeat">
141
				<h4>{{ data.l10n['background-repeat'] }}</h4>
142
				<#
143
				var repeats = [
144
						'no-repeat',
145
						'repeat-all',
146
						'repeat-x',
147
						'repeat-y'
148
					];
149
				#>
150
				<select {{{ data.inputAttrs }}} {{{ data.link }}}>
151
					<# _.each( repeats, function( repeat ) { #>
152
						<option value="{{ repeat }}"<# if ( repeat === data.value['background-repeat'] ) { #> selected <# } #>>{{ data.l10n[ repeat ] }}</option>
153
					<# }); #>
154
				</select>
155
			</div>
156
157
			<!-- background-size -->
158
			<div class="background-size">
159
				<h4>{{ data.l10n['background-size'] }}</h4>
160
				<#
161
				var sizes = [
162
						'cover',
163
						'contain',
164
						'auto'
165
					];
166
				#>
167
				<select {{{ data.inputAttrs }}} {{{ data.link }}}>
168
					<# _.each( sizes, function( size ) { #>
169
						<option value="{{ size }}"<# if ( size === data.value['background-size'] ) { #> selected <# } #>>{{ data.l10n[ size ] }}</option>
170
					<# }); #>
171
				</select>
172
			</div>
173
174
			<!-- background-attachment -->
175
			<!-- <div class="background-attachment">
176
				<h4>{{ data.l10n['background-attachment'] }}</h4>
177
			</div> -->
178
179
			<!-- background-position -->
180
			<div class="background-position">
181
				<h4>{{ data.l10n['background-position'] }}</h4>
182
				<#
183
				var positions = [
184
						'left top',
185
						'left center',
186
						'left bottom',
187
						'right top',
188
						'right center',
189
						'right bottom',
190
						'center top',
191
						'center center',
192
						'center bottom'
193
					];
194
				#>
195
				<select {{{ data.inputAttrs }}} {{{ data.link }}}>
196
					<# _.each( positions, function( position ) { #>
197
						<option value="{{ position }}"<# if ( position === data.value['background-position'] ) { #> selected <# } #>>{{ data.l10n[ position ] }}</option>
198
					<# }); #>
199
				</select>
200
			</div>
201
		<?php
202
	}
203
204
	/**
205
	 * Returns an array of translation strings.
206
	 *
207
	 * @access protected
208
	 * @since 3.0.0
209
	 * @param string|false $config_id The string-ID.
210
	 * @return array
211
	 */
212
	protected function l10n( $config_id ) {
213
		$translation_strings = array(
214
			'background-color'      => esc_attr__( 'Background Color', 'kirki' ),
215
			'background-image'      => esc_attr__( 'Background Image', 'kirki' ),
216
			'background-repeat'     => esc_attr__( 'Background Repeat', 'kirki' ),
217
			'background-attachment' => esc_attr__( 'Background Attachment', 'kirki' ),
218
			'background-position'   => esc_attr__( 'Background Position', 'kirki' ),
219
			'background-size'       => esc_attr__( 'Background Size', 'kirki' ),
220
221
			'no-repeat'             => esc_attr__( 'No Repeat', 'kirki' ),
222
			'repeat-all'            => esc_attr__( 'Repeat All', 'kirki' ),
223
			'repeat-x'              => esc_attr__( 'Repeat Horizontally', 'kirki' ),
224
			'repeat-y'              => esc_attr__( 'Repeat Vertically', 'kirki' ),
225
226
			'cover'                 => esc_attr__( 'Cover', 'kirki' ),
227
			'contain'               => esc_attr__( 'Contain', 'kirki' ),
228
			'auto'                  => esc_attr__( 'Auto', 'kirki' ),
229
			'inherit'               => esc_attr__( 'Inherit', 'kirki' ),
230
231
			'fixed'                 => esc_attr__( 'Fixed', 'kirki' ),
232
			'scroll'                => esc_attr__( 'Scroll', 'kirki' ),
233
234
			'left top'              => esc_attr__( 'Left Top', 'kirki' ),
235
			'left center'           => esc_attr__( 'Left Center', 'kirki' ),
236
			'left bottom'           => esc_attr__( 'Left Bottom', 'kirki' ),
237
			'right top'             => esc_attr__( 'Right Top', 'kirki' ),
238
			'right center'          => esc_attr__( 'Right Center', 'kirki' ),
239
			'right bottom'          => esc_attr__( 'Right Bottom', 'kirki' ),
240
			'center top'            => esc_attr__( 'Center Top', 'kirki' ),
241
			'center center'         => esc_attr__( 'Center Center', 'kirki' ),
242
			'center bottom'         => esc_attr__( 'Center Bottom', 'kirki' ),
243
244
			'no-file-selected'      => esc_attr__( 'No File Selected', 'kirki' ),
245
			'remove'                => esc_attr__( 'Remove', 'kirki' ),
246
			'select-file'           => esc_attr__( 'Select File', 'kirki' ),
247
		);
248
		return apply_filters( 'kirki/' . $config_id . '/l10n', $translation_strings );
249
	}
250
}
251