Completed
Pull Request — develop (#1282)
by Aristeides
03:07
created

Kirki_Control_Repeater::l10n()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 2
nop 1
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
1
<?php
2
/**
3
 * Customizer Control: repeater.
4
 *
5
 * @package     Kirki
6
 * @subpackage  Controls
7
 * @copyright   Copyright (c) 2016, Aristeides Stathopoulos
8
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
9
 * @since       2.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Repeater control
19
 */
20
class Kirki_Control_Repeater extends WP_Customize_Control {
21
22
	/**
23
	 * The control type.
24
	 *
25
	 * @access public
26
	 * @var string
27
	 */
28
	public $type = 'repeater';
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
	 * The fields that each container row will contain.
56
	 *
57
	 * @access public
58
	 * @var array
59
	 */
60
	public $fields = array();
61
62
	/**
63
	 * Will store a filtered version of value for advenced fields (like images).
64
	 *
65
	 * @access protected
66
	 * @var array
67
	 */
68
	protected $filtered_value = array();
69
70
	/**
71
	 * The row label
72
	 *
73
	 * @access public
74
	 * @var array
75
	 */
76
	public $row_label = array();
77
78
	/**
79
	 * Constructor.
80
	 * Supplied `$args` override class property defaults.
81
	 * If `$args['settings']` is not defined, use the $id as the setting ID.
82
	 *
83
	 * @param WP_Customize_Manager $manager Customizer bootstrap instance.
84
	 * @param string               $id      Control ID.
85
	 * @param array                $args    {@see WP_Customize_Control::__construct}.
86
	 */
87
	public function __construct( $manager, $id, $args = array() ) {
88
89
		parent::__construct( $manager, $id, $args );
90
91
		// Set up defaults for row labels.
92
		$this->row_label = array(
93
			'type' => 'text',
94
			'value' => $this->l10n( 'row' ),
95
			'field' => false,
96
		);
97
98
		// Validate row-labels.
99
		$this->row_label( $args );
100
101
		if ( empty( $this->button_label ) ) {
102
			$this->button_label = $this->l10n( 'add-new' ) . ' ' . $this->row_label['value'];
103
		}
104
105
		if ( empty( $args['fields'] ) || ! is_array( $args['fields'] ) ) {
106
			$args['fields'] = array();
107
		}
108
109
		// An array to store keys of fields that need to be filtered.
110
		$media_fields_to_filter = array();
111
112
		foreach ( $args['fields'] as $key => $value ) {
113
			if ( ! isset( $value['default'] ) ) {
114
				$args['fields'][ $key ]['default'] = '';
115
			}
116
			if ( ! isset( $value['label'] ) ) {
117
				$args['fields'][ $key ]['label'] = '';
118
			}
119
			$args['fields'][ $key ]['id']      = $key;
120
121
			// We check if the filed is an uploaded media ( image , file, video, etc.. ).
122
			if ( isset( $value['type'] ) ) {
123
				switch ( $value['type'] ) {
124
					case 'image':
125
					case 'cropped_image':
126
					case 'upload':
127
						// We add it to the list of fields that need some extra filtering/processing.
128
						$media_fields_to_filter[ $key ] = true;
129
						break;
130
131
					case 'dropdown-pages':
132
						// If the field is a dropdown-pages field then add it to args.
133
						$dropdown = wp_dropdown_pages(
134
							array(
135
								'name'              => '',
136
								'echo'              => 0,
137
								'show_option_none'  => esc_attr( $this->l10n( 'select-page' ) ),
138
								'option_none_value' => '0',
139
								'selected'          => '',
140
							)
141
						);
142
						// Hackily add in the data link parameter.
143
						$dropdown = str_replace( '<select', '<select data-field="' . esc_attr( $args['fields'][ $key ]['id'] ) . '"' . $this->get_link(), $dropdown );
144
						$args['fields'][ $key ]['dropdown'] = $dropdown;
145
						break;
146
				}
147
			}
148
		}
149
150
		$this->fields = $args['fields'];
151
152
		// Now we are going to filter the fields.
153
		// First we create a copy of the value that would be used otherwise.
154
		$this->filtered_value = $this->value();
155
156
		if ( is_array( $this->filtered_value ) && ! empty( $this->filtered_value ) ) {
157
158
			// We iterate over the list of fields.
159
			foreach ( $this->filtered_value as &$filtered_value_field ) {
160
161
				if ( is_array( $filtered_value_field ) && ! empty( $filtered_value_field ) ) {
162
163
					// We iterate over the list of properties for this field.
164
					foreach ( $filtered_value_field as $key => &$value ) {
165
166
						// We check if this field was marked as requiring extra filtering (in this case image, cropped_images, upload).
167
						if ( array_key_exists( $key, $media_fields_to_filter ) ) {
168
169
							// What follows was made this way to preserve backward compatibility.
170
							// The repeater control use to store the URL for images instead of the attachment ID.
171
							// We check if the value look like an ID (otherwise it's probably a URL so don't filter it).
172
							if ( is_numeric( $value ) ) {
173
174
								// "sanitize" the value.
175
								$attachment_id = (int) $value;
176
177
								// Try to get the attachment_url.
178
								$url = wp_get_attachment_url( $attachment_id );
179
180
								$filename = basename( get_attached_file( $attachment_id ) );
181
182
								// If we got a URL.
183
								if ( $url ) {
184
185
									// 'id' is needed for form hidden value, URL is needed to display the image.
186
									$value = array(
187
										'id'  => $attachment_id,
188
										'url' => $url,
189
										'filename' => $filename,
190
									);
191
								}
192
							}
193
						}
194
					}
195
				}
196
			}
197
		}
198
	}
199
200
	/**
201
	 * Refresh the parameters passed to the JavaScript via JSON.
202
	 *
203
	 * @access public
204
	 */
205
	public function to_json() {
206
		parent::to_json();
207
208
		$this->json['default'] = ( isset( $this->default ) ) ? $this->default : $this->setting->default;
209
		$this->json['output']  = $this->output;
210
		$this->json['value']   = $this->value();
211
		$this->json['choices'] = $this->choices;
212
		$this->json['link']    = $this->get_link();
213
		$this->json['id']      = $this->id;
214
215
		if ( 'user_meta' === $this->option_type ) {
216
			$this->json['value'] = get_user_meta( get_current_user_id(), $this->id, true );
217
		}
218
219
		$this->json['inputAttrs'] = '';
220
		foreach ( $this->input_attrs as $attr => $value ) {
221
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
222
		}
223
224
		$fields = $this->fields;
225
226
		$this->json['fields'] = $fields;
227
		$this->json['row_label'] = $this->row_label;
228
229
		// If filtered_value has been set and is not empty we use it instead of the actual value.
230
		if ( is_array( $this->filtered_value ) && ! empty( $this->filtered_value ) ) {
231
			$this->json['value'] = $this->filtered_value;
232
		}
233
	}
234
235
	/**
236
	 * Enqueue control related scripts/styles.
237
	 *
238
	 * @access public
239
	 */
240
	public function enqueue() {
241
242
		// If we have a color picker field we need to enqueue the Wordpress Color Picker style and script.
243
		if ( is_array( $this->fields ) && ! empty( $this->fields ) ) {
244
			foreach ( $this->fields as $field ) {
245
				if ( isset( $field['type'] ) && 'color' === $field['type'] ) {
246
					wp_enqueue_script( 'wp-color-picker' );
247
					wp_enqueue_style( 'wp-color-picker' );
248
					break;
249
				}
250
			}
251
		}
252
253
		wp_enqueue_script( 'kirki-repeater', trailingslashit( Kirki::$url ) . 'controls/repeater/repeater.js', array( 'jquery', 'customize-base', 'jquery-ui-core', 'jquery-ui-sortable' ), false, true );
254
		wp_enqueue_style( 'kirki-repeater-css', trailingslashit( Kirki::$url ) . 'controls/repeater/repeater.css', null );
255
	}
256
257
	/**
258
	 * Render the control's content.
259
	 * Allows the content to be overriden without having to rewrite the wrapper in $this->render().
260
	 *
261
	 * @access protected
262
	 */
263
	protected function render_content() {
264
		?>
265
		<label>
266
			<?php if ( ! empty( $this->label ) ) : ?>
267
				<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
268
			<?php endif; ?>
269
			<?php if ( ! empty( $this->description ) ) : ?>
270
				<span class="description customize-control-description"><?php echo wp_kses_post( $this->description ); ?></span>
271
			<?php endif; ?>
272
			<input type="hidden" {{{ data.inputAttrs }}} value="" <?php echo wp_kses_post( $this->get_link() ); ?> />
273
		</label>
274
275
		<ul class="repeater-fields"></ul>
276
277
		<?php if ( isset( $this->choices['limit'] ) ) : ?>
278
			<p class="limit"><?php printf( esc_html( $this->l10n( 'limit-rows' ) ), esc_html( $this->choices['limit'] ) ); ?></p>
279
		<?php endif; ?>
280
		<button class="button-secondary repeater-add"><?php echo esc_html( $this->button_label ); ?></button>
281
282
		<?php
283
284
		$this->repeater_js_template();
285
286
	}
287
288
	/**
289
	 * An Underscore (JS) template for this control's content (but not its container).
290
	 * Class variables for this control class are available in the `data` JS object.
291
	 *
292
	 * @access public
293
	 */
294
	public function repeater_js_template() {
295
		?>
296
		<script type="text/html" class="customize-control-repeater-content">
297
			<# var field; var index = data.index; #>
298
299
			<li class="repeater-row minimized" data-row="{{{ index }}}">
300
301
				<div class="repeater-row-header">
302
					<span class="repeater-row-label"></span>
303
					<i class="dashicons dashicons-arrow-down repeater-minimize"></i>
304
				</div>
305
				<div class="repeater-row-content">
306
					<# _.each( data, function( field, i ) { #>
307
308
						<div class="repeater-field repeater-field-{{{ field.type }}}">
309
310
							<# if ( 'text' === field.type || 'url' === field.type || 'link' === field.type || 'email' === field.type || 'tel' === field.type || 'date' === field.type ) { #>
311
312
								<# if ( 'link' === field.type ) { #>
313
									<# field.type = 'url' #>
314
								<# } #>
315
316
								<label>
317
									<# if ( field.label ) { #>
318
										<span class="customize-control-title">{{ field.label }}</span>
319
									<# } #>
320
									<# if ( field.description ) { #>
321
										<span class="description customize-control-description">{{ field.description }}</span>
322
									<# } #>
323
									<input type="{{field.type}}" name="" value="{{{ field.default }}}" data-field="{{{ field.id }}}">
324
								</label>
325
326
							<# } else if ( 'hidden' === field.type ) { #>
327
328
								<input type="hidden" data-field="{{{ field.id }}}" <# if ( field.default ) { #> value="{{{ field.default }}}" <# } #> />
329
330
							<# } else if ( 'checkbox' === field.type ) { #>
331
332
								<label>
333
									<input type="checkbox" value="true" data-field="{{{ field.id }}}" <# if ( field.default ) { #> checked="checked" <# } #> /> {{ field.label }}
334
									<# if ( field.description ) { #>
335
										{{ field.description }}
336
									<# } #>
337
								</label>
338
339
							<# } else if ( 'select' === field.type ) { #>
340
341
								<label>
342
									<# if ( field.label ) { #>
343
										<span class="customize-control-title">{{ field.label }}</span>
344
									<# } #>
345
									<# if ( field.description ) { #>
346
										<span class="description customize-control-description">{{ field.description }}</span>
347
									<# } #>
348
									<select data-field="{{{ field.id }}}">
349
										<# _.each( field.choices, function( choice, i ) { #>
350
											<option value="{{{ i }}}" <# if ( field.default == i ) { #> selected="selected" <# } #>>{{ choice }}</option>
351
										<# }); #>
352
									</select>
353
								</label>
354
355
							<# } else if ( 'dropdown-pages' === field.type ) { #>
356
357
								<label>
358
									<# if ( field.label ) { #>
359
										<span class="customize-control-title">{{{ data.label }}}</span>
360
									<# } #>
361
									<# if ( field.description ) { #>
362
										<span class="description customize-control-description">{{{ field.description }}}</span>
363
									<# } #>
364
									<div class="customize-control-content repeater-dropdown-pages">{{{ field.dropdown }}}</div>
365
								</label>
366
367
							<# } else if ( 'radio' === field.type ) { #>
368
369
								<label>
370
									<# if ( field.label ) { #>
371
										<span class="customize-control-title">{{ field.label }}</span>
372
									<# } #>
373
									<# if ( field.description ) { #>
374
										<span class="description customize-control-description">{{ field.description }}</span>
375
									<# } #>
376
377
									<# _.each( field.choices, function( choice, i ) { #>
378
										<label>
379
											<input type="radio" name="{{{ field.id }}}{{ index }}" data-field="{{{ field.id }}}" value="{{{ i }}}" <# if ( field.default == i ) { #> checked="checked" <# } #>> {{ choice }} <br/>
380
										</label>
381
									<# }); #>
382
								</label>
383
384
							<# } else if ( 'radio-image' === field.type ) { #>
385
386
								<label>
387
									<# if ( field.label ) { #>
388
										<span class="customize-control-title">{{ field.label }}</span>
389
									<# } #>
390
									<# if ( field.description ) { #>
391
										<span class="description customize-control-description">{{ field.description }}</span>
392
									<# } #>
393
394
									<# _.each( field.choices, function( choice, i ) { #>
395
										<input type="radio" id="{{{ field.id }}}_{{ index }}_{{{ i }}}" name="{{{ field.id }}}{{ index }}" data-field="{{{ field.id }}}" value="{{{ i }}}" <# if ( field.default == i ) { #> checked="checked" <# } #>>
396
											<label for="{{{ field.id }}}_{{ index }}_{{{ i }}}">
397
												<img src="{{ choice }}">
398
											</label>
399
										</input>
400
									<# }); #>
401
								</label>
402
403
							<# } else if ( 'color' === field.type ) { #>
404
405
								<# var defaultValue = '';
406
						        if ( field.default ) {
407
						            if ( '#' !== field.default.substring( 0, 1 ) ) {
408
						                defaultValue = '#' + field.default;
409
						            } else {
410
						                defaultValue = field.default;
411
						            }
412
						            defaultValue = ' data-default-color=' + defaultValue; // Quotes added automatically.
413
						        } #>
414
						        <label>
415
						            <# if ( field.label ) { #>
416
						                <span class="customize-control-title">{{{ field.label }}}</span>
417
						            <# } #>
418
						            <# if ( field.description ) { #>
419
						                <span class="description customize-control-description">{{{ field.description }}}</span>
420
						            <# } #>
421
						            <input class="color-picker-hex" type="text" maxlength="7" placeholder="<?php echo esc_attr( $this->l10n( 'hex-value' ) ); ?>"  value="{{{ field.default }}}" data-field="{{{ field.id }}}" {{ defaultValue }} />
422
423
						        </label>
424
425
							<# } else if ( 'textarea' === field.type ) { #>
426
427
								<# if ( field.label ) { #>
428
									<span class="customize-control-title">{{ field.label }}</span>
429
								<# } #>
430
								<# if ( field.description ) { #>
431
									<span class="description customize-control-description">{{ field.description }}</span>
432
								<# } #>
433
								<textarea rows="5" data-field="{{{ field.id }}}">{{ field.default }}</textarea>
434
435
							<# } else if ( field.type === 'image' || field.type === 'cropped_image' ) { #>
436
437
								<label>
438
									<# if ( field.label ) { #>
439
										<span class="customize-control-title">{{ field.label }}</span>
440
									<# } #>
441
									<# if ( field.description ) { #>
442
										<span class="description customize-control-description">{{ field.description }}</span>
443
									<# } #>
444
								</label>
445
446
								<figure class="kirki-image-attachment" data-placeholder="<?php echo esc_attr( $this->l10n( 'no-image-selected' ) ); ?>" >
447
									<# if ( field.default ) { #>
448
										<# var defaultImageURL = ( field.default.url ) ? field.default.url : field.default; #>
449
										<img src="{{{ defaultImageURL }}}">
450
									<# } else { #>
451
										<?php echo esc_attr( $this->l10n( 'no-image-selected' ) ); ?>
452
									<# } #>
453
								</figure>
454
455
								<div class="actions">
456
									<button type="button" class="button remove-button<# if ( ! field.default ) { #> hidden<# } #>"><?php echo esc_attr( $this->l10n( 'remove' ) ); ?></button>
457
									<button type="button" class="button upload-button" data-label=" <?php echo esc_attr( $this->l10n( 'add-image' ) ); ?>" data-alt-label="<?php echo esc_attr( $this->l10n( 'change-image' ) ); ?>" >
458
										<# if ( field.default ) { #>
459
											<?php echo esc_attr( $this->l10n( 'change-image' ) ); ?>
460
										<# } else { #>
461
											<?php echo esc_attr( $this->l10n( 'add-image' ) ); ?>
462
										<# } #>
463
									</button>
464
									<# if ( field.default.id ) { #>
465
										<input type="hidden" class="hidden-field" value="{{{ field.default.id }}}" data-field="{{{ field.id }}}" >
466
									<# } else { #>
467
										<input type="hidden" class="hidden-field" value="{{{ field.default }}}" data-field="{{{ field.id }}}" >
468
									<# } #>
469
								</div>
470
471
							<# } else if ( field.type === 'upload' ) { #>
472
473
								<label>
474
									<# if ( field.label ) { #>
475
										<span class="customize-control-title">{{ field.label }}</span>
476
									<# } #>
477
									<# if ( field.description ) { #>
478
										<span class="description customize-control-description">{{ field.description }}</span>
479
									<# } #>
480
								</label>
481
482
								<figure class="kirki-file-attachment" data-placeholder="<?php echo esc_attr( $this->l10n( 'no-file-selected' ) ); ?>" >
483
									<# if ( field.default ) { #>
484
										<# var defaultFilename = ( field.default.filename ) ? field.default.filename : field.default; #>
485
										<span class="file"><span class="dashicons dashicons-media-default"></span> {{ defaultFilename }}</span>
486
									<# } else { #>
487
										<?php echo esc_attr( $this->l10n( 'no-file-selected' ) ); ?>
488
									<# } #>
489
								</figure>
490
491
								<div class="actions">
492
									<button type="button" class="button remove-button<# if ( ! field.default ) { #> hidden<# } #>"></button>
493
									<button type="button" class="button upload-button" data-label="<?php echo esc_attr( $this->l10n( 'add-file' ) ); ?>" data-alt-label="<?php echo esc_attr( $this->l10n( 'change-file' ) ); ?>" >
494
										<# if ( field.default ) { #>
495
											<?php echo esc_attr( $this->l10n( 'change-file' ) ); ?>
496
										<# } else { #>
497
											<?php echo esc_attr( $this->l10n( 'add-file' ) ); ?>
498
										<# } #>
499
									</button>
500
									<# if ( field.default.id ) { #>
501
										<input type="hidden" class="hidden-field" value="{{{ field.default.id }}}" data-field="{{{ field.id }}}" >
502
									<# } else { #>
503
										<input type="hidden" class="hidden-field" value="{{{ field.default }}}" data-field="{{{ field.id }}}" >
504
									<# } #>
505
								</div>
506
507
							<# } else if ( 'custom' === field.type ) { #>
508
509
								<# if ( field.label ) { #>
510
									<span class="customize-control-title">{{ field.label }}</span>
511
								<# } #>
512
								<# if ( field.description ) { #>
513
									<span class="description customize-control-description">{{ field.description }}</span>
514
								<# } #>
515
								<div data-field="{{{ field.id }}}">{{{ field.default }}}</div>
516
517
							<# } #>
518
519
						</div>
520
					<# }); #>
521
					<button type="button" class="button-link repeater-row-remove"><?php echo esc_attr( $this->l10n( 'remove' ) ); ?></button>
522
				</div>
523
			</li>
524
		</script>
525
		<?php
526
	}
527
528
	/**
529
	 * Validate row-labels.
530
	 *
531
	 * @access protected
532
	 * @since 2.4.0
533
	 * @param array $args {@see WP_Customize_Control::__construct}.
534
	 */
535
	protected function row_label( $args ) {
536
537
		// Validating args for row labels.
538
		if ( isset( $args['row_label'] ) && is_array( $args['row_label'] ) && ! empty( $args['row_label'] ) ) {
539
540
			// Validating row label type.
541
			if ( isset( $args['row_label']['type'] ) && ( 'text' === $args['row_label']['type'] || 'field' === $args['row_label']['type'] ) ) {
542
				$this->row_label['type'] = $args['row_label']['type'];
543
			}
544
545
			// Validating row label type.
546
			if ( isset( $args['row_label']['value'] ) && ! empty( $args['row_label']['value'] ) ) {
547
				$this->row_label['value'] = esc_attr( $args['row_label']['value'] );
548
			}
549
550
			// Validating row label field.
551
			if ( isset( $args['row_label']['field'] ) && ! empty( $args['row_label']['field'] ) && isset( $args['fields'][ esc_attr( $args['row_label']['field'] ) ] ) ) {
552
				$this->row_label['field'] = esc_attr( $args['row_label']['field'] );
553
			} else {
554
				// If from field is not set correctly, making sure standard is set as the type.
555
				$this->row_label['type'] = 'text';
556
			}
557
		}
558
	}
559
560
	/**
561
	 * Returns an array of translation strings.
562
	 *
563
	 * @access protected
564
	 * @since 2.4.0
565
	 * @param string|false $id The string-ID.
566
	 * @return string
567
	 */
568
	protected function l10n( $id = false ) {
569
		$translation_strings = array(
570
			'row'               => esc_attr__( 'row', 'kirki' ),
571
			'add-new'           => esc_attr__( 'Add new', 'kirki' ),
572
			'select-page'       => esc_attr__( 'Select a Page', 'kirki' ),
573
			'limit-rows'        => esc_attr__( 'Limit: %s rows', 'kirki' ),
574
			'hex-value'         => esc_attr__( 'Hex Value', 'kirki' ),
575
			'no-image-selected' => esc_attr__( 'No Image Selected', 'kirki' ),
576
			'remove'            => esc_attr__( 'Remove', 'kirki' ),
577
			'add-image'         => esc_attr__( 'Add Image', 'kirki' ),
578
			'change-image'      => esc_attr__( 'Change Image', 'kirki' ),
579
			'no-file-selected'  => esc_attr__( 'No File Selected', 'kirki' ),
580
			'add-file'          => esc_attr__( 'Add File', 'kirki' ),
581
			'change-file'       => esc_attr__( 'Change File', 'kirki' ),
582
		);
583
		$translation_strings = apply_filters( 'kirki/' . $this->kirki_config . '/l10n', $translation_strings );
584
		if ( false === $id ) {
585
			return $translation_strings;
586
		}
587
		return $translation_strings[ $id ];
588
	}
589
}
590