Completed
Push — develop ( 11ecd6...6fc6a5 )
by Aristeides
04:10
created

Kirki_Field_Repeater::set_transport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Override field methods
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.2.7
10
 */
11
12
/**
13
 * Field overrides.
14
 */
15
class Kirki_Field_Repeater extends Kirki_Field {
16
17
	/**
18
	 * Used only on repeaters.
19
	 * Contains an array of the fields.
20
	 *
21
	 * @access protected
22
	 * @var array
23
	 */
24
	protected $fields = array();
25
26
	/**
27
	 * Sets the control type.
28
	 *
29
	 * @access protected
30
	 */
31
	protected function set_type() {
32
33
		$this->type = 'repeater';
34
35
	}
36
37
	/**
38
	 * Sets the $transport
39
	 *
40
	 * @access protected
41
	 */
42
	protected function set_transport() {
43
44
		// Force using refresh mode.
45
		// Currently the repeater control does not support postMessage.
46
		$this->transport = 'refresh';
47
48
	}
49
50
51
	/**
52
	 * Sets the $sanitize_callback
53
	 *
54
	 * @access protected
55
	 */
56
	protected function set_sanitize_callback() {
57
58
		// If a custom sanitize_callback has been defined,
59
		// then we don't need to proceed any further.
60
		if ( ! empty( $this->sanitize_callback ) ) {
61
			return;
62
		}
63
		$this->sanitize_callback = array( $this, 'sanitize' );
64
65
	}
66
67
	/**
68
	 * The sanitize method that will be used as a falback
69
	 *
70
	 * @param string|array $value The control's value.
71
	 */
72
	public function sanitize( $value ) {
73
74
		// is the value formatted as a string?
75
		if ( is_string( $value ) ) {
76
			$value = rawurldecode( $value );
77
			$value = json_decode( $value, true );
78
		}
79
80
		// Nothing to sanitize if we don't have fields.
81
		if ( empty( $this->fields ) ) {
82
			return $value;
83
		}
84
85
		foreach ( $value as $row_id => $row_value ) {
86
87
			// Make sure the row is formatted as an array.
88
			if ( ! is_array( $row_value ) ) {
89
				$value[ $row_id ] = array();
90
				continue;
91
			}
92
			// Start parsing sub-fields in rows.
93
			foreach ( $row_value as $subfield_id => $subfield_value ) {
94
				// Make sure this is a valid subfield.
95
				// If it's not, then unset it.
96
				if ( ! isset( $this->fields[ $subfield_id ] ) ) {
97
					unset( $value[ $row_id ][ $subfield_id ] );
98
				}
99
				// Get the subfield-type.
100
				$subfield_type = $this->fields[ $subfield_id ]['type'];
101
102
				// Allow using a sanitize-callback on a per-field basis.
103
				if ( isset( $this->fields[ $subfield_id ]['sanitize_callback'] ) ) {
104
105
					$subfield_value = call_user_func( $this->fields[ $subfield_id ]['sanitize_callback'], $subfield_value );
106
107
				} else {
108
109
					switch ( $subfield_type ) {
110
						case 'image':
111
						case 'cropped_image':
112
						case 'upload':
113
							if ( ! is_numeric( $subfield_value ) && is_string( $subfield_value ) ) {
114
								$subfield_value = esc_url_raw( $subfield_value );
115
							}
116
							break;
117
						case 'dropdown-pages':
118
							$subfield_value = (int) $subfield_value;
119
							break;
120
						case 'color':
121
							// Instantiate the object.
122
							$color_obj = ariColor::newColor( $subfield_value );
123
							$$subfield_value = $color_obj->toCSS( $color_obj->mode );
124
							break;
125
						case 'text':
126
							$subfield_value = esc_textarea( $subfield_value );
127
							break;
128
						case 'url':
129
						case 'link':
130
							$subfield_value = esc_url_raw( $subfield_value );
131
							break;
132
						case 'email':
133
							$subfield_value = filter_var( $subfield_value, FILTER_SANITIZE_EMAIL );
134
							break;
135
						case 'tel':
136
							$subfield_value = esc_attr( $subfield_value );
137
							break;
138
						case 'checkbox':
139
							$subfield_value = (string) intval( $subfield_value );
140
							break;
141
						case 'select':
142
							if ( isset( $this->fields[ $subfield_id ]['multiple'] ) ) {
143
								if ( true === $this->fields[ $subfield_id ]['multiple'] ) {
144
									$multiple = 2;
0 ignored issues
show
Unused Code introduced by
$multiple is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
145
								}
146
								$multiple = (int) $this->fields[ $subfield_id ]['multiple'];
147
								if ( 1 < $multiple ) {
148
									$subfield_value = (array) $subfield_value;
149
									foreach ( $subfield_value as $sub_subfield_key => $sub_subfield_value ) {
150
										$subfield_value[ $sub_subfield_key ] = esc_attr( $sub_subfield_value );
151
									}
152
								} else {
153
									$subfield_value = esc_attr( $subfield_value );
154
								}
155
							}
156
							break;
157
						case 'radio':
158
						case 'radio-image':
159
							$subfield_value = esc_attr( $subfield_value );
160
							break;
161
						case 'textarea':
162
							$subfield_value = wp_kses_post( $subfield_value );
163
164
					} // End switch().
165
				} // End if().
166
				$value[ $row_id ][ $subfield_id ] = $subfield_value;
167
			} // End foreach().
168
		} // End foreach().
169
170
		return $value;
171
	}
172
}
173