Completed
Push — develop ( d9c9c1...345147 )
by Aristeides
33:42 queued 26:47
created

Kirki_Active_Callback::evaluate_requirement()   D

Complexity

Conditions 10
Paths 7

Size

Total Lines 43
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 18
nc 7
nop 3
dl 0
loc 43
rs 4.8196
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
 * Active callback used with the "required" argument in fields
4
 *
5
 * @package     Kirki
6
 * @category    Core
7
 * @author      Aristeides Stathopoulos
8
 * @copyright   Copyright (c) 2016, Aristeides Stathopoulos
9
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
10
 */
11
12
/**
13
 * Callback class for use with the "required" argument
14
 */
15
class Kirki_Active_Callback {
16
17
	/**
18
	 * Figure out whether the current object should be displayed or not.
19
	 *
20
	 * @param  WP_Customize_Setting $object The current field.
21
	 * @return boolean
22
	 */
23
	public static function evaluate( $object ) {
24
25
		$show = true;
26
27
		// Get all fields.
28
		$fields = Kirki::$fields;
29
30
		// Make sure the current object matches a registered field.
31
		if ( ! isset( $object->setting->id ) || ! isset( $fields[ $object->setting->id ] ) ) {
32
			return true;
33
		}
34
35
		$field = $fields[ $object->setting->id ];
36
37
		if ( isset( $field['required'] ) ) {
38
39
			foreach ( $field['required'] as $requirement ) {
40
				// Handles "AND" functionality.
41
				$show = self::evaluate_requirement( $object, $field, $requirement );
42
				// No need to process further if one requirement returns false.
43
				if ( ! $show ) {
44
					return false;
45
				}
46
			}
47
		}
48
49
		return true;
50
51
	}
52
53
	/**
54
	 * Figure out whether the current object should be displayed or not.
55
	 * We're only parsing a single requirement here from the array of requirements.
56
	 * This is a proxy function that facilitates evaluating and/or conditions.
57
	 *
58
	 * @param  WP_Customize_Setting $object      The current field.
59
	 * @param  object               $field       The current object.
60
	 * @param  array                $requirement A single requirement.
61
	 * @return boolean
62
	 */
63
	private static function evaluate_requirement( $object, $field, $requirement ) {
64
65
		// Test for callables first.
66
		if ( is_callable( $requirement ) ) {
67
			return call_user_func_array( $requirement, array( $field, $object ) );
68
		}
69
70
		// Look for comparison array.
71
		if ( is_array( $requirement ) && isset( $requirement['operator'], $requirement['value'], $requirement['setting'] ) ) {
72
73
			if ( isset( $field['option_name'] ) && '' !== $field['option_name'] ) {
74
				if ( false === strpos( $requirement['setting'], '[' ) ) {
75
					$requirement['setting'] = $field['option_name'] . '[' . $requirement['setting'] . ']';
76
				}
77
			}
78
79
			$current_setting = $object->manager->get_setting( $requirement['setting'] );
80
81
			/**
82
			 * Depending on the 'operator' argument we use,
83
			 * we'll need to perform the appropriate comparison
84
			 * and figure out if the control will be shown or not.
85
			 */
86
			$show = self::compare( $requirement['value'], $current_setting->value(), $requirement['operator'] );
87
88
		} else {
89
			if ( ! is_array( $requirement ) ) {
90
				return true;
91
			}
92
93
			// Handles "OR" functionality.
94
			$show = false;
95
			foreach ( $requirement as $sub_requirement ) {
96
				$show = self::evaluate_requirement( $object, $field, $sub_requirement );
97
				// No need to go on if one sub_requirement returns true.
98
				if ( $show ) {
99
					return true;
100
				}
101
			}
102
		}
103
104
		return $show;
105
	}
106
107
	/**
108
	 * Compares the 2 values given the condition
109
	 *
110
	 * @param mixed  $value1   The 1st value in the comparison.
111
	 * @param mixed  $value2   The 2nd value in the comparison.
112
	 * @param string $operator The operator we'll use for the comparison.
113
	 * @return boolean whether The comparison has succeded (true) or failed (false).
114
	 */
115
	public static function compare( $value1, $value2, $operator ) {
116
		switch ( $operator ) {
117
			case '===':
118
				$show = ( $value1 === $value2 ) ? true : false;
119
				break;
120
			case '==':
121
			case '=':
122
			case 'equals':
123
			case 'equal':
124
				$show = ( $value1 == $value2 ) ? true : false;
125
				break;
126
			case '!==':
127
				$show = ( $value1 !== $value2 ) ? true : false;
128
				break;
129
			case '!=':
130
			case 'not equal':
131
				$show = ( $value1 != $value2 ) ? true : false;
132
				break;
133
			case '>=':
134
			case 'greater or equal':
135
			case 'equal or greater':
136
				$show = ( $value1 >= $value2 ) ? true : false;
137
				break;
138
			case '<=':
139
			case 'smaller or equal':
140
			case 'equal or smaller':
141
				$show = ( $value1 <= $value2 ) ? true : false;
142
				break;
143
			case '>':
144
			case 'greater':
145
				$show = ( $value1 > $value2 ) ? true : false;
146
				break;
147
			case '<':
148
			case 'smaller':
149
				$show = ( $value1 < $value2 ) ? true : false;
150
				break;
151
			case 'contains':
152
			case 'in':
153
				if ( is_array( $value1 ) && ! is_array( $value2 ) ) {
154
					$array  = $value1;
155
					$string = $value2;
156
				} elseif ( is_array( $value2 ) && ! is_array( $value1 ) ) {
157
					$array  = $value2;
158
					$string = $value1;
159
				}
160
				if ( isset( $array ) && isset( $string ) ) {
161
					if ( ! in_array( $string, $array ) ) {
162
						$show = false;
163
					}
164
				} else {
165
					if ( false === strrpos( $value1, $value2 ) && false === strpos( $value2, $value1 ) ) {
166
						$show = false;
167
					}
168
				}
169
				break;
170
			default:
171
				$show = ( $value1 == $value2 ) ? true : false;
172
173
		} // End switch().
174
175
		if ( isset( $show ) ) {
176
			return $show;
177
		}
178
179
		return true;
180
	}
181
}
182