class-redux-required.php (8366B)
1 <?php 2 /** 3 * Redux Required Class 4 * 5 * @class Redux_Required 6 * @version 4.0.0 7 * @package Redux Framework 8 */ 9 10 defined( 'ABSPATH' ) || exit; 11 12 if ( ! class_exists( 'Redux_Required', false ) ) { 13 14 /** 15 * Class Redux_Required 16 */ 17 class Redux_Required extends Redux_Class { 18 19 /** 20 * Array of fields to reload. 21 * 22 * @var array 23 */ 24 public $reload_fields = array(); 25 26 /** 27 * Checks dependencies between objects based on the $field['required'] array 28 * If the array is set it needs to have exactly 3 entries. 29 * The first entry describes which field should be monitored by the current field. eg: "content" 30 * The second entry describes the comparison parameter. eg: "equals, not, is_larger, is_smaller ,contains" 31 * The third entry describes the value that we are comparing against. 32 * Example: if the required array is set to array('content','equals','Hello World'); then the current 33 * field will only be displayed if the field with id "content" has exactly the value "Hello World" 34 * 35 * @param array $field Field array. 36 */ 37 public function check_dependencies( array $field ) { 38 $core = $this->core(); 39 40 if ( isset( $field['ajax_save'] ) && false === $field['ajax_save'] ) { 41 $core->required_class->reload_fields[] = $field['id']; 42 } 43 44 if ( ! empty( $field['required'] ) ) { 45 if ( ! isset( $core->required_child[ $field['id'] ] ) ) { 46 $core->required_child[ $field['id'] ] = array(); 47 } 48 49 if ( ! isset( $core->required[ $field['id'] ] ) ) { 50 $core->required[ $field['id'] ] = array(); 51 } 52 53 if ( is_array( $field['required'][0] ) ) { 54 55 foreach ( $field['required'] as $value ) { 56 if ( is_array( $value ) && 3 === count( $value ) ) { 57 $data = array(); 58 $data['parent'] = $value[0]; 59 $data['operation'] = $value[1]; 60 $data['checkValue'] = $value[2]; 61 62 $core->required[ $data['parent'] ][ $field['id'] ][] = $data; 63 64 if ( ! in_array( $data['parent'], $core->required_child[ $field['id'] ], true ) ) { 65 $core->required_child[ $field['id'] ][] = $data; 66 } 67 68 $this->check_required_dependencies( $core, $field, $data ); 69 } 70 } 71 } else { 72 $data = array(); 73 $data['parent'] = $field['required'][0]; 74 $data['operation'] = $field['required'][1]; 75 $data['checkValue'] = $field['required'][2]; 76 77 $core->required[ $data['parent'] ][ $field['id'] ][] = $data; 78 79 if ( ! in_array( $data['parent'], $core->required_child[ $field['id'] ], true ) ) { 80 $core->required_child[ $field['id'] ][] = $data; 81 } 82 83 $this->check_required_dependencies( $core, $field, $data ); 84 } 85 } 86 } 87 88 /** 89 * Check field for require deps. 90 * 91 * @param object $core ReduxFramework core pointer. 92 * @param array $field Field array. 93 * @param array $data Required data. 94 */ 95 private function check_required_dependencies( $core, array $field, array $data ) { 96 // required field must not be hidden. Otherwise, hide this one by default. 97 if ( ! in_array( $data['parent'], $core->fields_hidden, true ) && ( ! isset( $core->folds[ $field['id'] ] ) || 'hide' !== $core->folds[ $field['id'] ] ) ) { 98 if ( isset( $core->options[ $data['parent'] ] ) ) { 99 $return = $this->compare_value_dependencies( $core->options[ $data['parent'] ], $data['checkValue'], $data['operation'] ); 100 } 101 } 102 103 if ( ( isset( $return ) && $return ) && ( ! isset( $core->folds[ $field['id'] ] ) || 'hide' !== $core->folds[ $field['id'] ] ) ) { 104 $core->folds[ $field['id'] ] = 'show'; 105 } else { 106 $core->folds[ $field['id'] ] = 'hide'; 107 108 if ( ! in_array( $field['id'], $core->fields_hidden, true ) ) { 109 $core->fields_hidden[] = $field['id']; 110 } 111 } 112 } 113 114 /** 115 * Compare data for required field. 116 * 117 * @param mixed $parent_value Parent value. 118 * @param mixed $check_value Check value. 119 * @param string $operation Operation. 120 * 121 * @return bool 122 */ 123 public function compare_value_dependencies( $parent_value, $check_value, string $operation ): bool { 124 $return = false; 125 126 switch ( $operation ) { 127 case '=': 128 case 'equals': 129 $data['operation'] = '='; 130 131 if ( is_array( $parent_value ) ) { 132 foreach ( $parent_value as $idx => $val ) { 133 if ( is_array( $check_value ) ) { 134 foreach ( $check_value as $i => $v ) { 135 if ( Redux_Helpers::make_bool_str( $val ) === Redux_Helpers::make_bool_str( $v ) ) { 136 $return = true; 137 } 138 } 139 } else { 140 if ( Redux_Helpers::make_bool_str( $val ) === Redux_Helpers::make_bool_str( $check_value ) ) { 141 $return = true; 142 } 143 } 144 } 145 } else { 146 if ( is_array( $check_value ) ) { 147 foreach ( $check_value as $i => $v ) { 148 if ( Redux_Helpers::make_bool_str( $parent_value ) === Redux_Helpers::make_bool_str( $v ) ) { 149 $return = true; 150 } 151 } 152 } else { 153 if ( Redux_Helpers::make_bool_str( $parent_value ) === Redux_Helpers::make_bool_str( $check_value ) ) { 154 $return = true; 155 } 156 } 157 } 158 break; 159 160 case '!=': 161 case 'not': 162 $data['operation'] = '!=='; 163 if ( is_array( $parent_value ) ) { 164 foreach ( $parent_value as $idx => $val ) { 165 if ( is_array( $check_value ) ) { 166 foreach ( $check_value as $i => $v ) { 167 if ( Redux_Helpers::make_bool_str( $val ) !== Redux_Helpers::make_bool_str( $v ) ) { 168 $return = true; 169 } 170 } 171 } else { 172 if ( Redux_Helpers::make_bool_str( $val ) !== Redux_Helpers::make_bool_str( $check_value ) ) { 173 $return = true; 174 } 175 } 176 } 177 } else { 178 if ( is_array( $check_value ) ) { 179 foreach ( $check_value as $i => $v ) { 180 if ( Redux_Helpers::make_bool_str( $parent_value ) !== Redux_Helpers::make_bool_str( $v ) ) { 181 $return = true; 182 } 183 } 184 } else { 185 if ( Redux_Helpers::make_bool_str( $parent_value ) !== Redux_Helpers::make_bool_str( $check_value ) ) { 186 $return = true; 187 } 188 } 189 } 190 191 break; 192 case '>': 193 case 'greater': 194 case 'is_larger': 195 $data['operation'] = '>'; 196 if ( $parent_value > $check_value ) { 197 $return = true; 198 } 199 break; 200 case '>=': 201 case 'greater_equal': 202 case 'is_larger_equal': 203 $data['operation'] = '>='; 204 if ( $parent_value >= $check_value ) { 205 $return = true; 206 } 207 break; 208 case '<': 209 case 'less': 210 case 'is_smaller': 211 $data['operation'] = '<'; 212 if ( $parent_value < $check_value ) { 213 $return = true; 214 } 215 break; 216 case '<=': 217 case 'less_equal': 218 case 'is_smaller_equal': 219 $data['operation'] = '<='; 220 if ( $parent_value <= $check_value ) { 221 $return = true; 222 } 223 break; 224 case 'contains': 225 if ( is_array( $parent_value ) ) { 226 $parent_value = implode( ',', $parent_value ); 227 } 228 229 if ( is_array( $check_value ) ) { 230 foreach ( $check_value as $idx => $opt ) { 231 if ( strpos( $parent_value, (string) $opt ) !== false ) { 232 $return = true; 233 } 234 } 235 } else { 236 if ( strpos( $parent_value, (string) $check_value ) !== false ) { 237 $return = true; 238 } 239 } 240 241 break; 242 case 'doesnt_contain': 243 case 'not_contain': 244 if ( is_array( $parent_value ) ) { 245 $parent_value = implode( ',', $parent_value ); 246 } 247 248 if ( is_array( $check_value ) ) { 249 foreach ( $check_value as $idx => $opt ) { 250 if ( strpos( $parent_value, (string) $opt ) === false ) { 251 $return = true; 252 } 253 } 254 } else { 255 if ( strpos( $parent_value, (string) $check_value ) === false ) { 256 $return = true; 257 } 258 } 259 260 break; 261 case 'is_empty_or': 262 if ( empty( $parent_value ) || $check_value === $parent_value ) { 263 $return = true; 264 } 265 break; 266 case 'not_empty_and': 267 if ( ! empty( $parent_value ) && $check_value !== $parent_value ) { 268 $return = true; 269 } 270 break; 271 case 'is_empty': 272 case 'empty': 273 case '!isset': 274 if ( empty( $parent_value ) || '' === $parent_value ) { 275 $return = true; 276 } 277 break; 278 case 'not_empty': 279 case '!empty': 280 case 'isset': 281 if ( ! empty( $parent_value ) && '' !== $parent_value ) { 282 $return = true; 283 } 284 break; 285 } 286 287 return $return; 288 } 289 } 290 }