PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / trunk
Secure Custom Fields vtrunk
6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / api / api-helpers.php
secure-custom-fields / includes / api Last commit date
api-helpers.php 3 days ago api-template.php 1 month ago api-term.php 1 year ago index.php 1 year ago
api-helpers.php
4084 lines
1 <?php
2
3 /**
4 * This function will return true for a non empty array
5 *
6 * @since ACF 5.4.0
7 *
8 * @param mixed $array The variable to test.
9 * @return boolean
10 */
11 function acf_is_array( $array ) {
12 return ( is_array( $array ) && ! empty( $array ) );
13 }
14
15 /**
16 * Alias of acf()->has_setting()
17 *
18 * @since ACF 5.6.5
19 *
20 * @param string $name Name of the setting to check for.
21 * @return boolean
22 */
23 function acf_has_setting( $name = '' ) {
24 return acf()->has_setting( $name );
25 }
26
27 /**
28 * acf_raw_setting
29 *
30 * alias of acf()->get_setting()
31 *
32 * @since ACF 5.6.5
33 *
34 * @param n/a
35 * @return n/a
36 */
37 function acf_raw_setting( $name = '' ) {
38 return acf()->get_setting( $name );
39 }
40
41 /**
42 * acf_update_setting
43 *
44 * alias of acf()->update_setting()
45 *
46 * @since ACF 5.0.0
47 *
48 * @param $name (string)
49 * @param $value (mixed)
50 * @return n/a
51 */
52 function acf_update_setting( $name, $value ) {
53 // validate name.
54 $name = acf_validate_setting( $name );
55
56 // update.
57 return acf()->update_setting( $name, $value );
58 }
59
60 /**
61 * acf_validate_setting
62 *
63 * Returns the changed setting name if available.
64 *
65 * @since ACF 5.6.5
66 *
67 * @param n/a
68 * @return n/a
69 */
70 function acf_validate_setting( $name = '' ) {
71 return apply_filters( 'acf/validate_setting', $name );
72 }
73
74 /**
75 * Alias of acf()->get_setting()
76 *
77 * @since ACF 5.0.0
78 *
79 * @param string $name The name of the setting to test.
80 * @param string $value An optional default value for the setting if it doesn't exist.
81 * @return n/a
82 */
83 function acf_get_setting( $name, $value = null ) {
84 $name = acf_validate_setting( $name );
85
86 // replace default setting value if it exists.
87 if ( acf_has_setting( $name ) ) {
88 $value = acf_raw_setting( $name );
89 }
90
91 // filter.
92 $value = apply_filters( "acf/settings/{$name}", $value );
93
94 return $value;
95 }
96
97 /**
98 * Returns whether the current plugin load is running with PRO features enabled.
99 *
100 * @since ACF 6.8
101 *
102 * @return bool
103 */
104 function acf_is_pro() {
105 return true;
106 }
107
108 /**
109 * Return an array of ACF's internal post type names
110 *
111 * @since ACF 6.1
112 * @return array An array of ACF's internal post type names
113 */
114 function acf_get_internal_post_types() {
115 return array( 'acf-field-group', 'acf-post-type', 'acf-taxonomy', 'acf-ui-options-page' );
116 }
117
118 /**
119 * acf_append_setting
120 *
121 * This function will add a value into the settings array found in the acf object
122 *
123 * @since ACF 5.0.0
124 *
125 * @param $name (string)
126 * @param $value (mixed)
127 * @return n/a
128 */
129 function acf_append_setting( $name, $value ) {
130
131 // vars
132 $setting = acf_raw_setting( $name );
133
134 // bail early if not array
135 if ( ! is_array( $setting ) ) {
136 $setting = array();
137 }
138
139 // append
140 $setting[] = $value;
141
142 // update
143 return acf_update_setting( $name, $setting );
144 }
145
146 /**
147 * acf_get_data
148 *
149 * Returns data.
150 *
151 * @since ACF 5.0.0
152 *
153 * @param string $name
154 * @return mixed
155 */
156 function acf_get_data( $name ) {
157 return acf()->get_data( $name );
158 }
159
160 /**
161 * acf_set_data
162 *
163 * Sets data.
164 *
165 * @since ACF 5.0.0
166 *
167 * @param string $name
168 * @param mixed $value
169 * @return n/a
170 */
171 function acf_set_data( $name, $value ) {
172 return acf()->set_data( $name, $value );
173 }
174
175 /**
176 * Appends data to an existing key.
177 *
178 * @since ACF 5.9.0
179 *
180 * @param string $name The data name.
181 * @param mixed $data The data to append to name.
182 */
183 function acf_append_data( $name, $data ) {
184 $prev_data = acf()->get_data( $name );
185 if ( is_array( $prev_data ) ) {
186 $data = array_merge( $prev_data, $data );
187 }
188 acf()->set_data( $name, $data );
189 }
190
191 /**
192 * Alias of acf()->init() - the core ACF init function.
193 *
194 * @since ACF 5.0.0
195 */
196 function acf_init() {
197 acf()->init();
198 }
199
200 /**
201 * acf_has_done
202 *
203 * This function will return true if this action has already been done
204 *
205 * @since ACF 5.3.2
206 *
207 * @param $name (string)
208 * @return (boolean)
209 */
210 function acf_has_done( $name ) {
211
212 // return true if already done
213 if ( acf_raw_setting( "has_done_{$name}" ) ) {
214 return true;
215 }
216
217 // update setting and return
218 acf_update_setting( "has_done_{$name}", true );
219 return false;
220 }
221
222 /**
223 * This function will return the path to a file within an external folder
224 *
225 * @since ACF 5.5.8
226 *
227 * @param string $file Directory path.
228 * @param string $path Optional file path.
229 * @return string File path.
230 */
231 function acf_get_external_path( $file, $path = '' ) {
232 return plugin_dir_path( $file ) . $path;
233 }
234
235 /**
236 * This function will return the url to a file within an internal ACF folder
237 *
238 * @since ACF 5.5.8
239 *
240 * @param string $file Directory path.
241 * @param string $path Optional file path.
242 * @return string File path.
243 */
244 function acf_get_external_dir( $file, $path = '' ) {
245 return acf_plugin_dir_url( $file ) . $path;
246 }
247
248 /**
249 * This function will calculate the url to a plugin folder.
250 * Different to the WP plugin_dir_url(), this function can calculate for urls outside of the plugins folder (theme include).
251 *
252 * @since ACF 5.6.8
253 *
254 * @param string $file A file path inside the ACF plugin to get the plugin directory path from.
255 * @return string The plugin directory path.
256 */
257 function acf_plugin_dir_url( $file ) {
258 $path = plugin_dir_path( $file );
259 $path = wp_normalize_path( $path );
260
261 // check plugins.
262 $check_path = wp_normalize_path( realpath( WP_PLUGIN_DIR ) );
263 if ( strpos( $path, $check_path ) === 0 ) {
264 return str_replace( $check_path, plugins_url(), $path );
265 }
266
267 // check wp-content.
268 $check_path = wp_normalize_path( realpath( WP_CONTENT_DIR ) );
269 if ( strpos( $path, $check_path ) === 0 ) {
270 return str_replace( $check_path, content_url(), $path );
271 }
272
273 // check root.
274 $check_path = wp_normalize_path( realpath( ABSPATH ) );
275 if ( strpos( $path, $check_path ) === 0 ) {
276 return str_replace( $check_path, site_url( '/' ), $path );
277 }
278
279 // return.
280 return plugin_dir_url( $file );
281 }
282
283 /**
284 * This function will merge together 2 arrays and also convert any numeric values to ints
285 *
286 * @since ACF 5.0.0
287 *
288 * @param array $args The configured arguments array.
289 * @param array $defaults The default properties for the passed args to inherit.
290 * @return array $args Parsed arguments with defaults applied.
291 */
292 function acf_parse_args( $args, $defaults = array() ) {
293 $args = wp_parse_args( $args, $defaults );
294
295 // parse types
296 $args = acf_parse_types( $args );
297
298 return $args;
299 }
300
301 /**
302 * acf_parse_types
303 *
304 * This function will convert any numeric values to int and trim strings
305 *
306 * @since ACF 5.0.0
307 *
308 * @param $var (mixed)
309 * @return $var (mixed)
310 */
311 function acf_parse_types( $array ) {
312 return array_map( 'acf_parse_type', $array );
313 }
314
315 /**
316 * acf_parse_type
317 *
318 * description
319 *
320 * @since ACF 5.0.9
321 *
322 * @param $post_id (int)
323 * @return $post_id (int)
324 */
325 function acf_parse_type( $v ) {
326
327 // Check if is string.
328 if ( is_string( $v ) ) {
329
330 // Trim ("Word " = "Word").
331 $v = trim( $v );
332
333 // Convert int strings to int ("123" = 123).
334 if ( is_numeric( $v ) && strval( intval( $v ) ) === $v ) {
335 $v = intval( $v );
336 }
337 }
338
339 // return.
340 return $v;
341 }
342
343 /**
344 * This function will load in a file from the 'admin/views' folder and allow variables to be passed through
345 *
346 * @since ACF 5.0.0
347 *
348 * @param string $view_path
349 * @param array $view_args
350 */
351 function acf_get_view( $view_path = '', $view_args = array() ) {
352 // allow view file name shortcut
353 if ( substr( $view_path, -4 ) !== '.php' ) {
354 $view_path = acf_get_path( "includes/admin/views/{$view_path}.php" );
355 }
356
357 // include
358 if ( file_exists( $view_path ) ) {
359 // Use `EXTR_SKIP` here to prevent `$view_path` from being accidentally/maliciously overridden.
360 extract( $view_args, EXTR_SKIP );
361 include $view_path;
362 }
363 }
364
365 /**
366 * acf_merge_atts
367 *
368 * description
369 *
370 * @since ACF 5.0.9
371 *
372 * @param $post_id (int)
373 * @return $post_id (int)
374 */
375 function acf_merge_atts( $atts, $extra = array() ) {
376
377 // bail early if no $extra
378 if ( empty( $extra ) ) {
379 return $atts;
380 }
381
382 // trim
383 $extra = array_map( 'trim', $extra );
384 $extra = array_filter( $extra );
385
386 // merge in new atts
387 foreach ( $extra as $k => $v ) {
388
389 // append
390 if ( $k == 'class' || $k == 'style' ) {
391 $atts[ $k ] .= ' ' . $v;
392
393 // merge
394 } else {
395 $atts[ $k ] = $v;
396 }
397 }
398
399 return $atts;
400 }
401
402 /**
403 * This function will create and echo a basic nonce input
404 *
405 * @since ACF 5.6.0
406 *
407 * @param string $nonce The nonce parameter string.
408 */
409 function acf_nonce_input( $nonce = '' ) {
410 echo '<input type="hidden" name="_acf_nonce" value="' . esc_attr( wp_create_nonce( $nonce ) ) . '" />';
411 }
412
413 /**
414 * This function will remove the var from the array, and return the var
415 *
416 * @since ACF 5.0.0
417 *
418 * @param array $extract_array an array passed as reference to be extracted.
419 * @param string $key The key to extract from the array.
420 * @param mixed $default_value The default value if it doesn't exist in the extract array.
421 * @return mixed Extracted var or default.
422 */
423 function acf_extract_var( &$extract_array, $key, $default_value = null ) {
424 // check if exists - uses array_key_exists to extract NULL values (isset will fail).
425 if ( is_array( $extract_array ) && array_key_exists( $key, $extract_array ) ) {
426
427 // store and unset value.
428 $v = $extract_array[ $key ];
429 unset( $extract_array[ $key ] );
430
431 return $v;
432 }
433
434 return $default_value;
435 }
436
437 /**
438 * This function will remove the vars from the array, and return the vars
439 *
440 * @since ACF 5.0.0
441 *
442 * @param array $extract_array an array passed as reference to be extracted.
443 * @param array $keys An array of keys to extract from the original array.
444 * @return array An array of extracted values.
445 */
446 function acf_extract_vars( &$extract_array, $keys ) {
447 $r = array();
448
449 foreach ( $keys as $key ) {
450 $r[ $key ] = acf_extract_var( $extract_array, $key );
451 }
452
453 return $r;
454 }
455
456 /**
457 * acf_get_sub_array
458 *
459 * This function will return a sub array of data
460 *
461 * @since ACF 5.3.2
462 *
463 * @param $post_id (int)
464 * @return $post_id (int)
465 */
466 function acf_get_sub_array( $array, $keys ) {
467
468 $r = array();
469
470 foreach ( $keys as $key ) {
471 $r[ $key ] = $array[ $key ];
472 }
473
474 return $r;
475 }
476
477 /**
478 * Returns an array of post type names.
479 *
480 * @since ACF 5.0.0
481 *
482 * @param array $args Optional. An array of key => value arguments to match against the post type objects. Default empty array.
483 * @return array A list of post type names.
484 */
485 function acf_get_post_types( $args = array() ) {
486 $post_types = array();
487
488 // extract special arg
489 $exclude = acf_extract_var( $args, 'exclude', array() );
490 $exclude[] = 'acf-field';
491 $exclude[] = 'acf-field-group';
492 $exclude[] = 'acf-post-type';
493 $exclude[] = 'acf-taxonomy';
494 $exclude[] = 'acf-ui-options-page';
495
496 // Get post type objects.
497 $objects = get_post_types( $args, 'objects' );
498
499 foreach ( $objects as $i => $object ) {
500 // Bail early if is exclude.
501 if ( in_array( $i, $exclude ) ) {
502 continue;
503 }
504
505 // Bail early if is builtin (WP) private post type
506 // i.e. nav_menu_item, revision, customize_changeset, etc.
507 if ( $object->_builtin && ! $object->public ) {
508 continue;
509 }
510
511 $post_types[] = $i;
512 }
513
514 return apply_filters( 'acf/get_post_types', $post_types, $args );
515 }
516
517 function acf_get_pretty_post_types( $post_types = array() ) {
518
519 // get post types
520 if ( empty( $post_types ) ) {
521
522 // get all custom post types
523 $post_types = acf_get_post_types();
524 }
525
526 // get labels
527 $ref = array();
528 $r = array();
529
530 foreach ( $post_types as $post_type ) {
531
532 // vars
533 $label = acf_get_post_type_label( $post_type );
534
535 // append to r
536 $r[ $post_type ] = $label;
537
538 // increase counter
539 if ( ! isset( $ref[ $label ] ) ) {
540 $ref[ $label ] = 0;
541 }
542
543 ++$ref[ $label ];
544 }
545
546 // get slugs
547 foreach ( array_keys( $r ) as $i ) {
548
549 // vars
550 $post_type = $r[ $i ];
551
552 if ( $ref[ $post_type ] > 1 ) {
553 $r[ $i ] .= ' (' . $i . ')';
554 }
555 }
556
557 // return
558 return $r;
559 }
560
561 /**
562 * Function acf_get_post_stati()
563 *
564 * Returns an array of post status names.
565 *
566 * @since ACF 6.1.0
567 *
568 * @param array $args Optional. An array of key => value arguments to match against the post status objects. Default empty array.
569 * @return array A list of post status names.
570 */
571 function acf_get_post_stati( $args = array() ) {
572
573 $args['internal'] = false;
574
575 $post_statuses = get_post_stati( $args );
576
577 unset( $post_statuses['acf-disabled'] );
578
579 $post_statuses = (array) apply_filters( 'acf/get_post_stati', $post_statuses, $args );
580
581 return $post_statuses;
582 }
583 /**
584 * Function acf_get_pretty_post_statuses()
585 *
586 * Returns a clean array of post status names.
587 *
588 * @since ACF 6.1.0
589 *
590 * @param array $post_statuses Optional. An array of post status objects. Default empty array.
591 * @return array An array of post status names.
592 */
593 function acf_get_pretty_post_statuses( $post_statuses = array() ) {
594
595 // Get all post statuses.
596 $post_statuses = array_merge( $post_statuses, acf_get_post_stati() );
597
598 $ref = array();
599 $result = array();
600
601 foreach ( $post_statuses as $post_status ) {
602 $label = acf_get_post_status_label( $post_status );
603
604 $result[ $post_status ] = $label;
605
606 if ( ! isset( $ref[ $label ] ) ) {
607 $ref[ $label ] = 0;
608 }
609
610 ++$ref[ $label ];
611 }
612
613 foreach ( array_keys( $result ) as $i ) {
614 $post_status = $result[ $i ];
615
616 if ( $ref[ $post_status ] > 1 ) {
617 $result[ $i ] .= ' (' . $i . ')';
618 }
619 }
620
621 return $result;
622 }
623
624 /**
625 * acf_get_post_type_label
626 *
627 * This function will return a pretty label for a specific post_type
628 *
629 * @since ACF 5.4.0
630 *
631 * @param $post_type (string)
632 * @return (string)
633 */
634 function acf_get_post_type_label( $post_type ) {
635
636 // vars
637 $label = $post_type;
638
639 // check that object exists
640 // - case exists when importing field group from another install and post type does not exist
641 if ( post_type_exists( $post_type ) ) {
642 $obj = get_post_type_object( $post_type );
643 $label = $obj->labels->singular_name;
644 }
645
646 // return
647 return $label;
648 }
649
650 /**
651 * Function acf_get_post_status_label()
652 *
653 * This function will return a pretty label for a specific post_status
654 *
655 * @since ACF 6.1.0
656 *
657 * @param string $post_status The post status.
658 * @return string The post status label.
659 */
660 function acf_get_post_status_label( $post_status ) {
661 $label = $post_status;
662 $obj = get_post_status_object( $post_status );
663 $label = is_object( $obj ) ? $obj->label : '';
664
665 return $label;
666 }
667
668 /**
669 * acf_verify_nonce
670 *
671 * This function will look at the $_POST['_acf_nonce'] value and return true or false
672 *
673 * @since ACF 5.0.0
674 *
675 * @param $nonce (string)
676 * @return (boolean)
677 */
678 function acf_verify_nonce( $value ) {
679
680 // vars
681 $nonce = acf_maybe_get_POST( '_acf_nonce' );
682
683 // bail early nonce does not match (post|user|comment|term)
684 if ( ! $nonce || ! wp_verify_nonce( $nonce, $value ) ) {
685 return false;
686 }
687
688 // reset nonce (only allow 1 save)
689 $_POST['_acf_nonce'] = false;
690
691 // return
692 return true;
693 }
694
695 /**
696 * Returns true if the current AJAX request is valid.
697 * It's action will also allow WPML to set the lang and avoid AJAX get_posts issues
698 *
699 * @since ACF 5.2.3
700 *
701 * @param string $nonce The nonce to check.
702 * @param string $action The action of the nonce.
703 * @param bool $action_is_field Whether the action is a field key or not. Defaults to false.
704 * @param string $expected_field_type Optional field type the resolved field must be when $action_is_field is true. Prevents a nonce minted for one field type from being accepted by an AJAX handler that expects a different one. Defaults to empty (no type validation).
705 * @return boolean
706 */
707 function acf_verify_ajax( $nonce = '', $action = '', $action_is_field = false, $expected_field_type = '' ) {
708
709 // Bail early if we don't have a nonce to check.
710 if ( empty( $nonce ) && empty( $_REQUEST['nonce'] ) ) {
711 return false;
712 }
713
714 // Build the action if we're trying to validate a specific field nonce.
715 if ( $action_is_field ) {
716 if ( ! acf_is_field_key( $action ) ) {
717 return false;
718 }
719
720 $field = acf_get_field( $action );
721
722 if ( empty( $field['type'] ) ) {
723 return false;
724 }
725
726 if ( ! empty( $expected_field_type ) && $field['type'] !== $expected_field_type ) {
727 return false;
728 }
729
730 $action = 'acf_field_' . $field['type'] . '_' . $action;
731 }
732
733 $nonce_to_check = ! empty( $nonce ) ? $nonce : $_REQUEST['nonce']; // phpcs:ignore WordPress.Security -- We're verifying a nonce here.
734 $nonce_action = ! empty( $action ) ? $action : 'acf_nonce';
735
736 // Bail if nonce can't be verified.
737 if ( ! wp_verify_nonce( sanitize_text_field( $nonce_to_check ), $nonce_action ) ) {
738 return false;
739 }
740
741 // Action for 3rd party customization (WPML).
742 do_action( 'acf/verify_ajax' );
743
744 return true;
745 }
746
747 /**
748 * acf_get_image_sizes
749 *
750 * This function will return an array of available image sizes
751 *
752 * @since ACF 5.0.0
753 *
754 * @param n/a
755 * @return (array)
756 */
757 function acf_get_image_sizes() {
758
759 // vars
760 $sizes = array(
761 'thumbnail' => __( 'Thumbnail', 'secure-custom-fields' ),
762 'medium' => __( 'Medium', 'secure-custom-fields' ),
763 'large' => __( 'Large', 'secure-custom-fields' ),
764 );
765
766 // find all sizes
767 $all_sizes = get_intermediate_image_sizes();
768
769 // add extra registered sizes
770 if ( ! empty( $all_sizes ) ) {
771 foreach ( $all_sizes as $size ) {
772
773 // bail early if already in array
774 if ( isset( $sizes[ $size ] ) ) {
775 continue;
776 }
777
778 // append to array
779 $label = str_replace( '-', ' ', $size );
780 $label = ucwords( $label );
781 $sizes[ $size ] = $label;
782 }
783 }
784
785 // add sizes
786 foreach ( array_keys( $sizes ) as $s ) {
787
788 // vars
789 $data = acf_get_image_size( $s );
790
791 // append
792 if ( $data['width'] && $data['height'] ) {
793 $sizes[ $s ] .= ' (' . $data['width'] . ' x ' . $data['height'] . ')';
794 }
795 }
796
797 // add full end
798 $sizes['full'] = __( 'Full Size', 'secure-custom-fields' );
799
800 // filter for 3rd party customization
801 $sizes = apply_filters( 'acf/get_image_sizes', $sizes );
802
803 // return
804 return $sizes;
805 }
806
807 function acf_get_image_size( $s = '' ) {
808
809 // global
810 global $_wp_additional_image_sizes;
811
812 // rename for nicer code
813 $_sizes = $_wp_additional_image_sizes;
814
815 // vars
816 $data = array(
817 'width' => isset( $_sizes[ $s ]['width'] ) ? $_sizes[ $s ]['width'] : get_option( "{$s}_size_w" ),
818 'height' => isset( $_sizes[ $s ]['height'] ) ? $_sizes[ $s ]['height'] : get_option( "{$s}_size_h" ),
819 );
820
821 // return
822 return $data;
823 }
824
825 /**
826 * acf_version_compare
827 *
828 * Similar to the version_compare() function but with extra functionality.
829 *
830 * @since ACF 5.5.0
831 *
832 * @param string $left The left version number.
833 * @param string $compare The compare operator.
834 * @param string $right The right version number.
835 * @return boolean
836 */
837 function acf_version_compare( $left = '', $compare = '>', $right = '' ) {
838
839 // Detect 'wp' placeholder.
840 if ( $left === 'wp' ) {
841 global $wp_version;
842 $left = $wp_version;
843 }
844
845 // Return result.
846 return version_compare( $left, $right, $compare );
847 }
848
849 /**
850 * acf_get_full_version
851 *
852 * This function will remove any '-beta1' or '-RC1' strings from a version
853 *
854 * @since ACF 5.5.0
855 *
856 * @param $version (string)
857 * @return (string)
858 */
859 function acf_get_full_version( $version = '1' ) {
860
861 // remove '-beta1' or '-RC1'
862 if ( $pos = strpos( $version, '-' ) ) {
863 $version = substr( $version, 0, $pos );
864 }
865
866 // return
867 return $version;
868 }
869
870 /**
871 * acf_get_terms
872 *
873 * This function is a wrapper for the get_terms() function
874 *
875 * @since ACF 5.4.0
876 *
877 * @param $args (array)
878 * @return (array)
879 */
880 function acf_get_terms( $args ) {
881
882 // defaults
883 $args = wp_parse_args(
884 $args,
885 array(
886 'taxonomy' => null,
887 'hide_empty' => false,
888 'update_term_meta_cache' => false,
889 )
890 );
891
892 // return
893 return get_terms( $args );
894 }
895
896 /**
897 * acf_get_taxonomy_terms
898 *
899 * This function will return an array of available taxonomy terms
900 *
901 * @since ACF 5.0.0
902 *
903 * @param $taxonomies (array)
904 * @return (array)
905 */
906 function acf_get_taxonomy_terms( $taxonomies = array() ) {
907
908 // force array
909 $taxonomies = acf_get_array( $taxonomies );
910
911 // get pretty taxonomy names
912 $taxonomies = acf_get_pretty_taxonomies( $taxonomies );
913
914 // vars
915 $r = array();
916
917 // populate $r
918 foreach ( array_keys( $taxonomies ) as $taxonomy ) {
919
920 // vars
921 $label = $taxonomies[ $taxonomy ];
922 $is_hierarchical = is_taxonomy_hierarchical( $taxonomy );
923 $terms = acf_get_terms(
924 array(
925 'taxonomy' => $taxonomy,
926 'hide_empty' => false,
927 )
928 );
929
930 // bail early i no terms
931 if ( empty( $terms ) ) {
932 continue;
933 }
934
935 // sort into hierarchical order!
936 if ( $is_hierarchical ) {
937 $terms = _get_term_children( 0, $terms, $taxonomy );
938 }
939
940 // add placeholder
941 $r[ $label ] = array();
942
943 // add choices
944 foreach ( $terms as $term ) {
945 $k = "{$taxonomy}:{$term->slug}";
946 $r[ $label ][ $k ] = acf_get_term_title( $term );
947 }
948 }
949
950 // return
951 return $r;
952 }
953
954 /**
955 * acf_decode_taxonomy_terms
956 *
957 * This function decodes the $taxonomy:$term strings into a nested array
958 *
959 * @since ACF 5.0.0
960 *
961 * @param $terms (array)
962 * @return (array)
963 */
964 function acf_decode_taxonomy_terms( $strings = false ) {
965
966 // bail early if no terms
967 if ( empty( $strings ) ) {
968 return false;
969 }
970
971 // vars
972 $terms = array();
973
974 // loop
975 foreach ( $strings as $string ) {
976
977 // vars
978 $data = acf_decode_taxonomy_term( $string );
979 $taxonomy = $data['taxonomy'];
980 $term = $data['term'];
981
982 // create empty array
983 if ( ! isset( $terms[ $taxonomy ] ) ) {
984 $terms[ $taxonomy ] = array();
985 }
986
987 // append
988 $terms[ $taxonomy ][] = $term;
989 }
990
991 // return
992 return $terms;
993 }
994
995 /**
996 * acf_decode_taxonomy_term
997 *
998 * This function will return the taxonomy and term slug for a given value
999 *
1000 * @since ACF 5.0.0
1001 *
1002 * @param $string (string)
1003 * @return (array)
1004 */
1005 function acf_decode_taxonomy_term( $value ) {
1006
1007 // vars
1008 $data = array(
1009 'taxonomy' => '',
1010 'term' => '',
1011 );
1012
1013 // int
1014 if ( is_numeric( $value ) ) {
1015 $data['term'] = $value;
1016
1017 // string
1018 } elseif ( is_string( $value ) ) {
1019 $value = explode( ':', $value );
1020 $data['taxonomy'] = isset( $value[0] ) ? $value[0] : '';
1021 $data['term'] = isset( $value[1] ) ? $value[1] : '';
1022
1023 // error
1024 } else {
1025 return false;
1026 }
1027
1028 // allow for term_id (Used by ACF v4)
1029 if ( is_numeric( $data['term'] ) ) {
1030
1031 // global
1032 global $wpdb;
1033
1034 // find taxonomy
1035 if ( ! $data['taxonomy'] ) {
1036 $data['taxonomy'] = $wpdb->get_var( $wpdb->prepare( "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d LIMIT 1", $data['term'] ) );
1037 }
1038
1039 // find term (may have numeric slug '123')
1040 $term = get_term_by( 'slug', $data['term'], $data['taxonomy'] );
1041
1042 // attempt get term via ID (ACF4 uses ID)
1043 if ( ! $term ) {
1044 $term = get_term( $data['term'], $data['taxonomy'] );
1045 }
1046
1047 // bail early if no term
1048 if ( ! $term ) {
1049 return false;
1050 }
1051
1052 // update
1053 $data['taxonomy'] = $term->taxonomy;
1054 $data['term'] = $term->slug;
1055 }
1056
1057 // return
1058 return $data;
1059 }
1060
1061 /**
1062 * acf_array
1063 *
1064 * Casts the value into an array.
1065 *
1066 * @since ACF 5.7.10
1067 *
1068 * @param mixed $val The value to cast.
1069 * @return array
1070 */
1071 function acf_array( $val = array() ) {
1072 return (array) $val;
1073 }
1074
1075 /**
1076 * Returns a non-array value.
1077 *
1078 * @since ACF 5.8.10
1079 *
1080 * @param mixed $val The value to review.
1081 * @return mixed
1082 */
1083 function acf_unarray( $val ) {
1084 if ( is_array( $val ) ) {
1085 return reset( $val );
1086 }
1087 return $val;
1088 }
1089
1090 /**
1091 * acf_get_array
1092 *
1093 * This function will force a variable to become an array
1094 *
1095 * @since ACF 5.0.0
1096 *
1097 * @param $var (mixed)
1098 * @return (array)
1099 */
1100 function acf_get_array( $var = false, $delimiter = '' ) {
1101
1102 // array
1103 if ( is_array( $var ) ) {
1104 return $var;
1105 }
1106
1107 // bail early if empty
1108 if ( acf_is_empty( $var ) ) {
1109 return array();
1110 }
1111
1112 // string
1113 if ( is_string( $var ) && $delimiter ) {
1114 return explode( $delimiter, $var );
1115 }
1116
1117 // place in array
1118 return (array) $var;
1119 }
1120
1121 /**
1122 * acf_get_numeric
1123 *
1124 * This function will return numeric values
1125 *
1126 * @since ACF 5.4.0
1127 *
1128 * @param $value (mixed)
1129 * @return (mixed)
1130 */
1131 function acf_get_numeric( $value = '' ) {
1132
1133 // vars
1134 $numbers = array();
1135 $is_array = is_array( $value );
1136
1137 // loop
1138 foreach ( (array) $value as $v ) {
1139 if ( is_numeric( $v ) ) {
1140 $numbers[] = (int) $v;
1141 }
1142 }
1143
1144 // bail early if is empty
1145 if ( empty( $numbers ) ) {
1146 return false;
1147 }
1148
1149 // convert array
1150 if ( ! $is_array ) {
1151 $numbers = $numbers[0];
1152 }
1153
1154 // return
1155 return $numbers;
1156 }
1157
1158 /**
1159 * acf_get_posts
1160 *
1161 * Similar to the get_posts() function but with extra functionality.
1162 *
1163 * @since ACF 5.1.5
1164 *
1165 * @param array $args The query args.
1166 * @return array
1167 */
1168 function acf_get_posts( $args = array() ) {
1169
1170 // Vars.
1171 $posts = array();
1172
1173 // Apply default args.
1174 $args = wp_parse_args(
1175 $args,
1176 array(
1177 'posts_per_page' => -1,
1178 'post_type' => '',
1179 'post_status' => 'any',
1180 'update_post_meta_cache' => false,
1181 'update_post_term_cache' => false,
1182 )
1183 );
1184
1185 // Avoid default 'post' post_type by providing all public types.
1186 if ( ! $args['post_type'] ) {
1187 $args['post_type'] = acf_get_post_types();
1188 }
1189
1190 if ( ! $args['post_status'] ) {
1191 $args['post_status'] = acf_get_post_stati();
1192 }
1193
1194 // Check if specific post IDs have been provided.
1195 if ( $args['post__in'] ) {
1196
1197 // Clean value into an array of IDs.
1198 $args['post__in'] = array_map( 'intval', acf_array( $args['post__in'] ) );
1199 }
1200
1201 /**
1202 * Filters the args used in `acf_get_posts()` that are passed to `get_posts()`.
1203 *
1204 * @since ACF 6.1.7
1205 *
1206 * @param array $args The args passed to `get_posts()`.
1207 */
1208 $args = apply_filters( 'acf/acf_get_posts/args', $args );
1209
1210 // Query posts.
1211 $posts = get_posts( $args );
1212
1213 // Remove any potential empty results.
1214 $posts = array_filter( $posts );
1215
1216 // Manually order results.
1217 if ( $posts && $args['post__in'] ) {
1218 $order = array();
1219 foreach ( $posts as $i => $post ) {
1220 $order[ $i ] = array_search( $post->ID, $args['post__in'] );
1221 }
1222 array_multisort( $order, $posts );
1223 }
1224
1225 /**
1226 * Filters the results found in the `acf_get_posts()` function.
1227 *
1228 * @since ACF 6.1.7
1229 *
1230 * @param array $posts The results from the `get_posts()` call.
1231 */
1232 return apply_filters( 'acf/acf_get_posts/results', $posts );
1233 }
1234
1235 /**
1236 * _acf_query_remove_post_type
1237 *
1238 * This function will remove the 'wp_posts.post_type' WHERE clause completely
1239 * When using 'post__in', this clause is unnecessary and slow.
1240 *
1241 * @since ACF 5.1.5
1242 *
1243 * @param $sql (string)
1244 * @return $sql
1245 */
1246 function _acf_query_remove_post_type( $sql ) {
1247
1248 // global
1249 global $wpdb;
1250
1251 // bail early if no 'wp_posts.ID IN'
1252 if ( strpos( $sql, "$wpdb->posts.ID IN" ) === false ) {
1253 return $sql;
1254 }
1255
1256 // get bits
1257 $glue = 'AND';
1258 $bits = explode( $glue, $sql );
1259
1260 // loop through $where and remove any post_type queries
1261 foreach ( $bits as $i => $bit ) {
1262 if ( strpos( $bit, "$wpdb->posts.post_type" ) !== false ) {
1263 unset( $bits[ $i ] );
1264 }
1265 }
1266
1267 // join $where back together
1268 $sql = implode( $glue, $bits );
1269
1270 // return
1271 return $sql;
1272 }
1273
1274 /**
1275 * acf_get_grouped_posts
1276 *
1277 * This function will return all posts grouped by post_type
1278 * This is handy for select settings
1279 *
1280 * @since ACF 5.0.0
1281 *
1282 * @param $args (array)
1283 * @return (array)
1284 */
1285 function acf_get_grouped_posts( $args ) {
1286
1287 // vars
1288 $data = array();
1289
1290 // defaults
1291 $args = wp_parse_args(
1292 $args,
1293 array(
1294 'posts_per_page' => -1,
1295 'paged' => 0,
1296 'post_type' => 'post',
1297 'orderby' => 'menu_order title',
1298 'order' => 'ASC',
1299 'post_status' => 'any',
1300 'suppress_filters' => false,
1301 'update_post_meta_cache' => false,
1302 )
1303 );
1304
1305 // find array of post_type
1306 $post_types = acf_get_array( $args['post_type'] );
1307 $is_single_post_type = ( count( $post_types ) === 1 );
1308
1309 // WordPress 6.8+ sorts post_type arrays for cache key generation
1310 // We need to use the same sorted order when processing results
1311 if (
1312 ! $is_single_post_type &&
1313 -1 !== $args['posts_per_page'] &&
1314 version_compare( get_bloginfo( 'version' ), '6.8', '>=' )
1315 ) {
1316 sort( $post_types );
1317 }
1318
1319 $post_types_labels = acf_get_pretty_post_types( $post_types );
1320
1321 // attachment doesn't work if it is the only item in an array
1322 if ( $is_single_post_type ) {
1323 $args['post_type'] = reset( $post_types );
1324 }
1325
1326 // add filter to orderby post type
1327 if ( ! $is_single_post_type ) {
1328 add_filter( 'posts_orderby', '_acf_orderby_post_type', 10, 2 );
1329 }
1330
1331 // get posts
1332 $posts = get_posts( $args );
1333
1334 // remove this filter (only once)
1335 if ( ! $is_single_post_type ) {
1336 remove_filter( 'posts_orderby', '_acf_orderby_post_type', 10 );
1337 }
1338
1339 // loop
1340 foreach ( $post_types as $post_type ) {
1341
1342 // vars
1343 $this_posts = array();
1344 $this_group = array();
1345
1346 // populate $this_posts
1347 foreach ( $posts as $post ) {
1348 if ( $post->post_type == $post_type ) {
1349 $this_posts[] = $post;
1350 }
1351 }
1352
1353 // bail early if no posts for this post type
1354 if ( empty( $this_posts ) ) {
1355 continue;
1356 }
1357
1358 // sort into hierarchical order!
1359 // this will fail if a search has taken place because parents wont exist
1360 if ( is_post_type_hierarchical( $post_type ) && empty( $args['s'] ) ) {
1361
1362 // vars
1363 $post_id = $this_posts[0]->ID;
1364 $parent_id = acf_maybe_get( $args, 'post_parent', 0 );
1365 $offset = 0;
1366 $length = count( $this_posts );
1367
1368 // get all posts from this post type
1369 $all_posts = get_posts(
1370 array_merge(
1371 $args,
1372 array(
1373 'posts_per_page' => -1,
1374 'paged' => 0,
1375 'post_type' => $post_type,
1376 )
1377 )
1378 );
1379
1380 // find starting point (offset)
1381 foreach ( $all_posts as $i => $post ) {
1382 if ( $post->ID == $post_id ) {
1383 $offset = $i;
1384 break;
1385 }
1386 }
1387
1388 // order posts
1389 $ordered_posts = get_page_children( $parent_id, $all_posts );
1390
1391 // compare array lengths
1392 // if $ordered_posts is smaller than $all_posts, WP has lost posts during the get_page_children() function
1393 // this is possible when get_post( $args ) filter out parents (via taxonomy, meta and other search parameters)
1394 if ( count( $ordered_posts ) == count( $all_posts ) ) {
1395 $this_posts = array_slice( $ordered_posts, $offset, $length );
1396 }
1397 }
1398
1399 // populate $this_posts
1400 foreach ( $this_posts as $post ) {
1401 $this_group[ $post->ID ] = $post;
1402 }
1403
1404 // group by post type
1405 $label = $post_types_labels[ $post_type ];
1406 $data[ $label ] = $this_group;
1407 }
1408
1409 // return
1410 return $data;
1411 }
1412
1413 /**
1414 * The internal ACF function to add order by post types for use in `acf_get_grouped_posts`
1415 *
1416 * @param string $orderby The current orderby value for a query.
1417 * @param object $wp_query The WP_Query.
1418 * @return string The potentially modified orderby string.
1419 */
1420 function _acf_orderby_post_type( $orderby, $wp_query ) {
1421 global $wpdb;
1422
1423 $post_types = $wp_query->get( 'post_type' );
1424
1425 // Prepend the SQL.
1426 if ( is_array( $post_types ) ) {
1427 $post_types = array_map( 'esc_sql', $post_types );
1428 $post_types = implode( "','", $post_types );
1429 $orderby = "FIELD({$wpdb->posts}.post_type,'$post_types')," . $orderby;
1430 }
1431
1432 return $orderby;
1433 }
1434
1435 function acf_get_post_title( $post = 0, $is_search = false ) {
1436
1437 // vars
1438 $post = get_post( $post );
1439 $title = '';
1440 $prepend = '';
1441 $append = '';
1442
1443 // bail early if no post
1444 if ( ! $post ) {
1445 return '';
1446 }
1447
1448 // title
1449 $title = get_the_title( $post->ID );
1450
1451 // empty
1452 if ( $title === '' ) {
1453 $title = __( '(no title)', 'secure-custom-fields' );
1454 }
1455
1456 // status
1457 if ( get_post_status( $post->ID ) != 'publish' ) {
1458 $append .= ' (' . get_post_status( $post->ID ) . ')';
1459 }
1460
1461 // ancestors
1462 if ( $post->post_type !== 'attachment' ) {
1463
1464 // get ancestors
1465 $ancestors = get_ancestors( $post->ID, $post->post_type );
1466 $prepend .= str_repeat( '- ', count( $ancestors ) );
1467 }
1468
1469 // merge
1470 $title = $prepend . $title . $append;
1471
1472 // return
1473 return $title;
1474 }
1475
1476 function acf_order_by_search( $array, $search ) {
1477
1478 // vars
1479 $weights = array();
1480 $needle = strtolower( $search );
1481
1482 // add key prefix
1483 foreach ( array_keys( $array ) as $k ) {
1484 $array[ '_' . $k ] = acf_extract_var( $array, $k );
1485 }
1486
1487 // add search weight
1488 foreach ( $array as $k => $v ) {
1489
1490 // vars
1491 $weight = 0;
1492 $haystack = strtolower( $v );
1493 $strpos = strpos( $haystack, $needle );
1494
1495 // detect search match
1496 if ( $strpos !== false ) {
1497
1498 // set weight to length of match
1499 $weight = strlen( $search );
1500
1501 // increase weight if match starts at beginning of string
1502 if ( $strpos == 0 ) {
1503 ++$weight;
1504 }
1505 }
1506
1507 // append to wights
1508 $weights[ $k ] = $weight;
1509 }
1510
1511 // sort the array with menu_order ascending
1512 array_multisort( $weights, SORT_DESC, $array );
1513
1514 // remove key prefix
1515 foreach ( array_keys( $array ) as $k ) {
1516 $array[ substr( $k, 1 ) ] = acf_extract_var( $array, $k );
1517 }
1518
1519 // return
1520 return $array;
1521 }
1522
1523 /**
1524 * acf_get_pretty_user_roles
1525 *
1526 * description
1527 *
1528 * @since ACF 5.3.2
1529 *
1530 * @param $post_id (int)
1531 * @return $post_id (int)
1532 */
1533 function acf_get_pretty_user_roles( $allowed = false ) {
1534
1535 // vars
1536 $editable_roles = get_editable_roles();
1537 $allowed = acf_get_array( $allowed );
1538 $roles = array();
1539
1540 // loop
1541 foreach ( $editable_roles as $role_name => $role_details ) {
1542
1543 // bail early if not allowed
1544 if ( ! empty( $allowed ) && ! in_array( $role_name, $allowed ) ) {
1545 continue;
1546 }
1547
1548 // append
1549 $roles[ $role_name ] = translate_user_role( $role_details['name'] );
1550 }
1551
1552 // return
1553 return $roles;
1554 }
1555
1556 /**
1557 * acf_get_grouped_users
1558 *
1559 * This function will return all users grouped by role
1560 * This is handy for select settings
1561 *
1562 * @since ACF 5.0.0
1563 *
1564 * @param $args (array)
1565 * @return (array)
1566 */
1567 function acf_get_grouped_users( $args = array() ) {
1568
1569 // vars
1570 $r = array();
1571
1572 // defaults
1573 $args = wp_parse_args(
1574 $args,
1575 array(
1576 'users_per_page' => -1,
1577 'paged' => 0,
1578 'role' => '',
1579 'orderby' => 'login',
1580 'order' => 'ASC',
1581 )
1582 );
1583
1584 // offset
1585 $i = 0;
1586 $min = 0;
1587 $max = 0;
1588 $users_per_page = acf_extract_var( $args, 'users_per_page' );
1589 $paged = acf_extract_var( $args, 'paged' );
1590
1591 if ( $users_per_page > 0 ) {
1592
1593 // prevent paged from being -1
1594 $paged = max( 0, $paged );
1595
1596 // set min / max
1597 $min = ( ( $paged - 1 ) * $users_per_page ) + 1; // 1, 11
1598 $max = ( $paged * $users_per_page ); // 10, 20
1599
1600 }
1601
1602 // find array of post_type
1603 $user_roles = acf_get_pretty_user_roles( $args['role'] );
1604
1605 // fix role
1606 if ( is_array( $args['role'] ) ) {
1607
1608 // global
1609 global $wp_version, $wpdb;
1610
1611 // vars
1612 $roles = acf_extract_var( $args, 'role' );
1613
1614 // new WP has role__in
1615 if ( version_compare( $wp_version, '4.4', '>=' ) ) {
1616 $args['role__in'] = $roles;
1617
1618 // old WP doesn't have role__in
1619 } else {
1620
1621 // vars
1622 $blog_id = get_current_blog_id();
1623 $meta_query = array( 'relation' => 'OR' );
1624
1625 // loop
1626 foreach ( $roles as $role ) {
1627 $meta_query[] = array(
1628 'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
1629 'value' => '"' . $role . '"',
1630 'compare' => 'LIKE',
1631 );
1632 }
1633
1634 // append
1635 $args['meta_query'] = $meta_query;
1636 }
1637 }
1638
1639 // get posts
1640 $users = get_users( $args );
1641
1642 // loop
1643 foreach ( $user_roles as $user_role_name => $user_role_label ) {
1644
1645 // vars
1646 $this_users = array();
1647 $this_group = array();
1648
1649 // populate $this_posts
1650 foreach ( array_keys( $users ) as $key ) {
1651
1652 // bail early if not correct role
1653 if ( ! in_array( $user_role_name, $users[ $key ]->roles ) ) {
1654 continue;
1655 }
1656
1657 // extract user
1658 $user = acf_extract_var( $users, $key );
1659
1660 // increase
1661 ++$i;
1662
1663 // bail early if too low
1664 if ( $min && $i < $min ) {
1665 continue;
1666 }
1667
1668 // bail early if too high (don't bother looking at any more users)
1669 if ( $max && $i > $max ) {
1670 break;
1671 }
1672
1673 // group by post type
1674 $this_users[ $user->ID ] = $user;
1675 }
1676
1677 // bail early if no posts for this post type
1678 if ( empty( $this_users ) ) {
1679 continue;
1680 }
1681
1682 // append
1683 $r[ $user_role_label ] = $this_users;
1684 }
1685
1686 // return
1687 return $r;
1688 }
1689
1690 /**
1691 * acf_json_encode
1692 *
1693 * Returns json_encode() ready for file / database use.
1694 *
1695 * @since ACF 5.0.0
1696 *
1697 * @param array $json The array of data to encode.
1698 * @return string
1699 */
1700 function acf_json_encode( $json ) {
1701 return json_encode( $json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE );
1702 }
1703
1704 /**
1705 * acf_str_exists
1706 *
1707 * This function will return true if a sub string is found
1708 *
1709 * @since ACF 5.0.0
1710 *
1711 * @param $needle (string)
1712 * @param $haystack (string)
1713 * @return (boolean)
1714 */
1715 function acf_str_exists( $needle, $haystack ) {
1716
1717 // return true if $haystack contains the $needle
1718 if ( is_string( $haystack ) && strpos( $haystack, $needle ) !== false ) {
1719 return true;
1720 }
1721
1722 // return
1723 return false;
1724 }
1725
1726 /**
1727 * A legacy function designed for developer debugging.
1728 *
1729 * @deprecated 6.2.6 Removed for security, but keeping the definition in case third party devs have it in their code.
1730 * @since ACF 5.0.0
1731 *
1732 * @return false
1733 */
1734 function acf_debug() {
1735 _deprecated_function( __FUNCTION__, '6.2.7' );
1736 return false;
1737 }
1738
1739 /**
1740 * A legacy function designed for developer debugging.
1741 *
1742 * @deprecated 6.2.6 Removed for security, but keeping the definition in case third party devs have it in their code.
1743 * @since ACF 5.0.0
1744 *
1745 * @return false
1746 */
1747 function acf_debug_start() {
1748 _deprecated_function( __FUNCTION__, '6.2.7' );
1749 return false;
1750 }
1751
1752 /**
1753 * A legacy function designed for developer debugging.
1754 *
1755 * @deprecated 6.2.6 Removed for security, but keeping the definition in case third party devs have it in their code.
1756 * @since ACF 5.0.0
1757 *
1758 * @return false
1759 */
1760 function acf_debug_end() {
1761 _deprecated_function( __FUNCTION__, '6.2.7' );
1762 return false;
1763 }
1764
1765 /**
1766 * acf_encode_choices
1767 *
1768 * description
1769 *
1770 * @since ACF 5.0.0
1771 *
1772 * @param $post_id (int)
1773 * @return $post_id (int)
1774 */
1775 function acf_encode_choices( $array = array(), $show_keys = true ) {
1776
1777 // bail early if not array (maybe a single string)
1778 if ( ! is_array( $array ) ) {
1779 return $array;
1780 }
1781
1782 // bail early if empty array
1783 if ( empty( $array ) ) {
1784 return '';
1785 }
1786
1787 // vars
1788 $string = '';
1789
1790 // if allowed to show keys (good for choices, not for default values)
1791 if ( $show_keys ) {
1792
1793 // loop
1794 foreach ( $array as $k => $v ) {
1795
1796 // ignore if key and value are the same
1797 if ( strval( $k ) == strval( $v ) ) {
1798 continue;
1799 }
1800
1801 // show key in the value
1802 $array[ $k ] = $k . ' : ' . $v;
1803 }
1804 }
1805
1806 // implode
1807 $string = implode( "\n", $array );
1808
1809 // return
1810 return $string;
1811 }
1812
1813 function acf_decode_choices( $string = '', $array_keys = false ) {
1814
1815 // bail early if already array
1816 if ( is_array( $string ) ) {
1817 return $string;
1818
1819 // allow numeric values (same as string)
1820 } elseif ( is_numeric( $string ) ) {
1821
1822 // do nothing
1823 // bail early if not a string
1824 } elseif ( ! is_string( $string ) ) {
1825 return array();
1826
1827 // bail early if is empty string
1828 } elseif ( $string === '' ) {
1829 return array();
1830 }
1831
1832 // vars
1833 $array = array();
1834
1835 // explode
1836 $lines = explode( "\n", $string );
1837
1838 // key => value
1839 foreach ( $lines as $line ) {
1840
1841 // vars
1842 $k = trim( $line );
1843 $v = trim( $line );
1844
1845 // look for ' : '
1846 if ( acf_str_exists( ' : ', $line ) ) {
1847 $line = explode( ' : ', $line );
1848
1849 $k = trim( $line[0] );
1850 $v = trim( $line[1] );
1851 }
1852
1853 // append
1854 $array[ $k ] = $v;
1855 }
1856
1857 // return only array keys? (good for checkbox default_value)
1858 if ( $array_keys ) {
1859 return array_keys( $array );
1860 }
1861
1862 // return
1863 return $array;
1864 }
1865
1866 /**
1867 * acf_str_replace
1868 *
1869 * This function will replace an array of strings much like str_replace
1870 * The difference is the extra logic to avoid replacing a string that has already been replaced
1871 * This is very useful for replacing date characters as they overlap with each other
1872 *
1873 * @since ACF 5.3.8
1874 *
1875 * @param $post_id (int)
1876 * @return $post_id (int)
1877 */
1878 function acf_str_replace( $string = '', $search_replace = array() ) {
1879
1880 // vars
1881 $ignore = array();
1882
1883 // remove potential empty search to avoid PHP error
1884 unset( $search_replace[''] );
1885
1886 // loop over conversions
1887 foreach ( $search_replace as $search => $replace ) {
1888
1889 // ignore this search, it was a previous replace
1890 if ( in_array( $search, $ignore ) ) {
1891 continue;
1892 }
1893
1894 // bail early if substring not found
1895 if ( strpos( $string, $search ) === false ) {
1896 continue;
1897 }
1898
1899 // replace
1900 $string = str_replace( $search, $replace, $string );
1901
1902 // append to ignore
1903 $ignore[] = $replace;
1904 }
1905
1906 // return
1907 return $string;
1908 }
1909
1910 /**
1911 * date & time formats
1912 *
1913 * These settings contain an association of format strings from PHP => JS
1914 *
1915 * @since ACF 5.3.8
1916 *
1917 * @param n/a
1918 * @return n/a
1919 */
1920
1921 acf_update_setting(
1922 'php_to_js_date_formats',
1923 array(
1924
1925 // Year
1926 'Y' => 'yy', // Numeric, 4 digits 1999, 2003
1927 'y' => 'y', // Numeric, 2 digits 99, 03
1928
1929
1930 // Month
1931 'm' => 'mm', // Numeric, with leading zeros 01–12
1932 'n' => 'm', // Numeric, without leading zeros 1–12
1933 'F' => 'MM', // Textual full January – December
1934 'M' => 'M', // Textual three letters Jan - Dec
1935
1936
1937 // Weekday
1938 'l' => 'DD', // Full name (lowercase 'L') Sunday – Saturday
1939 'D' => 'D', // Three letter name Mon – Sun
1940
1941
1942 // Day of Month
1943 'd' => 'dd', // Numeric, with leading zeros 01–31
1944 'j' => 'd', // Numeric, without leading zeros 1–31
1945 'S' => '', // The English suffix for the day of the month st, nd or th in the 1st, 2nd or 15th.
1946
1947 )
1948 );
1949
1950 acf_update_setting(
1951 'php_to_js_time_formats',
1952 array(
1953
1954 'a' => 'tt', // Lowercase Ante meridiem and Post meridiem am or pm
1955 'A' => 'TT', // Uppercase Ante meridiem and Post meridiem AM or PM
1956 'h' => 'hh', // 12-hour format of an hour with leading zeros 01 through 12
1957 'g' => 'h', // 12-hour format of an hour without leading zeros 1 through 12
1958 'H' => 'HH', // 24-hour format of an hour with leading zeros 00 through 23
1959 'G' => 'H', // 24-hour format of an hour without leading zeros 0 through 23
1960 'i' => 'mm', // Minutes with leading zeros 00 to 59
1961 's' => 'ss', // Seconds, with leading zeros 00 through 59
1962
1963 )
1964 );
1965
1966
1967 /**
1968 * acf_split_date_time
1969 *
1970 * This function will split a format string into separate date and time
1971 *
1972 * @since ACF 5.3.8
1973 *
1974 * @param $date_time (string)
1975 * @return $formats (array)
1976 */
1977 function acf_split_date_time( $date_time = '' ) {
1978
1979 // vars
1980 $php_date = acf_get_setting( 'php_to_js_date_formats' );
1981 $php_time = acf_get_setting( 'php_to_js_time_formats' );
1982 $chars = str_split( $date_time );
1983 $type = 'date';
1984
1985 // default
1986 $data = array(
1987 'date' => '',
1988 'time' => '',
1989 );
1990
1991 // loop
1992 foreach ( $chars as $i => $c ) {
1993
1994 // find type
1995 // - allow misc characters to append to previous type
1996 if ( isset( $php_date[ $c ] ) ) {
1997 $type = 'date';
1998 } elseif ( isset( $php_time[ $c ] ) ) {
1999 $type = 'time';
2000 }
2001
2002 // append char
2003 $data[ $type ] .= $c;
2004 }
2005
2006 // trim
2007 $data['date'] = trim( $data['date'] );
2008 $data['time'] = trim( $data['time'] );
2009
2010 // return
2011 return $data;
2012 }
2013
2014 /**
2015 * acf_convert_date_to_php
2016 *
2017 * This function converts a date format string from JS to PHP
2018 *
2019 * @since ACF 5.0.0
2020 *
2021 * @param $date (string)
2022 * @return (string)
2023 */
2024 function acf_convert_date_to_php( $date = '' ) {
2025
2026 // vars
2027 $php_to_js = acf_get_setting( 'php_to_js_date_formats' );
2028 $js_to_php = array_flip( $php_to_js );
2029
2030 // return
2031 return acf_str_replace( $date, $js_to_php );
2032 }
2033
2034 /**
2035 * acf_convert_date_to_js
2036 *
2037 * This function converts a date format string from PHP to JS
2038 *
2039 * @since ACF 5.0.0
2040 *
2041 * @param $date (string)
2042 * @return (string)
2043 */
2044 function acf_convert_date_to_js( $date = '' ) {
2045
2046 // vars
2047 $php_to_js = acf_get_setting( 'php_to_js_date_formats' );
2048
2049 // return
2050 return acf_str_replace( $date, $php_to_js );
2051 }
2052
2053 /**
2054 * acf_convert_time_to_php
2055 *
2056 * This function converts a time format string from JS to PHP
2057 *
2058 * @since ACF 5.0.0
2059 *
2060 * @param $time (string)
2061 * @return (string)
2062 */
2063 function acf_convert_time_to_php( $time = '' ) {
2064
2065 // vars
2066 $php_to_js = acf_get_setting( 'php_to_js_time_formats' );
2067 $js_to_php = array_flip( $php_to_js );
2068
2069 // return
2070 return acf_str_replace( $time, $js_to_php );
2071 }
2072
2073 /**
2074 * acf_convert_time_to_js
2075 *
2076 * This function converts a date format string from PHP to JS
2077 *
2078 * @since ACF 5.0.0
2079 *
2080 * @param $time (string)
2081 * @return (string)
2082 */
2083 function acf_convert_time_to_js( $time = '' ) {
2084
2085 // vars
2086 $php_to_js = acf_get_setting( 'php_to_js_time_formats' );
2087
2088 // return
2089 return acf_str_replace( $time, $php_to_js );
2090 }
2091
2092 /**
2093 * acf_update_user_setting
2094 *
2095 * description
2096 *
2097 * @since ACF 5.0.0
2098 *
2099 * @param $post_id (int)
2100 * @return $post_id (int)
2101 */
2102 function acf_update_user_setting( $name, $value ) {
2103
2104 // get current user id
2105 $user_id = get_current_user_id();
2106
2107 // get user settings
2108 $settings = get_user_meta( $user_id, 'acf_user_settings', true );
2109
2110 // ensure array
2111 $settings = acf_get_array( $settings );
2112
2113 // delete setting (allow 0 to save)
2114 if ( acf_is_empty( $value ) ) {
2115 unset( $settings[ $name ] );
2116
2117 // append setting
2118 } else {
2119 $settings[ $name ] = $value;
2120 }
2121
2122 // update user data
2123 return update_metadata( 'user', $user_id, 'acf_user_settings', $settings );
2124 }
2125
2126 /**
2127 * acf_get_user_setting
2128 *
2129 * description
2130 *
2131 * @since ACF 5.0.0
2132 *
2133 * @param $post_id (int)
2134 * @return $post_id (int)
2135 */
2136 function acf_get_user_setting( $name = '', $default = false ) {
2137
2138 // get current user id
2139 $user_id = get_current_user_id();
2140
2141 // get user settings
2142 $settings = get_user_meta( $user_id, 'acf_user_settings', true );
2143
2144 // ensure array
2145 $settings = acf_get_array( $settings );
2146
2147 // bail arly if no settings
2148 if ( ! isset( $settings[ $name ] ) ) {
2149 return $default;
2150 }
2151
2152 // return
2153 return $settings[ $name ];
2154 }
2155
2156 /**
2157 * acf_in_array
2158 *
2159 * description
2160 *
2161 * @since ACF 5.0.0
2162 *
2163 * @param $post_id (int)
2164 * @return $post_id (int)
2165 */
2166 function acf_in_array( $value = '', $array = false ) {
2167
2168 // bail early if not array
2169 if ( ! is_array( $array ) ) {
2170 return false;
2171 }
2172
2173 // find value in array
2174 return in_array( $value, $array );
2175 }
2176
2177 /**
2178 * acf_get_valid_post_id
2179 *
2180 * This function will return a valid post_id based on the current screen / parameter
2181 *
2182 * @since ACF 5.0.0
2183 *
2184 * @param $post_id (mixed)
2185 * @return $post_id (mixed)
2186 */
2187 function acf_get_valid_post_id( $post_id = 0 ) {
2188
2189 // allow filter to short-circuit load_value logic
2190 $preload = apply_filters( 'acf/pre_load_post_id', null, $post_id );
2191 if ( $preload !== null ) {
2192 return $preload;
2193 }
2194
2195 // vars
2196 $_post_id = $post_id;
2197
2198 // if not $post_id, load queried object
2199 if ( ! $post_id ) {
2200
2201 // try for global post (needed for setup_postdata)
2202 $post_id = (int) get_the_ID();
2203
2204 // try for current screen
2205 if ( ! $post_id ) {
2206 $post_id = get_queried_object();
2207 }
2208 }
2209
2210 // $post_id may be an object.
2211 // todo: Compare class types instead.
2212 if ( is_object( $post_id ) ) {
2213
2214 // post
2215 if ( isset( $post_id->post_type, $post_id->ID ) ) {
2216 $post_id = $post_id->ID;
2217
2218 // user
2219 } elseif ( isset( $post_id->roles, $post_id->ID ) ) {
2220 $post_id = 'user_' . $post_id->ID;
2221
2222 // term
2223 } elseif ( isset( $post_id->taxonomy, $post_id->term_id ) ) {
2224 $post_id = 'term_' . $post_id->term_id;
2225
2226 // comment
2227 } elseif ( isset( $post_id->comment_ID ) ) {
2228 $post_id = 'comment_' . $post_id->comment_ID;
2229
2230 // default
2231 } else {
2232 $post_id = 0;
2233 }
2234 }
2235
2236 // allow for option == options
2237 if ( $post_id === 'option' ) {
2238 $post_id = 'options';
2239 }
2240
2241 // append language code
2242 if ( $post_id == 'options' ) {
2243 $dl = acf_get_setting( 'default_language' );
2244 $cl = acf_get_setting( 'current_language' );
2245
2246 if ( $cl && $cl !== $dl ) {
2247 $post_id .= '_' . $cl;
2248 }
2249 }
2250
2251 // filter for 3rd party
2252 $post_id = apply_filters( 'acf/validate_post_id', $post_id, $_post_id );
2253
2254 // return
2255 return $post_id;
2256 }
2257
2258
2259
2260 /**
2261 * acf_get_post_id_info
2262 *
2263 * This function will return the type and id for a given $post_id string
2264 *
2265 * @since ACF 5.4.0
2266 *
2267 * @param $post_id (mixed)
2268 * @return $info (array)
2269 */
2270 function acf_get_post_id_info( $post_id = 0 ) {
2271
2272 // vars
2273 $info = array(
2274 'type' => 'post',
2275 'id' => 0,
2276 );
2277
2278 // bail early if no $post_id
2279 if ( ! $post_id ) {
2280 return $info;
2281 }
2282
2283 // check cache
2284 // - this function will most likely be called multiple times (saving loading fields from post)
2285 // $cache_key = "get_post_id_info/post_id={$post_id}";
2286 // if( acf_isset_cache($cache_key) ) return acf_get_cache($cache_key);
2287 // numeric
2288 if ( is_numeric( $post_id ) ) {
2289 $info['id'] = scf_numeric_to_int( $post_id );
2290
2291 // string
2292 } elseif ( is_string( $post_id ) ) {
2293
2294 // vars
2295 $glue = '_';
2296 $type = explode( $glue, $post_id );
2297 $id = array_pop( $type );
2298 $type = implode( $glue, $type );
2299 $meta = array( 'post', 'user', 'comment', 'term' );
2300
2301 // check if is taxonomy (ACF < 5.5)
2302 // - avoid scenario where taxonomy exists with name of meta type
2303 if ( ! in_array( $type, $meta ) && acf_isset_termmeta( $type ) ) {
2304 $type = 'term';
2305 }
2306
2307 // meta
2308 if ( is_numeric( $id ) && in_array( $type, $meta ) ) {
2309 $info['type'] = $type;
2310 $info['id'] = (int) $id;
2311
2312 // option
2313 } else {
2314 $info['type'] = 'option';
2315 $info['id'] = $post_id;
2316 }
2317 }
2318
2319 // update cache
2320 // acf_set_cache($cache_key, $info);
2321 // filter
2322 $info = apply_filters( 'acf/get_post_id_info', $info, $post_id );
2323
2324 // return
2325 return $info;
2326 }
2327
2328 /**
2329 * acf_isset_termmeta
2330 *
2331 * This function will return true if the termmeta table exists
2332 * https://developer.wordpress.org/reference/functions/get_term_meta/
2333 *
2334 * @since ACF 5.4.0
2335 *
2336 * @param $post_id (int)
2337 * @return $post_id (int)
2338 */
2339 function acf_isset_termmeta( $taxonomy = '' ) {
2340
2341 // bail early if no table
2342 if ( get_option( 'db_version' ) < 34370 ) {
2343 return false;
2344 }
2345
2346 // check taxonomy
2347 if ( $taxonomy && ! taxonomy_exists( $taxonomy ) ) {
2348 return false;
2349 }
2350
2351 // return
2352 return true;
2353 }
2354
2355 /**
2356 * This function will walk through the $_FILES data and upload each found.
2357 *
2358 * @since ACF 5.0.9
2359 *
2360 * @param array $ancestors An internal parameter, not required.
2361 */
2362 function acf_upload_files( $ancestors = array() ) {
2363
2364 if ( empty( $_FILES['acf'] ) ) {
2365 return;
2366 }
2367
2368 $file = acf_sanitize_files_array( $_FILES['acf'] ); // phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified upstream.
2369
2370 // walk through ancestors.
2371 if ( ! empty( $ancestors ) ) {
2372 foreach ( $ancestors as $a ) {
2373 foreach ( array_keys( $file ) as $k ) {
2374 $file[ $k ] = $file[ $k ][ $a ];
2375 }
2376 }
2377 }
2378
2379 // is array?
2380 if ( is_array( $file['name'] ) ) {
2381 foreach ( array_keys( $file['name'] ) as $k ) {
2382 $_ancestors = array_merge( $ancestors, array( $k ) );
2383
2384 acf_upload_files( $_ancestors );
2385 }
2386
2387 return;
2388 }
2389
2390 // Bail early if file has error (no file uploaded).
2391 if ( $file['error'] ) {
2392 return;
2393 }
2394
2395 $field_key = end( $ancestors );
2396 $nonce_name = $field_key . '_file_nonce';
2397
2398 if ( empty( $_REQUEST['acf'][ $nonce_name ] ) || ! wp_verify_nonce( sanitize_text_field( $_REQUEST['acf'][ $nonce_name ] ), 'acf/file_uploader_nonce/' . $field_key ) ) {
2399 return;
2400 }
2401
2402 // Assign global _acfuploader for media validation.
2403 $_POST['_acfuploader'] = $field_key;
2404
2405 // file found!
2406 $attachment_id = acf_upload_file( $file );
2407
2408 // update $_POST
2409 array_unshift( $ancestors, 'acf' );
2410 acf_update_nested_array( $_POST, $ancestors, $attachment_id );
2411 }
2412
2413 /**
2414 * acf_upload_file
2415 *
2416 * This function will upload a $_FILE
2417 *
2418 * @since ACF 5.0.9
2419 *
2420 * @param $uploaded_file (array) array found from $_FILE data
2421 * @return $id (int) new attachment ID
2422 */
2423 function acf_upload_file( $uploaded_file ) {
2424
2425 // required
2426 // require_once( ABSPATH . "/wp-load.php" ); // WP should already be loaded
2427 require_once ABSPATH . '/wp-admin/includes/media.php'; // video functions
2428 require_once ABSPATH . '/wp-admin/includes/file.php';
2429 require_once ABSPATH . '/wp-admin/includes/image.php';
2430
2431 // required for wp_handle_upload() to upload the file
2432 $upload_overrides = array( 'test_form' => false );
2433
2434 // upload
2435 $file = wp_handle_upload( $uploaded_file, $upload_overrides );
2436
2437 // bail early if upload failed
2438 if ( isset( $file['error'] ) ) {
2439 return $file['error'];
2440 }
2441
2442 // vars
2443 $url = $file['url'];
2444 $type = $file['type'];
2445 $file = $file['file'];
2446 $filename = basename( $file );
2447
2448 // Construct the object array
2449 $object = array(
2450 'post_title' => $filename,
2451 'post_mime_type' => $type,
2452 'guid' => $url,
2453 );
2454
2455 // Save the data
2456 $id = wp_insert_attachment( $object, $file );
2457
2458 // Add the meta-data
2459 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
2460
2461 /** This action is documented in wp-admin/custom-header.php */
2462 do_action( 'wp_create_file_in_uploads', $file, $id ); // For replication
2463
2464 // return new ID
2465 return $id;
2466 }
2467
2468 /**
2469 * acf_update_nested_array
2470 *
2471 * This function will update a nested array value. Useful for modifying the $_POST array
2472 *
2473 * @since ACF 5.0.9
2474 *
2475 * @param $array (array) target array to be updated
2476 * @param $ancestors (array) array of keys to navigate through to find the child
2477 * @param $value (mixed) The new value
2478 * @return (boolean)
2479 */
2480 function acf_update_nested_array( &$array, $ancestors, $value ) {
2481
2482 // if no more ancestors, update the current var
2483 if ( empty( $ancestors ) ) {
2484 $array = $value;
2485
2486 // return
2487 return true;
2488 }
2489
2490 // shift the next ancestor from the array
2491 $k = array_shift( $ancestors );
2492
2493 // if exists
2494 if ( isset( $array[ $k ] ) ) {
2495 return acf_update_nested_array( $array[ $k ], $ancestors, $value );
2496 }
2497
2498 // return
2499 return false;
2500 }
2501
2502 /**
2503 * acf_is_screen
2504 *
2505 * This function will return true if all args are matched for the current screen
2506 *
2507 * @since ACF 5.1.5
2508 *
2509 * @param $post_id (int)
2510 * @return $post_id (int)
2511 */
2512 function acf_is_screen( $id = '' ) {
2513
2514 // bail early if not defined
2515 if ( ! function_exists( 'get_current_screen' ) ) {
2516 return false;
2517 }
2518
2519 // vars
2520 $current_screen = get_current_screen();
2521
2522 // no screen
2523 if ( ! $current_screen ) {
2524 return false;
2525
2526 // array
2527 } elseif ( is_array( $id ) ) {
2528 return in_array( $current_screen->id, $id );
2529
2530 // string
2531 } else {
2532 return ( $id === $current_screen->id );
2533 }
2534 }
2535
2536 /**
2537 * Check if we're in an ACF admin screen
2538 *
2539 * @since ACF 6.2.2
2540 *
2541 * @return boolean Returns true if the current screen is an ACF admin screen.
2542 */
2543 function acf_is_acf_admin_screen() {
2544 if ( ! is_admin() || ! function_exists( 'get_current_screen' ) ) {
2545 return false;
2546 }
2547 $screen = get_current_screen();
2548 if ( $screen && ! empty( $screen->post_type ) && substr( $screen->post_type, 0, 4 ) === 'acf-' ) {
2549 return true;
2550 }
2551
2552 return false;
2553 }
2554
2555 /**
2556 * acf_maybe_get
2557 *
2558 * This function will return a var if it exists in an array
2559 *
2560 * @since ACF 5.1.5
2561 *
2562 * @param $array (array) the array to look within
2563 * @param $key (key) the array key to look for. Nested values may be found using '/'
2564 * @param $default (mixed) the value returned if not found
2565 * @return $post_id (int)
2566 */
2567 function acf_maybe_get( $array = array(), $key = 0, $default = null ) {
2568
2569 return isset( $array[ $key ] ) ? $array[ $key ] : $default;
2570 }
2571
2572 function acf_maybe_get_POST( $key = '', $default = null ) {
2573
2574 return isset( $_POST[ $key ] ) ? acf_sanitize_request_args( $_POST[ $key ] ) : $default; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing -- Checked elsewhere.
2575 }
2576
2577 function acf_maybe_get_GET( $key = '', $default = null ) {
2578
2579 return isset( $_GET[ $key ] ) ? acf_sanitize_request_args( $_GET[ $key ] ) : $default; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Checked elsewhere.
2580 }
2581
2582 /**
2583 * Returns an array of attachment data.
2584 *
2585 * @since ACF 5.1.5
2586 *
2587 * @param integer|WP_Post The attachment ID or object
2588 * @return array|false
2589 */
2590 function acf_get_attachment( $attachment ) {
2591
2592 // Allow filter to short-circuit load attachment logic.
2593 // Alternatively, this filter may be used to switch blogs for multisite media functionality.
2594 $response = apply_filters( 'acf/pre_load_attachment', null, $attachment );
2595 if ( $response !== null ) {
2596 return $response;
2597 }
2598
2599 // Get the attachment post object.
2600 $attachment = get_post( $attachment );
2601 if ( ! $attachment ) {
2602 return false;
2603 }
2604 if ( $attachment->post_type !== 'attachment' ) {
2605 return false;
2606 }
2607
2608 // Load various attachment details.
2609 $meta = wp_get_attachment_metadata( $attachment->ID );
2610 $attached_file = get_attached_file( $attachment->ID );
2611 if ( strpos( $attachment->post_mime_type, '/' ) !== false ) {
2612 list($type, $subtype) = explode( '/', $attachment->post_mime_type );
2613 } else {
2614 list($type, $subtype) = array( $attachment->post_mime_type, '' );
2615 }
2616
2617 // Generate response.
2618 $response = array(
2619 'ID' => $attachment->ID,
2620 'id' => $attachment->ID,
2621 'title' => $attachment->post_title,
2622 'filename' => wp_basename( $attached_file ),
2623 'filesize' => 0,
2624 'url' => wp_get_attachment_url( $attachment->ID ),
2625 'link' => get_attachment_link( $attachment->ID ),
2626 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
2627 'author' => $attachment->post_author,
2628 'description' => $attachment->post_content,
2629 'caption' => $attachment->post_excerpt,
2630 'name' => $attachment->post_name,
2631 'status' => $attachment->post_status,
2632 'uploaded_to' => $attachment->post_parent,
2633 'date' => $attachment->post_date_gmt,
2634 'modified' => $attachment->post_modified_gmt,
2635 'menu_order' => $attachment->menu_order,
2636 'mime_type' => $attachment->post_mime_type,
2637 'type' => $type,
2638 'subtype' => $subtype,
2639 'icon' => wp_mime_type_icon( $attachment->ID ),
2640 );
2641
2642 // Append filesize data.
2643 if ( isset( $meta['filesize'] ) ) {
2644 $response['filesize'] = $meta['filesize'];
2645 } else {
2646 /**
2647 * Allows shortcutting our ACF's `filesize` call to prevent us making filesystem calls.
2648 * Mostly useful for third party plugins which may offload media to other services, and filesize calls will induce a remote download.
2649 *
2650 * @since ACF 6.2.2
2651 *
2652 * @param int|null $shortcut_filesize The default filesize.
2653 * @param WP_Post $attachment The attachment post object we're looking for the filesize for.
2654 */
2655 $shortcut_filesize = apply_filters( 'acf/filesize', null, $attachment );
2656 if ( $shortcut_filesize ) {
2657 $response['filesize'] = intval( $shortcut_filesize );
2658 } elseif ( file_exists( $attached_file ) ) {
2659 $response['filesize'] = filesize( $attached_file );
2660 }
2661 }
2662
2663 // Restrict the loading of image "sizes".
2664 $sizes_id = 0;
2665
2666 // Type specific logic.
2667 switch ( $type ) {
2668 case 'image':
2669 $sizes_id = $attachment->ID;
2670 $src = wp_get_attachment_image_src( $attachment->ID, 'full' );
2671 if ( $src ) {
2672 $response['url'] = $src[0];
2673 $response['width'] = $src[1];
2674 $response['height'] = $src[2];
2675 }
2676 break;
2677 case 'video':
2678 $response['width'] = acf_maybe_get( $meta, 'width', 0 );
2679 $response['height'] = acf_maybe_get( $meta, 'height', 0 );
2680 if ( $featured_id = get_post_thumbnail_id( $attachment->ID ) ) {
2681 $sizes_id = $featured_id;
2682 }
2683 break;
2684 case 'audio':
2685 if ( $featured_id = get_post_thumbnail_id( $attachment->ID ) ) {
2686 $sizes_id = $featured_id;
2687 }
2688 break;
2689 }
2690
2691 // Load array of image sizes.
2692 if ( $sizes_id ) {
2693 $sizes = get_intermediate_image_sizes();
2694 $sizes_data = array();
2695 foreach ( $sizes as $size ) {
2696 $src = wp_get_attachment_image_src( $sizes_id, $size );
2697 if ( $src ) {
2698 $sizes_data[ $size ] = $src[0];
2699 $sizes_data[ $size . '-width' ] = $src[1];
2700 $sizes_data[ $size . '-height' ] = $src[2];
2701 }
2702 }
2703 $response['sizes'] = $sizes_data;
2704 }
2705
2706 /**
2707 * Filters the attachment $response after it has been loaded.
2708 *
2709 * @since ACF 5.9.0
2710 *
2711 * @param array $response Array of loaded attachment data.
2712 * @param WP_Post $attachment Attachment object.
2713 * @param array|false $meta Array of attachment meta data, or false if there is none.
2714 */
2715 return apply_filters( 'acf/load_attachment', $response, $attachment, $meta );
2716 }
2717
2718 /**
2719 * This function will truncate and return a string
2720 *
2721 * @since ACF 5.0.0
2722 *
2723 * @param string $text The text to truncate.
2724 * @param integer $length The number of characters to allow in the string.
2725 *
2726 * @return string
2727 */
2728 function acf_get_truncated( $text, $length = 64 ) {
2729 $text = trim( $text );
2730 $the_length = function_exists( 'mb_strlen' ) ? mb_strlen( $text ) : strlen( $text );
2731
2732 $cut_length = $length - 3;
2733 $return = function_exists( 'mb_substr' ) ? mb_substr( $text, 0, $cut_length ) : substr( $text, 0, $cut_length );
2734
2735 if ( $the_length > $cut_length ) {
2736 $return .= '...';
2737 }
2738
2739 return $return;
2740 }
2741
2742 /**
2743 * acf_current_user_can_admin
2744 *
2745 * This function will return true if the current user can administrate the ACF field groups
2746 *
2747 * @since ACF 5.1.5
2748 *
2749 * @param $post_id (int)
2750 * @return $post_id (int)
2751 */
2752 function acf_current_user_can_admin() {
2753
2754 if ( acf_get_setting( 'show_admin' ) && current_user_can( acf_get_setting( 'capability' ) ) ) {
2755 return true;
2756 }
2757
2758 // return
2759 return false;
2760 }
2761
2762 /**
2763 * Checks if the current user has the SCF capability for programmatic access, without considering show_admin setting.
2764 *
2765 * @since 6.6.0
2766 * @return bool True if the user has the ACF capability.
2767 */
2768 function scf_current_user_has_capability() {
2769 return current_user_can( acf_get_setting( 'capability' ) );
2770 }
2771
2772 /**
2773 * Casts a numeric value to an integer, returning 0 for floats that cannot
2774 * be represented as an integer (NAN or outside the integer range). Casting
2775 * such floats directly raises a deprecation notice on PHP 8.5+.
2776 *
2777 * @since 6.9.1
2778 *
2779 * @param mixed $value A numeric value (int, float, or numeric string).
2780 * @return integer
2781 */
2782 function scf_numeric_to_int( $value ) {
2783 if ( is_float( $value ) && ( is_nan( $value ) || $value < (float) PHP_INT_MIN || $value >= (float) PHP_INT_MAX ) ) {
2784 return 0;
2785 }
2786 return (int) $value;
2787 }
2788
2789 /**
2790 * Wrapper function for current_user_can( 'edit_post', $post_id ).
2791 *
2792 * @since ACF 6.3.4
2793 *
2794 * @param integer $post_id The post ID to check.
2795 * @return boolean
2796 */
2797 function acf_current_user_can_edit_post( int $post_id ): bool {
2798 /**
2799 * The `edit_post` capability is a meta capability, which
2800 * gets converted to the correct post type object `edit_post`
2801 * equivalent.
2802 *
2803 * If the post type does not have `map_meta_cap` enabled and the user is
2804 * not manually mapping the `edit_post` capability, this will fail
2805 * unless the role has the `edit_post` capability added to a user/role.
2806 *
2807 * However, more (core) stuff will likely break in this scenario.
2808 */
2809 $user_can_edit = current_user_can( 'edit_post', $post_id );
2810
2811 return (bool) apply_filters( 'acf/current_user_can_edit_post', $user_can_edit, $post_id );
2812 }
2813
2814
2815 /**
2816 * Checks if the current user can edit a given ACF context.
2817 *
2818 * Handles post, user, term, comment, woo_order, block, and option contexts returned by acf_decode_post_id().
2819 *
2820 * @since 6.7.2
2821 *
2822 * @param array $post_id_info The result of acf_decode_post_id(), containing 'type' and 'id'.
2823 * @param string $options_page_slug Optional. The options page menu slug, used to look up the page's capability.
2824 * @return boolean
2825 */
2826 function acf_current_user_can_edit_in_context( array $post_id_info, string $options_page_slug = '' ): bool {
2827 $type = $post_id_info['type'] ?? '';
2828 $id = $post_id_info['id'] ?? 0;
2829
2830 switch ( $type ) {
2831 case 'post':
2832 return acf_current_user_can_edit_post( (int) $id );
2833
2834 case 'user':
2835 return current_user_can( 'edit_user', (int) $id );
2836
2837 case 'term':
2838 return current_user_can( 'edit_term', (int) $id );
2839
2840 case 'comment':
2841 return current_user_can( 'edit_comment', (int) $id );
2842
2843 case 'woo_order':
2844 return current_user_can( 'edit_shop_orders' ); // phpcs:ignore
2845
2846 case 'block':
2847 return current_user_can( 'edit_posts' );
2848
2849 case 'option':
2850 if ( ! empty( $options_page_slug ) && function_exists( 'acf_get_options_page' ) ) {
2851 $page = acf_get_options_page( $options_page_slug );
2852
2853 if ( ! empty( $page['capability'] ) && ! empty( $page['post_id'] ) ) {
2854 // Ensure the page's post_id matches the requested post_id.
2855 if ( acf_get_valid_post_id( $page['post_id'] ) !== $id ) {
2856 return false;
2857 }
2858
2859 return current_user_can( $page['capability'] );
2860 }
2861 }
2862
2863 return current_user_can( 'manage_options' );
2864
2865 default:
2866 return (bool) apply_filters( 'acf/current_user_can_edit_in_context', false, $post_id_info );
2867 }
2868 }
2869
2870 /**
2871 * acf_get_filesize
2872 *
2873 * This function will return a numeric value of bytes for a given filesize string
2874 *
2875 * @since ACF 5.1.5
2876 *
2877 * @param $size (mixed)
2878 * @return (int)
2879 */
2880 function acf_get_filesize( $size = 1 ) {
2881
2882 // vars
2883 $unit = 'MB';
2884 $units = array(
2885 'TB' => 4,
2886 'GB' => 3,
2887 'MB' => 2,
2888 'KB' => 1,
2889 );
2890
2891 // look for $unit within the $size parameter (123 KB)
2892 if ( is_string( $size ) ) {
2893
2894 // vars
2895 $custom = strtoupper( substr( $size, -2 ) );
2896
2897 foreach ( $units as $k => $v ) {
2898 if ( $custom === $k ) {
2899 $unit = $k;
2900 $size = substr( $size, 0, -2 );
2901 }
2902 }
2903 }
2904
2905 // calc bytes
2906 $bytes = floatval( $size ) * pow( 1024, $units[ $unit ] );
2907
2908 // return
2909 return $bytes;
2910 }
2911
2912 /**
2913 * acf_format_filesize
2914 *
2915 * This function will return a formatted string containing the filesize and unit
2916 *
2917 * @since ACF 5.1.5
2918 *
2919 * @param $size (mixed)
2920 * @return (int)
2921 */
2922 function acf_format_filesize( $size = 1 ) {
2923
2924 // convert
2925 $bytes = acf_get_filesize( $size );
2926
2927 // vars
2928 $units = array(
2929 'TB' => 4,
2930 'GB' => 3,
2931 'MB' => 2,
2932 'KB' => 1,
2933 );
2934
2935 // loop through units
2936 foreach ( $units as $k => $v ) {
2937 $result = $bytes / pow( 1024, $v );
2938
2939 if ( $result >= 1 ) {
2940 return $result . ' ' . $k;
2941 }
2942 }
2943
2944 // return
2945 return $bytes . ' B';
2946 }
2947
2948 /**
2949 * acf_get_valid_terms
2950 *
2951 * This function will replace old terms with new split term ids
2952 *
2953 * @since ACF 5.1.5
2954 *
2955 * @param $terms (int|array)
2956 * @param $taxonomy (string)
2957 * @return $terms
2958 */
2959 function acf_get_valid_terms( $terms = false, $taxonomy = 'category' ) {
2960
2961 // force into array
2962 $terms = acf_get_array( $terms );
2963
2964 // force ints
2965 $terms = array_map( 'intval', $terms );
2966
2967 // bail early if function does not yet exist or
2968 if ( ! function_exists( 'wp_get_split_term' ) || empty( $terms ) ) {
2969 return $terms;
2970 }
2971
2972 // attempt to find new terms
2973 foreach ( $terms as $i => $term_id ) {
2974 $new_term_id = wp_get_split_term( $term_id, $taxonomy );
2975
2976 if ( $new_term_id ) {
2977 $terms[ $i ] = $new_term_id;
2978 }
2979 }
2980
2981 // return
2982 return $terms;
2983 }
2984
2985 /**
2986 * acf_validate_attachment
2987 *
2988 * This function will validate an attachment based on a field's restrictions and return an array of errors
2989 *
2990 * @since ACF 5.2.3
2991 *
2992 * @param array $attachment attachment data. Changes based on context.
2993 * @param array $field field settings containing restrictions.
2994 * @param string $context context is different when uploading / preparing.
2995 * @return $errors (array)
2996 */
2997 function acf_validate_attachment( $attachment, $field, $context = 'prepare' ) {
2998
2999 // vars
3000 $errors = array();
3001 $file = array(
3002 'type' => '',
3003 'width' => 0,
3004 'height' => 0,
3005 'size' => 0,
3006 );
3007
3008 // upload
3009 if ( $context == 'upload' ) {
3010
3011 // vars
3012 $file['type'] = pathinfo( $attachment['name'], PATHINFO_EXTENSION );
3013 $file['size'] = filesize( $attachment['tmp_name'] );
3014
3015 if ( strpos( $attachment['type'], 'image' ) !== false ) {
3016 $size = getimagesize( $attachment['tmp_name'] );
3017 $file['width'] = acf_maybe_get( $size, 0 );
3018 $file['height'] = acf_maybe_get( $size, 1 );
3019 }
3020
3021 // prepare
3022 } elseif ( $context == 'prepare' ) {
3023 $use_path = isset( $attachment['filename'] ) ? $attachment['filename'] : $attachment['url'];
3024 $file['type'] = pathinfo( $use_path, PATHINFO_EXTENSION );
3025 $file['size'] = acf_maybe_get( $attachment, 'filesizeInBytes', 0 );
3026 $file['width'] = acf_maybe_get( $attachment, 'width', 0 );
3027 $file['height'] = acf_maybe_get( $attachment, 'height', 0 );
3028
3029 // custom
3030 } else {
3031 $file = array_merge( $file, $attachment );
3032 $use_path = isset( $attachment['filename'] ) ? $attachment['filename'] : $attachment['url'];
3033 $file['type'] = pathinfo( $use_path, PATHINFO_EXTENSION );
3034 }
3035
3036 // image
3037 if ( $file['width'] || $file['height'] ) {
3038
3039 // width
3040 $min_width = (int) acf_maybe_get( $field, 'min_width', 0 );
3041 $max_width = (int) acf_maybe_get( $field, 'max_width', 0 );
3042
3043 if ( $file['width'] ) {
3044 if ( $min_width && $file['width'] < $min_width ) {
3045
3046 // min width
3047 /* translators: 1: image width */
3048 $errors['min_width'] = sprintf( __( 'Image width must be at least %dpx.', 'secure-custom-fields' ), $min_width );
3049 } elseif ( $max_width && $file['width'] > $max_width ) {
3050
3051 // min width
3052 /* translators: 1: image width */
3053 $errors['max_width'] = sprintf( __( 'Image width must not exceed %dpx.', 'secure-custom-fields' ), $max_width );
3054 }
3055 }
3056
3057 // height
3058 $min_height = (int) acf_maybe_get( $field, 'min_height', 0 );
3059 $max_height = (int) acf_maybe_get( $field, 'max_height', 0 );
3060
3061 if ( $file['height'] ) {
3062 if ( $min_height && $file['height'] < $min_height ) {
3063
3064 // min height
3065 /* translators: 1: image height */
3066 $errors['min_height'] = sprintf( __( 'Image height must be at least %dpx.', 'secure-custom-fields' ), $min_height );
3067 } elseif ( $max_height && $file['height'] > $max_height ) {
3068
3069 // min height
3070 /* translators: 1: image height */
3071 $errors['max_height'] = sprintf( __( 'Image height must not exceed %dpx.', 'secure-custom-fields' ), $max_height );
3072 }
3073 }
3074 }
3075
3076 // file size
3077 if ( $file['size'] ) {
3078 $min_size = acf_maybe_get( $field, 'min_size', 0 );
3079 $max_size = acf_maybe_get( $field, 'max_size', 0 );
3080
3081 if ( $min_size && $file['size'] < acf_get_filesize( $min_size ) ) {
3082
3083 // min width
3084 /* translators: 1: file size */
3085 $errors['min_size'] = sprintf( __( 'File size must be at least %s.', 'secure-custom-fields' ), acf_format_filesize( $min_size ) );
3086 } elseif ( $max_size && $file['size'] > acf_get_filesize( $max_size ) ) {
3087
3088 // min width
3089 /* translators: 1: file size */
3090 $errors['max_size'] = sprintf( __( 'File size must not exceed %s.', 'secure-custom-fields' ), acf_format_filesize( $max_size ) );
3091 }
3092 }
3093
3094 // file type
3095 if ( $file['type'] ) {
3096 $mime_types = acf_maybe_get( $field, 'mime_types', '' );
3097
3098 // lower case
3099 $file['type'] = strtolower( $file['type'] );
3100 $mime_types = strtolower( $mime_types );
3101
3102 // explode
3103 $mime_types = str_replace( array( ' ', '.' ), '', $mime_types );
3104 $mime_types = explode( ',', $mime_types ); // split pieces
3105 $mime_types = array_filter( $mime_types ); // remove empty pieces
3106
3107 if ( ! empty( $mime_types ) && ! in_array( $file['type'], $mime_types ) ) {
3108
3109 // glue together last 2 types
3110 if ( count( $mime_types ) > 1 ) {
3111 $last1 = array_pop( $mime_types );
3112 $last2 = array_pop( $mime_types );
3113
3114 $mime_types[] = $last2 . ' ' . __( 'or', 'secure-custom-fields' ) . ' ' . $last1;
3115 }
3116 /* translators: 1: file type(s) */
3117 $errors['mime_types'] = sprintf( __( 'File type must be %s.', 'secure-custom-fields' ), implode( ', ', $mime_types ) );
3118 }
3119 }
3120
3121 /**
3122 * Filters the errors for a file before it is uploaded or displayed in the media modal.
3123 *
3124 * @since ACF 5.2.3
3125 *
3126 * @param array $errors An array of errors.
3127 * @param array $file An array of data for a single file.
3128 * @param array $attachment An array of attachment data which differs based on the context.
3129 * @param array $field The field array.
3130 * @param string $context The current context (uploading, preparing)
3131 */
3132 $errors = apply_filters( "acf/validate_attachment/type={$field['type']}", $errors, $file, $attachment, $field, $context );
3133 $errors = apply_filters( "acf/validate_attachment/name={$field['_name']}", $errors, $file, $attachment, $field, $context );
3134 $errors = apply_filters( "acf/validate_attachment/key={$field['key']}", $errors, $file, $attachment, $field, $context );
3135 $errors = apply_filters( 'acf/validate_attachment', $errors, $file, $attachment, $field, $context );
3136
3137 // return
3138 return $errors;
3139 }
3140
3141 /**
3142 * _acf_settings_uploader
3143 *
3144 * Dynamic logic for uploader setting
3145 *
3146 * @since ACF 5.2.3
3147 *
3148 * @param $uploader (string)
3149 * @return $uploader
3150 */
3151
3152 add_filter( 'acf/settings/uploader', '_acf_settings_uploader' );
3153
3154 function _acf_settings_uploader( $uploader ) {
3155
3156 // if can't upload files
3157 if ( ! current_user_can( 'upload_files' ) ) {
3158 $uploader = 'basic';
3159 }
3160
3161 // return
3162 return $uploader;
3163 }
3164
3165 /**
3166 * acf_translate
3167 *
3168 * This function will translate a string using the new 'l10n_textdomain' setting
3169 * Also works for arrays which is great for fields - select -> choices
3170 *
3171 * @since ACF 5.3.2
3172 *
3173 * @param mixed $string String or array containing strings to be translated.
3174 * @return mixed
3175 */
3176 function acf_translate( $string ) {
3177
3178 // vars
3179 $l10n = acf_get_setting( 'l10n' );
3180 $textdomain = acf_get_setting( 'l10n_textdomain' );
3181
3182 // bail early if not enabled
3183 if ( ! $l10n ) {
3184 return $string;
3185 }
3186
3187 // bail early if no textdomain
3188 if ( ! $textdomain ) {
3189 return $string;
3190 }
3191
3192 // is array
3193 if ( is_array( $string ) ) {
3194 return array_map( 'acf_translate', $string );
3195 }
3196
3197 // bail early if empty
3198 if ( '' === $string ) {
3199 return $string;
3200 }
3201
3202 if ( acf_get_setting( 'l10n_var_export' ) ) {
3203 return "!!__(!!'{$string}!!', !!'{$textdomain}!!')!!";
3204 }
3205
3206 // translate
3207 return __( $string, $textdomain );
3208 }
3209
3210 /**
3211 * acf_maybe_add_action
3212 *
3213 * This function will determine if the action has already run before adding / calling the function
3214 *
3215 * @since ACF 5.3.2
3216 *
3217 * @param $post_id (int)
3218 * @return $post_id (int)
3219 */
3220 function acf_maybe_add_action( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
3221
3222 // if action has already run, execute it
3223 // - if currently doing action, allow $tag to be added as per usual to allow $priority ordering needed for 3rd party asset compatibility
3224 if ( did_action( $tag ) && ! doing_action( $tag ) ) {
3225 call_user_func( $function_to_add );
3226
3227 // if action has not yet run, add it
3228 } else {
3229 add_action( $tag, $function_to_add, $priority, $accepted_args );
3230 }
3231 }
3232
3233 /**
3234 * acf_is_row_collapsed
3235 *
3236 * This function will return true if the field's row is collapsed
3237 *
3238 * @since ACF 5.3.2
3239 *
3240 * @param $post_id (int)
3241 * @return $post_id (int)
3242 */
3243 function acf_is_row_collapsed( $field_key = '', $row_index = 0 ) {
3244
3245 // collapsed
3246 $collapsed = acf_get_user_setting( 'collapsed_' . $field_key, '' );
3247
3248 // cookie fallback ( version < 5.3.2 )
3249 if ( $collapsed === '' ) {
3250 $collapsed = acf_extract_var( $_COOKIE, "acf_collapsed_{$field_key}", '' );
3251 $collapsed = str_replace( '|', ',', $collapsed );
3252
3253 // update
3254 acf_update_user_setting( 'collapsed_' . $field_key, $collapsed );
3255 }
3256
3257 // explode
3258 $collapsed = explode( ',', $collapsed );
3259 $collapsed = array_filter( $collapsed, 'is_numeric' );
3260
3261 // collapsed class
3262 return in_array( $row_index, $collapsed );
3263 }
3264
3265 /**
3266 * Return an image tag for the provided attachment ID
3267 *
3268 * @since ACF 5.5.0
3269 * @deprecated 6.3.2
3270 *
3271 * @param integer $attachment_id The attachment ID
3272 * @param string $size The image size to use in the image tag.
3273 * @return false
3274 */
3275 function acf_get_attachment_image( $attachment_id = 0, $size = 'thumbnail' ) {
3276 // report function as deprecated
3277 _deprecated_function( __FUNCTION__, '6.3.2' );
3278 return false;
3279 }
3280
3281 /**
3282 * acf_get_post_thumbnail
3283 *
3284 * This function will return a thumbnail image url for a given post
3285 *
3286 * @since ACF 5.3.8
3287 *
3288 * @param $post (obj)
3289 * @param $size (mixed)
3290 * @return (string)
3291 */
3292 function acf_get_post_thumbnail( $post = null, $size = 'thumbnail' ) {
3293
3294 // vars
3295 $data = array(
3296 'url' => '',
3297 'type' => '',
3298 'html' => '',
3299 );
3300
3301 // post
3302 $post = get_post( $post );
3303
3304 // bail early if no post
3305 if ( ! $post ) {
3306 return $data;
3307 }
3308
3309 // vars
3310 $thumb_id = $post->ID;
3311 $mime_type = acf_maybe_get( explode( '/', $post->post_mime_type ), 0 );
3312
3313 // attachment
3314 if ( $post->post_type === 'attachment' ) {
3315
3316 // change $thumb_id
3317 if ( $mime_type === 'audio' || $mime_type === 'video' ) {
3318 $thumb_id = get_post_thumbnail_id( $post->ID );
3319 }
3320
3321 // post
3322 } else {
3323 $thumb_id = get_post_thumbnail_id( $post->ID );
3324 }
3325
3326 // try url
3327 $data['url'] = wp_get_attachment_image_src( $thumb_id, $size );
3328 $data['url'] = acf_maybe_get( $data['url'], 0 );
3329
3330 // default icon
3331 if ( ! $data['url'] && $post->post_type === 'attachment' ) {
3332 $data['url'] = wp_mime_type_icon( $post->ID );
3333 $data['type'] = 'icon';
3334 }
3335
3336 // html
3337 $data['html'] = '<img src="' . $data['url'] . '" alt="" />';
3338
3339 // return
3340 return $data;
3341 }
3342
3343 /**
3344 * acf_get_browser
3345 *
3346 * Returns the name of the current browser.
3347 *
3348 * @since ACF 5.0.0
3349 *
3350 * @return string
3351 */
3352 function acf_get_browser() {
3353
3354 // Check server var.
3355 if ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
3356 $agent = sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] );
3357
3358 // Loop over search terms.
3359 $browsers = array(
3360 'Firefox' => 'firefox',
3361 'Trident' => 'msie',
3362 'MSIE' => 'msie',
3363 'Edge' => 'edge',
3364 'Chrome' => 'chrome',
3365 'Safari' => 'safari',
3366 );
3367 foreach ( $browsers as $k => $v ) {
3368 if ( strpos( $agent, $k ) !== false ) {
3369 return $v;
3370 }
3371 }
3372 }
3373
3374 // Return default.
3375 return '';
3376 }
3377
3378 /**
3379 * acf_is_ajax
3380 *
3381 * This function will return true if performing a wp ajax call
3382 *
3383 * @since ACF 5.3.8
3384 *
3385 * @param n/a
3386 * @return (boolean)
3387 */
3388 function acf_is_ajax( $action = '' ) {
3389
3390 // vars
3391 $is_ajax = false;
3392
3393 // check if is doing ajax
3394 if ( wp_doing_ajax() ) {
3395 $is_ajax = true;
3396 }
3397
3398 // phpcs:disable WordPress.Security.NonceVerification.Missing
3399 // check $action
3400 if ( $action && acf_maybe_get( $_POST, 'action' ) !== $action ) {
3401 // phpcs:enable WordPress.Security.NonceVerification.Missing
3402 $is_ajax = false;
3403 }
3404
3405 // return
3406 return $is_ajax;
3407 }
3408
3409 /**
3410 * Returns a date value in a formatted string.
3411 *
3412 * @since ACF 5.3.8
3413 *
3414 * @param string $value The date value to format.
3415 * @param string $format The format to use.
3416 * @return string
3417 */
3418 function acf_format_date( $value, $format ) {
3419 // Bail early if no value or value is not what we expect.
3420 if ( ! $value || ( ! is_string( $value ) && ! is_int( $value ) ) ) {
3421 return $value;
3422 }
3423
3424 // Numeric (either unix or YYYYMMDD).
3425 if ( is_numeric( $value ) && strlen( $value ) !== 8 ) {
3426 $unixtimestamp = $value;
3427 } else {
3428 $unixtimestamp = strtotime( $value );
3429 }
3430
3431 return date_i18n( $format, $unixtimestamp );
3432 }
3433
3434 /**
3435 * Previously, deletes the debug.log file.
3436 *
3437 * @since ACF 5.7.10
3438 * @deprecated 6.2.7
3439 */
3440 function acf_clear_log() {
3441 _deprecated_function( __FUNCTION__, '6.2.7' );
3442 return false;
3443 }
3444
3445 /**
3446 * acf_log
3447 *
3448 * description
3449 *
3450 * @since ACF 5.3.8
3451 *
3452 * @param $post_id (int)
3453 * @return $post_id (int)
3454 */
3455 function acf_log() {
3456
3457 // vars
3458 $args = func_get_args();
3459
3460 // loop
3461 foreach ( $args as $i => $arg ) {
3462
3463 // array | object
3464 if ( is_array( $arg ) || is_object( $arg ) ) {
3465 $arg = print_r( $arg, true );
3466
3467 // bool
3468 } elseif ( is_bool( $arg ) ) {
3469 $arg = 'bool(' . ( $arg ? 'true' : 'false' ) . ')';
3470 }
3471
3472 // update
3473 $args[ $i ] = $arg;
3474 }
3475
3476 // log
3477 error_log( implode( ' ', $args ) );
3478 }
3479
3480 /**
3481 * acf_dev_log
3482 *
3483 * Used to log variables only if ACF_DEV is defined
3484 *
3485 * @since ACF 5.7.4
3486 *
3487 * @param mixed
3488 * @return void
3489 */
3490 function acf_dev_log() {
3491 if ( defined( 'ACF_DEV' ) && ACF_DEV ) {
3492 call_user_func_array( 'acf_log', func_get_args() );
3493 }
3494 }
3495
3496 /**
3497 * acf_doing
3498 *
3499 * This function will tell ACF what task it is doing
3500 *
3501 * @since ACF 5.3.8
3502 *
3503 * @param $event (string)
3504 * @param context (string)
3505 * @return n/a
3506 */
3507 function acf_doing( $event = '', $context = '' ) {
3508
3509 acf_update_setting( 'doing', $event );
3510 acf_update_setting( 'doing_context', $context );
3511 }
3512
3513 /**
3514 * acf_is_doing
3515 *
3516 * This function can be used to state what ACF is doing, or to check
3517 *
3518 * @since ACF 5.3.8
3519 *
3520 * @param $event (string)
3521 * @param context (string)
3522 * @return (boolean)
3523 */
3524 function acf_is_doing( $event = '', $context = '' ) {
3525
3526 // vars
3527 $doing = false;
3528
3529 // task
3530 if ( acf_get_setting( 'doing' ) === $event ) {
3531 $doing = true;
3532 }
3533
3534 // context
3535 if ( $context && acf_get_setting( 'doing_context' ) !== $context ) {
3536 $doing = false;
3537 }
3538
3539 // return
3540 return $doing;
3541 }
3542
3543 /**
3544 * acf_is_plugin_active
3545 *
3546 * This function will return true if the ACF plugin is active
3547 * - May be included within a theme or other plugin
3548 *
3549 * @since ACF 5.4.0
3550 *
3551 * @param $basename (int)
3552 * @return $post_id (int)
3553 */
3554 function acf_is_plugin_active() {
3555
3556 // vars
3557 $basename = acf_get_setting( 'basename' );
3558
3559 // ensure is_plugin_active() exists (not on frontend)
3560 if ( ! function_exists( 'is_plugin_active' ) ) {
3561 include_once ABSPATH . 'wp-admin/includes/plugin.php';
3562 }
3563
3564 // return
3565 return is_plugin_active( $basename );
3566 }
3567
3568 /**
3569 * acf_send_ajax_results
3570 *
3571 * This function will print JSON data for a Select2 AJAX query
3572 *
3573 * @since ACF 5.4.0
3574 *
3575 * @param $response (array)
3576 * @return n/a
3577 */
3578 function acf_send_ajax_results( $response ) {
3579
3580 // validate
3581 $response = wp_parse_args(
3582 $response,
3583 array(
3584 'results' => array(),
3585 'more' => false,
3586 'limit' => 0,
3587 )
3588 );
3589
3590 // limit
3591 if ( $response['limit'] && $response['results'] ) {
3592
3593 // vars
3594 $total = 0;
3595
3596 foreach ( $response['results'] as $result ) {
3597
3598 // parent
3599 ++$total;
3600
3601 // children
3602 if ( ! empty( $result['children'] ) ) {
3603 $total += count( $result['children'] );
3604 }
3605 }
3606
3607 // calc
3608 if ( $total >= $response['limit'] ) {
3609 $response['more'] = true;
3610 }
3611 }
3612
3613 // return
3614 wp_send_json( $response );
3615 }
3616
3617 /**
3618 * acf_is_sequential_array
3619 *
3620 * This function will return true if the array contains only numeric keys
3621 *
3622 * @source http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential
3623 *
3624 * @since ACF 5.4.0
3625 *
3626 * @param $array (array)
3627 * @return (boolean)
3628 */
3629 function acf_is_sequential_array( $array ) {
3630
3631 // bail early if not array
3632 if ( ! is_array( $array ) ) {
3633 return false;
3634 }
3635
3636 // loop
3637 foreach ( $array as $key => $value ) {
3638
3639 // bail early if is string
3640 if ( is_string( $key ) ) {
3641 return false;
3642 }
3643 }
3644
3645 // return
3646 return true;
3647 }
3648
3649 /**
3650 * acf_is_associative_array
3651 *
3652 * This function will return true if the array contains one or more string keys
3653 *
3654 * @source http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential
3655 *
3656 * @since ACF 5.4.0
3657 *
3658 * @param $array (array)
3659 * @return (boolean)
3660 */
3661 function acf_is_associative_array( $array ) {
3662
3663 // bail early if not array
3664 if ( ! is_array( $array ) ) {
3665 return false;
3666 }
3667
3668 // loop
3669 foreach ( $array as $key => $value ) {
3670
3671 // bail early if is string
3672 if ( is_string( $key ) ) {
3673 return true;
3674 }
3675 }
3676
3677 // return
3678 return false;
3679 }
3680
3681 /**
3682 * acf_add_array_key_prefix
3683 *
3684 * This function will add a prefix to all array keys
3685 * Useful to preserve numeric keys when performing array_multisort
3686 *
3687 * @since ACF 5.4.0
3688 *
3689 * @param $array (array)
3690 * @param $prefix (string)
3691 * @return (array)
3692 */
3693 function acf_add_array_key_prefix( $array, $prefix ) {
3694
3695 // vars
3696 $array2 = array();
3697
3698 // loop
3699 foreach ( $array as $k => $v ) {
3700 $k2 = $prefix . $k;
3701 $array2[ $k2 ] = $v;
3702 }
3703
3704 // return
3705 return $array2;
3706 }
3707
3708 /**
3709 * acf_remove_array_key_prefix
3710 *
3711 * This function will remove a prefix to all array keys
3712 * Useful to preserve numeric keys when performing array_multisort
3713 *
3714 * @since ACF 5.4.0
3715 *
3716 * @param $array (array)
3717 * @param $prefix (string)
3718 * @return (array)
3719 */
3720 function acf_remove_array_key_prefix( $array, $prefix ) {
3721
3722 // vars
3723 $array2 = array();
3724 $l = strlen( $prefix );
3725
3726 // loop
3727 foreach ( $array as $k => $v ) {
3728 $k2 = ( substr( $k, 0, $l ) === $prefix ) ? substr( $k, $l ) : $k;
3729 $array2[ $k2 ] = $v;
3730 }
3731
3732 // return
3733 return $array2;
3734 }
3735
3736 /**
3737 * This function will connect an attachment (image etc) to the post
3738 * Used to connect attachments uploaded directly to media that have not been attached to a post
3739 *
3740 * @since ACF 5.8.0 Added filter to prevent connection.
3741 * @since ACF 5.5.4
3742 *
3743 * @param integer $attachment_id The attachment ID.
3744 * @param integer $post_id The post ID.
3745 * @return boolean True if attachment was connected.
3746 */
3747 function acf_connect_attachment_to_post( $attachment_id = 0, $post_id = 0 ) {
3748
3749 // bail early if $attachment_id is not valid.
3750 if ( ! $attachment_id || ! is_numeric( $attachment_id ) ) {
3751 return false;
3752 }
3753
3754 // bail early if $post_id is not valid.
3755 if ( ! $post_id || ! is_numeric( $post_id ) ) {
3756 return false;
3757 }
3758
3759 /**
3760 * Filters whether or not to connect the attachment.
3761 *
3762 * @since ACF 5.8.0
3763 *
3764 * @param bool $bool Returning false will prevent the connection. Default true.
3765 * @param int $attachment_id The attachment ID.
3766 * @param int $post_id The post ID.
3767 */
3768 if ( ! apply_filters( 'acf/connect_attachment_to_post', true, $attachment_id, $post_id ) ) {
3769 return false;
3770 }
3771
3772 // vars
3773 $post = get_post( $attachment_id );
3774
3775 // Check if is valid post.
3776 if ( $post && $post->post_type == 'attachment' && $post->post_parent == 0 ) {
3777
3778 // update
3779 wp_update_post(
3780 array(
3781 'ID' => $post->ID,
3782 'post_parent' => $post_id,
3783 )
3784 );
3785
3786 // return
3787 return true;
3788 }
3789
3790 // return
3791 return true;
3792 }
3793
3794 /**
3795 * acf_encrypt
3796 *
3797 * This function will encrypt a string using PHP
3798 * https://bhoover.com/using-php-openssl_encrypt-openssl_decrypt-encrypt-decrypt-data/
3799 *
3800 * @since ACF 5.5.8
3801 *
3802 * @param $data (string)
3803 * @return (string)
3804 */
3805 function acf_encrypt( $data = '' ) {
3806
3807 // bail early if no encrypt function
3808 if ( ! function_exists( 'openssl_encrypt' ) ) {
3809 return base64_encode( $data );
3810 }
3811
3812 // generate a key
3813 $key = wp_hash( 'acf_encrypt' );
3814
3815 // Generate an initialization vector
3816 $iv = openssl_random_pseudo_bytes( openssl_cipher_iv_length( 'aes-256-cbc' ) );
3817
3818 // Encrypt the data using AES 256 encryption in CBC mode using our encryption key and initialization vector.
3819 $encrypted_data = openssl_encrypt( $data, 'aes-256-cbc', $key, 0, $iv );
3820
3821 // The $iv is just as important as the key for decrypting, so save it with our encrypted data using a unique separator (::)
3822 return base64_encode( $encrypted_data . '::' . $iv );
3823 }
3824
3825 /**
3826 * Decrypts an encrypted string using PHP.
3827 * https://bhoover.com/using-php-openssl_encrypt-openssl_decrypt-encrypt-decrypt-data/
3828 *
3829 * @since ACF 5.5.8
3830 *
3831 * @param string $data The string to decrypt.
3832 * @return string|false Decrypted string, or false if the payload is malformed or decryption fails.
3833 */
3834 function acf_decrypt( $data = '' ) {
3835 // bail early if no decrypt function
3836 if ( ! function_exists( 'openssl_decrypt' ) ) {
3837 return base64_decode( (string) $data ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode -- Decoding our own encrypted payload.
3838 }
3839
3840 // Treat malformed input as a decrypt failure: list() destructuring below would
3841 // otherwise warn on PHP 8 when the payload isn't the "base64(data::iv)" shape.
3842 $raw = base64_decode( (string) $data, true ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode -- Decoding our own encrypted payload.
3843 if ( false === $raw || strpos( $raw, '::' ) === false ) {
3844 return false;
3845 }
3846
3847 // generate a key
3848 $key = wp_hash( 'acf_encrypt' );
3849
3850 // To decrypt, split the encrypted data from our IV - our unique separator used was "::"
3851 list( $encrypted_data, $iv ) = explode( '::', $raw, 2 );
3852
3853 // decrypt
3854 return openssl_decrypt( $encrypted_data, 'aes-256-cbc', $key, 0, $iv );
3855 }
3856
3857 /**
3858 * acf_parse_markdown
3859 *
3860 * A very basic regex-based Markdown parser function based off [slimdown](https://gist.github.com/jbroadway/2836900).
3861 *
3862 * @since ACF 5.7.2
3863 *
3864 * @param string $text The string to parse.
3865 * @return string
3866 */
3867 function acf_parse_markdown( $text = '' ) {
3868
3869 // trim
3870 $text = trim( $text );
3871
3872 // rules
3873 $rules = array(
3874 '/=== (.+?) ===/' => '<h2>$1</h2>', // headings
3875 '/== (.+?) ==/' => '<h3>$1</h3>', // headings
3876 '/= (.+?) =/' => '<h4>$1</h4>', // headings
3877 '/\[([^\[]+)\]\(([^\)]+)\)/' => '<a href="$2">$1</a>', // links
3878 '/(\*\*)(.*?)\1/' => '<strong>$2</strong>', // bold
3879 '/(\*)(.*?)\1/' => '<em>$2</em>', // italic
3880 '/`(.*?)`/' => '<code>$1</code>', // inline code
3881 '/\n\*(.*)/' => "\n<ul>\n\t<li>$1</li>\n</ul>", // ul lists
3882 '/\n[0-9]+\.(.*)/' => "\n<ol>\n\t<li>$1</li>\n</ol>", // ol lists
3883 '/<\/ul>\s?<ul>/' => '', // fix extra ul
3884 '/<\/ol>\s?<ol>/' => '', // fix extra ol
3885 );
3886 foreach ( $rules as $k => $v ) {
3887 $text = preg_replace( $k, $v, $text );
3888 }
3889
3890 // autop
3891 $text = wpautop( $text );
3892
3893 // return
3894 return $text;
3895 }
3896
3897 /**
3898 * acf_get_sites
3899 *
3900 * Returns an array of sites for a network.
3901 *
3902 * @since ACF 5.4.0
3903 *
3904 * @return array
3905 */
3906 function acf_get_sites() {
3907 $results = array();
3908 $sites = get_sites( array( 'number' => 0 ) );
3909 if ( $sites ) {
3910 foreach ( $sites as $site ) {
3911 $results[] = get_site( $site )->to_array();
3912 }
3913 }
3914 return $results;
3915 }
3916
3917 /**
3918 * acf_convert_rules_to_groups
3919 *
3920 * Converts an array of rules from ACF4 to an array of groups for ACF5
3921 *
3922 * @since ACF 5.7.4
3923 *
3924 * @param array $rules An array of rules.
3925 * @param string $anyorall The anyorall setting used in ACF4. Defaults to 'any'.
3926 * @return array
3927 */
3928 function acf_convert_rules_to_groups( $rules, $anyorall = 'any' ) {
3929
3930 // vars
3931 $groups = array();
3932 $index = 0;
3933
3934 // loop
3935 foreach ( $rules as $rule ) {
3936
3937 // extract vars
3938 $group = acf_extract_var( $rule, 'group_no' );
3939 $order = acf_extract_var( $rule, 'order_no' );
3940
3941 // calculate group if not defined
3942 if ( $group === null ) {
3943 $group = $index;
3944
3945 // use $anyorall to determine if a new group is needed
3946 if ( $anyorall == 'any' ) {
3947 ++$index;
3948 }
3949 }
3950
3951 // calculate order if not defined
3952 if ( $order === null ) {
3953 $order = isset( $groups[ $group ] ) ? count( $groups[ $group ] ) : 0;
3954 }
3955
3956 // append to group
3957 $groups[ $group ][ $order ] = $rule;
3958
3959 // sort groups
3960 ksort( $groups[ $group ] );
3961 }
3962
3963 // sort groups
3964 ksort( $groups );
3965
3966 // return
3967 return $groups;
3968 }
3969
3970 /**
3971 * acf_register_ajax
3972 *
3973 * Registers an ajax callback.
3974 *
3975 * @since ACF 5.7.7
3976 *
3977 * @param string $name The ajax action name.
3978 * @param array $callback The callback function or array.
3979 * @param boolean $public Whether to allow access to non logged in users.
3980 * @return void
3981 */
3982 function acf_register_ajax( $name = '', $callback = false, $public = false ) {
3983
3984 // vars
3985 $action = "acf/ajax/$name";
3986
3987 // add action for logged-in users
3988 add_action( "wp_ajax_$action", $callback );
3989
3990 // add action for non logged-in users
3991 if ( $public ) {
3992 add_action( "wp_ajax_nopriv_$action", $callback );
3993 }
3994 }
3995
3996 /**
3997 * acf_str_camel_case
3998 *
3999 * Converts a string into camelCase.
4000 * Thanks to https://stackoverflow.com/questions/31274782/convert-array-keys-from-underscore-case-to-camelcase-recursively
4001 *
4002 * @since ACF 5.8.0
4003 *
4004 * @param string $string The string ot convert.
4005 * @return string
4006 */
4007 function acf_str_camel_case( $string = '' ) {
4008 return lcfirst( str_replace( ' ', '', ucwords( str_replace( '_', ' ', $string ) ) ) );
4009 }
4010
4011 /**
4012 * acf_array_camel_case
4013 *
4014 * Converts all array keys to camelCase.
4015 *
4016 * @since ACF 5.8.0
4017 *
4018 * @param array $array The array to convert.
4019 * @return array
4020 */
4021 function acf_array_camel_case( $array = array() ) {
4022 $array2 = array();
4023 foreach ( $array as $k => $v ) {
4024 $array2[ acf_str_camel_case( $k ) ] = $v;
4025 }
4026 return $array2;
4027 }
4028
4029 /**
4030 * Returns true if the current screen is using the block editor.
4031 *
4032 * @since ACF 5.8.0
4033 *
4034 * @return boolean
4035 */
4036 function acf_is_block_editor() {
4037 if ( function_exists( 'get_current_screen' ) ) {
4038 $screen = get_current_screen();
4039 if ( $screen && method_exists( $screen, 'is_block_editor' ) ) {
4040 return $screen->is_block_editor();
4041 }
4042 }
4043 return false;
4044 }
4045
4046 /**
4047 * Return an array of the WordPress reserved terms
4048 *
4049 * @since ACF 6.1
4050 *
4051 * @return array The WordPress reserved terms list.
4052 */
4053 function acf_get_wp_reserved_terms() {
4054 return array( 'action', 'attachment', 'attachment_id', 'author', 'author_name', 'calendar', 'cat', 'category', 'category__and', 'category__in', 'category__not_in', 'category_name', 'comments_per_page', 'comments_popup', 'custom', 'customize_messenger_channel', 'customized', 'cpage', 'day', 'debug', 'embed', 'error', 'exact', 'feed', 'fields', 'hour', 'link', 'link_category', 'm', 'minute', 'monthnum', 'more', 'name', 'nav_menu', 'nonce', 'nopaging', 'offset', 'order', 'orderby', 'p', 'page', 'page_id', 'paged', 'pagename', 'pb', 'perm', 'post', 'post__in', 'post__not_in', 'post_format', 'post_mime_type', 'post_status', 'post_tag', 'post_type', 'posts', 'posts_per_archive_page', 'posts_per_page', 'preview', 'robots', 's', 'search', 'second', 'sentence', 'showposts', 'static', 'status', 'subpost', 'subpost_id', 'tag', 'tag__and', 'tag__in', 'tag__not_in', 'tag_id', 'tag_slug__and', 'tag_slug__in', 'taxonomy', 'tb', 'term', 'terms', 'theme', 'themes', 'title', 'type', 'types', 'w', 'withcomments', 'withoutcomments', 'year' );
4055 }
4056
4057 /**
4058 * Detect if we're on a multisite subsite.
4059 *
4060 * @since ACF 6.2.4
4061 *
4062 * @return boolean true if we're in a multisite install and not on the main site
4063 */
4064 function acf_is_multisite_sub_site() {
4065 if ( is_multisite() && ! is_main_site() ) {
4066 return true;
4067 }
4068 return false;
4069 }
4070
4071 /**
4072 * Detect if we're on a multisite main site.
4073 *
4074 * @since ACF 6.2.4
4075 *
4076 * @return boolean true if we're in a multisite install and on the main site
4077 */
4078 function acf_is_multisite_main_site() {
4079 if ( is_multisite() && is_main_site() ) {
4080 return true;
4081 }
4082 return false;
4083 }
4084