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