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 / acfe-helper-functions.php
acf-extended / includes Last commit date
admin 3 months ago field-groups 3 months ago fields 1 week ago fields-settings 3 months ago forms 3 months ago locations 3 months ago modules 1 week ago screens 3 months ago acfe-deprecated-functions.php 2 years ago acfe-field-functions.php 3 months ago acfe-field-group-functions.php 3 months ago acfe-file-functions.php 1 year ago acfe-form-functions.php 3 months ago acfe-helper-array-functions.php 3 months ago acfe-helper-functions.php 3 months ago acfe-helper-multi-functions.php 3 months ago acfe-helper-string-functions.php 3 months ago acfe-meta-functions.php 3 months ago acfe-post-functions.php 3 months ago acfe-screen-functions.php 3 months ago acfe-template-functions.php 3 months ago acfe-term-functions.php 3 months ago acfe-user-functions.php 3 months ago acfe-wp-functions.php 3 years ago assets.php 3 months ago compatibility-acf-5.8.php 4 months ago compatibility-acf-5.9.php 3 months ago compatibility-acf-6.5.php 3 months ago compatibility-acf-6.8.6.php 1 week ago compatibility.php 3 months ago field-extend.php 4 months ago field.php 4 months ago hooks.php 3 months ago init.php 3 months ago local-meta.php 3 years ago media.php 3 months ago module-acf.php 3 months ago module-db.php 3 months ago module-l10n.php 3 months ago module-legacy.php 3 years ago module-manager.php 4 months ago module-post.php 3 months ago module-posts.php 4 months ago module-upgrades.php 3 years ago module.php 3 months ago multilang.php 3 months ago revisions.php 4 months ago screen.php 3 months ago settings.php 3 months ago template-tags.php 3 months ago third-party.php 3 years ago upgrades.php 3 months ago
acfe-helper-functions.php
727 lines
1 <?php
2
3 if(!defined('ABSPATH')){
4 exit;
5 }
6
7 /**
8 * acfe_maybe_get_REQUEST
9 *
10 * Similar to acf_maybe_get_POST() but works with $_REQUEST
11 *
12 * @param string $key
13 * @param null $default
14 *
15 * @return mixed|null
16 */
17 function acfe_maybe_get_REQUEST($key = '', $default = null){
18 return isset($_REQUEST[ $key ]) ? $_REQUEST[ $key ] : $default;
19 }
20
21
22 /**
23 * acfe_filter_acf_values_by_keys
24 *
25 * This is used when value is saved as post meta, during a Post Action creation for example
26 *
27 * @param $acf
28 * @param $field_keys
29 *
30 * @return array
31 */
32 function acfe_filter_acf_values_by_keys($acf, $field_keys){
33
34 $acf = acfe_as_array($acf);
35 $field_keys = acfe_as_array($field_keys);
36 $filtered_keys = $field_keys;
37
38 foreach($field_keys as $field_key){
39
40 $field = acf_get_field($field_key);
41
42 if($field && $field['type'] === 'clone' && $field['display'] === 'seamless' && $field['sub_fields']){
43
44 $sub_fields = $field['sub_fields'];
45 $sub_fields = wp_list_pluck($sub_fields, 'key');
46
47 if($sub_fields){
48 $filtered_keys = array_merge($filtered_keys, $sub_fields);
49 }
50
51 }
52
53 }
54
55 // cleanup acf array, only allowing field keys and their sub fields if available
56 $acf = acfe_array_rewrite($acf, function($value, $key) use($filtered_keys){
57
58 // top level key found
59 // allow row and ignore child array
60 if(acfe_contains($filtered_keys, $key)){
61 return true;
62 }
63
64 // child array key not found and value is not array (so no child array available)
65 // do not allow row and ignore child array
66 if(!acfe_contains($filtered_keys, $key) && !is_array($value)){
67 return false;
68 }
69
70 // continue loop, and process child array if available
71 return array($key => $value);
72
73 }, true);
74
75 // return
76 return $acf;
77
78 }
79
80
81 /**
82 * acfe_get_value_from_acf_values_by_key
83 *
84 * This is used to load value from template tags.
85 * For example in the Mail action, when using {fields} template tag
86 *
87 * @param $acf
88 * @param $field_key
89 *
90 * @return mixed|null
91 */
92 function acfe_get_value_from_acf_values_by_key($acf, $field_key){
93
94 // vars
95 $value = null;
96 $acf = acfe_as_array($acf);
97
98 // seamless clone rule
99 $field = acf_get_field($field_key);
100 $is_seamless = false;
101
102 if($field && $field['type'] === 'clone' && $field['display'] === 'seamless'){
103 $value = array();
104 $is_seamless = true;
105 }
106
107 // loop values
108 acfe_array_rewrite($acf, function($val, $key) use($field_key, $is_seamless, &$value){
109
110 if($is_seamless){
111
112 if(acfe_starts_with($key, "{$field_key}_")){
113 $value[ $key ] = $val;
114 }
115
116 }else{
117
118 // found key
119 if($key === $field_key){
120 $value = $val;
121 }
122
123 }
124
125 // continue loop
126 return array($key => $val);
127
128 }, true);
129
130 // reset value back to null
131 if($is_seamless && empty($value)){
132 $value = null;
133 }
134
135 // return
136 return $value;
137
138 }
139
140
141 /**
142 * acfe_add_validation_error
143 *
144 * Similar to acf_add_validation_error() but allows to use field name or field key
145 *
146 * @param string $selector
147 * @param string $message
148 *
149 * @return mixed
150 */
151 function acfe_add_validation_error($selector = '', $message = ''){
152
153 // general error
154 if(empty($selector)){
155 return acf_add_validation_error('', $message);
156 }
157
158 // selector is a field key
159 if(acf_is_field_key($selector)){
160
161 return add_filter("acf/validate_value/key={$selector}", function() use($message){
162 return $message;
163 });
164
165 }
166
167 // get field by name
168 $field = acf_get_field($selector);
169
170 // check form data
171 // todo: make it more clean
172 if($form = acf_get_form_data('acfe/form')){
173
174 // vars
175 $fields = array();
176 $field_groups = acfe_as_array($form['field_groups']);
177
178 // loop field groups
179 foreach($field_groups as $key){
180 $fields = array_merge($fields, acf_get_fields($key));
181 }
182
183 foreach($fields as $_field){
184
185 // field name is different
186 if($_field['name'] !== $selector){
187 continue;
188 }
189
190 // assign field
191 $field = $_field;
192 break;
193
194 }
195
196 }
197
198 // check active loop
199 $row = acf_get_loop();
200
201 // exclude acfe form actions
202 if($row && acfe_get($row, 'selector') !== 'acfe_form_actions'){
203
204 // get sub field
205 $field = acf_get_sub_field($selector, $row['field']);
206
207 }
208
209 // field not found: add general error
210 if(!$field){
211 return acf_add_validation_error('', $message);
212 }
213
214 // add validation error
215 add_filter("acf/validate_value/key={$field['key']}", function() use($message){
216 return $message;
217 });
218
219 return false;
220
221 }
222
223
224 /**
225 * acfe_is_dev
226 *
227 * Check if the developer mode is enabled
228 *
229 * @return bool
230 */
231 function acfe_is_dev(){
232
233 // deprecated
234 if(defined('ACFE_dev')){
235
236 acfe_deprecated_constant('ACFE_dev', '0.8.8.7', 'ACFE_DEV');
237 return ACFE_dev;
238
239 }
240
241 return acf_get_setting('acfe/dev', false) || (defined('ACFE_DEV') && ACFE_DEV);
242
243 }
244
245 /**
246 * acfe_is_super_dev
247 *
248 * Only for awesome developers!
249 *
250 * @return bool
251 */
252 function acfe_is_super_dev(){
253
254 // deprecated
255 if(defined('ACFE_super_dev')){
256
257 acfe_deprecated_constant('ACFE_super_dev', '0.8.8.7', 'ACFE_SUPER_DEV');
258 return ACFE_super_dev;
259
260 }
261
262 return acf_get_setting('acfe/super_dev', false) || (defined('ACFE_SUPER_DEV') && ACFE_SUPER_DEV);
263
264 }
265
266 /**
267 * acfe_is_post_type_reserved
268 *
269 * Check if the post type is reserved
270 *
271 * @param $post_type
272 *
273 * @return bool
274 */
275 function acfe_is_post_type_reserved($post_type){
276
277 // restricted post types
278 $reserved = acfe_get_setting('reserved_post_types', array());
279
280 return in_array($post_type, $reserved);
281
282 }
283
284 /**
285 * acfe_is_post_type_reserved_dev
286 *
287 * Check if the post type is reserved in dev mode
288 *
289 * @param $post_type
290 *
291 * @return bool
292 */
293 function acfe_is_post_type_reserved_dev($post_type){
294
295 // restricted post types
296 $reserved = acfe_get_setting('reserved_post_types', array());
297
298 return !acfe_is_super_dev() && in_array($post_type, $reserved);
299
300 }
301
302 /**
303 * acfe_is_taxonomy_reserved
304 *
305 * Check if the taxonomy is reserved
306 *
307 * @param $taxonomy
308 *
309 * @return bool
310 */
311 function acfe_is_taxonomy_reserved($taxonomy){
312
313 // restricted post types
314 $reserved = acfe_get_setting('reserved_taxonomies', array());
315
316 return in_array($taxonomy, $reserved);
317
318 }
319
320 /**
321 * acfe_is_taxonomy_reserved_dev
322 *
323 * Check if the taxonomy is reserved in dev mode
324 *
325 * @param $taxonomy
326 *
327 * @return bool
328 */
329 function acfe_is_taxonomy_reserved_dev($taxonomy){
330
331 // restricted post types
332 $reserved = acfe_get_setting('reserved_taxonomies', array());
333
334 return !acfe_is_super_dev() && in_array($taxonomy, $reserved);
335
336 }
337
338 /**
339 * acfe_update_setting
340 *
341 * Similar to acf_update_setting() but with the 'acfe' prefix
342 *
343 * @param $name
344 * @param $value
345 *
346 * @return mixed
347 */
348 function acfe_update_setting($name, $value){
349 return acf_update_setting("acfe/{$name}", $value);
350 }
351
352 /**
353 * acfe_append_setting
354 *
355 * Similar to acf_append_setting() but with the 'acfe' prefix
356 *
357 * @param $name
358 * @param $value
359 *
360 * @return mixed
361 */
362 function acfe_append_setting($name, $value){
363 return acf_append_setting("acfe/{$name}", $value);
364 }
365
366 /**
367 * acfe_get_setting
368 *
369 * Similar to acf_get_setting() but with the 'acfe' prefix
370 *
371 * @param $name
372 * @param null $value
373 *
374 * @return mixed
375 */
376 function acfe_get_setting($name, $value = null){
377 return acf_get_setting("acfe/{$name}", $value);
378 }
379
380 /**
381 * acfe_unarray
382 *
383 * Retrieve and return only the first value of an array
384 *
385 * @param $val
386 *
387 * @return false|mixed
388 */
389 function acfe_unarray($val){
390
391 if(is_array($val)){
392 return reset($val);
393 }
394
395 return $val;
396 }
397
398 /**
399 * acfe_get_ip
400 *
401 * @return mixed
402 */
403 function acfe_get_ip(){
404
405 // default
406 $ip = false;
407
408 // try cloudflare first
409 if(!empty($_SERVER['HTTP_CF_CONNECTING_IP'])){
410 $ip = filter_var(wp_unslash($_SERVER['HTTP_CF_CONNECTING_IP']), FILTER_VALIDATE_IP);
411
412 // http client
413 }elseif(!empty($_SERVER['HTTP_CLIENT_IP'])){
414 $ip = filter_var(wp_unslash($_SERVER['HTTP_CLIENT_IP']), FILTER_VALIDATE_IP);
415
416 // proxy pass
417 }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
418
419 // can include more than 1 ip, first is the public one.
420 $ips = explode(',', wp_unslash($_SERVER['HTTP_X_FORWARDED_FOR']));
421
422 if(is_array($ips)){
423 $ip = filter_var($ips[0], FILTER_VALIDATE_IP);
424 }
425
426 // remote addr
427 }elseif(!empty($_SERVER['REMOTE_ADDR'])){
428 $ip = filter_var(wp_unslash($_SERVER['REMOTE_ADDR']), FILTER_VALIDATE_IP);
429
430 }
431
432 // default
433 $ip = $ip !== false ? $ip : '127.0.0.1';
434
435 // fix potential csv return
436 $ip_array = explode(',', $ip);
437 $ip_array = array_map('trim', $ip_array);
438
439 // return first ip
440 $ip = $ip_array[0];
441 $ip = apply_filters('acfe/load_ip', $ip);
442
443 // return
444 return $ip;
445
446 }
447
448 /**
449 * acfe_var_export
450 *
451 * export php code
452 *
453 * @param $code
454 * @param $esc
455 *
456 * @return array|string|string[]|null
457 */
458 function acfe_var_export($code, $esc = true){
459
460 $str_replace = array(
461 " " => " ",
462 "'!!__(!!\'" => "__('",
463 "!!\', !!\'" => "', '",
464 "!!\')!!'" => "')",
465 "array (" => "array(",
466 " NULL," => " null,",
467 );
468
469 $preg_replace = array(
470 '/([ \r\n]+?)array/' => ' array',
471 '/array\(\n\)/' => 'array()',
472 '/array\(\n([ ]+)\)/' => 'array()',
473 '/[0-9]+ => /' => '',
474 //'/[0-9]+ => array/' => 'array',
475 );
476
477 // code
478 $code = var_export($code, true);
479
480 // change double spaces to tabs
481 $code = str_replace(array_keys($str_replace), array_values($str_replace), $code);
482
483 // correctly formats "=> array("
484 $code = preg_replace(array_keys($preg_replace), array_values($preg_replace), $code);
485
486 // esc_textarea
487 if($esc){
488 $code = esc_textarea($code);
489 }
490
491 // return
492 return $code;
493
494 }
495
496 /**
497 * acfe_parse_types
498 *
499 * cousin of acf_parse_type() but also handle 'false' | 'true' | 'null' values
500 *
501 * @param $v
502 * @param $filters
503 *
504 * @return array|bool|int|mixed|string|null
505 */
506 function acfe_parse_types($v, $filters = array('trim', 'int', 'bool', 'null')){
507
508 // validate filters
509 $filters = acfe_as_array($filters);
510
511 // check array
512 if(is_array($v) && !empty($v)){
513
514 $v = array_map(function($v) use($filters){
515 return acfe_parse_types($v, $filters);
516 }, $v);
517
518 // check if string
519 }elseif(is_string($v)){
520
521 // trim ('word ' = 'word')
522 if(in_array('trim', $filters)){
523 $v = trim($v);
524 }
525
526 // convert int strings to int ('123' = 123)
527 if(in_array('int', $filters) && is_numeric($v) && strval(intval($v)) === $v){
528 $v = intval($v);
529
530 // convert ('false' = false)
531 }elseif(in_array('bool', $filters) && strtolower($v) === 'false'){
532 $v = false;
533
534 // convert ('true' = true)
535 }elseif(in_array('bool', $filters) && strtolower($v) === 'true'){
536 $v = true;
537
538 // convert ('null' = null)
539 }elseif(in_array('null', $filters) && strtolower($v) === 'null'){
540 $v = null;
541
542 }
543
544 }
545
546 // return
547 return $v;
548
549 }
550
551 /**
552 * acfe_unparse_types
553 *
554 * reverse of acfe_parse_types
555 *
556 * @param $v
557 * @param $filters
558 *
559 * @return array|mixed|string
560 */
561 function acfe_unparse_types($v, $filters = array('int', 'bool', 'null')){
562
563 // validate filters
564 $filters = acfe_as_array($filters);
565
566 // check array
567 if(is_array($v) && !empty($v)){
568
569 $v = array_map(function($v) use($filters){
570 return acfe_unparse_types($v, $filters);
571 }, $v);
572
573 // others
574 }else{
575
576 // convert int strings to int (123 = '123')
577 if(in_array('int', $filters) && is_int($v)){
578 $v = strval($v);
579
580 // convert (false = 'false')
581 }elseif(in_array('bool', $filters) && $v === false){
582 $v = 'false';
583
584 // convert (true = 'true')
585 }elseif(in_array('bool', $filters) && $v === true){
586 $v = 'true';
587
588 // convert (null = 'null')
589 }elseif(in_array('null', $filters) && $v === null){
590 $v = 'null';
591
592 }
593
594 }
595
596 // return
597 return $v;
598
599 }
600
601
602 /**
603 * acfe_redirect
604 *
605 * @param $location
606 * @param $status
607 *
608 * @return void
609 */
610 function acfe_redirect($location, $status = 302){
611
612 // filter
613 $location = apply_filters('acfe/redirect', $location, $status);
614
615 // do not redirect
616 if($location === false){
617 return;
618 }
619
620 // sanitize
621 $location = trim($location);
622
623 // empty location
624 // redirect to 'current page'
625 if(empty($location)){
626
627 global $wp;
628 if(!isset($wp->request)){
629 return;
630 }
631
632 $location = home_url($wp->request);
633
634 }
635
636 wp_redirect($location);
637 exit;
638
639 }
640
641
642 /**
643 * acfe_doing_action
644 *
645 * Returns the current priority of a running action.
646 * From acf_doing_action(), but also works with ACF 5.8
647 *
648 * @param $action
649 *
650 * @return false|int
651 */
652 function acfe_doing_action($action){
653 global $wp_filter;
654 if(isset($wp_filter[ $action ])){
655 return $wp_filter[ $action ]->current_priority();
656 }
657 return false;
658 }
659
660
661 /**
662 * acfe_log
663 *
664 * Similar to acf_log(), but better handle true/false/null inside arrays
665 *
666 * @return void
667 */
668 function acfe_log(){
669
670 // vars
671 $args = func_get_args();
672
673 // loop arguments
674 foreach($args as $i => $arg){
675
676 // parse log
677 $arg = acfe_parse_log($arg);
678
679 // array | object
680 if(is_array($arg) || is_object($arg)){
681 $arg = print_r($arg, true);
682 }
683
684 // update
685 $args[ $i ] = $arg;
686
687 }
688
689 // log
690 error_log(implode( ' ', $args));
691
692 }
693
694
695 /**
696 * acfe_parse_log
697 *
698 * @param $arg
699 *
700 * @return mixed|string
701 */
702 function acfe_parse_log($arg){
703
704 // array
705 if(is_array($arg)){
706
707 foreach($arg as &$value){
708 $value = acfe_parse_log($value);
709 }
710
711 // boolean
712 }elseif($arg === true || $arg === false){
713 $arg = $arg ? '(true)' : '(false)';
714
715 // null
716 }elseif($arg === null){
717 $arg = '(null)';
718
719 // empty string
720 }elseif($arg === ''){
721 $arg = '""';
722 }
723
724 // return
725 return $arg;
726
727 }