No Description

acf-value-functions.php 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. // Register store.
  3. acf_register_store( 'values' )->prop( 'multisite', true );
  4. /**
  5. * acf_get_reference
  6. *
  7. * Retrieves the field key for a given field name and post_id.
  8. *
  9. * @date 26/1/18
  10. * @since 5.6.5
  11. *
  12. * @param string $field_name The name of the field. eg 'sub_heading'.
  13. * @param mixed $post_id The post_id of which the value is saved against.
  14. * @return string The field key.
  15. */
  16. function acf_get_reference( $field_name, $post_id ) {
  17. // Allow filter to short-circuit load_value logic.
  18. $reference = apply_filters( 'acf/pre_load_reference', null, $field_name, $post_id );
  19. if ( $reference !== null ) {
  20. return $reference;
  21. }
  22. // Get hidden meta for this field name.
  23. $reference = acf_get_metadata( $post_id, $field_name, true );
  24. /**
  25. * Filters the reference value.
  26. *
  27. * @date 25/1/19
  28. * @since 5.7.11
  29. *
  30. * @param string $reference The reference value.
  31. * @param string $field_name The field name.
  32. * @param (int|string) $post_id The post ID where meta is stored.
  33. */
  34. return apply_filters( 'acf/load_reference', $reference, $field_name, $post_id );
  35. }
  36. /**
  37. * Retrieves the value for a given field and post_id.
  38. *
  39. * @date 28/09/13
  40. * @since 5.0.0
  41. *
  42. * @param int|string $post_id The post id.
  43. * @param array $field The field array.
  44. * @return mixed
  45. */
  46. function acf_get_value( $post_id, $field ) {
  47. // Allow filter to short-circuit load_value logic.
  48. $value = apply_filters( 'acf/pre_load_value', null, $post_id, $field );
  49. if ( $value !== null ) {
  50. return $value;
  51. }
  52. // Get field name.
  53. $field_name = $field['name'];
  54. // If we still don't have a proper field array, the field doesn't exist currently.
  55. if ( empty( $field['type'] ) && empty( $field['key'] ) ) {
  56. // Get field ID & type.
  57. $decoded = acf_decode_post_id( $post_id );
  58. if ( apply_filters( 'acf/prevent_access_to_unknown_fields', false ) || ( 'option' === $decoded['type'] && 'options' !== $decoded['id'] ) ) {
  59. return null;
  60. }
  61. do_action( 'acf/get_invalid_field_value', $field, __FUNCTION__ );
  62. }
  63. // Check store.
  64. $store = acf_get_store( 'values' );
  65. if ( $store->has( "$post_id:$field_name" ) ) {
  66. return $store->get( "$post_id:$field_name" );
  67. }
  68. // Load value from database.
  69. $value = acf_get_metadata( $post_id, $field_name );
  70. // Use field's default_value if no meta was found.
  71. if ( $value === null && isset( $field['default_value'] ) ) {
  72. $value = $field['default_value'];
  73. }
  74. /**
  75. * Filters the $value after it has been loaded.
  76. *
  77. * @date 28/09/13
  78. * @since 5.0.0
  79. *
  80. * @param mixed $value The value to preview.
  81. * @param string $post_id The post ID for this value.
  82. * @param array $field The field array.
  83. */
  84. $value = apply_filters( 'acf/load_value', $value, $post_id, $field );
  85. // Update store.
  86. $store->set( "$post_id:$field_name", $value );
  87. // Return value.
  88. return $value;
  89. }
  90. // Register variation.
  91. acf_add_filter_variations( 'acf/load_value', array( 'type', 'name', 'key' ), 2 );
  92. /**
  93. * acf_format_value
  94. *
  95. * Returns a formatted version of the provided value.
  96. *
  97. * @date 28/09/13
  98. * @since 5.0.0
  99. *
  100. * @param mixed $value The field value.
  101. * @param (int|string) $post_id The post id.
  102. * @param array $field The field array.
  103. * @return mixed.
  104. */
  105. function acf_format_value( $value, $post_id, $field ) {
  106. // Allow filter to short-circuit load_value logic.
  107. $check = apply_filters( 'acf/pre_format_value', null, $value, $post_id, $field );
  108. if ( $check !== null ) {
  109. return $check;
  110. }
  111. // Get field name.
  112. $field_name = $field['name'];
  113. // Check store.
  114. $store = acf_get_store( 'values' );
  115. if ( $store->has( "$post_id:$field_name:formatted" ) ) {
  116. return $store->get( "$post_id:$field_name:formatted" );
  117. }
  118. /**
  119. * Filters the $value for use in a template function.
  120. *
  121. * @date 28/09/13
  122. * @since 5.0.0
  123. *
  124. * @param mixed $value The value to preview.
  125. * @param string $post_id The post ID for this value.
  126. * @param array $field The field array.
  127. */
  128. $value = apply_filters( 'acf/format_value', $value, $post_id, $field );
  129. // Update store.
  130. $store->set( "$post_id:$field_name:formatted", $value );
  131. // Return value.
  132. return $value;
  133. }
  134. // Register variation.
  135. acf_add_filter_variations( 'acf/format_value', array( 'type', 'name', 'key' ), 2 );
  136. /**
  137. * acf_update_value
  138. *
  139. * Updates the value for a given field and post_id.
  140. *
  141. * @date 28/09/13
  142. * @since 5.0.0
  143. *
  144. * @param mixed $value The new value.
  145. * @param (int|string) $post_id The post id.
  146. * @param array $field The field array.
  147. * @return bool.
  148. */
  149. function acf_update_value( $value, $post_id, $field ) {
  150. // Allow filter to short-circuit update_value logic.
  151. $check = apply_filters( 'acf/pre_update_value', null, $value, $post_id, $field );
  152. if ( $check !== null ) {
  153. return $check;
  154. }
  155. /**
  156. * Filters the $value before it is updated.
  157. *
  158. * @date 28/09/13
  159. * @since 5.0.0
  160. *
  161. * @param mixed $value The value to update.
  162. * @param string $post_id The post ID for this value.
  163. * @param array $field The field array.
  164. * @param mixed $original The original value before modification.
  165. */
  166. $value = apply_filters( 'acf/update_value', $value, $post_id, $field, $value );
  167. // Allow null to delete value.
  168. if ( $value === null ) {
  169. return acf_delete_value( $post_id, $field );
  170. }
  171. // Update meta.
  172. $return = acf_update_metadata( $post_id, $field['name'], $value );
  173. // Update reference.
  174. acf_update_metadata( $post_id, $field['name'], $field['key'], true );
  175. // Delete stored data.
  176. acf_flush_value_cache( $post_id, $field['name'] );
  177. // Return update status.
  178. return $return;
  179. }
  180. // Register variation.
  181. acf_add_filter_variations( 'acf/update_value', array( 'type', 'name', 'key' ), 2 );
  182. /**
  183. * acf_update_values
  184. *
  185. * Updates an array of values.
  186. *
  187. * @date 26/2/19
  188. * @since 5.7.13
  189. *
  190. * @param array values The array of values.
  191. * @param (int|string) $post_id The post id.
  192. * @return void
  193. */
  194. function acf_update_values( $values, $post_id ) {
  195. // Loop over values.
  196. foreach ( $values as $key => $value ) {
  197. // Get field.
  198. $field = acf_get_field( $key );
  199. // Update value.
  200. if ( $field ) {
  201. acf_update_value( $value, $post_id, $field );
  202. }
  203. }
  204. }
  205. /**
  206. * acf_flush_value_cache
  207. *
  208. * Deletes all cached data for this value.
  209. *
  210. * @date 22/1/19
  211. * @since 5.7.10
  212. *
  213. * @param (int|string) $post_id The post id.
  214. * @param string $field_name The field name.
  215. * @return void
  216. */
  217. function acf_flush_value_cache( $post_id = 0, $field_name = '' ) {
  218. // Delete stored data.
  219. acf_get_store( 'values' )
  220. ->remove( "$post_id:$field_name" )
  221. ->remove( "$post_id:$field_name:formatted" );
  222. }
  223. /**
  224. * acf_delete_value
  225. *
  226. * Deletes the value for a given field and post_id.
  227. *
  228. * @date 28/09/13
  229. * @since 5.0.0
  230. *
  231. * @param (int|string) $post_id The post id.
  232. * @param array $field The field array.
  233. * @return bool.
  234. */
  235. function acf_delete_value( $post_id, $field ) {
  236. /**
  237. * Fires before a value is deleted.
  238. *
  239. * @date 28/09/13
  240. * @since 5.0.0
  241. *
  242. * @param string $post_id The post ID for this value.
  243. * @param mixed $name The meta name.
  244. * @param array $field The field array.
  245. */
  246. do_action( 'acf/delete_value', $post_id, $field['name'], $field );
  247. // Delete meta.
  248. $return = acf_delete_metadata( $post_id, $field['name'] );
  249. // Delete reference.
  250. acf_delete_metadata( $post_id, $field['name'], true );
  251. // Delete stored data.
  252. acf_flush_value_cache( $post_id, $field['name'] );
  253. // Return delete status.
  254. return $return;
  255. }
  256. // Register variation.
  257. acf_add_filter_variations( 'acf/delete_value', array( 'type', 'name', 'key' ), 2 );
  258. /**
  259. * acf_preview_value
  260. *
  261. * Return a human friendly 'preview' for a given field value.
  262. *
  263. * @date 28/09/13
  264. * @since 5.0.0
  265. *
  266. * @param mixed $value The new value.
  267. * @param (int|string) $post_id The post id.
  268. * @param array $field The field array.
  269. * @return bool.
  270. */
  271. function acf_preview_value( $value, $post_id, $field ) {
  272. /**
  273. * Filters the $value before used in HTML.
  274. *
  275. * @date 24/10/16
  276. * @since 5.5.0
  277. *
  278. * @param mixed $value The value to preview.
  279. * @param string $post_id The post ID for this value.
  280. * @param array $field The field array.
  281. */
  282. return apply_filters( 'acf/preview_value', $value, $post_id, $field );
  283. }
  284. // Register variation.
  285. acf_add_filter_variations( 'acf/preview_value', array( 'type', 'name', 'key' ), 2 );
  286. /**
  287. * Potentially log an error if a field doesn't exist when we expect it to.
  288. *
  289. * @param array $field An array representing the field that a value was requested for.
  290. * @param string $function The function that noticed the problem.
  291. *
  292. * @return void
  293. */
  294. function acf_log_invalid_field_notice( $field, $function ) {
  295. // If "init" has fired, ACF probably wasn't initialized early.
  296. if ( did_action( 'init' ) ) {
  297. return;
  298. }
  299. $error_text = sprintf(
  300. __( '<strong>%1$s</strong> - We\'ve detected one or more calls to retrieve ACF field values before ACF has been initialized. This is not supported and can result in malformed or missing data. <a href="%2$s" target="_blank">Learn how to fix this</a>.', 'acf' ),
  301. acf_get_setting( 'name' ),
  302. 'https://www.advancedcustomfields.com/resources/acf-field-functions/'
  303. );
  304. _doing_it_wrong( $function, $error_text, '5.11.1' );
  305. }
  306. add_action( 'acf/get_invalid_field_value', 'acf_log_invalid_field_notice', 10, 2 );