Нет описания

acf-helper-functions.php 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 ] ) ? $_REQUEST[ $k ] : $args[ $k ];
  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 ] ) ? $_REQUEST[ $name ] : $default;
  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. * acf_slugify
  332. *
  333. * Returns a slug friendly string.
  334. *
  335. * @date 24/12/17
  336. * @since 5.6.5
  337. *
  338. * @param string $str The string to convert.
  339. * @param string $glue The glue between each slug piece.
  340. * @return string
  341. */
  342. function acf_slugify( $str = '', $glue = '-' ) {
  343. return str_replace( array( '_', '-', '/', ' ' ), $glue, sanitize_title( strtolower( $str ) ) );
  344. }
  345. /**
  346. * Returns a string with correct full stop punctuation.
  347. *
  348. * @date 12/7/19
  349. * @since 5.8.2
  350. *
  351. * @param string $str The string to format.
  352. * @return string
  353. */
  354. function acf_punctify( $str = '' ) {
  355. if ( substr( trim( strip_tags( $str ) ), -1 ) !== '.' ) {
  356. return trim( $str ) . '.';
  357. }
  358. return trim( $str );
  359. }
  360. /**
  361. * acf_did
  362. *
  363. * Returns true if ACF already did an event.
  364. *
  365. * @date 30/8/19
  366. * @since 5.8.1
  367. *
  368. * @param string $name The name of the event.
  369. * @return bool
  370. */
  371. function acf_did( $name ) {
  372. // Return true if already did the event (preventing event).
  373. if ( acf_get_data( "acf_did_$name" ) ) {
  374. return true;
  375. // Otherwise, update store and return false (alowing event).
  376. } else {
  377. acf_set_data( "acf_did_$name", true );
  378. return false;
  379. }
  380. }
  381. /**
  382. * Returns the length of a string that has been submitted via $_POST.
  383. *
  384. * Uses the following process:
  385. * 1. Unslash the string because posted values will be slashed.
  386. * 2. Decode special characters because wp_kses() will normalize entities.
  387. * 3. Treat line-breaks as a single character instead of two.
  388. * 4. Use mb_strlen() to accomodate special characters.
  389. *
  390. * @date 04/06/2020
  391. * @since 5.9.0
  392. *
  393. * @param string $str The string to review.
  394. * @return int
  395. */
  396. function acf_strlen( $str ) {
  397. return mb_strlen( str_replace( "\r\n", "\n", wp_specialchars_decode( wp_unslash( $str ) ) ) );
  398. }
  399. /**
  400. * Returns a value with default fallback.
  401. *
  402. * @date 6/4/20
  403. * @since 5.9.0
  404. *
  405. * @param mixed $value The value.
  406. * @param mixed $default_value The default value.
  407. * @return mixed
  408. */
  409. function acf_with_default( $value, $default_value ) {
  410. return $value ? $value : $default_value;
  411. }
  412. /**
  413. * Returns the current priority of a running action.
  414. *
  415. * @date 14/07/2020
  416. * @since 5.9.0
  417. *
  418. * @param string $action The action name.
  419. * @return int|bool
  420. */
  421. function acf_doing_action( $action ) {
  422. global $wp_filter;
  423. if ( isset( $wp_filter[ $action ] ) ) {
  424. return $wp_filter[ $action ]->current_priority();
  425. }
  426. return false;
  427. }
  428. /**
  429. * Returns the current URL.
  430. *
  431. * @date 23/01/2015
  432. * @since 5.1.5
  433. *
  434. * @param void
  435. * @return string
  436. */
  437. function acf_get_current_url() {
  438. // Ensure props exist to avoid PHP Notice during CLI commands.
  439. if ( isset( $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'] ) ) {
  440. return ( is_ssl() ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  441. }
  442. return '';
  443. }