Completed
Push — develop ( 952465...555a2d )
by Aristeides
06:54 queued 03:28
created

php/class-kirki-settings-repeater-setting.php (2 issues)

1
<?php
2
/**
3
 * Repeater Customizer Setting.
4
 *
5
 * @package     Kirki
6
 * @subpackage  Controls
7
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
8
 * @license    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 Settings.
19
 */
20
class Kirki_Settings_Repeater_Setting extends WP_Customize_Setting {
21
22
	/**
23
	 * Constructor.
24
	 *
25
	 * Any supplied $args override class property defaults.
26
	 *
27
	 * @access public
28
	 * @param WP_Customize_Manager $manager The WordPress WP_Customize_Manager object.
29
	 * @param string               $id       A specific ID of the setting. Can be a theme mod or option name.
30
	 * @param array                $args     Setting arguments.
31
	 */
32
	public function __construct( $manager, $id, $args = array() ) {
33
		parent::__construct( $manager, $id, $args );
34
35
		// Will onvert the setting from JSON to array. Must be triggered very soon.
36
		add_filter( "customize_sanitize_{$this->id}", array( $this, 'sanitize_repeater_setting' ), 10, 1 );
37
	}
38
39
	/**
40
	 * Fetch the value of the setting.
41
	 *
42
	 * @access public
43
	 * @return mixed The value.
44
	 */
45
	public function value() {
46
		return (array) parent::value();
47
	}
48
49
	/**
50
	 * Convert the JSON encoded setting coming from Customizer to an Array.
51
	 *
52
	 * @access public
53
	 * @param string $value URL Encoded JSON Value.
54
	 * @return array
55
	 */
56
	public function sanitize_repeater_setting( $value ) {
57
		if ( ! is_array( $value ) ) {
0 ignored issues
show
The condition is_array($value) is always false.
Loading history...
58
			$value = json_decode( urldecode( $value ) );
59
		}
60
		if ( empty( $value ) || ! is_array( $value ) ) {
61
			$value = array();
62
		}
63
64
		// Make sure that every row is an array, not an object.
65
		foreach ( $value as $key => $val ) {
66
			$value[ $key ] = (array) $val;
67
			if ( empty( $val ) ) {
68
				unset( $value[ $key ] );
69
			}
70
		}
71
72
		// Reindex array.
73
		if ( is_array( $value ) ) {
0 ignored issues
show
The condition is_array($value) is always true.
Loading history...
74
			$value = array_values( $value );
75
		}
76
77
		return $value;
78
	}
79
}
80