Completed
Push — develop ( 758947...76df30 )
by Aristeides
02:42
created

Kirki_Output::apply_value_pattern()   D

Complexity

Conditions 17
Paths 61

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 21
nc 61
nop 2
dl 0
loc 35
rs 4.9807
c 0
b 0
f 0

How to fix   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
 * Handles CSS output for fields.
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.0
10
 */
11
12
/**
13
 * Handles field CSS output.
14
 */
15
class Kirki_Output {
16
17
	/**
18
	 * The Kirki configuration used in the field.
19
	 *
20
	 * @access protected
21
	 * @var string
22
	 */
23
	protected $config_id = 'global';
24
25
	/**
26
	 * The field's `output` argument.
27
	 *
28
	 * @access protected
29
	 * @var array
30
	 */
31
	protected $output = array();
32
33
	/**
34
	 * An array of the generated styles.
35
	 *
36
	 * @access protected
37
	 * @var array
38
	 */
39
	protected $styles = array();
40
41
	/**
42
	 * The value.
43
	 *
44
	 * @access protected
45
	 * @var string|array
46
	 */
47
	protected $value;
48
49
	/**
50
	 * The class constructor.
51
	 *
52
	 * @access public
53
	 * @param string       $config_id The config ID.
54
	 * @param array        $output    The output argument.
55
	 * @param string|array $value     The value.
56
	 */
57
	public function __construct( $config_id, $output, $value ) {
58
59
		$this->config_id = $config_id;
60
		$this->value     = $value;
61
		$this->output    = $output;
62
63
		$this->parse_output();
64
	}
65
66
	/**
67
	 * If we have a sanitize_callback defined, apply it to the value.
68
	 *
69
	 * @param array        $output The output args.
70
	 * @param string|array $value  The value.
71
	 *
72
	 * @return string|array
73
	 */
74
	protected function apply_sanitize_callback( $output, $value ) {
75
76
		if ( isset( $output['sanitize_callback'] ) && null !== $output['sanitize_callback'] ) {
77
78
			// If the sanitize_callback is invalid, return the value.
79
			if ( ! is_callable( $output['sanitize_callback'] ) ) {
80
				return $value;
81
			}
82
			return call_user_func( $output['sanitize_callback'], $this->value );
83
		}
84
85
		return $value;
86
87
	}
88
89
	/**
90
	 * If we have a value_pattern defined, apply it to the value.
91
	 *
92
	 * @param array        $output The output args.
93
	 * @param string|array $value  The value.
94
	 *
95
	 * @return string|array
96
	 */
97
	protected function apply_value_pattern( $output, $value ) {
98
99
		if ( isset( $output['value_pattern'] ) && ! empty( $output['value_pattern'] ) && is_string( $output['value_pattern'] ) ) {
100
			if ( is_string( $value ) ) {
101
				$value = str_replace( '$', $value, $output['value_pattern'] );
102
			}
103
			if ( is_array( $value ) ) {
104
				if ( isset( $output['choice'] ) && isset( $value[ $output['choice'] ] ) ) {
105
					$value[ $output['choice'] ] = str_replace( '$', $value[ $output['choice'] ], $output['value_pattern'] );
106
				} else {
107
					foreach ( array_keys( $value ) as $value_k ) {
108
						if ( is_string( $value[ $value_k ] ) ) {
109
							$value[ $value_k ] = str_replace( '$', $value[ $value_k ], $output['value_pattern'] );
110
						}
111
					}
112
				}
113
			}
114
			if ( isset( $output['pattern_replace'] ) && is_array( $output['pattern_replace'] ) ) {
115
				foreach ( $output['pattern_replace'] as $search => $replace ) {
116
					$replacement = Kirki_Values::get_value( $replace );
117
					$replacement = ( false === $replacement || null === $replacement ) ? '' : $replacement;
118
					if ( is_array( $value ) ) {
119
						foreach ( $value as $k => $v ) {
120
							$value[ $k ] = str_replace( $search, $replacement, $value[ $v ] );
121
						}
122
						return $value;
123
					}
124
					$value = str_replace( $search, $replacement, $value );
125
				} // End foreach().
126
			} // End if().
127
		} // End if().
128
129
		return $value;
130
131
	}
132
133
	/**
134
	 * Parses the output arguments.
135
	 * Calls the process_output method for each of them.
136
	 *
137
	 * @access protected
138
	 */
139
	protected function parse_output() {
140
		foreach ( $this->output as $output ) {
141
			$skip = false;
142
143
			// Apply any sanitization callbacks defined.
144
			$value = $this->apply_sanitize_callback( $output, $this->value );
145
146
			// Skip if value is empty.
147
			if ( '' === $this->value ) {
148
				$skip = true;
149
			}
150
151
			// No need to proceed this if the current value is the same as in the "exclude" value.
152
			if ( isset( $output['exclude'] ) && is_array( $output['exclude'] ) ) {
153
				foreach ( $output['exclude'] as $exclude ) {
154
					if ( is_array( $value ) ) {
155
						if ( is_array( $exclude ) ) {
156
							$diff1 = array_diff( $value, $exclude );
157
							$diff2 = array_diff( $exclude, $value );
158
159
							if ( empty( $diff1 ) && empty( $diff2 ) ) {
160
								$skip = true;
161
							}
162
						}
163
						// If 'choice' is defined check for sub-values too.
164
						// Fixes https://github.com/aristath/kirki/issues/1416.
165
						if ( isset( $output['choice'] ) && isset( $value[ $output['choice'] ] ) && $exclude == $value[ $output['choice'] ] ) {
166
							$skip = true;
167
						}
168
					}
169
					if ( $skip ) {
170
						continue;
171
					}
172
173
					// Skip if value is defined as excluded.
174
					if ( $exclude === $value ) {
175
						$skip = true;
176
					}
177
				}
178
			}
179
			if ( $skip ) {
180
				continue;
181
			}
182
183
			// Apply any value patterns defined.
184
			$value = $this->apply_value_pattern( $output, $value );
185
186
			if ( isset( $output['element'] ) && is_array( $output['element'] ) ) {
187
				$output['element'] = array_unique( $output['element'] );
188
				sort( $output['element'] );
189
				$output['element'] = implode( ',', $output['element'] );
190
			}
191
192
			$value = $this->process_value( $value, $output );
193
			$this->process_output( $output, $value );
0 ignored issues
show
Bug introduced by
It seems like $value defined by $this->process_value($value, $output) on line 192 can also be of type array; however, Kirki_Output::process_output() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
194
		} // End foreach().
195
	}
196
197
	/**
198
	 * Parses an output and creates the styles array for it.
199
	 *
200
	 * @access protected
201
	 * @param array  $output The field output.
202
	 * @param string $value  The value.
203
	 *
204
	 * @return void
205
	 */
206
	protected function process_output( $output, $value ) {
207
		if ( ! isset( $output['element'] ) || ! isset( $output['property'] ) ) {
208
			return;
209
		}
210
		$output['media_query'] = ( isset( $output['media_query'] ) ) ? $output['media_query'] : 'global';
211
		$output['prefix']      = ( isset( $output['prefix'] ) ) ? $output['prefix'] : '';
212
		$output['units']       = ( isset( $output['units'] ) ) ? $output['units'] : '';
213
		$output['suffix']      = ( isset( $output['suffix'] ) ) ? $output['suffix'] : '';
214
215
		// Properties that can accept multiple values.
216
		// Useful for example for gradients where all browsers use the "background-image" property
217
		// and the browser prefixes go in the value_pattern arg.
218
		$accepts_multiple = array(
219
			'background-image',
220
			'background',
221
		);
222
		if ( in_array( $output['property'], $accepts_multiple, true ) ) {
223
			if ( isset( $this->styles[ $output['media_query'] ][ $output['element'] ][ $output['property'] ] ) && ! is_array( $this->styles[ $output['media_query'] ][ $output['element'] ][ $output['property'] ] ) ) {
224
				$this->styles[ $output['media_query'] ][ $output['element'] ][ $output['property'] ] = (array) $this->styles[ $output['media_query'] ][ $output['element'] ][ $output['property'] ];
225
			}
226
			$this->styles[ $output['media_query'] ][ $output['element'] ][ $output['property'] ][] = $output['prefix'] . $value . $output['units'] . $output['suffix'];
227
			return;
228
		}
229
		$this->styles[ $output['media_query'] ][ $output['element'] ][ $output['property'] ] = $output['prefix'] . $value . $output['units'] . $output['suffix'];
230
	}
231
232
	/**
233
	 * Some CSS properties are unique.
234
	 * We need to tweak the value to make everything works as expected.
235
	 *
236
	 * @access protected
237
	 * @param string $property The CSS property.
238
	 * @param string $value    The value.
239
	 *
240
	 * @return array
241
	 */
242
	protected function process_property_value( $property, $value ) {
243
		$properties = apply_filters( "kirki/{$this->config_id}/output/property-classnames", array(
244
			'font-family'         => 'Kirki_Output_Property_Font_Family',
245
			'background-image'    => 'Kirki_Output_Property_Background_Image',
246
			'background-position' => 'Kirki_Output_Property_Background_Position',
247
		) );
248
		if ( array_key_exists( $property, $properties ) ) {
249
			$classname = $properties[ $property ];
250
			$obj = new $classname( $property, $value );
251
			return $obj->get_value();
252
		}
253
		return $value;
254
	}
255
256
	/**
257
	 * Returns the value.
258
	 *
259
	 * @access protected
260
	 * @param string|array $value The value.
261
	 * @param array        $output The field "output".
262
	 * @return string|array
263
	 */
264
	protected function process_value( $value, $output ) {
265
		if ( isset( $output['property'] ) ) {
266
			return $this->process_property_value( $output['property'], $value );
0 ignored issues
show
Bug introduced by
It seems like $value defined by parameter $value on line 264 can also be of type array; however, Kirki_Output::process_property_value() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
267
		}
268
		return $value;
269
	}
270
271
	/**
272
	 * Exploses the private $styles property to the world
273
	 *
274
	 * @access protected
275
	 * @return array
276
	 */
277
	public function get_styles() {
278
		return $this->styles;
279
	}
280
}
281