PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.0.13
Kirki – Freeform Page Builder, Website Builder & Customizer v6.0.13
6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / customizer / packages / fields / background / src / Background.php
kirki / customizer / packages / fields / background / src Last commit date
CSS 5 months ago Background.php 2 months ago
Background.php
470 lines
1 <?php
2 /**
3 * Override field methods
4 *
5 * @package kirki-framework/control-background
6 * @copyright Copyright (c) 2023, Themeum
7 * @license https://opensource.org/licenses/MIT
8 * @since 1.0
9 */
10
11 namespace Kirki\Field;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 use Kirki;
18 use Kirki\Field;
19 use Kirki\URL;
20
21 /**
22 * Field overrides.
23 *
24 * @since 1.0
25 */
26 class Background extends Field {
27
28 /**
29 * The field type.
30 *
31 * @access public
32 * @since 1.0
33 * @var string
34 */
35 public $type = 'kirki-background';
36
37 /**
38 * Extra logic for the field.
39 *
40 * Adds all sub-fields.
41 *
42 * @access public
43 * @param array $args The arguments of the field.
44 */
45 public function init( $args ) {
46
47 $args['required'] = isset( $args['required'] ) ? (array) $args['required'] : [];
48 $args['kirki_config'] = isset( $args['kirki_config'] ) ? $args['kirki_config'] : 'global';
49
50 /**
51 * Add a hidden field, the label & description.
52 */
53 new \Kirki\Field\Generic(
54 wp_parse_args(
55 [
56 'type' => 'kirki-generic',
57 'default' => '',
58 'choices' => [
59 'type' => 'hidden',
60 'parent_type' => 'kirki-background',
61 ],
62 'sanitize_callback' => [ '\Kirki\Field\Background', 'sanitize' ],
63 ],
64 $args
65 )
66 );
67
68 $args['parent_setting'] = $args['settings'];
69 $args['output'] = [];
70 $args['wrapper_attrs'] = [
71 'data-kirki-parent-control-type' => 'kirki-background',
72 ];
73
74 if ( isset( $args['transport'] ) && 'auto' === $args['transport'] ) {
75 $args['transport'] = 'postMessage';
76 }
77
78 $default_bg_color = isset( $args['default']['background-color'] ) ? $args['default']['background-color'] : '';
79
80 /**
81 * Background Color.
82 */
83 new \Kirki\Field\Color(
84 wp_parse_args(
85 [
86 'settings' => $args['settings'] . '[background-color]',
87 'label' => '',
88 'description' => esc_html__( 'Background Color', 'kirki' ),
89 'default' => $default_bg_color,
90 'section' => $args['section'],
91 'choices' => [
92 'alpha' => true,
93 ],
94 ],
95 $args
96 )
97 );
98
99 /**
100 * Background Image.
101 */
102 new \Kirki\Field\Image(
103 wp_parse_args(
104 [
105 'settings' => $args['settings'] . '[background-image]',
106 'label' => '',
107 'description' => esc_html__( 'Background Image', 'kirki' ),
108 'default' => isset( $args['default']['background-image'] ) ? $args['default']['background-image'] : '',
109 'section' => $args['section'],
110 ],
111 $args
112 )
113 );
114
115 /**
116 * Background Repeat.
117 */
118 new Kirki\Field\Select(
119 wp_parse_args(
120 [
121 'settings' => $args['settings'] . '[background-repeat]',
122 'label' => '',
123 'description' => esc_html__( 'Background Repeat', 'kirki' ),
124 'section' => $args['section'],
125 'default' => isset( $args['default']['background-repeat'] ) ? $args['default']['background-repeat'] : '',
126 'choices' => [
127 'no-repeat' => esc_html__( 'No Repeat', 'kirki' ),
128 'repeat' => esc_html__( 'Repeat All', 'kirki' ),
129 'repeat-x' => esc_html__( 'Repeat Horizontally', 'kirki' ),
130 'repeat-y' => esc_html__( 'Repeat Vertically', 'kirki' ),
131 ],
132 'required' => array_merge(
133 $args['required'],
134 [
135 [
136 'setting' => $args['settings'],
137 'operator' => '!=',
138 'value' => '',
139 'choice' => 'background-image',
140 ],
141 ]
142 ),
143 ],
144 $args
145 )
146 );
147
148 /**
149 * Background Position.
150 */
151 new Kirki\Field\Select(
152 wp_parse_args(
153 [
154 'settings' => $args['settings'] . '[background-position]',
155 'label' => '',
156 'description' => esc_html__( 'Background Position', 'kirki' ),
157 'default' => isset( $args['default']['background-position'] ) ? $args['default']['background-position'] : '',
158 'section' => $args['section'],
159 'choices' => [
160 'left top' => esc_html__( 'Left Top', 'kirki' ),
161 'left center' => esc_html__( 'Left Center', 'kirki' ),
162 'left bottom' => esc_html__( 'Left Bottom', 'kirki' ),
163 'center top' => esc_html__( 'Center Top', 'kirki' ),
164 'center center' => esc_html__( 'Center Center', 'kirki' ),
165 'center bottom' => esc_html__( 'Center Bottom', 'kirki' ),
166 'right top' => esc_html__( 'Right Top', 'kirki' ),
167 'right center' => esc_html__( 'Right Center', 'kirki' ),
168 'right bottom' => esc_html__( 'Right Bottom', 'kirki' ),
169 ],
170 'required' => array_merge(
171 $args['required'],
172 [
173 [
174 'setting' => $args['settings'],
175 'operator' => '!=',
176 'value' => '',
177 'choice' => 'background-image',
178 ],
179 ]
180 ),
181 ],
182 $args
183 )
184 );
185
186 /**
187 * Background size.
188 */
189 new Kirki\Field\Radio_Buttonset(
190 wp_parse_args(
191 [
192 'settings' => $args['settings'] . '[background-size]',
193 'label' => '',
194 'description' => esc_html__( 'Background Size', 'kirki' ),
195 'default' => isset( $args['default']['background-size'] ) ? $args['default']['background-size'] : '',
196 'section' => $args['section'],
197 'choices' => [
198 'cover' => esc_html__( 'Cover', 'kirki' ),
199 'contain' => esc_html__( 'Contain', 'kirki' ),
200 'auto' => esc_html__( 'Auto', 'kirki' ),
201 ],
202 'required' => array_merge(
203 $args['required'],
204 [
205 [
206 'setting' => $args['settings'],
207 'operator' => '!=',
208 'value' => '',
209 'choice' => 'background-image',
210 ],
211 ]
212 ),
213 ],
214 $args
215 )
216 );
217
218 /**
219 * Background attachment.
220 */
221 new Kirki\Field\Radio_Buttonset(
222 wp_parse_args(
223 [
224 'type' => 'kirki-radio-buttonset',
225 'settings' => $args['settings'] . '[background-attachment]',
226 'description' => esc_html__( 'Background Attachment', 'kirki' ),
227 'label' => '',
228 'default' => isset( $args['default']['background-attachment'] ) ? $args['default']['background-attachment'] : '',
229 'section' => $args['section'],
230 'choices' => [
231 'scroll' => esc_html__( 'Scroll', 'kirki' ),
232 'fixed' => esc_html__( 'Fixed', 'kirki' ),
233 ],
234 'required' => array_merge(
235 $args['required'],
236 [
237 [
238 'setting' => $args['settings'],
239 'operator' => '!=',
240 'value' => '',
241 'choice' => 'background-image',
242 ],
243 ]
244 ),
245 ],
246 $args
247 )
248 );
249
250 add_action( 'customize_preview_init', [ $this, 'enqueue_scripts' ] );
251 add_filter( 'kirki_output_control_classnames', [ $this, 'output_control_classnames' ] );
252
253 }
254
255 /**
256 * Sets the $sanitize_callback
257 *
258 * @access protected
259 * @since 1.0
260 * @return void
261 */
262 protected function set_sanitize_callback() {
263
264 // If a custom sanitize_callback has been defined,
265 // then we don't need to proceed any further.
266 if ( ! empty( $this->sanitize_callback ) ) {
267 return;
268 }
269
270 $this->sanitize_callback = [ '\Kirki\Field\Background', 'sanitize' ];
271
272 }
273
274 /**
275 * Sanitizes background controls
276 *
277 * @static
278 * @access public
279 * @since 1.0
280 * @param array $value The value.
281 * @return array
282 */
283 public static function sanitize( $value ) {
284
285 if ( ! is_array( $value ) ) {
286 return [];
287 }
288
289 $sanitized_value = [
290 'background-color' => '',
291 'background-image' => '',
292 'background-repeat' => '',
293 'background-position' => '',
294 'background-size' => '',
295 'background-attachment' => '',
296 ];
297
298 if ( isset( $value['background-color'] ) ) {
299 $sanitized_value['background-color'] = \Kirki\Field\Color::sanitize( $value['background-color'] );
300 }
301
302 if ( isset( $value['background-image'] ) ) {
303 $sanitized_value['background-image'] = esc_url_raw( $value['background-image'] );
304 }
305
306 if ( isset( $value['background-repeat'] ) ) {
307 $sanitized_value['background-repeat'] = in_array(
308 $value['background-repeat'],
309 [
310 'no-repeat',
311 'repeat',
312 'repeat-x',
313 'repeat-y',
314 ],
315 true
316 ) ? $value['background-repeat'] : '';
317 }
318
319 if ( isset( $value['background-position'] ) ) {
320 $sanitized_value['background-position'] = in_array(
321 $value['background-position'],
322 [
323 'left top',
324 'left center',
325 'left bottom',
326 'center top',
327 'center center',
328 'center bottom',
329 'right top',
330 'right center',
331 'right bottom',
332 ],
333 true
334 ) ? $value['background-position'] : '';
335 }
336
337 if ( isset( $value['background-size'] ) ) {
338 $sanitized_value['background-size'] = in_array(
339 $value['background-size'],
340 [
341 'cover',
342 'contain',
343 'auto',
344 ],
345 true
346 ) ? $value['background-size'] : '';
347 }
348
349 if ( isset( $value['background-attachment'] ) ) {
350 $sanitized_value['background-attachment'] = in_array(
351 $value['background-attachment'],
352 [
353 'scroll',
354 'fixed',
355 ],
356 true
357 ) ? $value['background-attachment'] : '';
358 }
359
360 return $sanitized_value;
361
362 }
363
364 /**
365 * Sets the $js_vars
366 *
367 * @access protected
368 * @since 1.0
369 * @return void
370 */
371 protected function set_js_vars() {
372
373 // Typecast to array.
374 $this->js_vars = (array) $this->js_vars;
375
376 // Check if transport is set to auto.
377 // If not, then skip the auto-calculations and exit early.
378 if ( 'auto' !== $this->transport ) {
379 return;
380 }
381
382 // Set transport to refresh initially.
383 // Serves as a fallback in case we failed to auto-calculate js_vars.
384 $this->transport = 'refresh';
385
386 $js_vars = [];
387
388 // Try to auto-generate js_vars.
389 // First we need to check if js_vars are empty, and that output is not empty.
390 if ( empty( $this->js_vars ) && ! empty( $this->output ) ) {
391
392 // Start going through each item in the $output array.
393 foreach ( $this->output as $output ) {
394
395 // If 'element' is not defined, skip this.
396 if ( ! isset( $output['element'] ) ) {
397 continue;
398 }
399 if ( is_array( $output['element'] ) ) {
400 $output['element'] = implode( ',', $output['element'] );
401 }
402
403 // If there's a sanitize_callback defined, skip this.
404 if ( isset( $output['sanitize_callback'] ) && ! empty( $output['sanitize_callback'] ) ) {
405 continue;
406 }
407
408 // If we got this far, it's safe to add this.
409 $js_vars[] = $output;
410 }
411
412 // Did we manage to get all the items from 'output'?
413 // If not, then we're missing something so don't add this.
414 if ( count( $js_vars ) !== count( $this->output ) ) {
415 return;
416 }
417 $this->js_vars = $js_vars;
418 $this->transport = 'postMessage';
419 }
420
421 }
422
423 /**
424 * Override parent method. No need to register any setting.
425 *
426 * @access public
427 * @since 0.1
428 * @param WP_Customize_Manager $wp_customize The customizer instance.
429 * @return void
430 */
431 public function add_setting( $wp_customize ) {}
432
433 /**
434 * Override the parent method. No need for a control.
435 *
436 * @access public
437 * @since 0.1
438 * @param WP_Customize_Manager $wp_customize The customizer instance.
439 * @return void
440 */
441 public function add_control( $wp_customize ) {}
442
443 /**
444 * Enqueue scripts & styles.
445 *
446 * @access public
447 * @since 1.0
448 * @return void
449 */
450 public function enqueue_scripts() {
451
452
453 }
454
455 /**
456 * Adds a custom output class for typography fields.
457 *
458 * @access public
459 * @since 1.0
460 * @param array $classnames The array of classnames.
461 * @return array
462 */
463 public function output_control_classnames( $classnames ) {
464
465 $classnames['kirki-background'] = '\Kirki\Field\CSS\Background';
466 return $classnames;
467
468 }
469
470 }