PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.8.8
Secure Custom Fields v6.8.8
6.9.2 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 1 month ago api-template.php 2 months ago api-term.php 1 year ago index.php 1 year ago
api-helpers.php
4063 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'] = (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 * Wrapper function for current_user_can( 'edit_post', $post_id ).
2774 *
2775 * @since ACF 6.3.4
2776 *
2777 * @param integer $post_id The post ID to check.
2778 * @return boolean
2779 */
2780 function acf_current_user_can_edit_post( int $post_id ): bool {
2781 /**
2782 * The `edit_post` capability is a meta capability, which
2783 * gets converted to the correct post type object `edit_post`
2784 * equivalent.
2785 *
2786 * If the post type does not have `map_meta_cap` enabled and the user is
2787 * not manually mapping the `edit_post` capability, this will fail
2788 * unless the role has the `edit_post` capability added to a user/role.
2789 *
2790 * However, more (core) stuff will likely break in this scenario.
2791 */
2792 $user_can_edit = current_user_can( 'edit_post', $post_id );
2793
2794 return (bool) apply_filters( 'acf/current_user_can_edit_post', $user_can_edit, $post_id );
2795 }
2796
2797
2798 /**
2799 * Checks if the current user can edit a given ACF context.
2800 *
2801 * Handles post, user, term, comment, woo_order, block, and option contexts returned by acf_decode_post_id().
2802 *
2803 * @since 6.7.2
2804 *
2805 * @param array $post_id_info The result of acf_decode_post_id(), containing 'type' and 'id'.
2806 * @param string $options_page_slug Optional. The options page menu slug, used to look up the page's capability.
2807 * @return boolean
2808 */
2809 function acf_current_user_can_edit_in_context( array $post_id_info, string $options_page_slug = '' ): bool {
2810 $type = $post_id_info['type'] ?? '';
2811 $id = $post_id_info['id'] ?? 0;
2812
2813 switch ( $type ) {
2814 case 'post':
2815 return acf_current_user_can_edit_post( (int) $id );
2816
2817 case 'user':
2818 return current_user_can( 'edit_user', (int) $id );
2819
2820 case 'term':
2821 return current_user_can( 'edit_term', (int) $id );
2822
2823 case 'comment':
2824 return current_user_can( 'edit_comment', (int) $id );
2825
2826 case 'woo_order':
2827 return current_user_can( 'edit_shop_orders' ); // phpcs:ignore
2828
2829 case 'block':
2830 return current_user_can( 'edit_posts' );
2831
2832 case 'option':
2833 if ( ! empty( $options_page_slug ) && function_exists( 'acf_get_options_page' ) ) {
2834 $page = acf_get_options_page( $options_page_slug );
2835
2836 if ( ! empty( $page['capability'] ) && ! empty( $page['post_id'] ) ) {
2837 // Ensure the page's post_id matches the requested post_id.
2838 if ( acf_get_valid_post_id( $page['post_id'] ) !== $id ) {
2839 return false;
2840 }
2841
2842 return current_user_can( $page['capability'] );
2843 }
2844 }
2845
2846 return current_user_can( 'manage_options' );
2847
2848 default:
2849 return (bool) apply_filters( 'acf/current_user_can_edit_in_context', false, $post_id_info );
2850 }
2851 }
2852
2853 /**
2854 * acf_get_filesize
2855 *
2856 * This function will return a numeric value of bytes for a given filesize string
2857 *
2858 * @since ACF 5.1.5
2859 *
2860 * @param $size (mixed)
2861 * @return (int)
2862 */
2863 function acf_get_filesize( $size = 1 ) {
2864
2865 // vars
2866 $unit = 'MB';
2867 $units = array(
2868 'TB' => 4,
2869 'GB' => 3,
2870 'MB' => 2,
2871 'KB' => 1,
2872 );
2873
2874 // look for $unit within the $size parameter (123 KB)
2875 if ( is_string( $size ) ) {
2876
2877 // vars
2878 $custom = strtoupper( substr( $size, -2 ) );
2879
2880 foreach ( $units as $k => $v ) {
2881 if ( $custom === $k ) {
2882 $unit = $k;
2883 $size = substr( $size, 0, -2 );
2884 }
2885 }
2886 }
2887
2888 // calc bytes
2889 $bytes = floatval( $size ) * pow( 1024, $units[ $unit ] );
2890
2891 // return
2892 return $bytes;
2893 }
2894
2895 /**
2896 * acf_format_filesize
2897 *
2898 * This function will return a formatted string containing the filesize and unit
2899 *
2900 * @since ACF 5.1.5
2901 *
2902 * @param $size (mixed)
2903 * @return (int)
2904 */
2905 function acf_format_filesize( $size = 1 ) {
2906
2907 // convert
2908 $bytes = acf_get_filesize( $size );
2909
2910 // vars
2911 $units = array(
2912 'TB' => 4,
2913 'GB' => 3,
2914 'MB' => 2,
2915 'KB' => 1,
2916 );
2917
2918 // loop through units
2919 foreach ( $units as $k => $v ) {
2920 $result = $bytes / pow( 1024, $v );
2921
2922 if ( $result >= 1 ) {
2923 return $result . ' ' . $k;
2924 }
2925 }
2926
2927 // return
2928 return $bytes . ' B';
2929 }
2930
2931 /**
2932 * acf_get_valid_terms
2933 *
2934 * This function will replace old terms with new split term ids
2935 *
2936 * @since ACF 5.1.5
2937 *
2938 * @param $terms (int|array)
2939 * @param $taxonomy (string)
2940 * @return $terms
2941 */
2942 function acf_get_valid_terms( $terms = false, $taxonomy = 'category' ) {
2943
2944 // force into array
2945 $terms = acf_get_array( $terms );
2946
2947 // force ints
2948 $terms = array_map( 'intval', $terms );
2949
2950 // bail early if function does not yet exist or
2951 if ( ! function_exists( 'wp_get_split_term' ) || empty( $terms ) ) {
2952 return $terms;
2953 }
2954
2955 // attempt to find new terms
2956 foreach ( $terms as $i => $term_id ) {
2957 $new_term_id = wp_get_split_term( $term_id, $taxonomy );
2958
2959 if ( $new_term_id ) {
2960 $terms[ $i ] = $new_term_id;
2961 }
2962 }
2963
2964 // return
2965 return $terms;
2966 }
2967
2968 /**
2969 * acf_validate_attachment
2970 *
2971 * This function will validate an attachment based on a field's restrictions and return an array of errors
2972 *
2973 * @since ACF 5.2.3
2974 *
2975 * @param array $attachment attachment data. Changes based on context.
2976 * @param array $field field settings containing restrictions.
2977 * @param string $context context is different when uploading / preparing.
2978 * @return $errors (array)
2979 */
2980 function acf_validate_attachment( $attachment, $field, $context = 'prepare' ) {
2981
2982 // vars
2983 $errors = array();
2984 $file = array(
2985 'type' => '',
2986 'width' => 0,
2987 'height' => 0,
2988 'size' => 0,
2989 );
2990
2991 // upload
2992 if ( $context == 'upload' ) {
2993
2994 // vars
2995 $file['type'] = pathinfo( $attachment['name'], PATHINFO_EXTENSION );
2996 $file['size'] = filesize( $attachment['tmp_name'] );
2997
2998 if ( strpos( $attachment['type'], 'image' ) !== false ) {
2999 $size = getimagesize( $attachment['tmp_name'] );
3000 $file['width'] = acf_maybe_get( $size, 0 );
3001 $file['height'] = acf_maybe_get( $size, 1 );
3002 }
3003
3004 // prepare
3005 } elseif ( $context == 'prepare' ) {
3006 $use_path = isset( $attachment['filename'] ) ? $attachment['filename'] : $attachment['url'];
3007 $file['type'] = pathinfo( $use_path, PATHINFO_EXTENSION );
3008 $file['size'] = acf_maybe_get( $attachment, 'filesizeInBytes', 0 );
3009 $file['width'] = acf_maybe_get( $attachment, 'width', 0 );
3010 $file['height'] = acf_maybe_get( $attachment, 'height', 0 );
3011
3012 // custom
3013 } else {
3014 $file = array_merge( $file, $attachment );
3015 $use_path = isset( $attachment['filename'] ) ? $attachment['filename'] : $attachment['url'];
3016 $file['type'] = pathinfo( $use_path, PATHINFO_EXTENSION );
3017 }
3018
3019 // image
3020 if ( $file['width'] || $file['height'] ) {
3021
3022 // width
3023 $min_width = (int) acf_maybe_get( $field, 'min_width', 0 );
3024 $max_width = (int) acf_maybe_get( $field, 'max_width', 0 );
3025
3026 if ( $file['width'] ) {
3027 if ( $min_width && $file['width'] < $min_width ) {
3028
3029 // min width
3030 /* translators: 1: image width */
3031 $errors['min_width'] = sprintf( __( 'Image width must be at least %dpx.', 'secure-custom-fields' ), $min_width );
3032 } elseif ( $max_width && $file['width'] > $max_width ) {
3033
3034 // min width
3035 /* translators: 1: image width */
3036 $errors['max_width'] = sprintf( __( 'Image width must not exceed %dpx.', 'secure-custom-fields' ), $max_width );
3037 }
3038 }
3039
3040 // height
3041 $min_height = (int) acf_maybe_get( $field, 'min_height', 0 );
3042 $max_height = (int) acf_maybe_get( $field, 'max_height', 0 );
3043
3044 if ( $file['height'] ) {
3045 if ( $min_height && $file['height'] < $min_height ) {
3046
3047 // min height
3048 /* translators: 1: image height */
3049 $errors['min_height'] = sprintf( __( 'Image height must be at least %dpx.', 'secure-custom-fields' ), $min_height );
3050 } elseif ( $max_height && $file['height'] > $max_height ) {
3051
3052 // min height
3053 /* translators: 1: image height */
3054 $errors['max_height'] = sprintf( __( 'Image height must not exceed %dpx.', 'secure-custom-fields' ), $max_height );
3055 }
3056 }
3057 }
3058
3059 // file size
3060 if ( $file['size'] ) {
3061 $min_size = acf_maybe_get( $field, 'min_size', 0 );
3062 $max_size = acf_maybe_get( $field, 'max_size', 0 );
3063
3064 if ( $min_size && $file['size'] < acf_get_filesize( $min_size ) ) {
3065
3066 // min width
3067 /* translators: 1: file size */
3068 $errors['min_size'] = sprintf( __( 'File size must be at least %s.', 'secure-custom-fields' ), acf_format_filesize( $min_size ) );
3069 } elseif ( $max_size && $file['size'] > acf_get_filesize( $max_size ) ) {
3070
3071 // min width
3072 /* translators: 1: file size */
3073 $errors['max_size'] = sprintf( __( 'File size must not exceed %s.', 'secure-custom-fields' ), acf_format_filesize( $max_size ) );
3074 }
3075 }
3076
3077 // file type
3078 if ( $file['type'] ) {
3079 $mime_types = acf_maybe_get( $field, 'mime_types', '' );
3080
3081 // lower case
3082 $file['type'] = strtolower( $file['type'] );
3083 $mime_types = strtolower( $mime_types );
3084
3085 // explode
3086 $mime_types = str_replace( array( ' ', '.' ), '', $mime_types );
3087 $mime_types = explode( ',', $mime_types ); // split pieces
3088 $mime_types = array_filter( $mime_types ); // remove empty pieces
3089
3090 if ( ! empty( $mime_types ) && ! in_array( $file['type'], $mime_types ) ) {
3091
3092 // glue together last 2 types
3093 if ( count( $mime_types ) > 1 ) {
3094 $last1 = array_pop( $mime_types );
3095 $last2 = array_pop( $mime_types );
3096
3097 $mime_types[] = $last2 . ' ' . __( 'or', 'secure-custom-fields' ) . ' ' . $last1;
3098 }
3099 /* translators: 1: file type(s) */
3100 $errors['mime_types'] = sprintf( __( 'File type must be %s.', 'secure-custom-fields' ), implode( ', ', $mime_types ) );
3101 }
3102 }
3103
3104 /**
3105 * Filters the errors for a file before it is uploaded or displayed in the media modal.
3106 *
3107 * @since ACF 5.2.3
3108 *
3109 * @param array $errors An array of errors.
3110 * @param array $file An array of data for a single file.
3111 * @param array $attachment An array of attachment data which differs based on the context.
3112 * @param array $field The field array.
3113 * @param string $context The current context (uploading, preparing)
3114 */
3115 $errors = apply_filters( "acf/validate_attachment/type={$field['type']}", $errors, $file, $attachment, $field, $context );
3116 $errors = apply_filters( "acf/validate_attachment/name={$field['_name']}", $errors, $file, $attachment, $field, $context );
3117 $errors = apply_filters( "acf/validate_attachment/key={$field['key']}", $errors, $file, $attachment, $field, $context );
3118 $errors = apply_filters( 'acf/validate_attachment', $errors, $file, $attachment, $field, $context );
3119
3120 // return
3121 return $errors;
3122 }
3123
3124 /**
3125 * _acf_settings_uploader
3126 *
3127 * Dynamic logic for uploader setting
3128 *
3129 * @since ACF 5.2.3
3130 *
3131 * @param $uploader (string)
3132 * @return $uploader
3133 */
3134
3135 add_filter( 'acf/settings/uploader', '_acf_settings_uploader' );
3136
3137 function _acf_settings_uploader( $uploader ) {
3138
3139 // if can't upload files
3140 if ( ! current_user_can( 'upload_files' ) ) {
3141 $uploader = 'basic';
3142 }
3143
3144 // return
3145 return $uploader;
3146 }
3147
3148 /**
3149 * acf_translate
3150 *
3151 * This function will translate a string using the new 'l10n_textdomain' setting
3152 * Also works for arrays which is great for fields - select -> choices
3153 *
3154 * @since ACF 5.3.2
3155 *
3156 * @param mixed $string String or array containing strings to be translated.
3157 * @return mixed
3158 */
3159 function acf_translate( $string ) {
3160
3161 // vars
3162 $l10n = acf_get_setting( 'l10n' );
3163 $textdomain = acf_get_setting( 'l10n_textdomain' );
3164
3165 // bail early if not enabled
3166 if ( ! $l10n ) {
3167 return $string;
3168 }
3169
3170 // bail early if no textdomain
3171 if ( ! $textdomain ) {
3172 return $string;
3173 }
3174
3175 // is array
3176 if ( is_array( $string ) ) {
3177 return array_map( 'acf_translate', $string );
3178 }
3179
3180 // bail early if empty
3181 if ( '' === $string ) {
3182 return $string;
3183 }
3184
3185 // translate
3186 return __( $string, $textdomain );
3187 }
3188
3189 /**
3190 * acf_maybe_add_action
3191 *
3192 * This function will determine if the action has already run before adding / calling the function
3193 *
3194 * @since ACF 5.3.2
3195 *
3196 * @param $post_id (int)
3197 * @return $post_id (int)
3198 */
3199 function acf_maybe_add_action( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
3200
3201 // if action has already run, execute it
3202 // - if currently doing action, allow $tag to be added as per usual to allow $priority ordering needed for 3rd party asset compatibility
3203 if ( did_action( $tag ) && ! doing_action( $tag ) ) {
3204 call_user_func( $function_to_add );
3205
3206 // if action has not yet run, add it
3207 } else {
3208 add_action( $tag, $function_to_add, $priority, $accepted_args );
3209 }
3210 }
3211
3212 /**
3213 * acf_is_row_collapsed
3214 *
3215 * This function will return true if the field's row is collapsed
3216 *
3217 * @since ACF 5.3.2
3218 *
3219 * @param $post_id (int)
3220 * @return $post_id (int)
3221 */
3222 function acf_is_row_collapsed( $field_key = '', $row_index = 0 ) {
3223
3224 // collapsed
3225 $collapsed = acf_get_user_setting( 'collapsed_' . $field_key, '' );
3226
3227 // cookie fallback ( version < 5.3.2 )
3228 if ( $collapsed === '' ) {
3229 $collapsed = acf_extract_var( $_COOKIE, "acf_collapsed_{$field_key}", '' );
3230 $collapsed = str_replace( '|', ',', $collapsed );
3231
3232 // update
3233 acf_update_user_setting( 'collapsed_' . $field_key, $collapsed );
3234 }
3235
3236 // explode
3237 $collapsed = explode( ',', $collapsed );
3238 $collapsed = array_filter( $collapsed, 'is_numeric' );
3239
3240 // collapsed class
3241 return in_array( $row_index, $collapsed );
3242 }
3243
3244 /**
3245 * Return an image tag for the provided attachment ID
3246 *
3247 * @since ACF 5.5.0
3248 * @deprecated 6.3.2
3249 *
3250 * @param integer $attachment_id The attachment ID
3251 * @param string $size The image size to use in the image tag.
3252 * @return false
3253 */
3254 function acf_get_attachment_image( $attachment_id = 0, $size = 'thumbnail' ) {
3255 // report function as deprecated
3256 _deprecated_function( __FUNCTION__, '6.3.2' );
3257 return false;
3258 }
3259
3260 /**
3261 * acf_get_post_thumbnail
3262 *
3263 * This function will return a thumbnail image url for a given post
3264 *
3265 * @since ACF 5.3.8
3266 *
3267 * @param $post (obj)
3268 * @param $size (mixed)
3269 * @return (string)
3270 */
3271 function acf_get_post_thumbnail( $post = null, $size = 'thumbnail' ) {
3272
3273 // vars
3274 $data = array(
3275 'url' => '',
3276 'type' => '',
3277 'html' => '',
3278 );
3279
3280 // post
3281 $post = get_post( $post );
3282
3283 // bail early if no post
3284 if ( ! $post ) {
3285 return $data;
3286 }
3287
3288 // vars
3289 $thumb_id = $post->ID;
3290 $mime_type = acf_maybe_get( explode( '/', $post->post_mime_type ), 0 );
3291
3292 // attachment
3293 if ( $post->post_type === 'attachment' ) {
3294
3295 // change $thumb_id
3296 if ( $mime_type === 'audio' || $mime_type === 'video' ) {
3297 $thumb_id = get_post_thumbnail_id( $post->ID );
3298 }
3299
3300 // post
3301 } else {
3302 $thumb_id = get_post_thumbnail_id( $post->ID );
3303 }
3304
3305 // try url
3306 $data['url'] = wp_get_attachment_image_src( $thumb_id, $size );
3307 $data['url'] = acf_maybe_get( $data['url'], 0 );
3308
3309 // default icon
3310 if ( ! $data['url'] && $post->post_type === 'attachment' ) {
3311 $data['url'] = wp_mime_type_icon( $post->ID );
3312 $data['type'] = 'icon';
3313 }
3314
3315 // html
3316 $data['html'] = '<img src="' . $data['url'] . '" alt="" />';
3317
3318 // return
3319 return $data;
3320 }
3321
3322 /**
3323 * acf_get_browser
3324 *
3325 * Returns the name of the current browser.
3326 *
3327 * @since ACF 5.0.0
3328 *
3329 * @return string
3330 */
3331 function acf_get_browser() {
3332
3333 // Check server var.
3334 if ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
3335 $agent = sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] );
3336
3337 // Loop over search terms.
3338 $browsers = array(
3339 'Firefox' => 'firefox',
3340 'Trident' => 'msie',
3341 'MSIE' => 'msie',
3342 'Edge' => 'edge',
3343 'Chrome' => 'chrome',
3344 'Safari' => 'safari',
3345 );
3346 foreach ( $browsers as $k => $v ) {
3347 if ( strpos( $agent, $k ) !== false ) {
3348 return $v;
3349 }
3350 }
3351 }
3352
3353 // Return default.
3354 return '';
3355 }
3356
3357 /**
3358 * acf_is_ajax
3359 *
3360 * This function will return true if performing a wp ajax call
3361 *
3362 * @since ACF 5.3.8
3363 *
3364 * @param n/a
3365 * @return (boolean)
3366 */
3367 function acf_is_ajax( $action = '' ) {
3368
3369 // vars
3370 $is_ajax = false;
3371
3372 // check if is doing ajax
3373 if ( wp_doing_ajax() ) {
3374 $is_ajax = true;
3375 }
3376
3377 // phpcs:disable WordPress.Security.NonceVerification.Missing
3378 // check $action
3379 if ( $action && acf_maybe_get( $_POST, 'action' ) !== $action ) {
3380 // phpcs:enable WordPress.Security.NonceVerification.Missing
3381 $is_ajax = false;
3382 }
3383
3384 // return
3385 return $is_ajax;
3386 }
3387
3388 /**
3389 * Returns a date value in a formatted string.
3390 *
3391 * @since ACF 5.3.8
3392 *
3393 * @param string $value The date value to format.
3394 * @param string $format The format to use.
3395 * @return string
3396 */
3397 function acf_format_date( $value, $format ) {
3398 // Bail early if no value or value is not what we expect.
3399 if ( ! $value || ( ! is_string( $value ) && ! is_int( $value ) ) ) {
3400 return $value;
3401 }
3402
3403 // Numeric (either unix or YYYYMMDD).
3404 if ( is_numeric( $value ) && strlen( $value ) !== 8 ) {
3405 $unixtimestamp = $value;
3406 } else {
3407 $unixtimestamp = strtotime( $value );
3408 }
3409
3410 return date_i18n( $format, $unixtimestamp );
3411 }
3412
3413 /**
3414 * Previously, deletes the debug.log file.
3415 *
3416 * @since ACF 5.7.10
3417 * @deprecated 6.2.7
3418 */
3419 function acf_clear_log() {
3420 _deprecated_function( __FUNCTION__, '6.2.7' );
3421 return false;
3422 }
3423
3424 /**
3425 * acf_log
3426 *
3427 * description
3428 *
3429 * @since ACF 5.3.8
3430 *
3431 * @param $post_id (int)
3432 * @return $post_id (int)
3433 */
3434 function acf_log() {
3435
3436 // vars
3437 $args = func_get_args();
3438
3439 // loop
3440 foreach ( $args as $i => $arg ) {
3441
3442 // array | object
3443 if ( is_array( $arg ) || is_object( $arg ) ) {
3444 $arg = print_r( $arg, true );
3445
3446 // bool
3447 } elseif ( is_bool( $arg ) ) {
3448 $arg = 'bool(' . ( $arg ? 'true' : 'false' ) . ')';
3449 }
3450
3451 // update
3452 $args[ $i ] = $arg;
3453 }
3454
3455 // log
3456 error_log( implode( ' ', $args ) );
3457 }
3458
3459 /**
3460 * acf_dev_log
3461 *
3462 * Used to log variables only if ACF_DEV is defined
3463 *
3464 * @since ACF 5.7.4
3465 *
3466 * @param mixed
3467 * @return void
3468 */
3469 function acf_dev_log() {
3470 if ( defined( 'ACF_DEV' ) && ACF_DEV ) {
3471 call_user_func_array( 'acf_log', func_get_args() );
3472 }
3473 }
3474
3475 /**
3476 * acf_doing
3477 *
3478 * This function will tell ACF what task it is doing
3479 *
3480 * @since ACF 5.3.8
3481 *
3482 * @param $event (string)
3483 * @param context (string)
3484 * @return n/a
3485 */
3486 function acf_doing( $event = '', $context = '' ) {
3487
3488 acf_update_setting( 'doing', $event );
3489 acf_update_setting( 'doing_context', $context );
3490 }
3491
3492 /**
3493 * acf_is_doing
3494 *
3495 * This function can be used to state what ACF is doing, or to check
3496 *
3497 * @since ACF 5.3.8
3498 *
3499 * @param $event (string)
3500 * @param context (string)
3501 * @return (boolean)
3502 */
3503 function acf_is_doing( $event = '', $context = '' ) {
3504
3505 // vars
3506 $doing = false;
3507
3508 // task
3509 if ( acf_get_setting( 'doing' ) === $event ) {
3510 $doing = true;
3511 }
3512
3513 // context
3514 if ( $context && acf_get_setting( 'doing_context' ) !== $context ) {
3515 $doing = false;
3516 }
3517
3518 // return
3519 return $doing;
3520 }
3521
3522 /**
3523 * acf_is_plugin_active
3524 *
3525 * This function will return true if the ACF plugin is active
3526 * - May be included within a theme or other plugin
3527 *
3528 * @since ACF 5.4.0
3529 *
3530 * @param $basename (int)
3531 * @return $post_id (int)
3532 */
3533 function acf_is_plugin_active() {
3534
3535 // vars
3536 $basename = acf_get_setting( 'basename' );
3537
3538 // ensure is_plugin_active() exists (not on frontend)
3539 if ( ! function_exists( 'is_plugin_active' ) ) {
3540 include_once ABSPATH . 'wp-admin/includes/plugin.php';
3541 }
3542
3543 // return
3544 return is_plugin_active( $basename );
3545 }
3546
3547 /**
3548 * acf_send_ajax_results
3549 *
3550 * This function will print JSON data for a Select2 AJAX query
3551 *
3552 * @since ACF 5.4.0
3553 *
3554 * @param $response (array)
3555 * @return n/a
3556 */
3557 function acf_send_ajax_results( $response ) {
3558
3559 // validate
3560 $response = wp_parse_args(
3561 $response,
3562 array(
3563 'results' => array(),
3564 'more' => false,
3565 'limit' => 0,
3566 )
3567 );
3568
3569 // limit
3570 if ( $response['limit'] && $response['results'] ) {
3571
3572 // vars
3573 $total = 0;
3574
3575 foreach ( $response['results'] as $result ) {
3576
3577 // parent
3578 ++$total;
3579
3580 // children
3581 if ( ! empty( $result['children'] ) ) {
3582 $total += count( $result['children'] );
3583 }
3584 }
3585
3586 // calc
3587 if ( $total >= $response['limit'] ) {
3588 $response['more'] = true;
3589 }
3590 }
3591
3592 // return
3593 wp_send_json( $response );
3594 }
3595
3596 /**
3597 * acf_is_sequential_array
3598 *
3599 * This function will return true if the array contains only numeric keys
3600 *
3601 * @source http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential
3602 *
3603 * @since ACF 5.4.0
3604 *
3605 * @param $array (array)
3606 * @return (boolean)
3607 */
3608 function acf_is_sequential_array( $array ) {
3609
3610 // bail early if not array
3611 if ( ! is_array( $array ) ) {
3612 return false;
3613 }
3614
3615 // loop
3616 foreach ( $array as $key => $value ) {
3617
3618 // bail early if is string
3619 if ( is_string( $key ) ) {
3620 return false;
3621 }
3622 }
3623
3624 // return
3625 return true;
3626 }
3627
3628 /**
3629 * acf_is_associative_array
3630 *
3631 * This function will return true if the array contains one or more string keys
3632 *
3633 * @source http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential
3634 *
3635 * @since ACF 5.4.0
3636 *
3637 * @param $array (array)
3638 * @return (boolean)
3639 */
3640 function acf_is_associative_array( $array ) {
3641
3642 // bail early if not array
3643 if ( ! is_array( $array ) ) {
3644 return false;
3645 }
3646
3647 // loop
3648 foreach ( $array as $key => $value ) {
3649
3650 // bail early if is string
3651 if ( is_string( $key ) ) {
3652 return true;
3653 }
3654 }
3655
3656 // return
3657 return false;
3658 }
3659
3660 /**
3661 * acf_add_array_key_prefix
3662 *
3663 * This function will add a prefix to all array keys
3664 * Useful to preserve numeric keys when performing array_multisort
3665 *
3666 * @since ACF 5.4.0
3667 *
3668 * @param $array (array)
3669 * @param $prefix (string)
3670 * @return (array)
3671 */
3672 function acf_add_array_key_prefix( $array, $prefix ) {
3673
3674 // vars
3675 $array2 = array();
3676
3677 // loop
3678 foreach ( $array as $k => $v ) {
3679 $k2 = $prefix . $k;
3680 $array2[ $k2 ] = $v;
3681 }
3682
3683 // return
3684 return $array2;
3685 }
3686
3687 /**
3688 * acf_remove_array_key_prefix
3689 *
3690 * This function will remove a prefix to all array keys
3691 * Useful to preserve numeric keys when performing array_multisort
3692 *
3693 * @since ACF 5.4.0
3694 *
3695 * @param $array (array)
3696 * @param $prefix (string)
3697 * @return (array)
3698 */
3699 function acf_remove_array_key_prefix( $array, $prefix ) {
3700
3701 // vars
3702 $array2 = array();
3703 $l = strlen( $prefix );
3704
3705 // loop
3706 foreach ( $array as $k => $v ) {
3707 $k2 = ( substr( $k, 0, $l ) === $prefix ) ? substr( $k, $l ) : $k;
3708 $array2[ $k2 ] = $v;
3709 }
3710
3711 // return
3712 return $array2;
3713 }
3714
3715 /**
3716 * This function will connect an attachment (image etc) to the post
3717 * Used to connect attachments uploaded directly to media that have not been attached to a post
3718 *
3719 * @since ACF 5.8.0 Added filter to prevent connection.
3720 * @since ACF 5.5.4
3721 *
3722 * @param integer $attachment_id The attachment ID.
3723 * @param integer $post_id The post ID.
3724 * @return boolean True if attachment was connected.
3725 */
3726 function acf_connect_attachment_to_post( $attachment_id = 0, $post_id = 0 ) {
3727
3728 // bail early if $attachment_id is not valid.
3729 if ( ! $attachment_id || ! is_numeric( $attachment_id ) ) {
3730 return false;
3731 }
3732
3733 // bail early if $post_id is not valid.
3734 if ( ! $post_id || ! is_numeric( $post_id ) ) {
3735 return false;
3736 }
3737
3738 /**
3739 * Filters whether or not to connect the attachment.
3740 *
3741 * @since ACF 5.8.0
3742 *
3743 * @param bool $bool Returning false will prevent the connection. Default true.
3744 * @param int $attachment_id The attachment ID.
3745 * @param int $post_id The post ID.
3746 */
3747 if ( ! apply_filters( 'acf/connect_attachment_to_post', true, $attachment_id, $post_id ) ) {
3748 return false;
3749 }
3750
3751 // vars
3752 $post = get_post( $attachment_id );
3753
3754 // Check if is valid post.
3755 if ( $post && $post->post_type == 'attachment' && $post->post_parent == 0 ) {
3756
3757 // update
3758 wp_update_post(
3759 array(
3760 'ID' => $post->ID,
3761 'post_parent' => $post_id,
3762 )
3763 );
3764
3765 // return
3766 return true;
3767 }
3768
3769 // return
3770 return true;
3771 }
3772
3773 /**
3774 * acf_encrypt
3775 *
3776 * This function will encrypt a string using PHP
3777 * https://bhoover.com/using-php-openssl_encrypt-openssl_decrypt-encrypt-decrypt-data/
3778 *
3779 * @since ACF 5.5.8
3780 *
3781 * @param $data (string)
3782 * @return (string)
3783 */
3784 function acf_encrypt( $data = '' ) {
3785
3786 // bail early if no encrypt function
3787 if ( ! function_exists( 'openssl_encrypt' ) ) {
3788 return base64_encode( $data );
3789 }
3790
3791 // generate a key
3792 $key = wp_hash( 'acf_encrypt' );
3793
3794 // Generate an initialization vector
3795 $iv = openssl_random_pseudo_bytes( openssl_cipher_iv_length( 'aes-256-cbc' ) );
3796
3797 // Encrypt the data using AES 256 encryption in CBC mode using our encryption key and initialization vector.
3798 $encrypted_data = openssl_encrypt( $data, 'aes-256-cbc', $key, 0, $iv );
3799
3800 // The $iv is just as important as the key for decrypting, so save it with our encrypted data using a unique separator (::)
3801 return base64_encode( $encrypted_data . '::' . $iv );
3802 }
3803
3804 /**
3805 * Decrypts an encrypted string using PHP.
3806 * https://bhoover.com/using-php-openssl_encrypt-openssl_decrypt-encrypt-decrypt-data/
3807 *
3808 * @since ACF 5.5.8
3809 *
3810 * @param string $data The string to decrypt.
3811 * @return string|false Decrypted string, or false if the payload is malformed or decryption fails.
3812 */
3813 function acf_decrypt( $data = '' ) {
3814 // bail early if no decrypt function
3815 if ( ! function_exists( 'openssl_decrypt' ) ) {
3816 return base64_decode( (string) $data ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode -- Decoding our own encrypted payload.
3817 }
3818
3819 // Treat malformed input as a decrypt failure: list() destructuring below would
3820 // otherwise warn on PHP 8 when the payload isn't the "base64(data::iv)" shape.
3821 $raw = base64_decode( (string) $data, true ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode -- Decoding our own encrypted payload.
3822 if ( false === $raw || strpos( $raw, '::' ) === false ) {
3823 return false;
3824 }
3825
3826 // generate a key
3827 $key = wp_hash( 'acf_encrypt' );
3828
3829 // To decrypt, split the encrypted data from our IV - our unique separator used was "::"
3830 list( $encrypted_data, $iv ) = explode( '::', $raw, 2 );
3831
3832 // decrypt
3833 return openssl_decrypt( $encrypted_data, 'aes-256-cbc', $key, 0, $iv );
3834 }
3835
3836 /**
3837 * acf_parse_markdown
3838 *
3839 * A very basic regex-based Markdown parser function based off [slimdown](https://gist.github.com/jbroadway/2836900).
3840 *
3841 * @since ACF 5.7.2
3842 *
3843 * @param string $text The string to parse.
3844 * @return string
3845 */
3846 function acf_parse_markdown( $text = '' ) {
3847
3848 // trim
3849 $text = trim( $text );
3850
3851 // rules
3852 $rules = array(
3853 '/=== (.+?) ===/' => '<h2>$1</h2>', // headings
3854 '/== (.+?) ==/' => '<h3>$1</h3>', // headings
3855 '/= (.+?) =/' => '<h4>$1</h4>', // headings
3856 '/\[([^\[]+)\]\(([^\)]+)\)/' => '<a href="$2">$1</a>', // links
3857 '/(\*\*)(.*?)\1/' => '<strong>$2</strong>', // bold
3858 '/(\*)(.*?)\1/' => '<em>$2</em>', // italic
3859 '/`(.*?)`/' => '<code>$1</code>', // inline code
3860 '/\n\*(.*)/' => "\n<ul>\n\t<li>$1</li>\n</ul>", // ul lists
3861 '/\n[0-9]+\.(.*)/' => "\n<ol>\n\t<li>$1</li>\n</ol>", // ol lists
3862 '/<\/ul>\s?<ul>/' => '', // fix extra ul
3863 '/<\/ol>\s?<ol>/' => '', // fix extra ol
3864 );
3865 foreach ( $rules as $k => $v ) {
3866 $text = preg_replace( $k, $v, $text );
3867 }
3868
3869 // autop
3870 $text = wpautop( $text );
3871
3872 // return
3873 return $text;
3874 }
3875
3876 /**
3877 * acf_get_sites
3878 *
3879 * Returns an array of sites for a network.
3880 *
3881 * @since ACF 5.4.0
3882 *
3883 * @return array
3884 */
3885 function acf_get_sites() {
3886 $results = array();
3887 $sites = get_sites( array( 'number' => 0 ) );
3888 if ( $sites ) {
3889 foreach ( $sites as $site ) {
3890 $results[] = get_site( $site )->to_array();
3891 }
3892 }
3893 return $results;
3894 }
3895
3896 /**
3897 * acf_convert_rules_to_groups
3898 *
3899 * Converts an array of rules from ACF4 to an array of groups for ACF5
3900 *
3901 * @since ACF 5.7.4
3902 *
3903 * @param array $rules An array of rules.
3904 * @param string $anyorall The anyorall setting used in ACF4. Defaults to 'any'.
3905 * @return array
3906 */
3907 function acf_convert_rules_to_groups( $rules, $anyorall = 'any' ) {
3908
3909 // vars
3910 $groups = array();
3911 $index = 0;
3912
3913 // loop
3914 foreach ( $rules as $rule ) {
3915
3916 // extract vars
3917 $group = acf_extract_var( $rule, 'group_no' );
3918 $order = acf_extract_var( $rule, 'order_no' );
3919
3920 // calculate group if not defined
3921 if ( $group === null ) {
3922 $group = $index;
3923
3924 // use $anyorall to determine if a new group is needed
3925 if ( $anyorall == 'any' ) {
3926 ++$index;
3927 }
3928 }
3929
3930 // calculate order if not defined
3931 if ( $order === null ) {
3932 $order = isset( $groups[ $group ] ) ? count( $groups[ $group ] ) : 0;
3933 }
3934
3935 // append to group
3936 $groups[ $group ][ $order ] = $rule;
3937
3938 // sort groups
3939 ksort( $groups[ $group ] );
3940 }
3941
3942 // sort groups
3943 ksort( $groups );
3944
3945 // return
3946 return $groups;
3947 }
3948
3949 /**
3950 * acf_register_ajax
3951 *
3952 * Registers an ajax callback.
3953 *
3954 * @since ACF 5.7.7
3955 *
3956 * @param string $name The ajax action name.
3957 * @param array $callback The callback function or array.
3958 * @param boolean $public Whether to allow access to non logged in users.
3959 * @return void
3960 */
3961 function acf_register_ajax( $name = '', $callback = false, $public = false ) {
3962
3963 // vars
3964 $action = "acf/ajax/$name";
3965
3966 // add action for logged-in users
3967 add_action( "wp_ajax_$action", $callback );
3968
3969 // add action for non logged-in users
3970 if ( $public ) {
3971 add_action( "wp_ajax_nopriv_$action", $callback );
3972 }
3973 }
3974
3975 /**
3976 * acf_str_camel_case
3977 *
3978 * Converts a string into camelCase.
3979 * Thanks to https://stackoverflow.com/questions/31274782/convert-array-keys-from-underscore-case-to-camelcase-recursively
3980 *
3981 * @since ACF 5.8.0
3982 *
3983 * @param string $string The string ot convert.
3984 * @return string
3985 */
3986 function acf_str_camel_case( $string = '' ) {
3987 return lcfirst( str_replace( ' ', '', ucwords( str_replace( '_', ' ', $string ) ) ) );
3988 }
3989
3990 /**
3991 * acf_array_camel_case
3992 *
3993 * Converts all array keys to camelCase.
3994 *
3995 * @since ACF 5.8.0
3996 *
3997 * @param array $array The array to convert.
3998 * @return array
3999 */
4000 function acf_array_camel_case( $array = array() ) {
4001 $array2 = array();
4002 foreach ( $array as $k => $v ) {
4003 $array2[ acf_str_camel_case( $k ) ] = $v;
4004 }
4005 return $array2;
4006 }
4007
4008 /**
4009 * Returns true if the current screen is using the block editor.
4010 *
4011 * @since ACF 5.8.0
4012 *
4013 * @return boolean
4014 */
4015 function acf_is_block_editor() {
4016 if ( function_exists( 'get_current_screen' ) ) {
4017 $screen = get_current_screen();
4018 if ( $screen && method_exists( $screen, 'is_block_editor' ) ) {
4019 return $screen->is_block_editor();
4020 }
4021 }
4022 return false;
4023 }
4024
4025 /**
4026 * Return an array of the WordPress reserved terms
4027 *
4028 * @since ACF 6.1
4029 *
4030 * @return array The WordPress reserved terms list.
4031 */
4032 function acf_get_wp_reserved_terms() {
4033 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' );
4034 }
4035
4036 /**
4037 * Detect if we're on a multisite subsite.
4038 *
4039 * @since ACF 6.2.4
4040 *
4041 * @return boolean true if we're in a multisite install and not on the main site
4042 */
4043 function acf_is_multisite_sub_site() {
4044 if ( is_multisite() && ! is_main_site() ) {
4045 return true;
4046 }
4047 return false;
4048 }
4049
4050 /**
4051 * Detect if we're on a multisite main site.
4052 *
4053 * @since ACF 6.2.4
4054 *
4055 * @return boolean true if we're in a multisite install and on the main site
4056 */
4057 function acf_is_multisite_main_site() {
4058 if ( is_multisite() && is_main_site() ) {
4059 return true;
4060 }
4061 return false;
4062 }
4063