/wp-content/plugins/advanced-custom-fields-pro/api/api-value.php

https://gitlab.com/sihabudinahmad/asppi · PHP · 372 lines · 136 code · 122 blank · 114 comment · 19 complexity · 0e934aa006c24af9653b0aea4bd89353 MD5 · raw file

  1. <?php
  2. /*
  3. * acf_get_value
  4. *
  5. * This function will load in a field's value
  6. *
  7. * @type function
  8. * @date 28/09/13
  9. * @since 5.0.0
  10. *
  11. * @param $value (mixed)
  12. * @param $post_id (int)
  13. * @param $field (array)
  14. * @param $format (boolean)
  15. * @param $format_template (boolean)
  16. * @return (mixed)
  17. */
  18. function acf_get_value( $post_id, $field, $db_only = false ) {
  19. // vars
  20. $value = null;
  21. // try cache
  22. $found = false;
  23. $cache = wp_cache_get( "load_value/post_id={$post_id}/name={$field['name']}", 'acf', false, $found );
  24. if( $found ) {
  25. return $cache;
  26. }
  27. // load value depending on the $type
  28. if( empty($post_id) ) {
  29. // do nothing
  30. } elseif( is_numeric($post_id) ) {
  31. $v = get_post_meta( $post_id, $field['name'], false );
  32. // value is an array
  33. if( isset($v[0]) ) {
  34. $value = $v[0];
  35. }
  36. } elseif( strpos($post_id, 'user_') !== false ) {
  37. $user_id = str_replace('user_', '', $post_id);
  38. $user_id = intval( $user_id );
  39. $v = get_user_meta( $user_id, $field['name'], false );
  40. // value is an array
  41. if( isset($v[0]) ) {
  42. $value = $v[0];
  43. }
  44. } elseif( strpos($post_id, 'comment_') !== false ) {
  45. $comment_id = str_replace('comment_', '', $post_id);
  46. $comment_id = intval( $comment_id );
  47. $v = get_comment_meta( $comment_id, $field['name'], false );
  48. // value is an array
  49. if( isset($v[0]) ) {
  50. $value = $v[0];
  51. }
  52. } else {
  53. $v = get_option( "{$post_id}_{$field['name']}", null );
  54. if( ! is_null($v) ) {
  55. $value = $v;
  56. }
  57. }
  58. // no value? try default_value
  59. if( $value === null && isset($field['default_value']) ) {
  60. $value = $field['default_value'];
  61. }
  62. // if value was duplicated, it may now be a serialized string!
  63. $value = maybe_unserialize( $value );
  64. // bail early if db only value (no need to update cache)
  65. if( $db_only ) {
  66. return $value;
  67. }
  68. // filter for 3rd party customization
  69. $value = apply_filters( "acf/load_value", $value, $post_id, $field );
  70. $value = apply_filters( "acf/load_value/type={$field['type']}", $value, $post_id, $field );
  71. $value = apply_filters( "acf/load_value/name={$field['name']}", $value, $post_id, $field );
  72. $value = apply_filters( "acf/load_value/key={$field['key']}", $value, $post_id, $field );
  73. //update cache
  74. wp_cache_set( "load_value/post_id={$post_id}/name={$field['name']}", $value, 'acf' );
  75. // return
  76. return $value;
  77. }
  78. /*
  79. * acf_format_value
  80. *
  81. * This function will format the value for front end use
  82. *
  83. * @type function
  84. * @date 3/07/2014
  85. * @since 5.0.0
  86. *
  87. * @param $value (mixed)
  88. * @param $post_id (mixed)
  89. * @param $field (array)
  90. * @return $value
  91. */
  92. function acf_format_value( $value, $post_id, $field ) {
  93. // apply filters
  94. $value = apply_filters( "acf/format_value", $value, $post_id, $field );
  95. $value = apply_filters( "acf/format_value/type={$field['type']}", $value, $post_id, $field );
  96. // return
  97. return $value;
  98. }
  99. /*
  100. * acf_update_value
  101. *
  102. * updates a value into the db
  103. *
  104. * @type action
  105. * @date 23/01/13
  106. *
  107. * @param {mixed} $value the value to be saved
  108. * @param {int} $post_id the post ID to save the value to
  109. * @param {array} $field the field array
  110. * @param {boolean} $exact allows the update_value filter to be skipped
  111. * @return N/A
  112. */
  113. function acf_update_value( $value = null, $post_id = 0, $field ) {
  114. // vars
  115. $return = false;
  116. // strip slashes
  117. // allow 3rd party customisation
  118. if( acf_get_setting('stripslashes') )
  119. {
  120. $value = stripslashes_deep($value);
  121. }
  122. // filter for 3rd party customization
  123. $value = apply_filters( "acf/update_value", $value, $post_id, $field );
  124. $value = apply_filters( "acf/update_value/type={$field['type']}", $value, $post_id, $field );
  125. $value = apply_filters( "acf/update_value/name={$field['name']}", $value, $post_id, $field );
  126. $value = apply_filters( "acf/update_value/key={$field['key']}", $value, $post_id, $field );
  127. // note:
  128. // attempted to save values as individual rows for better WP_Query compatibility. Issues are clear that order would not work.
  129. if( is_numeric($post_id) )
  130. {
  131. // allow ACF to save to revision!
  132. $return = update_metadata('post', $post_id, $field['name'], $value );
  133. update_metadata('post', $post_id, '_' . $field['name'], $field['key']);
  134. }
  135. elseif( strpos($post_id, 'user_') !== false )
  136. {
  137. $user_id = str_replace('user_', '', $post_id);
  138. $return = update_metadata('user', $user_id, $field['name'], $value);
  139. update_metadata('user', $user_id, '_' . $field['name'], $field['key']);
  140. }
  141. elseif( strpos($post_id, 'comment_') !== false )
  142. {
  143. $comment_id = str_replace('comment_', '', $post_id);
  144. $return = update_metadata('comment', $comment_id, $field['name'], $value);
  145. update_metadata('comment', $comment_id, '_' . $field['name'], $field['key']);
  146. }
  147. else
  148. {
  149. // for some reason, update_option does not use stripslashes_deep.
  150. // update_metadata -> http://core.trac.wordpress.org/browser/tags/3.4.2/wp-includes/meta.php#L82: line 101 (does use stripslashes_deep)
  151. // update_option -> http://core.trac.wordpress.org/browser/tags/3.5.1/wp-includes/option.php#L0: line 215 (does not use stripslashes_deep)
  152. $value = stripslashes_deep($value);
  153. $return = acf_update_option( $post_id . '_' . $field['name'], $value );
  154. acf_update_option( '_' . $post_id . '_' . $field['name'], $field['key'] );
  155. }
  156. // clear cache
  157. wp_cache_delete( "load_value/post_id={$post_id}/name={$field['name']}", 'acf' );
  158. // return
  159. return $return;
  160. }
  161. /*
  162. * acf_update_option
  163. *
  164. * This function is a wrapper for the WP update_option but provides logic for a 'no' autoload
  165. *
  166. * @type function
  167. * @date 4/01/2014
  168. * @since 5.0.0
  169. *
  170. * @param $option (string)
  171. * @param $value (mixed)
  172. * @return (boolean)
  173. */
  174. function acf_update_option( $option = '', $value = false, $autoload = 'no' ) {
  175. // vars
  176. $deprecated = '';
  177. $return = false;
  178. // add or update
  179. if( get_option($option) !== false ) {
  180. $return = update_option( $option, $value );
  181. } else {
  182. $return = add_option( $option, $value, $deprecated, $autoload );
  183. }
  184. // return
  185. return $return;
  186. }
  187. /*
  188. * acf_delete_value
  189. *
  190. * This function will delete a value from the database
  191. *
  192. * @type function
  193. * @date 28/09/13
  194. * @since 5.0.0
  195. *
  196. * @param $post_id (mixed)
  197. * @param $key (string|array) the meta_name or $field
  198. * @return (boolean)
  199. */
  200. function acf_delete_value( $post_id = 0, $key = '' ) {
  201. // vars
  202. $field = false;
  203. $return = false;
  204. // string
  205. if( is_string($key) ) {
  206. // find selector
  207. $field = acf_get_field_reference( $key, $post_id );
  208. // get field key
  209. $field = acf_get_field( $field );
  210. // field
  211. } elseif( is_array($key) ) {
  212. // set vars
  213. $field = $key;
  214. $key = $field['name'];
  215. // bail early if not valid key
  216. } else {
  217. return false;
  218. }
  219. // post
  220. if( is_numeric($post_id) ) {
  221. $return = delete_metadata('post', $post_id, $key );
  222. delete_metadata('post', $post_id, '_' . $key );
  223. // user
  224. } elseif( strpos($post_id, 'user_') !== false ) {
  225. $user_id = str_replace('user_', '', $post_id);
  226. $return = delete_metadata('user', $user_id, $key);
  227. delete_metadata('user', $user_id, '_' . $key);
  228. // comment
  229. } elseif( strpos($post_id, 'comment_') !== false ) {
  230. $comment_id = str_replace('comment_', '', $post_id);
  231. $return = delete_metadata('comment', $comment_id, $key);
  232. delete_metadata('comment', $comment_id, '_' . $key);
  233. // option
  234. } else {
  235. $return = delete_option( $post_id . '_' . $key );
  236. delete_option( '_' . $post_id . '_' . $key );
  237. }
  238. // clear cache
  239. wp_cache_delete( "load_value/post_id={$post_id}/name={$key}", 'acf' );
  240. // action for 3rd party customization
  241. do_action("acf/delete_value", $post_id, $key, $field);
  242. if( $field ) {
  243. do_action("acf/delete_value/type={$field['type']}", $post_id, $key, $field);
  244. do_action("acf/delete_value/name={$field['_name']}", $post_id, $key, $field);
  245. do_action("acf/delete_value/key={$field['key']}", $post_id, $key, $field);
  246. }
  247. // return
  248. return $return;
  249. }
  250. ?>