PluginProbe ʕ •ᴥ•ʔ
Advanced Custom Fields: Extended / 0.9.2.7
Advanced Custom Fields: Extended v0.9.2.7
0.9.2.7 0.9.2.6 0.9.2.5 0.8.6 0.8.6.1 0.8.6.3 0.8.6.5 0.8.6.6 0.8.6.7 0.8.6.8 0.8.6.9 0.8.7 0.8.7.1 0.8.7.2 0.8.7.3 0.8.7.4 0.8.7.5 0.8.7.6 0.8.8 0.8.8.1 0.8.8.10 0.8.8.11 0.8.8.2 0.8.8.3 0.8.8.4 0.8.8.5 0.8.8.6 0.8.8.7 0.8.8.8 0.8.8.9 0.8.9 0.8.9.1 0.8.9.2 0.8.9.3 0.8.9.4 0.8.9.5 0.9 0.9.0.1 0.9.0.2 0.9.0.3 0.9.0.4 0.9.0.5 0.9.0.6 0.9.0.7 0.9.0.8 0.9.0.9 0.9.1 0.9.1.1 0.9.2 0.9.2.1 0.9.2.2 0.9.2.3 0.9.2.4 trunk 0.5 0.5.1 0.5.2 0.5.2.1 0.5.2.3 0.5.5 0.5.5.1 0.5.8 0.5.8.1 0.6 0.6.0.1 0.6.0.2 0.6.1 0.6.3 0.6.5 0.6.7 0.6.7.2 0.7 0.7.0.3 0.7.5 0.7.5.5 0.7.8 0.7.9 0.7.9.3 0.7.9.4 0.7.9.9.8 0.7.9.9.9 0.8 0.8.1 0.8.2 0.8.3 0.8.3.1 0.8.4 0.8.4.1 0.8.4.5 0.8.4.6 0.8.5 0.8.5.5
acf-extended / includes / fields-settings / validation.php
acf-extended / includes / fields-settings Last commit date
bidirectional.php 3 months ago choices-label.php 3 months ago data.php 3 months ago instructions.php 3 months ago permissions.php 3 months ago settings.php 3 months ago validation.php 3 months ago
validation.php
783 lines
1 <?php
2
3 if(!defined('ABSPATH')){
4 exit;
5 }
6
7 if(!class_exists('acfe_field_validation')):
8
9 class acfe_field_validation{
10
11 /**
12 * construct
13 */
14 function __construct(){
15
16 // actions
17 add_action('acf/field_group/admin_head', array($this, 'load'));
18 add_action('wp_ajax_acf/field_group/render_field_settings', array($this, 'load_ajax'), 5);
19
20 // filters
21 add_filter('acf/validate_value', array($this, 'validate_value'), 99, 4);
22
23 }
24
25
26 /**
27 * load
28 */
29 function load(){
30
31 if(!acf_is_filter_enabled('acfe/field_group/advanced')){
32 return;
33 }
34
35 $this->prepare_settings();
36 $this->add_settings();
37
38 }
39
40
41 /**
42 * load_ajax
43 */
44 function load_ajax(){
45
46 // verify request
47 if(!acf_verify_ajax() || !acf_current_user_can_admin()){
48 wp_send_json_error();
49 }
50
51 $post_id = acf_maybe_get_POST('post_id');
52 $field_group = acf_get_field_group($post_id);
53
54 if(!$field_group){
55 return;
56 }
57
58 if(!acfe_get($field_group, 'acfe_form')){
59 return;
60 }
61
62 $this->add_settings();
63
64 }
65
66
67 /**
68 * add_settings
69 */
70 function add_settings(){
71
72 // exclude
73 $exclude = array('accordion', 'acfe_button', 'acfe_column', 'acfe_dynamic_render', 'clone', 'flexible_content', 'group', 'message', 'repeater', 'tab');
74
75 // get fields types
76 foreach(acf_get_field_types_info() as $field){
77
78 // field type
79 $field_type = $field['name'];
80
81 // check
82 if(in_array($field_type, $exclude)){
83 continue;
84 }
85
86 add_action("acf/render_field_settings/type={$field_type}", array($this, 'render_field_settings'), 99);
87
88 }
89
90 }
91
92
93 /**
94 * render_field_settings
95 *
96 * @param $field
97 */
98 function render_field_settings($field){
99
100 $functions = array(
101
102 'General' => array(
103 'value' => 'If value',
104 'strlen' => 'If value length - strlen(value)',
105 'count' => 'If count value - count(value)',
106 ),
107
108 'Exists' => array(
109 'email_exists' => 'If email exists - email_exists(value)',
110 'post_type_exists' => 'If post type exists - post_type_exists(value)',
111 'taxonomy_exists' => 'If taxonomy exists - taxonomy_exists(value)',
112 'term_exists' => 'If term exists - term_exists(value)',
113 'username_exists' => 'If username exists - username_exists(value)',
114 ),
115
116 'Is' => array(
117 'is_email' => 'If is email - is_email(value)',
118 'is_user_logged_in' => 'If is user logged in - is_user_logged_in()',
119 'is_array' => 'If is array - is_array(value)',
120 'is_string' => 'If is string - is_string(value)',
121 'is_numeric' => 'If is numeric - is_numeric(value)',
122 ),
123
124 'Post' => array(
125 'get_post_type' => 'If get post type - get_post_type(value)',
126 'get_post_by_id' => 'If post id exists - get_post_by_id(value)',
127 'get_post_by_slug' => 'If post slug exists - get_post_by_slug(value)',
128 'get_post_by_title' => 'If post title exists - get_post_by_title(value)',
129 ),
130
131 'Sanitize' => array(
132 'sanitize_email' => 'If sanitize email - sanitize_email(value)',
133 'sanitize_file_name' => 'If sanitize file name - sanitize_file_name(value)',
134 'sanitize_html_class' => 'If sanitize html class - sanitize_html_class(value)',
135 'sanitize_key' => 'If sanitize key - sanitize_key(value)',
136 'sanitize_meta' => 'If sanitize meta - sanitize_meta(value)',
137 'sanitize_mime_type' => 'If sanitize mime type - sanitize_mime_type(value)',
138 'sanitize_option' => 'If sanitize option - sanitize_option(value)',
139 'sanitize_text_field' => 'If sanitize text field - sanitize_text_field(value)',
140 'sanitize_title' => 'If sanitize title - sanitize_title(value)',
141 'sanitize_user' => 'If sanitize user - sanitize_user(value)',
142 ),
143
144 'User' => array(
145 'get_user_by_id' => 'If get user by id - get_user_by(\'id\', value)',
146 'get_user_by_slug' => 'If get user by slug - get_user_by(\'slug\', value)',
147 'get_user_by_email' => 'If get user by email - get_user_by(\'email\', value)',
148 'get_user_by_login' => 'If get user by login - get_user_by(\'login\', value)',
149 )
150
151 );
152
153 $choices = apply_filters('acfe/validate/functions', $functions, $field);
154
155 if(empty($choices)){
156 return;
157 }
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 'wrapper' => array(
170 'data-enable-switch' => true
171 ),
172 'sub_fields' => array(
173 array(
174 'label' => 'Location',
175 'name' => 'acfe_validate_location',
176 'key' => 'acfe_validate_location',
177 'type' => 'select',
178 'instructions' => '',
179 'required' => 0,
180 'conditional_logic' => 0,
181 'wrapper' => array(
182 'width' => '',
183 'class' => '',
184 'id' => '',
185 ),
186 'choices' => array(
187 'admin' => 'Administration',
188 'front' => 'Front-end',
189 ),
190 'allow_null' => true,
191 'multiple' => 0,
192 'ui' => 0,
193 'return_format' => 'value',
194 'ajax' => 0,
195 'placeholder' => 'Everywhere',
196 ),
197 array(
198 'label' => __('Rules'),
199 'name' => 'acfe_validate_rules_and',
200 'key' => 'acfe_validate_rules_and',
201 'instructions' => '',
202 'type' => 'repeater',
203 'button_label' => __('+ AND'),
204 'required' => false,
205 'layout' => 'table',
206 'sub_fields' => array(
207 array(
208 'ID' => false,
209 'label' => 'Function',
210 'name' => 'acfe_validate_function',
211 'key' => 'acfe_validate_function',
212 'prefix' => '',
213 '_name' => '',
214 '_prepare' => '',
215 'type' => 'select',
216 'choices' => $choices,
217 'instructions' => false,
218 'required' => false,
219 'wrapper' => array(
220 'width' => '',
221 'class' => '',
222 'id' => '',
223 ),
224 ),
225 array(
226 'ID' => false,
227 'label' => 'Operator / Value',
228 'name' => 'acfe_validate_operator',
229 'key' => 'acfe_validate_operator',
230 'prefix' => '',
231 '_name' => '',
232 '_prepare' => '',
233 'type' => 'select',
234 'choices' => array(
235 'Operators' => array(
236 '==' => '==',
237 '!=' => '!=',
238 '>' => '>',
239 '>=' => '>=',
240 '<' => '<',
241 '<=' => '<=',
242 'contains' => 'Contains',
243 '!contains' => 'Doesn\'t contain',
244 'starts' => 'Starts with',
245 '!starts' => 'Doesn\'t start with',
246 'ends' => 'Ends with',
247 '!ends' => 'Doesn\'t end with',
248 'regex' => 'Matches regex',
249 '!regex' => 'Doesn\'t matches regex',
250 ),
251 'Values' => array(
252 'true' => '== true',
253 '!true' => '!= true',
254 'false' => '== false',
255 '!false'=> '!= false',
256 'null' => '== null',
257 '!null' => '!= null',
258 'empty' => '== (empty)',
259 '!empty'=> '!= (empty)',
260 )
261 ),
262 'instructions' => false,
263 'required' => false,
264 'wrapper' => array(
265 'width' => '',
266 'class' => '',
267 'id' => '',
268 ),
269 ),
270 array(
271 'ID' => false,
272 'label' => 'Value',
273 'name' => 'acfe_validate_match',
274 'key' => 'acfe_validate_match',
275 'prefix' => '',
276 '_name' => '',
277 '_prepare' => '',
278 'type' => 'text',
279 'instructions' => false,
280 'placeholder' => '',
281 'required' => false,
282 'wrapper' => array(
283 'width' => '',
284 'class' => '',
285 'id' => '',
286 ),
287 'conditional_logic' => array(
288 array(
289 array(
290 'field' => 'acfe_validate_operator',
291 'operator' => '==',
292 'value' => '==',
293 )
294 ),
295 array(
296 array(
297 'field' => 'acfe_validate_operator',
298 'operator' => '==',
299 'value' => '!=',
300 )
301 ),
302 array(
303 array(
304 'field' => 'acfe_validate_operator',
305 'operator' => '==',
306 'value' => '>',
307 )
308 ),
309 array(
310 array(
311 'field' => 'acfe_validate_operator',
312 'operator' => '==',
313 'value' => '>=',
314 )
315 ),
316 array(
317 array(
318 'field' => 'acfe_validate_operator',
319 'operator' => '==',
320 'value' => '<',
321 )
322 ),
323 array(
324 array(
325 'field' => 'acfe_validate_operator',
326 'operator' => '==',
327 'value' => '<=',
328 )
329 ),
330 array(
331 array(
332 'field' => 'acfe_validate_operator',
333 'operator' => '==',
334 'value' => 'contains',
335 )
336 ),
337 array(
338 array(
339 'field' => 'acfe_validate_operator',
340 'operator' => '==',
341 'value' => '!contains',
342 )
343 ),
344 array(
345 array(
346 'field' => 'acfe_validate_operator',
347 'operator' => '==',
348 'value' => 'starts',
349 )
350 ),
351 array(
352 array(
353 'field' => 'acfe_validate_operator',
354 'operator' => '==',
355 'value' => '!starts',
356 )
357 ),
358 array(
359 array(
360 'field' => 'acfe_validate_operator',
361 'operator' => '==',
362 'value' => 'ends',
363 )
364 ),
365 array(
366 array(
367 'field' => 'acfe_validate_operator',
368 'operator' => '==',
369 'value' => '!ends',
370 )
371 ),
372 array(
373 array(
374 'field' => 'acfe_validate_operator',
375 'operator' => '==',
376 'value' => 'regex',
377 )
378 ),
379 array(
380 array(
381 'field' => 'acfe_validate_operator',
382 'operator' => '==',
383 'value' => '!regex',
384 )
385 ),
386 )
387 ),
388 )
389 ),
390 array(
391 'label' => 'Error',
392 'name' => 'acfe_validate_error',
393 'key' => 'acfe_validate_error',
394 'prefix' => '',
395 '_name' => '',
396 '_prepare' => '',
397 'type' => 'text',
398 'instructions' => false,
399 'required' => false,
400 'wrapper' => array(
401 'width' => '',
402 'class' => '',
403 'id' => '',
404 ),
405 ),
406 )
407 ));
408
409 }
410
411
412 /**
413 * validate_value
414 *
415 * @param $valid
416 * @param $value
417 * @param $field
418 * @param $input
419 *
420 * @return mixed
421 */
422 function validate_value($valid, $value, $field, $input){
423
424 if(!$valid){
425 return $valid;
426 }
427
428 if(!acfe_get($field, 'acfe_validate')){
429 return $valid;
430 }
431
432 foreach($field['acfe_validate'] as $k => $rule){
433
434 // fix possible acf clone index
435 if($k === 'acfcloneindex'){
436 continue;
437 }
438
439 // screen
440 $screen = isset($rule['acfe_validate_location']) ? $rule['acfe_validate_location'] : '';
441 $screen_allow = false;
442
443 // screen: all
444 if(empty($screen)){
445 $screen_allow = true;
446 }
447
448 // screen: admin
449 elseif($screen === 'admin' && acfe_is_admin()){
450 $screen_allow = true;
451 }
452
453 // screen: front
454 elseif($screen === 'front' && acfe_is_front()){
455 $screen_allow = true;
456 }
457
458 if(!$screen_allow){
459 continue;
460 }
461
462 if(!acfe_get($rule, 'acfe_validate_rules_and')){
463 continue;
464 }
465
466 $rule_match = true;
467
468 foreach($rule['acfe_validate_rules_and'] as $k => $function){
469
470 if(!$rule_match){
471 break;
472 }
473
474 $rule_match = false;
475
476 // check filters
477 $filters = array(
478 'acfe/validate/function/' . $function['acfe_validate_function'] . '/key=' . $field['key'],
479 'acfe/validate/function/' . $function['acfe_validate_function'] . '/name=' . $field['name'],
480 'acfe/validate/function/' . $function['acfe_validate_function'] . '/type=' . $field['type'],
481 'acfe/validate/function/' . $function['acfe_validate_function'],
482 );
483
484 $filter_call = false;
485 foreach($filters as $filter){
486
487 if(has_filter($filter)){
488 $filter_call = $filter;
489 }
490
491 }
492
493 // filter
494 if($filter_call){
495 $result = apply_filters($filter_call, false, $value, $field);
496 }
497
498 // class
499 elseif(is_callable(array($this, $function['acfe_validate_function']))){
500 $result = call_user_func(array($this, $function['acfe_validate_function']), $value);
501 }
502
503 // function
504 elseif(is_callable($function['acfe_validate_function'])){
505 $result = call_user_func($function['acfe_validate_function'], $value);
506 }
507
508 // nothing
509 else{
510 continue;
511 }
512
513 // vars
514 $operator = $function['acfe_validate_operator'];
515 $match = acfe_get($function, 'acfe_validate_match');
516
517 if($operator === '==' && $result == $match){
518 $rule_match = true;
519 }
520
521 elseif($operator === '!=' && $result != $match){
522 $rule_match = true;
523 }
524
525 elseif($operator === '>' && $result > $match){
526 $rule_match = true;
527 }
528
529 elseif($operator === '>=' && $result >= $match){
530 $rule_match = true;
531 }
532
533 elseif($operator === '<' && $result < $match){
534 $rule_match = true;
535 }
536
537 elseif($operator === '<=' && $result <= $match){
538 $rule_match = true;
539 }
540
541 elseif($operator === 'contains' && stripos($result, $match) !== false){
542 $rule_match = true;
543 }
544
545 elseif($operator === '!contains' && stripos($result, $match) === false){
546 $rule_match = true;
547 }
548
549 elseif($operator === 'starts' && stripos($result, $match) === 0){
550 $rule_match = true;
551 }
552
553 elseif($operator === '!starts' && stripos($result, $match) !== 0){
554 $rule_match = true;
555 }
556
557 elseif($operator === 'ends' && acfe_ends_with($result, $match)){
558 $rule_match = true;
559 }
560
561 elseif($operator === '!ends' && !acfe_ends_with($result, $match)){
562 $rule_match = true;
563 }
564
565 elseif($operator === 'regex' && preg_match('/' . $match . '/u', $result)){
566 $rule_match = true;
567 }
568
569 elseif($operator === '!regex' && !preg_match('/' . $match . '/u', $result)){
570 $rule_match = true;
571 }
572
573 elseif($operator === 'true' && $result === true){
574 $rule_match = true;
575 }
576
577 elseif($operator === '!true' && $result !== true){
578 $rule_match = true;
579 }
580
581 elseif($operator === 'false' && $result === false){
582 $rule_match = true;
583 }
584
585 elseif($operator === '!false' && $result !== false){
586 $rule_match = true;
587 }
588
589 elseif($operator === 'null' && $result === null){
590 $rule_match = true;
591 }
592
593 elseif($operator === '!null' && $result !== null){
594 $rule_match = true;
595 }
596
597 elseif($operator === 'empty' && empty($result)){
598 $rule_match = true;
599 }
600
601 elseif($operator === '!empty' && !empty($result)){
602 $rule_match = true;
603 }
604
605 }
606
607 // rrror
608 $error = $rule['acfe_validate_error'];
609
610 if($rule_match && !empty($error)){
611 $valid = $error;
612 }
613
614 if(!$valid || is_string($valid)){
615 break;
616 }
617
618 }
619
620 return $valid;
621
622 }
623
624
625 /**
626 * get_user_by_id
627 *
628 * @param $value
629 *
630 * @return false|WP_User
631 */
632 function get_user_by_id($value){
633 return get_user_by('id', $value);
634 }
635
636
637 /**
638 * get_user_by_slug
639 *
640 * @param $value
641 *
642 * @return false|WP_User
643 */
644 function get_user_by_slug($value){
645 return get_user_by('slug', $value);
646 }
647
648
649 /**
650 * get_user_by_email
651 *
652 * @param $value
653 *
654 * @return false|WP_User
655 */
656 function get_user_by_email($value){
657 return get_user_by('email', $value);
658 }
659
660
661 /**
662 * get_user_by_login
663 *
664 * @param $value
665 *
666 * @return false|WP_User
667 */
668 function get_user_by_login($value){
669 return get_user_by('login', $value);
670 }
671
672
673 /**
674 * value
675 *
676 * @param $value
677 *
678 * @return mixed
679 */
680 function value($value){
681 return $value;
682 }
683
684
685 /**
686 * get_post_by_id
687 *
688 * @param $value
689 *
690 * @return bool
691 */
692 function get_post_by_id($value){
693
694 $get_post = get_post($value);
695
696 if(!$get_post || is_wp_error($get_post)){
697 return false;
698 }
699
700 return true;
701
702 }
703
704
705 /**
706 * get_post_by_slug
707 *
708 * @param $value
709 *
710 * @return bool
711 */
712 function get_post_by_slug($value){
713
714 $get_posts = get_posts(array(
715 'name' => $value,
716 'post_type' => 'any',
717 'post_status' => 'any',
718 'posts_per_page' => 1
719 ));
720
721 if(empty($get_posts)){
722 return false;
723 }
724
725 return true;
726
727 }
728
729
730 /**
731 * get_post_by_title
732 *
733 * @param $value
734 *
735 * @return bool
736 */
737 function get_post_by_title($value){
738
739 $get_posts = get_posts(array(
740 's' => $value,
741 'post_type' => 'any',
742 'post_status' => 'any',
743 'posts_per_page' => 1
744 ));
745
746 if(empty($get_posts)){
747 return false;
748 }
749
750 return true;
751
752 }
753
754
755 /**
756 * prepare_settings
757 */
758 function prepare_settings(){
759
760 $fields = array('acfe_validate', 'acfe_validate_location', 'acfe_validate_rules_and', 'acfe_validate_function', 'acfe_validate_operator', 'acfe_validate_match', 'acfe_validate_error');
761
762 foreach($fields as $name){
763
764 add_filter("acf/prepare_field/name={$name}", function($field){
765
766 $field['prefix'] = str_replace('row-', '', $field['prefix']);
767 $field['name'] = str_replace('row-', '', $field['name']);
768
769 return $field;
770
771 });
772
773 }
774
775 }
776
777
778 }
779
780 // initialize
781 new acfe_field_validation();
782
783 endif;