Completed
Push — develop ( 6bc4f7...cab9c0 )
by Aristeides
04:02
created

Kirki_Control_Background::to_json()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 4
nop 0
dl 0
loc 20
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) 2017, 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 ) . 'assets/vendor/wp-color-picker-alpha/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
		$this->json['inputAttrs'] = '';
76
		foreach ( $this->input_attrs as $attr => $value ) {
77
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
78
		}
79
		$config_id = 'global';
80
		$this->json['l10n'] = $this->l10n( $config_id );
81
	}
82
83
	/**
84
	 * Render the control's content.
85
	 *
86
	 * Allows the content to be overridden without having to rewrite the wrapper in `$this::render()`.
87
	 *
88
	 * Supports basic input types `text`, `checkbox`, `textarea`, `radio`, `select` and `dropdown-pages`.
89
	 * Additional input types such as `email`, `url`, `number`, `hidden` and `date` are supported implicitly.
90
	 *
91
	 * Control content can alternately be rendered in JS. See WP_Customize_Control::print_template().
92
	 *
93
	 * @since 3.4.0
94
	 */
95
	protected function render_content() {}
96
97
	/**
98
	 * An Underscore (JS) template for this control's content (but not its container).
99
	 *
100
	 * Class variables for this control class are available in the `data` JS object;
101
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
102
	 *
103
	 * @see WP_Customize_Control::print_template()
104
	 *
105
	 * @access protected
106
	 */
107
	protected function content_template() {
108
		?>
109
		<label>
110
			<span class="customize-control-title">
111
				{{{ data.label }}}
112
			</span>
113
			<# if ( data.description ) { #>
114
				<span class="description customize-control-description">{{{ data.description }}}</span>
115
			<# } #>
116
		</label>
117
		<div class="background-wrapper">
118
119
			<!-- background-color -->
120
			<div class="background-color">
121
				<h4>{{ data.l10n['background-color'] }}</h4>
122
				<input type="text" data-default-color="{{ data.default['background-color'] }}" data-alpha="true" value="{{ data.value['background-color'] }}" class="kirki-color-control color-picker" {{{ data.link }}} />
123
			</div>
124
125
			<!-- background-image -->
126
			<div class="background-image">
127
				<h4>{{ data.l10n['background-image'] }}</h4>
128
				<div class="attachment-media-view background-image-upload">
129
					<# if ( data.value['background-image'] ) { #>
130
						<div class="thumbnail thumbnail-image">
131
							<img src="{{ data.value['background-image'] }}" alt="" />
132
						</div>
133
					<# } else { #>
134
						<div class="placeholder">
135
							{{ data.l10n['no-file-selected'] }}
136
						</div>
137
					<# } #>
138
					<div class="actions">
139
						<button class="button background-image-upload-remove-button<# if ( ! data.value['background-image'] ) { #> hidden <# } #>">
140
							{{ data.l10n['remove'] }}
141
						</button>
142
						<button type="button" class="button background-image-upload-button">
143
							{{ data.l10n['select-file'] }}
144
						</button>
145
					</div>
146
				</div>
147
			</div>
148
149
			<!-- background-repeat -->
150
			<div class="background-repeat">
151
				<h4>{{ data.l10n['background-repeat'] }}</h4>
152
				<#
153
				var repeats = [
154
						'no-repeat',
155
						'repeat-all',
156
						'repeat-x',
157
						'repeat-y'
158
					];
159
				#>
160
				<select {{{ data.inputAttrs }}} {{{ data.link }}}>
161
					<# _.each( repeats, function( repeat ) { #>
162
						<option value="{{ repeat }}"<# if ( repeat === data.value['background-repeat'] ) { #> selected <# } #>>{{ data.l10n[ repeat ] }}</option>
163
					<# }); #>
164
				</select>
165
			</div>
166
167
			<!-- background-position -->
168
			<div class="background-position">
169
				<h4>{{ data.l10n['background-position'] }}</h4>
170
				<#
171
				var positions = [
172
						'left top',
173
						'left center',
174
						'left bottom',
175
						'right top',
176
						'right center',
177
						'right bottom',
178
						'center top',
179
						'center center',
180
						'center bottom'
181
					];
182
				#>
183
				<select {{{ data.inputAttrs }}} {{{ data.link }}}>
184
					<# _.each( positions, function( position ) { #>
185
						<option value="{{ position }}"<# if ( position === data.value['background-position'] ) { #> selected <# } #>>{{ data.l10n[ position ] }}</option>
186
					<# }); #>
187
				</select>
188
			</div>
189
190
			<!-- background-size -->
191
			<div class="background-size">
192
				<h4>{{ data.l10n['background-size'] }}</h4>
193
				<#
194
				var sizes = [
195
						'cover',
196
						'contain',
197
						'auto'
198
					];
199
				#>
200
				<div class="buttonset">
201
					<# _.each( sizes, function( size ) { #>
202
						<input {{{ data.inputAttrs }}} class="switch-input screen-reader-text" type="radio" value="{{ size }}" name="_customize-bg-{{{ data.id }}}-size" id="{{ data.id }}{{ size }}" {{{ data.link }}}<# if ( size === data.value['background-size'] ) { #> checked="checked" <# } #>>
203
							<label class="switch-label switch-label-<# if ( size === data.value['background-size'] ) { #>on <# } else { #>off<# } #>" for="{{ data.id }}{{ size }}">
204
								{{ data.l10n[ size ] }}
205
							</label>
206
						</input>
207
					<# }); #>
208
				</div>
209
			</div>
210
211
			<!-- background-attachment -->
212
			<div class="background-attachment">
213
				<h4>{{ data.l10n['background-attachment'] }}</h4>
214
				<#
215
				var attachments = [
216
						'scroll',
217
						'fixed',
218
						'local'
219
					];
220
				#>
221
				<div class="buttonset">
222
					<# _.each( attachments, function( attachment ) { #>
223
						<input {{{ data.inputAttrs }}} class="switch-input screen-reader-text" type="radio" value="{{ attachment }}" name="_customize-bg-{{{ data.id }}}-attachment" id="{{ data.id }}{{ attachment }}" {{{ data.link }}}<# if ( attachment === data.value['background-attachment'] ) { #> checked="checked" <# } #>>
224
							<label class="switch-label switch-label-<# if ( attachment === data.value['background-attachment'] ) { #>on <# } else { #>off<# } #>" for="{{ data.id }}{{ attachment }}">
225
								{{ data.l10n[ attachment ] }}
226
							</label>
227
						</input>
228
					<# }); #>
229
				</div>
230
			</div>
231
		<?php
232
	}
