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