Completed
Push — develop ( 84d569...bfd647 )
by Aristeides
06:29
created

Kirki_Control_Background::render_content()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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