Completed
Branch develop (98485c)
by Aristeides
12:56 queued 06:30
created

Kirki_Field_Repeater::sanitize()   D

Complexity

Conditions 30
Paths 8

Size

Total Lines 102
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 30
eloc 68
nc 8
nop 1
dl 0
loc 102
rs 4.425
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Override field methods
4
 *
5
 * @package     Kirki
6
 * @subpackage  Controls
7
 * @copyright   Copyright (c) 2017, 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
				if ( ! isset( $this->fields[ $subfield_id ]['type'] ) ) {
101
					continue;
102
				}
103
				$subfield_type = $this->fields[ $subfield_id ]['type'];
104
105
				// Allow using a sanitize-callback on a per-field basis.
106
				if ( isset( $this->fields[ $subfield_id ]['sanitize_callback'] ) ) {
107
108
					$subfield_value = call_user_func( $this->fields[ $subfield_id ]['sanitize_callback'], $subfield_value );
109
110
				} else {
111
112
					switch ( $subfield_type ) {
113
						case 'image':
114
						case 'cropped_image':
115
						case 'upload':
116
							if ( ! is_numeric( $subfield_value ) && is_string( $subfield_value ) ) {
117
								$subfield_value = esc_url_raw( $subfield_value );
1 ignored issue
show
Bug introduced by
The function esc_url_raw was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
								$subfield_value = /** @scrutinizer ignore-call */ esc_url_raw( $subfield_value );
Loading history...
118
							}
119
							break;
120
						case 'dropdown-pages':
121
							$subfield_value = (int) $subfield_value;
122
							break;
123
						case 'color':
124
							// Instantiate the object.
125
							$color_obj       = ariColor::newColor( $subfield_value );
1 ignored issue
show
Bug introduced by
The type ariColor was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
126
							$$subfield_value = $color_obj->toCSS( $color_obj->mode );
127
							break;
128
						case 'text':
129
							$subfield_value = sanitize_text_field( $subfield_value );
1 ignored issue
show
Bug introduced by
The function sanitize_text_field was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
							$subfield_value = /** @scrutinizer ignore-call */ sanitize_text_field( $subfield_value );
Loading history...
130
							break;
131
						case 'url':
132
						case 'link':
133
							$subfield_value = esc_url_raw( $subfield_value );
134
							break;
135
						case 'email':
136
							$subfield_value = filter_var( $subfield_value, FILTER_SANITIZE_EMAIL );
137
							break;
138
						case 'tel':
139
							$subfield_value = esc_attr( $subfield_value );
1 ignored issue
show
Bug introduced by
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

139
							$subfield_value = /** @scrutinizer ignore-call */ esc_attr( $subfield_value );
Loading history...
140
							break;
141
						case 'checkbox':
142
							$subfield_value = (bool) $subfield_value;
143
							break;
144
						case 'select':
145
							if ( isset( $this->fields[ $subfield_id ]['multiple'] ) ) {
146
								if ( true === $this->fields[ $subfield_id ]['multiple'] ) {
147
									$multiple = 2;
0 ignored issues
show
Unused Code introduced by
The assignment to $multiple is dead and can be removed.
Loading history...
148
								}
149
								$multiple = (int) $this->fields[ $subfield_id ]['multiple'];
150
								if ( 1 < $multiple ) {
151
									$subfield_value = (array) $subfield_value;
152
									foreach ( $subfield_value as $sub_subfield_key => $sub_subfield_value ) {
153
										$subfield_value[ $sub_subfield_key ] = esc_attr( $sub_subfield_value );
154
									}
155
								} else {
156
									$subfield_value = esc_attr( $subfield_value );
157
								}
158
							}
159
							break;
160
						case 'radio':
161
						case 'radio-image':
162
							$subfield_value = esc_attr( $subfield_value );
163
							break;
164
						case 'textarea':
165
							$subfield_value = html_entity_decode( wp_kses_post( $subfield_value ) );
1 ignored issue
show
Bug introduced by
The function wp_kses_post was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

165
							$subfield_value = html_entity_decode( /** @scrutinizer ignore-call */ wp_kses_post( $subfield_value ) );
Loading history...
166
167
					} // End switch().
168
				} // End if().
169
				$value[ $row_id ][ $subfield_id ] = $subfield_value;
170
			} // End foreach().
171
		} // End foreach().
172
173
		return $value;
174
	}
175
}
176