PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.4.0-beta2
Secure Custom Fields v6.4.0-beta2
6.9.5 6.9.4 6.9.3 6.9.2 6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / acf-helper-functions.php
secure-custom-fields / includes Last commit date
Blocks 1 year ago admin 1 year ago ajax 1 year ago api 1 year ago fields 1 year ago forms 1 year ago legacy 1 year ago locations 1 year ago post-types 1 year ago rest-api 1 year ago walkers 1 year ago acf-bidirectional-functions.php 1 year ago acf-field-functions.php 1 year ago acf-field-group-functions.php 1 year ago acf-form-functions.php 1 year ago acf-helper-functions.php 1 year ago acf-hook-functions.php 1 year ago acf-input-functions.php 1 year ago acf-internal-post-type-functions.php 1 year ago acf-meta-functions.php 1 year ago acf-post-functions.php 1 year ago acf-post-type-functions.php 1 year ago acf-taxonomy-functions.php 1 year ago acf-user-functions.php 1 year ago acf-utility-functions.php 1 year ago acf-value-functions.php 1 year ago acf-wp-functions.php 1 year ago assets.php 1 year ago class-acf-data.php 1 year ago class-acf-internal-post-type.php 1 year ago class-acf-site-health.php 1 year ago compatibility.php 1 year ago deprecated.php 1 year ago fields.php 1 year ago index.php 1 year ago l10n.php 1 year ago local-fields.php 1 year ago local-json.php 1 year ago local-meta.php 1 year ago locations.php 1 year ago loop.php 1 year ago media.php 1 year ago rest-api.php 1 year ago revisions.php 1 year ago third-party.php 1 year ago upgrades.php 1 year ago validation.php 1 year ago wpml.php 1 year ago
acf-helper-functions.php
657 lines
1 <?php
2
3 /**
4 * Returns true if the value provided is considered "empty". Allows numbers such as 0.
5 *
6 * @date 6/7/16
7 * @since 5.4.0
8 *
9 * @param mixed $var The value to check.
10 * @return boolean
11 */
12 function acf_is_empty( $var ) {
13 return ( ! $var && ! is_numeric( $var ) );
14 }
15
16 /**
17 * Returns true if the value provided is considered "not empty". Allows numbers such as 0.
18 *
19 * @date 15/7/19
20 * @since 5.8.1
21 *
22 * @param mixed $var The value to check.
23 * @return boolean
24 */
25 function acf_not_empty( $var ) {
26 return ( $var || is_numeric( $var ) );
27 }
28
29 /**
30 * Returns a unique numeric based id.
31 *
32 * @date 9/1/19
33 * @since 5.7.10
34 *
35 * @param string $prefix The id prefix. Defaults to 'acf'.
36 * @return string
37 */
38 function acf_uniqid( $prefix = 'acf' ) {
39
40 // Instantiate global counter.
41 global $acf_uniqid;
42 if ( ! isset( $acf_uniqid ) ) {
43 $acf_uniqid = 1;
44 }
45
46 // Return id.
47 return $prefix . '-' . $acf_uniqid++;
48 }
49
50 /**
51 * Merges together two arrays but with extra functionality to append class names.
52 *
53 * @date 22/1/19
54 * @since 5.7.10
55 *
56 * @param array $array1 An array of attributes.
57 * @param array $array2 An array of attributes.
58 * @return array
59 */
60 function acf_merge_attributes( $array1, $array2 ) {
61
62 // Merge together attributes.
63 $array3 = array_merge( $array1, $array2 );
64
65 // Append together special attributes.
66 foreach ( array( 'class', 'style' ) as $key ) {
67 if ( isset( $array1[ $key ] ) && isset( $array2[ $key ] ) ) {
68 $array3[ $key ] = trim( $array1[ $key ] ) . ' ' . trim( $array2[ $key ] );
69 }
70 }
71
72 // Return.
73 return $array3;
74 }
75
76 /**
77 * acf_cache_key
78 *
79 * Returns a filtered cache key.
80 *
81 * @date 25/1/19
82 * @since 5.7.11
83 *
84 * @param string $key The cache key.
85 * @return string
86 */
87 function acf_cache_key( $key = '' ) {
88
89 /**
90 * Filters the cache key.
91 *
92 * @date 25/1/19
93 * @since 5.7.11
94 *
95 * @param string $key The cache key.
96 * @param string $original_key The original cache key.
97 */
98 return apply_filters( 'acf/get_cache_key', $key, $key );
99 }
100
101 /**
102 * acf_request_args
103 *
104 * Returns an array of $_REQUEST values using the provided defaults.
105 *
106 * @date 28/2/19
107 * @since 5.7.13
108 *
109 * @param array $args An array of args.
110 * @return array
111 */
112 function acf_request_args( $args = array() ) {
113 foreach ( $args as $k => $v ) {
114 $args[ $k ] = isset( $_REQUEST[ $k ] ) ? acf_sanitize_request_args( $_REQUEST[ $k ] ) : $args[ $k ]; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Verified elsewhere.
115 }
116 return $args;
117 }
118
119 /**
120 * Returns a single $_REQUEST arg with fallback.
121 *
122 * @date 23/10/20
123 * @since 5.9.2
124 *
125 * @param string $key The property name.
126 * @param mixed $default The default value to fallback to.
127 * @return mixed
128 */
129 function acf_request_arg( $name = '', $default = null ) {
130 return isset( $_REQUEST[ $name ] ) ? acf_sanitize_request_args( $_REQUEST[ $name ] ) : $default; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
131 }
132
133 // Register store.
134 acf_register_store( 'filters' );
135
136 /**
137 * acf_enable_filter
138 *
139 * Enables a filter with the given name.
140 *
141 * @date 14/7/16
142 * @since 5.4.0
143 *
144 * @param string name The modifer name.
145 * @return void
146 */
147 function acf_enable_filter( $name = '' ) {
148 acf_get_store( 'filters' )->set( $name, true );
149 }
150
151 /**
152 * acf_disable_filter
153 *
154 * Disables a filter with the given name.
155 *
156 * @date 14/7/16
157 * @since 5.4.0
158 *
159 * @param string name The modifer name.
160 * @return void
161 */
162 function acf_disable_filter( $name = '' ) {
163 acf_get_store( 'filters' )->set( $name, false );
164 }
165
166 /**
167 * acf_is_filter_enabled
168 *
169 * Returns the state of a filter for the given name.
170 *
171 * @date 14/7/16
172 * @since 5.4.0
173 *
174 * @param string name The modifer name.
175 * @return array
176 */
177 function acf_is_filter_enabled( $name = '' ) {
178 return acf_get_store( 'filters' )->get( $name );
179 }
180
181 /**
182 * acf_get_filters
183 *
184 * Returns an array of filters in their current state.
185 *
186 * @date 14/7/16
187 * @since 5.4.0
188 *
189 * @param void
190 * @return array
191 */
192 function acf_get_filters() {
193 return acf_get_store( 'filters' )->get();
194 }
195
196 /**
197 * acf_set_filters
198 *
199 * Sets an array of filter states.
200 *
201 * @date 14/7/16
202 * @since 5.4.0
203 *
204 * @param array $filters An Array of modifers
205 * @return array
206 */
207 function acf_set_filters( $filters = array() ) {
208 acf_get_store( 'filters' )->set( $filters );
209 }
210
211 /**
212 * acf_disable_filters
213 *
214 * Disables all filters and returns the previous state.
215 *
216 * @date 14/7/16
217 * @since 5.4.0
218 *
219 * @param void
220 * @return array
221 */
222 function acf_disable_filters() {
223
224 // Get state.
225 $prev_state = acf_get_filters();
226
227 // Set all modifers as false.
228 acf_set_filters( array_map( '__return_false', $prev_state ) );
229
230 // Return prev state.
231 return $prev_state;
232 }
233
234 /**
235 * acf_enable_filters
236 *
237 * Enables all or an array of specific filters and returns the previous state.
238 *
239 * @date 14/7/16
240 * @since 5.4.0
241 *
242 * @param array $filters An Array of modifers
243 * @return array
244 */
245 function acf_enable_filters( $filters = array() ) {
246
247 // Get state.
248 $prev_state = acf_get_filters();
249
250 // Allow specific filters to be enabled.
251 if ( $filters ) {
252 acf_set_filters( $filters );
253
254 // Set all modifers as true.
255 } else {
256 acf_set_filters( array_map( '__return_true', $prev_state ) );
257 }
258
259 // Return prev state.
260 return $prev_state;
261 }
262
263 /**
264 * acf_idval
265 *
266 * Parses the provided value for an ID.
267 *
268 * @date 29/3/19
269 * @since 5.7.14
270 *
271 * @param mixed $value A value to parse.
272 * @return integer
273 */
274 function acf_idval( $value ) {
275
276 // Check if value is numeric.
277 if ( is_numeric( $value ) ) {
278 return (int) $value;
279
280 // Check if value is array.
281 } elseif ( is_array( $value ) ) {
282 return (int) isset( $value['ID'] ) ? $value['ID'] : 0;
283
284 // Check if value is object.
285 } elseif ( is_object( $value ) ) {
286 return (int) isset( $value->ID ) ? $value->ID : 0;
287 }
288
289 // Return default.
290 return 0;
291 }
292
293 /**
294 * acf_maybe_idval
295 *
296 * Checks value for potential id value.
297 *
298 * @date 6/4/19
299 * @since 5.7.14
300 *
301 * @param mixed $value A value to parse.
302 * @return mixed
303 */
304 function acf_maybe_idval( $value ) {
305 if ( $id = acf_idval( $value ) ) {
306 return $id;
307 }
308 return $value;
309 }
310
311 /**
312 * Convert any numeric strings into their equivalent numeric type. This function will
313 * work with both single values and arrays.
314 *
315 * @param mixed $value Either a single value or an array of values.
316 * @return mixed
317 */
318 function acf_format_numerics( $value ) {
319 if ( is_array( $value ) ) {
320 return array_map(
321 function ( $v ) {
322 return is_numeric( $v ) ? $v + 0 : $v;
323 },
324 $value
325 );
326 }
327
328 return is_numeric( $value ) ? $value + 0 : $value;
329 }
330
331 /**
332 * acf_numval
333 *
334 * Casts the provided value as eiter an int or float using a simple hack.
335 *
336 * @date 11/4/19
337 * @since 5.7.14
338 *
339 * @param mixed $value A value to parse.
340 * @return (int|float)
341 */
342 function acf_numval( $value ) {
343 return ( intval( $value ) == floatval( $value ) ) ? intval( $value ) : floatval( $value );
344 }
345
346 /**
347 * acf_idify
348 *
349 * Returns an id attribute friendly string.
350 *
351 * @date 24/12/17
352 * @since 5.6.5
353 *
354 * @param string $str The string to convert.
355 * @return string
356 */
357 function acf_idify( $str = '' ) {
358 return str_replace( array( '][', '[', ']' ), array( '-', '-', '' ), strtolower( $str ) );
359 }
360
361 /**
362 * Returns a slug friendly string.
363 *
364 * @date 24/12/17
365 * @since 5.6.5
366 *
367 * @param string $str The string to convert.
368 * @param string $glue The glue between each slug piece.
369 * @return string
370 */
371 function acf_slugify( $str = '', $glue = '-' ) {
372 $raw = $str;
373 $slug = str_replace( array( '_', '-', '/', ' ' ), $glue, strtolower( remove_accents( $raw ) ) );
374 $slug = preg_replace( '/[^A-Za-z0-9' . preg_quote( $glue ) . ']/', '', $slug );
375
376 /**
377 * Filters the slug created by acf_slugify().
378 *
379 * @since 5.11.4
380 *
381 * @param string $slug The newly created slug.
382 * @param string $raw The original string.
383 * @param string $glue The separator used to join the string into a slug.
384 */
385 return apply_filters( 'acf/slugify', $slug, $raw, $glue );
386 }
387
388 /**
389 * Returns a string with correct full stop punctuation.
390 *
391 * @date 12/7/19
392 * @since 5.8.2
393 *
394 * @param string $str The string to format.
395 * @return string
396 */
397 function acf_punctify( $str = '' ) {
398 if ( substr( trim( strip_tags( $str ) ), -1 ) !== '.' ) {
399 return trim( $str ) . '.';
400 }
401 return trim( $str );
402 }
403
404 /**
405 * acf_did
406 *
407 * Returns true if ACF already did an event.
408 *
409 * @date 30/8/19
410 * @since 5.8.1
411 *
412 * @param string $name The name of the event.
413 * @return boolean
414 */
415 function acf_did( $name ) {
416
417 // Return true if already did the event (preventing event).
418 if ( acf_get_data( "acf_did_$name" ) ) {
419 return true;
420
421 // Otherwise, update store and return false (alowing event).
422 } else {
423 acf_set_data( "acf_did_$name", true );
424 return false;
425 }
426 }
427
428 /**
429 * Returns the length of a string that has been submitted via $_POST.
430 *
431 * Uses the following process:
432 * 1. Unslash the string because posted values will be slashed.
433 * 2. Decode special characters because wp_kses() will normalize entities.
434 * 3. Treat line-breaks as a single character instead of two.
435 * 4. Use mb_strlen() to accomodate special characters.
436 *
437 * @date 04/06/2020
438 * @since 5.9.0
439 *
440 * @param string $str The string to review.
441 * @return integer
442 */
443 function acf_strlen( $str ) {
444 return mb_strlen( str_replace( "\r\n", "\n", wp_specialchars_decode( wp_unslash( $str ) ) ) );
445 }
446
447 /**
448 * Returns a value with default fallback.
449 *
450 * @date 6/4/20
451 * @since 5.9.0
452 *
453 * @param mixed $value The value.
454 * @param mixed $default_value The default value.
455 * @return mixed
456 */
457 function acf_with_default( $value, $default_value ) {
458 return $value ? $value : $default_value;
459 }
460
461 /**
462 * Returns the current priority of a running action.
463 *
464 * @date 14/07/2020
465 * @since 5.9.0
466 *
467 * @param string $action The action name.
468 * @return integer|boolean
469 */
470 function acf_doing_action( $action ) {
471 global $wp_filter;
472 if ( isset( $wp_filter[ $action ] ) ) {
473 return $wp_filter[ $action ]->current_priority();
474 }
475 return false;
476 }
477
478 /**
479 * Returns the current URL.
480 *
481 * @date 23/01/2015
482 * @since 5.1.5
483 *
484 * @param void
485 * @return string
486 */
487 function acf_get_current_url() {
488 // Ensure props exist to avoid PHP Notice during CLI commands.
489 if ( isset( $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'] ) ) {
490 return ( is_ssl() ? 'https' : 'http' ) . '://' . filter_var( $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL );
491 }
492 return '';
493 }
494
495 /**
496 * Sanitizes request arguments.
497 *
498 * @param mixed $args The data to sanitize.
499 *
500 * @return array|boolean|float|integer|mixed|string
501 */
502 function acf_sanitize_request_args( $args = array() ) {
503 switch ( gettype( $args ) ) {
504 case 'boolean':
505 return (bool) $args;
506 case 'integer':
507 return (int) $args;
508 case 'double':
509 return (float) $args;
510 case 'array':
511 $sanitized = array();
512 foreach ( $args as $key => $value ) {
513 $key = sanitize_text_field( $key );
514 $sanitized[ $key ] = acf_sanitize_request_args( $value );
515 }
516 return $sanitized;
517 case 'object':
518 return wp_kses_post_deep( $args );
519 case 'string':
520 default:
521 return wp_kses( $args, 'acf' );
522 }
523 }
524
525 /**
526 * Sanitizes file upload arrays.
527 *
528 * @since 6.0.4
529 *
530 * @param array $args The file array.
531 *
532 * @return array
533 */
534 function acf_sanitize_files_array( array $args = array() ) {
535 $defaults = array(
536 'name' => '',
537 'tmp_name' => '',
538 'type' => '',
539 'size' => 0,
540 'error' => '',
541 );
542
543 $args = wp_parse_args( $args, $defaults );
544
545 if ( empty( $args['name'] ) ) {
546 return $defaults;
547 }
548
549 if ( is_array( $args['name'] ) ) {
550 $files = array();
551 $files['name'] = acf_sanitize_files_value_array( $args['name'], 'sanitize_file_name' );
552 $files['tmp_name'] = acf_sanitize_files_value_array( $args['tmp_name'], 'sanitize_text_field' );
553 $files['type'] = acf_sanitize_files_value_array( $args['type'], 'sanitize_text_field' );
554 $files['size'] = acf_sanitize_files_value_array( $args['size'], 'absint' );
555 $files['error'] = acf_sanitize_files_value_array( $args['error'], 'absint' );
556 return $files;
557 }
558
559 $file = array();
560 $file['name'] = sanitize_file_name( $args['name'] );
561 $file['tmp_name'] = sanitize_text_field( $args['tmp_name'] );
562 $file['type'] = sanitize_text_field( $args['type'] );
563 $file['size'] = absint( $args['size'] );
564 $file['error'] = absint( $args['error'] );
565
566 return $file;
567 }
568
569 /**
570 * Sanitizes file upload values within the array.
571 *
572 * This addresses nested file fields within repeaters and groups.
573 *
574 * @since 6.0.5
575 *
576 * @param array $array The file upload array.
577 * @param string $sanitize_function Callback used to sanitize array value.
578 * @return array
579 */
580 function acf_sanitize_files_value_array( $array, $sanitize_function ) {
581 if ( ! function_exists( $sanitize_function ) ) {
582 return $array;
583 }
584
585 if ( ! is_array( $array ) ) {
586 return $sanitize_function( $array );
587 }
588
589 foreach ( $array as $key => $value ) {
590 if ( is_array( $value ) ) {
591 $array[ $key ] = acf_sanitize_files_value_array( $value, $sanitize_function );
592 } else {
593 $array[ $key ] = $sanitize_function( $value );
594 }
595 }
596
597 return $array;
598 }
599
600 /**
601 * Maybe unserialize, but don't allow any classes.
602 *
603 * @since 6.1
604 *
605 * @param string $data String to be unserialized, if serialized.
606 * @return mixed The unserialized, or original data.
607 */
608 function acf_maybe_unserialize( $data ) {
609 if ( is_serialized( $data ) ) { // Don't attempt to unserialize data that wasn't serialized going in.
610 return @unserialize( trim( $data ), array( 'allowed_classes' => false ) ); //phpcs:ignore -- allowed classes is false.
611 }
612
613 return $data;
614 }
615
616 /**
617 * Check if ACF is a beta-like release.
618 *
619 * @since 6.3
620 *
621 * @return boolean True if the current install version contains a dash, indicating a alpha, beta or RC release.
622 */
623 function acf_is_beta() {
624 return defined( 'ACF_VERSION' ) && strpos( ACF_VERSION, '-' ) !== false;
625 }
626
627 /**
628 * Returns the version of ACF when it was first activated.
629 * However, if ACF was first activated prior to the introduction of the acf_first_activated_version option,
630 * this function returns false (boolean) to indicate that the version could not be determined.
631 *
632 * @since 6.3
633 *
634 * @return string|boolean The (string) version of ACF when it was first activated, or false (boolean) if the version could not be determined.
635 */
636 function acf_get_version_when_first_activated() {
637 // Check if ACF is network-activated on a multisite.
638 if ( is_multisite() ) {
639 $acf_dir_and_filename = basename( ACF_PATH ) . '/acf.php';
640 $plugins = get_site_option( 'active_sitewide_plugins' );
641
642 if ( isset( $plugins[ $acf_dir_and_filename ] ) ) {
643 $main_site_id = get_main_site_id();
644
645 if ( empty( $main_site_id ) ) {
646 return false;
647 }
648
649 // ACF is network activated, so get the version from main site's options.
650 return get_blog_option( $main_site_id, 'acf_first_activated_version', false );
651 }
652 }
653
654 // Check if ACF is activated on this single site.
655 return get_option( 'acf_first_activated_version', false );
656 }
657