233
234
	/**
235
	 * Returns an array of translation strings.
236
	 *
237
	 * @access protected
238
	 * @since 3.0.0
239
	 * @param string|false $config_id The string-ID.
240
	 * @return array
241
	 */
242
	protected function l10n( $config_id ) {
243
		$translation_strings = array(
244
			'background-color'      => esc_attr__( 'Background Color', 'kirki' ),
245
			'background-image'      => esc_attr__( 'Background Image', 'kirki' ),
246
			'background-repeat'     => esc_attr__( 'Background Repeat', 'kirki' ),
247
			'background-attachment' => esc_attr__( 'Background Attachment', 'kirki' ),
248
			'background-position'   => esc_attr__( 'Background Position', 'kirki' ),
249
			'background-size'       => esc_attr__( 'Background Size', 'kirki' ),
250
251
			'no-repeat'             => esc_attr__( 'No Repeat', 'kirki' ),
252
			'repeat-all'            => esc_attr__( 'Repeat All', 'kirki' ),
253
			'repeat-x'              => esc_attr__( 'Repeat Horizontally', 'kirki' ),
254
			'repeat-y'              => esc_attr__( 'Repeat Vertically', 'kirki' ),
255
256
			'cover'                 => esc_attr__( 'Cover', 'kirki' ),
257
			'contain'               => esc_attr__( 'Contain', 'kirki' ),
258
			'auto'                  => esc_attr__( 'Auto', 'kirki' ),
259
			'inherit'               => esc_attr__( 'Inherit', 'kirki' ),
260
261
			'fixed'                 => esc_attr__( 'Fixed', 'kirki' ),
262
			'scroll'                => esc_attr__( 'Scroll', 'kirki' ),
263
			'local'                 => esc_attr__( 'Local', 'kirki' ),
264
265
			'left top'              => esc_attr__( 'Left Top', 'kirki' ),
266
			'left center'           => esc_attr__( 'Left Center', 'kirki' ),
267
			'left bottom'           => esc_attr__( 'Left Bottom', 'kirki' ),
268
			'right top'             => esc_attr__( 'Right Top', 'kirki' ),
269
			'right center'          => esc_attr__( 'Right Center', 'kirki' ),
270
			'right bottom'          => esc_attr__( 'Right Bottom', 'kirki' ),
271
			'center top'            => esc_attr__( 'Center Top', 'kirki' ),
272
			'center center'         => esc_attr__( 'Center Center', 'kirki' ),
273
			'center bottom'         => esc_attr__( 'Center Bottom', 'kirki' ),
274
275
			'no-file-selected'      => esc_attr__( 'No File Selected', 'kirki' ),
276
			'remove'                => esc_attr__( 'Remove', 'kirki' ),
277
			'select-file'           => esc_attr__( 'Select File', 'kirki' ),
278
		);
279
		return apply_filters( "kirki/{$config_id}/l10n", $translation_strings );
280
	}
281
}
282