bidirectional.php
6 years ago
data.php
6 years ago
fields.php
6 years ago
permissions.php
6 years ago
settings.php
6 years ago
validation.php
6 years ago
validation.php
635 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | if(!class_exists('acfe_field_validation')): |
| 7 | |
| 8 | class acfe_field_validation{ |
| 9 | |
| 10 | public $functions = array(); |
| 11 | |
| 12 | function __construct(){ |
| 13 | |
| 14 | // Actions |
| 15 | add_action('load-post.php', array($this, 'load')); |
| 16 | add_action('wp_ajax_acf/field_group/render_field_settings', array($this, 'field_types_action'), 5); |
| 17 | |
| 18 | // Filters |
| 19 | add_filter('acf/validate_value', array($this, 'validate_value'), 99, 4); |
| 20 | |
| 21 | // Validation functions |
| 22 | $this->functions = array( |
| 23 | |
| 24 | 'General' => array( |
| 25 | 'value' => 'If value', |
| 26 | 'strlen' => 'If value length - strlen(value)', |
| 27 | ), |
| 28 | |
| 29 | 'Exists' => array( |
| 30 | 'email_exists' => 'If email exists - email_exists(value)', |
| 31 | 'post_type_exists' => 'If post type exists - post_type_exists(value)', |
| 32 | 'taxonomy_exists' => 'If taxonomy exists - taxonomy_exists(value)', |
| 33 | 'term_exists' => 'If term exists - term_exists(value)', |
| 34 | 'username_exists' => 'If username exists - username_exists(value)', |
| 35 | ), |
| 36 | |
| 37 | 'Is' => array( |
| 38 | 'is_email' => 'If is email - is_email(value))', |
| 39 | 'is_user_logged_in' => 'If is user logged in - is_user_logged_in()', |
| 40 | ), |
| 41 | |
| 42 | 'Sanitize' => array( |
| 43 | 'sanitize_email' => 'If sanitize email - sanitize_email(value)', |
| 44 | 'sanitize_file_name' => 'If sanitize file name - sanitize_file_name(value)', |
| 45 | 'sanitize_html_class' => 'If sanitize html class - sanitize_html_class(value)', |
| 46 | 'sanitize_key' => 'If sanitize key - sanitize_key(value)', |
| 47 | 'sanitize_meta' => 'If sanitize meta - sanitize_meta(value)', |
| 48 | 'sanitize_mime_type' => 'If sanitize mime type - sanitize_mime_type(value)', |
| 49 | 'sanitize_option' => 'If sanitize option - sanitize_option(value)', |
| 50 | 'sanitize_text_field' => 'If sanitize text field - sanitize_text_field(value)', |
| 51 | 'sanitize_title' => 'If sanitize title - sanitize_title(value)', |
| 52 | 'sanitize_user' => 'If sanitize user - sanitize_user(value)', |
| 53 | ), |
| 54 | |
| 55 | 'User' => array( |
| 56 | 'get_user_by_id' => 'If get user by id - get_user_by(\'id\', value)', |
| 57 | 'get_user_by_slug' => 'If get user by slug - get_user_by(\'slug\', value)', |
| 58 | 'get_user_by_email' => 'If get user by email - get_user_by(\'email\', value)', |
| 59 | 'get_user_by_login' => 'If get user by login - get_user_by(\'login\', value)', |
| 60 | ) |
| 61 | |
| 62 | ); |
| 63 | |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Load |
| 68 | */ |
| 69 | function load(){ |
| 70 | |
| 71 | if(!acf_is_screen('acf-field-group')) |
| 72 | return; |
| 73 | |
| 74 | $this->field_types_action(); |
| 75 | |
| 76 | // Fix: Repeater |
| 77 | add_filter('acf/prepare_field/name=acfe_validate', array($this, 'fix_repeater')); |
| 78 | add_filter('acf/prepare_field/name=acfe_validate_location', array($this, 'fix_repeater')); |
| 79 | add_filter('acf/prepare_field/name=acfe_validate_rules_and', array($this, 'fix_repeater')); |
| 80 | add_filter('acf/prepare_field/name=acfe_validate_function', array($this, 'fix_repeater')); |
| 81 | add_filter('acf/prepare_field/name=acfe_validate_operator', array($this, 'fix_repeater')); |
| 82 | add_filter('acf/prepare_field/name=acfe_validate_match', array($this, 'fix_repeater')); |
| 83 | add_filter('acf/prepare_field/name=acfe_validate_error', array($this, 'fix_repeater')); |
| 84 | |
| 85 | // Fix: Clone |
| 86 | add_filter('acf/update_field', array($this, 'fix_clone')); |
| 87 | |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get field types |
| 92 | */ |
| 93 | function field_types_action(){ |
| 94 | |
| 95 | // Get Fields Types |
| 96 | foreach(acf_get_field_types_info() as $field){ |
| 97 | |
| 98 | // Field type |
| 99 | $field_type = $field['name']; |
| 100 | |
| 101 | // Exclude |
| 102 | if(in_array($field_type, array('message', 'accordion', 'tab', 'acfe_button', 'acfe_column', 'acfe_dynamic_message', 'group', 'repeater', 'flexible_content', 'clone'))) |
| 103 | continue; |
| 104 | |
| 105 | add_action('acf/render_field_settings/type=' . $field_type, array($this, 'render_field_settings'), 990); |
| 106 | |
| 107 | } |
| 108 | |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Add Setting |
| 113 | */ |
| 114 | function render_field_settings($field){ |
| 115 | |
| 116 | $valid = false; |
| 117 | |
| 118 | // Ajax |
| 119 | if(acf_verify_ajax()){ |
| 120 | |
| 121 | $field_group = acfe_get_field_group_from_field($field); |
| 122 | |
| 123 | if(acf_maybe_get($field_group, 'acfe_form')) |
| 124 | $valid = true; |
| 125 | |
| 126 | } |
| 127 | |
| 128 | // Display |
| 129 | else{ |
| 130 | |
| 131 | if(acf_maybe_get($field, 'acfe_form')) |
| 132 | $valid = true; |
| 133 | |
| 134 | if(!$valid && acf_maybe_get($field, '_name') === 'new_field'){ |
| 135 | |
| 136 | $field_group_id = get_the_ID(); |
| 137 | |
| 138 | if($field_group_id){ |
| 139 | |
| 140 | $field_group = acf_get_field_group($field_group_id); |
| 141 | |
| 142 | if(acf_maybe_get($field_group, 'acfe_form')) |
| 143 | $valid = true; |
| 144 | |
| 145 | } |
| 146 | |
| 147 | } |
| 148 | |
| 149 | } |
| 150 | |
| 151 | if(!$valid) |
| 152 | return; |
| 153 | |
| 154 | $choices = apply_filters('acfe/validate/functions', $this->functions, $field); |
| 155 | |
| 156 | if(empty($choices)) |
| 157 | return; |
| 158 | |
| 159 | // Settings |
| 160 | acf_render_field_setting($field, array( |
| 161 | 'label' => __('Advanced validation'), |
| 162 | 'name' => 'acfe_validate', |
| 163 | 'key' => 'acfe_validate', |
| 164 | 'instructions' => __('Validate value against rules'), |
| 165 | 'type' => 'repeater', |
| 166 | 'button_label' => __('Add validation'), |
| 167 | 'required' => false, |
| 168 | 'layout' => 'row', |
| 169 | 'sub_fields' => array( |
| 170 | array( |
| 171 | 'label' => 'Location', |
| 172 | 'name' => 'acfe_validate_location', |
| 173 | 'key' => 'acfe_validate_location', |
| 174 | 'type' => 'select', |
| 175 | 'instructions' => '', |
| 176 | 'required' => 0, |
| 177 | 'conditional_logic' => 0, |
| 178 | 'wrapper' => array( |
| 179 | 'width' => '', |
| 180 | 'class' => '', |
| 181 | 'id' => '', |
| 182 | ), |
| 183 | 'choices' => array( |
| 184 | 'admin' => 'Administration', |
| 185 | 'front' => 'Front-end', |
| 186 | ), |
| 187 | 'allow_null' => true, |
| 188 | 'multiple' => 0, |
| 189 | 'ui' => 0, |
| 190 | 'return_format' => 'value', |
| 191 | 'ajax' => 0, |
| 192 | 'placeholder' => 'Everywhere', |
| 193 | ), |
| 194 | array( |
| 195 | 'label' => __('Rules'), |
| 196 | 'name' => 'acfe_validate_rules_and', |
| 197 | 'key' => 'acfe_validate_rules_and', |
| 198 | 'instructions' => '', |
| 199 | 'type' => 'repeater', |
| 200 | 'button_label' => __('+ AND'), |
| 201 | 'required' => false, |
| 202 | 'layout' => 'table', |
| 203 | 'sub_fields' => array( |
| 204 | array( |
| 205 | 'label' => 'Function', |
| 206 | 'name' => 'acfe_validate_function', |
| 207 | 'key' => 'acfe_validate_function', |
| 208 | 'prefix' => '', |
| 209 | '_name' => '', |
| 210 | '_prepare' => '', |
| 211 | 'type' => 'select', |
| 212 | 'choices' => $choices, |
| 213 | 'instructions' => false, |
| 214 | 'required' => false, |
| 215 | 'wrapper' => array( |
| 216 | 'width' => '', |
| 217 | 'class' => '', |
| 218 | 'id' => '', |
| 219 | ), |
| 220 | ), |
| 221 | array( |
| 222 | 'label' => 'Operator / Value', |
| 223 | 'name' => 'acfe_validate_operator', |
| 224 | 'key' => 'acfe_validate_operator', |
| 225 | 'prefix' => '', |
| 226 | '_name' => '', |
| 227 | '_prepare' => '', |
| 228 | 'type' => 'select', |
| 229 | 'choices' => array( |
| 230 | 'Operators' => array( |
| 231 | '==' => '==', |
| 232 | '!=' => '!=', |
| 233 | '>' => '>', |
| 234 | '>=' => '>=', |
| 235 | '<' => '<', |
| 236 | '<=' => '<=', |
| 237 | 'contains' => 'Contains', |
| 238 | '!contains' => 'Doesn\'t contain', |
| 239 | 'starts' => 'Starts with', |
| 240 | '!starts' => 'Doesn\'t start with', |
| 241 | 'ends' => 'Ends with', |
| 242 | '!ends' => 'Doesn\'t end with', |
| 243 | ), |
| 244 | 'Values' => array( |
| 245 | 'true' => '== true', |
| 246 | 'false' => '== false', |
| 247 | 'null' => '== null', |
| 248 | 'empty' => '== (empty)', |
| 249 | '!empty' => '!= (empty)', |
| 250 | ) |
| 251 | ), |
| 252 | 'instructions' => false, |
| 253 | 'required' => false, |
| 254 | 'wrapper' => array( |
| 255 | 'width' => '', |
| 256 | 'class' => '', |
| 257 | 'id' => '', |
| 258 | ), |
| 259 | ), |
| 260 | array( |
| 261 | 'label' => 'Value', |
| 262 | 'name' => 'acfe_validate_match', |
| 263 | 'key' => 'acfe_validate_match', |
| 264 | 'prefix' => '', |
| 265 | '_name' => '', |
| 266 | '_prepare' => '', |
| 267 | 'type' => 'text', |
| 268 | 'instructions' => false, |
| 269 | 'placeholder' => '', |
| 270 | 'required' => false, |
| 271 | 'wrapper' => array( |
| 272 | 'width' => '', |
| 273 | 'class' => '', |
| 274 | 'id' => '', |
| 275 | ), |
| 276 | 'conditional_logic' => array( |
| 277 | array( |
| 278 | array( |
| 279 | 'field' => 'acfe_validate_operator', |
| 280 | 'operator' => '==', |
| 281 | 'value' => '==', |
| 282 | ) |
| 283 | ), |
| 284 | array( |
| 285 | array( |
| 286 | 'field' => 'acfe_validate_operator', |
| 287 | 'operator' => '==', |
| 288 | 'value' => '!=', |
| 289 | ) |
| 290 | ), |
| 291 | array( |
| 292 | array( |
| 293 | 'field' => 'acfe_validate_operator', |
| 294 | 'operator' => '==', |
| 295 | 'value' => '>', |
| 296 | ) |
| 297 | ), |
| 298 | array( |
| 299 | array( |
| 300 | 'field' => 'acfe_validate_operator', |
| 301 | 'operator' => '==', |
| 302 | 'value' => '>=', |
| 303 | ) |
| 304 | ), |
| 305 | array( |
| 306 | array( |
| 307 | 'field' => 'acfe_validate_operator', |
| 308 | 'operator' => '==', |
| 309 | 'value' => '<', |
| 310 | ) |
| 311 | ), |
| 312 | array( |
| 313 | array( |
| 314 | 'field' => 'acfe_validate_operator', |
| 315 | 'operator' => '==', |
| 316 | 'value' => '<=', |
| 317 | ) |
| 318 | ), |
| 319 | array( |
| 320 | array( |
| 321 | 'field' => 'acfe_validate_operator', |
| 322 | 'operator' => '==', |
| 323 | 'value' => 'contains', |
| 324 | ) |
| 325 | ), |
| 326 | array( |
| 327 | array( |
| 328 | 'field' => 'acfe_validate_operator', |
| 329 | 'operator' => '==', |
| 330 | 'value' => '!contains', |
| 331 | ) |
| 332 | ), |
| 333 | array( |
| 334 | array( |
| 335 | 'field' => 'acfe_validate_operator', |
| 336 | 'operator' => '==', |
| 337 | 'value' => 'starts', |
| 338 | ) |
| 339 | ), |
| 340 | array( |
| 341 | array( |
| 342 | 'field' => 'acfe_validate_operator', |
| 343 | 'operator' => '==', |
| 344 | 'value' => '!starts', |
| 345 | ) |
| 346 | ), |
| 347 | array( |
| 348 | array( |
| 349 | 'field' => 'acfe_validate_operator', |
| 350 | 'operator' => '==', |
| 351 | 'value' => 'ends', |
| 352 | ) |
| 353 | ), |
| 354 | array( |
| 355 | array( |
| 356 | 'field' => 'acfe_validate_operator', |
| 357 | 'operator' => '==', |
| 358 | 'value' => '!ends', |
| 359 | ) |
| 360 | ), |
| 361 | ) |
| 362 | ), |
| 363 | ) |
| 364 | ), |
| 365 | array( |
| 366 | 'label' => 'Error', |
| 367 | 'name' => 'acfe_validate_error', |
| 368 | 'key' => 'acfe_validate_error', |
| 369 | 'prefix' => '', |
| 370 | '_name' => '', |
| 371 | '_prepare' => '', |
| 372 | 'type' => 'text', |
| 373 | 'instructions' => false, |
| 374 | 'required' => false, |
| 375 | 'wrapper' => array( |
| 376 | 'width' => '', |
| 377 | 'class' => '', |
| 378 | 'id' => '', |
| 379 | ), |
| 380 | ), |
| 381 | ) |
| 382 | ), false); |
| 383 | |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Validate |
| 388 | */ |
| 389 | function validate_value($valid, $value, $field, $input){ |
| 390 | |
| 391 | if(!$valid) |
| 392 | return $valid; |
| 393 | |
| 394 | if(!acf_maybe_get($field, 'acfe_validate')) |
| 395 | return $valid; |
| 396 | |
| 397 | foreach($field['acfe_validate'] as $k => $rule){ |
| 398 | |
| 399 | // Fix possible ACF Clone Index |
| 400 | if($k === 'acfcloneindex') |
| 401 | continue; |
| 402 | |
| 403 | // Screen |
| 404 | $screen = isset($rule['acfe_validate_location']) ? $rule['acfe_validate_location'] : ''; |
| 405 | $screen_allow = false; |
| 406 | |
| 407 | // Screen: All |
| 408 | if(empty($screen)){ |
| 409 | |
| 410 | $screen_allow = true; |
| 411 | |
| 412 | } |
| 413 | |
| 414 | // Screen: Admin |
| 415 | elseif($screen === 'admin' && acfe_form_is_admin()){ |
| 416 | |
| 417 | $screen_allow = true; |
| 418 | |
| 419 | } |
| 420 | |
| 421 | // Screen: Front |
| 422 | elseif($screen === 'front' && acfe_form_is_front()){ |
| 423 | |
| 424 | $screen_allow = true; |
| 425 | |
| 426 | } |
| 427 | |
| 428 | if(!$screen_allow) |
| 429 | continue; |
| 430 | |
| 431 | if(!acf_maybe_get($rule, 'acfe_validate_rules_and')) |
| 432 | continue; |
| 433 | |
| 434 | $rule_match = true; |
| 435 | |
| 436 | foreach($rule['acfe_validate_rules_and'] as $k => $function){ |
| 437 | |
| 438 | if(!$rule_match) |
| 439 | break; |
| 440 | |
| 441 | $rule_match = false; |
| 442 | |
| 443 | // Check filters |
| 444 | $filters = array( |
| 445 | 'acfe/validate/function/' . $function['acfe_validate_function'] . '/key=' . $field['key'], |
| 446 | 'acfe/validate/function/' . $function['acfe_validate_function'] . '/name=' . $field['name'], |
| 447 | 'acfe/validate/function/' . $function['acfe_validate_function'] . '/type=' . $field['type'], |
| 448 | 'acfe/validate/function/' . $function['acfe_validate_function'], |
| 449 | ); |
| 450 | |
| 451 | $filter_call = false; |
| 452 | foreach($filters as $filter){ |
| 453 | |
| 454 | if(has_filter($filter)) |
| 455 | $filter_call = $filter; |
| 456 | |
| 457 | } |
| 458 | |
| 459 | // Filter |
| 460 | if($filter_call){ |
| 461 | |
| 462 | $result = apply_filters($filter_call, false, $value, $field); |
| 463 | |
| 464 | } |
| 465 | |
| 466 | // Class |
| 467 | elseif(is_callable(array($this, $function['acfe_validate_function']))){ |
| 468 | |
| 469 | $result = call_user_func(array($this, $function['acfe_validate_function']), $value); |
| 470 | |
| 471 | } |
| 472 | |
| 473 | // Function |
| 474 | elseif(is_callable($function['acfe_validate_function'])){ |
| 475 | |
| 476 | $result = call_user_func($function['acfe_validate_function'], $value); |
| 477 | |
| 478 | } |
| 479 | |
| 480 | // Nothing |
| 481 | else{ |
| 482 | |
| 483 | continue; |
| 484 | |
| 485 | } |
| 486 | |
| 487 | // Vars |
| 488 | $operator = $function['acfe_validate_operator']; |
| 489 | $match = $function['acfe_validate_match']; |
| 490 | |
| 491 | if($operator === '==' && $result == $match){ |
| 492 | $rule_match = true; |
| 493 | } |
| 494 | |
| 495 | elseif($operator === '!=' && $result != $match){ |
| 496 | $rule_match = true; |
| 497 | } |
| 498 | |
| 499 | elseif($operator === '>' && $result > $match){ |
| 500 | $rule_match = true; |
| 501 | } |
| 502 | |
| 503 | elseif($operator === '>=' && $result >= $match){ |
| 504 | $rule_match = true; |
| 505 | } |
| 506 | |
| 507 | elseif($operator === '<' && $result < $match){ |
| 508 | $rule_match = true; |
| 509 | } |
| 510 | |
| 511 | elseif($operator === '<=' && $result <= $match){ |
| 512 | $rule_match = true; |
| 513 | } |
| 514 | |
| 515 | elseif($operator === 'contains' && stripos($result, $match) !== false){ |
| 516 | $rule_match = true; |
| 517 | } |
| 518 | |
| 519 | elseif($operator === '!contains' && stripos($result, $match) === false){ |
| 520 | $rule_match = true; |
| 521 | } |
| 522 | |
| 523 | elseif($operator === 'starts' && stripos($result, $match) === 0){ |
| 524 | $rule_match = true; |
| 525 | } |
| 526 | |
| 527 | elseif($operator === '!starts' && stripos($result, $match) !== 0){ |
| 528 | $rule_match = true; |
| 529 | } |
| 530 | |
| 531 | elseif($operator === 'ends' && acfe_ends_with($result, $match)){ |
| 532 | $rule_match = true; |
| 533 | } |
| 534 | |
| 535 | elseif($operator === '!ends' && !acfe_ends_with($result, $match)){ |
| 536 | $rule_match = true; |
| 537 | } |
| 538 | |
| 539 | elseif($operator === 'true' && $result === true){ |
| 540 | $rule_match = true; |
| 541 | } |
| 542 | |
| 543 | elseif($operator === 'false' && $result === false){ |
| 544 | $rule_match = true; |
| 545 | } |
| 546 | |
| 547 | elseif($operator === 'null' && $result === null){ |
| 548 | $rule_match = true; |
| 549 | } |
| 550 | |
| 551 | elseif($operator === 'empty' && empty($result)){ |
| 552 | $rule_match = true; |
| 553 | } |
| 554 | |
| 555 | elseif($operator === '!empty' && !empty($result)){ |
| 556 | $rule_match = true; |
| 557 | } |
| 558 | |
| 559 | } |
| 560 | |
| 561 | // Error |
| 562 | $error = $rule['acfe_validate_error']; |
| 563 | |
| 564 | if($rule_match && !empty($error)) |
| 565 | $valid = $error; |
| 566 | |
| 567 | if(!$valid || is_string($valid)) |
| 568 | break; |
| 569 | |
| 570 | } |
| 571 | |
| 572 | return $valid; |
| 573 | |
| 574 | } |
| 575 | |
| 576 | function get_user_by_id($value){ |
| 577 | |
| 578 | return get_user_by('id', $value); |
| 579 | |
| 580 | } |
| 581 | |
| 582 | function get_user_by_slug($value){ |
| 583 | |
| 584 | return get_user_by('slug', $value); |
| 585 | |
| 586 | } |
| 587 | |
| 588 | function get_user_by_email($value){ |
| 589 | |
| 590 | return get_user_by('email', $value); |
| 591 | |
| 592 | } |
| 593 | |
| 594 | function get_user_by_login($value){ |
| 595 | |
| 596 | return get_user_by('login', $value); |
| 597 | |
| 598 | } |
| 599 | |
| 600 | function value($value){ |
| 601 | |
| 602 | return $value; |
| 603 | |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * Process Setting |
| 608 | */ |
| 609 | function fix_repeater($field){ |
| 610 | |
| 611 | $field['prefix'] = str_replace('row-', '', $field['prefix']); |
| 612 | $field['name'] = str_replace('row-', '', $field['name']); |
| 613 | |
| 614 | return $field; |
| 615 | |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * Setting: ACF Clone Index fix for flexible duplicate |
| 620 | */ |
| 621 | function fix_clone($field){ |
| 622 | |
| 623 | if(isset($field['acfe_validate']['acfcloneindex'])) |
| 624 | $field['acfe_validate'] = false; |
| 625 | |
| 626 | return $field; |
| 627 | |
| 628 | } |
| 629 | |
| 630 | } |
| 631 | |
| 632 | // initialize |
| 633 | new acfe_field_validation(); |
| 634 | |
| 635 | endif; |