acf-helper-functions.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. <?php
  2. /*
  3. * acf_is_empty
  4. *
  5. * Returns true if the value provided is considered "empty". Allows numbers such as 0.
  6. *
  7. * @date 6/7/16
  8. * @since 5.4.0
  9. *
  10. * @param mixed $var The value to check.
  11. * @return bool
  12. */
  13. function acf_is_empty( $var ) {
  14. return ( ! $var && ! is_numeric( $var ) );
  15. }
  16. /**
  17. * acf_not_empty
  18. *
  19. * Returns true if the value provided is considered "not empty". Allows numbers such as 0.
  20. *
  21. * @date 15/7/19
  22. * @since 5.8.1
  23. *
  24. * @param mixed $var The value to check.
  25. * @return bool
  26. */
  27. function acf_not_empty( $var ) {
  28. return ( $var || is_numeric( $var ) );
  29. }
  30. /**
  31. * acf_uniqid
  32. *
  33. * Returns a unique numeric based id.
  34. *
  35. * @date 9/1/19
  36. * @since 5.7.10
  37. *
  38. * @param string $prefix The id prefix. Defaults to 'acf'.
  39. * @return string
  40. */
  41. function acf_uniqid( $prefix = 'acf' ) {
  42. // Instantiate global counter.
  43. global $acf_uniqid;
  44. if ( ! isset( $acf_uniqid ) ) {
  45. $acf_uniqid = 1;
  46. }
  47. // Return id.
  48. return $prefix . '-' . $acf_uniqid++;
  49. }
  50. /**
  51. * acf_merge_attributes
  52. *
  53. * Merges together two arrays but with extra functionality to append class names.
  54. *
  55. * @date 22/1/19
  56. * @since 5.7.10
  57. *
  58. * @param array $array1 An array of attributes.
  59. * @param array $array2 An array of attributes.
  60. * @return array
  61. */
  62. function acf_merge_attributes( $array1, $array2 ) {
  63. // Merge together attributes.
  64. $array3 = array_merge( $array1, $array2 );
  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. // Return.
  72. return $array3;
  73. }
  74. /**
  75. * acf_cache_key
  76. *
  77. * Returns a filtered cache key.
  78. *
  79. * @date 25/1/19
  80. * @since 5.7.11
  81. *
  82. * @param string $key The cache key.
  83. * @return string
  84. */
  85. function acf_cache_key( $key = '' ) {
  86. /**
  87. * Filters the cache key.
  88. *
  89. * @date 25/1/19
  90. * @since 5.7.11
  91. *
  92. * @param string $key The cache key.
  93. * @param string $original_key The original cache key.
  94. */
  95. return apply_filters( 'acf/get_cache_key', $key, $key );
  96. }
  97. /**
  98. * acf_request_args
  99. *
  100. * Returns an array of $_REQUEST values using the provided defaults.
  101. *
  102. * @date 28/2/19
  103. * @since 5.7.13
  104. *
  105. * @param array $args An array of args.
  106. * @return array
  107. */
  108. function acf_request_args( $args = array() ) {
  109. foreach ( $args as $k => $v ) {
  110. $args[ $k ] = isset( $_REQUEST[ $k ] ) ? acf_sanitize_request_args( $_REQUEST[ $k ] ) : $args[ $k ]; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Verified elsewhere.
  111. }
  112. return $args;
  113. }
  114. /**
  115. * Returns a single $_REQUEST arg with fallback.
  116. *
  117. * @date 23/10/20
  118. * @since 5.9.2
  119. *
  120. * @param string $key The property name.
  121. * @param mixed $default The default value to fallback to.
  122. * @return mixed
  123. */
  124. function acf_request_arg( $name = '', $default = null ) {
  125. return isset( $_REQUEST[ $name ] ) ? acf_sanitize_request_args( $_REQUEST[ $name ] ) : $default; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
  126. }
  127. // Register store.
  128. acf_register_store( 'filters' );
  129. /**
  130. * acf_enable_filter
  131. *
  132. * Enables a filter with the given name.
  133. *
  134. * @date 14/7/16
  135. * @since 5.4.0
  136. *
  137. * @param string name The modifer name.
  138. * @return void
  139. */
  140. function acf_enable_filter( $name = '' ) {
  141. acf_get_store( 'filters' )->set( $name, true );
  142. }
  143. /**
  144. * acf_disable_filter
  145. *
  146. * Disables a filter with the given name.
  147. *
  148. * @date 14/7/16
  149. * @since 5.4.0
  150. *
  151. * @param string name The modifer name.
  152. * @return void
  153. */
  154. function acf_disable_filter( $name = '' ) {
  155. acf_get_store( 'filters' )->set( $name, false );
  156. }
  157. /**
  158. * acf_is_filter_enabled
  159. *
  160. * Returns the state of a filter for the given name.
  161. *
  162. * @date 14/7/16
  163. * @since 5.4.0
  164. *
  165. * @param string name The modifer name.
  166. * @return array
  167. */
  168. function acf_is_filter_enabled( $name = '' ) {
  169. return acf_get_store( 'filters' )->get( $name );
  170. }
  171. /**
  172. * acf_get_filters
  173. *
  174. * Returns an array of filters in their current state.
  175. *
  176. * @date 14/7/16
  177. * @since 5.4.0
  178. *
  179. * @param void
  180. * @return array
  181. */
  182. function acf_get_filters() {
  183. return acf_get_store( 'filters' )->get();
  184. }
  185. /**
  186. * acf_set_filters
  187. *
  188. * Sets an array of filter states.
  189. *
  190. * @date 14/7/16
  191. * @since 5.4.0
  192. *
  193. * @param array $filters An Array of modifers
  194. * @return array
  195. */
  196. function acf_set_filters( $filters = array() ) {
  197. acf_get_store( 'filters' )->set( $filters );
  198. }
  199. /**
  200. * acf_disable_filters
  201. *
  202. * Disables all filters and returns the previous state.
  203. *
  204. * @date 14/7/16
  205. * @since 5.4.0
  206. *
  207. * @param void
  208. * @return array
  209. */
  210. function acf_disable_filters() {
  211. // Get state.
  212. $prev_state = acf_get_filters();
  213. // Set all modifers as false.
  214. acf_set_filters( array_map( '__return_false', $prev_state ) );
  215. // Return prev state.
  216. return $prev_state;
  217. }
  218. /**
  219. * acf_enable_filters
  220. *
  221. * Enables all or an array of specific filters and returns the previous state.
  222. *
  223. * @date 14/7/16
  224. * @since 5.4.0
  225. *
  226. * @param array $filters An Array of modifers
  227. * @return array
  228. */
  229. function acf_enable_filters( $filters = array() ) {
  230. // Get state.
  231. $prev_state = acf_get_filters();
  232. // Allow specific filters to be enabled.
  233. if ( $filters ) {
  234. acf_set_filters( $filters );
  235. // Set all modifers as true.
  236. } else {
  237. acf_set_filters( array_map( '__return_true', $prev_state ) );
  238. }
  239. // Return prev state.
  240. return $prev_state;
  241. }
  242. /**
  243. * acf_idval
  244. *
  245. * Parses the provided value for an ID.
  246. *
  247. * @date 29/3/19
  248. * @since 5.7.14
  249. *
  250. * @param mixed $value A value to parse.
  251. * @return int
  252. */
  253. function acf_idval( $value ) {
  254. // Check if value is numeric.
  255. if ( is_numeric( $value ) ) {
  256. return (int) $value;
  257. // Check if value is array.
  258. } elseif ( is_array( $value ) ) {
  259. return (int) isset( $value['ID'] ) ? $value['ID'] : 0;
  260. // Check if value is object.
  261. } elseif ( is_object( $value ) ) {
  262. return (int) isset( $value->ID ) ? $value->ID : 0;
  263. }
  264. // Return default.
  265. return 0;
  266. }
  267. /**
  268. * acf_maybe_idval
  269. *
  270. * Checks value for potential id value.
  271. *
  272. * @date 6/4/19
  273. * @since 5.7.14
  274. *
  275. * @param mixed $value A value to parse.
  276. * @return mixed
  277. */
  278. function acf_maybe_idval( $value ) {
  279. if ( $id = acf_idval( $value ) ) {
  280. return $id;
  281. }
  282. return $value;
  283. }
  284. /**
  285. * Convert any numeric strings into their equivalent numeric type. This function will
  286. * work with both single values and arrays.
  287. *
  288. * @param mixed $value Either a single value or an array of values.
  289. * @return mixed
  290. */
  291. function acf_format_numerics( $value ) {
  292. if ( is_array( $value ) ) {
  293. return array_map(
  294. function ( $v ) {
  295. return is_numeric( $v ) ? $v + 0 : $v;
  296. },
  297. $value
  298. );
  299. }
  300. return is_numeric( $value ) ? $value + 0 : $value;
  301. }
  302. /**
  303. * acf_numval
  304. *
  305. * Casts the provided value as eiter an int or float using a simple hack.
  306. *
  307. * @date 11/4/19
  308. * @since 5.7.14
  309. *
  310. * @param mixed $value A value to parse.
  311. * @return (int|float)
  312. */
  313. function acf_numval( $value ) {
  314. return ( intval( $value ) == floatval( $value ) ) ? intval( $value ) : floatval( $value );
  315. }
  316. /**
  317. * acf_idify
  318. *
  319. * Returns an id attribute friendly string.
  320. *
  321. * @date 24/12/17
  322. * @since 5.6.5
  323. *
  324. * @param string $str The string to convert.
  325. * @return string
  326. */
  327. function acf_idify( $str = '' ) {
  328. return str_replace( array( '][', '[', ']' ), array( '-', '-', '' ), strtolower( $str ) );
  329. }
  330. /**
  331. * Returns a slug friendly string.
  332. *
  333. * @date 24/12/17
  334. * @since 5.6.5
  335. *
  336. * @param string $str The string to convert.
  337. * @param string $glue The glue between each slug piece.
  338. * @return string
  339. */
  340. function acf_slugify( $str = '', $glue = '-' ) {
  341. $raw = $str;
  342. $slug = str_replace( array( '_', '-', '/', ' ' ), $glue, strtolower( remove_accents( $raw ) ) );
  343. $slug = preg_replace( '/[^A-Za-z0-9' . preg_quote( $glue ) . ']/', '', $slug );
  344. /**
  345. * Filters the slug created by acf_slugify().
  346. *
  347. * @since 5.11.4
  348. *
  349. * @param string $slug The newly created slug.
  350. * @param string $raw The original string.
  351. * @param string $glue The separator used to join the string into a slug.
  352. */
  353. return apply_filters( 'acf/slugify', $slug, $raw, $glue );
  354. }
  355. /**
  356. * Returns a string with correct full stop punctuation.
  357. *
  358. * @date 12/7/19
  359. * @since 5.8.2
  360. *
  361. * @param string $str The string to format.
  362. * @return string
  363. */
  364. function acf_punctify( $str = '' ) {
  365. if ( substr( trim( strip_tags( $str ) ), -1 ) !== '.' ) {
  366. return trim( $str ) . '.';
  367. }
  368. return trim( $str );
  369. }
  370. /**
  371. * acf_did
  372. *
  373. * Returns true if ACF already did an event.
  374. *
  375. * @date 30/8/19
  376. * @since 5.8.1
  377. *
  378. * @param string $name The name of the event.
  379. * @return bool
  380. */
  381. function acf_did( $name ) {
  382. // Return true if already did the event (preventing event).
  383. if ( acf_get_data( "acf_did_$name" ) ) {
  384. return true;
  385. // Otherwise, update store and return false (alowing event).
  386. } else {
  387. acf_set_data( "acf_did_$name", true );
  388. return false;
  389. }
  390. }
  391. /**
  392. * Returns the length of a string that has been submitted via $_POST.
  393. *
  394. * Uses the following process:
  395. * 1. Unslash the string because posted values will be slashed.
  396. * 2. Decode special characters because wp_kses() will normalize entities.
  397. * 3. Treat line-breaks as a single character instead of two.
  398. * 4. Use mb_strlen() to accomodate special characters.
  399. *
  400. * @date 04/06/2020
  401. * @since 5.9.0
  402. *
  403. * @param string $str The string to review.
  404. * @return int
  405. */
  406. function acf_strlen( $str ) {
  407. return mb_strlen( str_replace( "\r\n", "\n", wp_specialchars_decode( wp_unslash( $str ) ) ) );
  408. }
  409. /**
  410. * Returns a value with default fallback.
  411. *
  412. * @date 6/4/20
  413. * @since 5.9.0
  414. *
  415. * @param mixed $value The value.
  416. * @param mixed $default_value The default value.
  417. * @return mixed
  418. */
  419. function acf_with_default( $value, $default_value ) {
  420. return $value ? $value : $default_value;
  421. }
  422. /**
  423. * Returns the current priority of a running action.
  424. *
  425. * @date 14/07/2020
  426. * @since 5.9.0
  427. *
  428. * @param string $action The action name.
  429. * @return int|bool
  430. */
  431. function acf_doing_action( $action ) {
  432. global $wp_filter;
  433. if ( isset( $wp_filter[ $action ] ) ) {
  434. return $wp_filter[ $action ]->current_priority();
  435. }
  436. return false;
  437. }
  438. /**
  439. * Returns the current URL.
  440. *
  441. * @date 23/01/2015
  442. * @since 5.1.5
  443. *
  444. * @param void
  445. * @return string
  446. */
  447. function acf_get_current_url() {
  448. // Ensure props exist to avoid PHP Notice during CLI commands.
  449. if ( isset( $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'] ) ) {
  450. return ( is_ssl() ? 'https' : 'http' ) . '://' . filter_var( $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL );
  451. }
  452. return '';
  453. }
  454. /**
  455. * Add UTM tracking tags to internal ACF URLs
  456. *
  457. * @since 6.0.0
  458. *
  459. * @param string $url The URL to be tagged.
  460. * @param string $campaign The campaign tag.
  461. * @param string $content The UTM content tag.
  462. * @return string
  463. */
  464. function acf_add_url_utm_tags( $url, $campaign, $content, $anchor = false ) {
  465. $anchor_url = $anchor ? '#' . $anchor : '';
  466. $query = http_build_query(
  467. apply_filters(
  468. 'acf/admin/acf_url_utm_parameters',
  469. array(
  470. 'utm_source' => ( defined( 'ACF_PRO' ) && ACF_PRO ) ? 'ACF PRO' : 'ACF Free',
  471. 'utm_medium' => 'insideplugin',
  472. 'utm_campagin' => $campaign,
  473. 'utm_content' => $content,
  474. )
  475. )
  476. );
  477. if ( $query ) {
  478. $query = '?' . $query;
  479. }
  480. return esc_url( $url . $query . $anchor_url );
  481. }
  482. /**
  483. * Sanitizes request arguments.
  484. *
  485. * @param mixed $args The data to sanitize.
  486. *
  487. * @return array|bool|float|int|mixed|string
  488. */
  489. function acf_sanitize_request_args( $args = array() ) {
  490. switch ( gettype( $args ) ) {
  491. case 'boolean':
  492. return (bool) $args;
  493. case 'integer':
  494. return (int) $args;
  495. case 'double':
  496. return (float) $args;
  497. case 'array':
  498. $sanitized = [];
  499. foreach ( $args as $key => $value ) {
  500. $key = sanitize_text_field( $key );
  501. $sanitized[ $key ] = acf_sanitize_request_args( $value );
  502. }
  503. return $sanitized;
  504. case 'object':
  505. return wp_kses_post_deep( $args );
  506. case 'string':
  507. default:
  508. return wp_kses( $args, 'acf' );
  509. }
  510. }
  511. /**
  512. * Sanitizes file upload arrays.
  513. *
  514. * @since 6.0.4
  515. *
  516. * @param array $args The file array.
  517. *
  518. * @return array
  519. */
  520. function acf_sanitize_files_array( array $args = array() ) {
  521. $defaults = array(
  522. 'name' => '',
  523. 'tmp_name' => '',
  524. 'type' => '',
  525. 'size' => 0,
  526. 'error' => '',
  527. );
  528. $args = wp_parse_args( $args, $defaults );
  529. if ( empty( $args['name'] ) ) {
  530. return $defaults;
  531. }
  532. if ( is_array( $args['name'] ) ) {
  533. $files = array();
  534. $files['name'] = acf_sanitize_files_value_array( $args['name'], 'sanitize_file_name' );
  535. $files['tmp_name'] = acf_sanitize_files_value_array( $args['tmp_name'], 'sanitize_text_field' );
  536. $files['type'] = acf_sanitize_files_value_array( $args['type'], 'sanitize_text_field' );
  537. $files['size'] = acf_sanitize_files_value_array( $args['size'], 'absint' );
  538. $files['error'] = acf_sanitize_files_value_array( $args['error'], 'absint' );
  539. return $files;
  540. }
  541. $file = array();
  542. $file['name'] = sanitize_file_name( $args['name'] );
  543. $file['tmp_name'] = sanitize_text_field( $args['tmp_name'] );
  544. $file['type'] = sanitize_text_field( $args['type'] );
  545. $file['size'] = absint( $args['size'] );
  546. $file['error'] = absint( $args['error'] );
  547. return $file;
  548. }
  549. /**
  550. * Sanitizes file upload values within the array.
  551. *
  552. * This addresses nested file fields within repeaters and groups.
  553. *
  554. * @since 6.0.5
  555. *
  556. * @param array $array The file upload array.
  557. * @param string $sanitize_function Callback used to sanitize array value.
  558. * @return array
  559. */
  560. function acf_sanitize_files_value_array( $array, $sanitize_function ) {
  561. if ( ! function_exists( $sanitize_function ) ) {
  562. return $array;
  563. }
  564. if ( ! is_array( $array ) ) {
  565. return $sanitize_function( $array );
  566. }
  567. foreach ( $array as $key => $value ) {
  568. if ( is_array( $value ) ) {
  569. $array[ $key ] = acf_sanitize_files_value_array( $value, $sanitize_function );
  570. } else {
  571. $array[ $key ] = $sanitize_function( $value );
  572. }
  573. }
  574. return $array;
  575. }