acf_field_functions

Field Functions.

Defined (1)

The class is defined in the following location(s).

/core/fields/_functions.php  
  1. class acf_field_functions 
  2.  
  3. /** 
  4. * __construct 
  5. * @description:  
  6. * @since 3.1.8 
  7. * @created: 23/06/12 
  8. */ 
  9.  
  10. function __construct() 
  11. //value 
  12. add_filter('acf/load_value', array($this, 'load_value'), 5, 3); 
  13. add_action('acf/update_value', array($this, 'update_value'), 5, 3); 
  14. add_action('acf/delete_value', array($this, 'delete_value'), 5, 2); 
  15. add_action('acf/format_value', array($this, 'format_value'), 5, 3); 
  16. add_action('acf/format_value_for_api', array($this, 'format_value_for_api'), 5, 3); 
  17.  
  18.  
  19. // field 
  20. add_filter('acf/load_field', array($this, 'load_field'), 5, 3); 
  21. add_action('acf/update_field', array($this, 'update_field'), 5, 2); 
  22. add_action('acf/delete_field', array($this, 'delete_field'), 5, 2); 
  23. add_action('acf/create_field', array($this, 'create_field'), 5, 1); 
  24. add_action('acf/create_field_options', array($this, 'create_field_options'), 5, 1); 
  25.  
  26.  
  27. // extra 
  28. add_filter('acf/load_field_defaults', array($this, 'load_field_defaults'), 5, 1); 
  29.  
  30.  
  31. /** 
  32. * load_value 
  33. * @description: loads basic value from the db 
  34. * @since: 3.6 
  35. * @created: 23/01/13 
  36. */ 
  37.  
  38. function load_value($value, $post_id, $field) 
  39. $found = false; 
  40. $cache = wp_cache_get( 'load_value/post_id=' . $post_id . '/name=' . $field['name'], 'acf', false, $found ); 
  41.  
  42. if( $found ) 
  43. return $cache; 
  44.  
  45.  
  46. // set default value 
  47. $value = false; 
  48.  
  49.  
  50. // if $post_id is a string, then it is used in the everything fields and can be found in the options table 
  51. if( is_numeric($post_id) ) 
  52. $v = get_post_meta( $post_id, $field['name'], false ); 
  53.  
  54. // value is an array 
  55. if( isset($v[0]) ) 
  56. $value = $v[0]; 
  57.  
  58. elseif( strpos($post_id, 'user_') !== false ) 
  59. $post_id = str_replace('user_', '', $post_id); 
  60.  
  61. $v = get_user_meta( $post_id, $field['name'], false ); 
  62.  
  63. // value is an array 
  64. if( isset($v[0]) ) 
  65. $value = $v[0]; 
  66.  
  67. else 
  68. $v = get_option( $post_id . '_' . $field['name'], false ); 
  69.  
  70. if( !is_null($value) ) 
  71. $value = $v; 
  72.  
  73.  
  74. // no value? 
  75. if( $value === false ) 
  76. if( isset($field['default_value']) && $field['default_value'] !== "" ) 
  77. $value = $field['default_value']; 
  78.  
  79.  
  80. // if value was duplicated, it may now be a serialized string! 
  81. $value = maybe_unserialize($value); 
  82.  
  83.  
  84. // apply filters 
  85. foreach( array('type', 'name', 'key') as $key ) 
  86. // run filters 
  87. $value = apply_filters('acf/load_value/' . $key . '=' . $field[ $key ], $value, $post_id, $field); // new filter 
  88.  
  89.  
  90. //update cache 
  91. wp_cache_set( 'load_value/post_id=' . $post_id . '/name=' . $field['name'], $value, 'acf' ); 
  92.  
  93.  
  94. return $value; 
  95.  
  96.  
  97. /** 
  98. * format_value 
  99. * @description: uses the basic value and allows the field type to format it 
  100. * @since: 3.6 
  101. * @created: 26/01/13 
  102. */ 
  103.  
  104. function format_value( $value, $post_id, $field ) 
  105. $value = apply_filters('acf/format_value/type=' . $field['type'], $value, $post_id, $field); 
  106.  
  107. return $value; 
  108.  
  109.  
  110. /** 
  111. * format_value_for_api 
  112. * @description: uses the basic value and allows the field type to format it or the api functions 
  113. * @since: 3.6 
  114. * @created: 26/01/13 
  115. */ 
  116.  
  117. function format_value_for_api( $value, $post_id, $field ) 
  118. $value = apply_filters('acf/format_value_for_api/type=' . $field['type'], $value, $post_id, $field); 
  119.  
  120. return $value; 
  121.  
  122.  
  123. /** 
  124. * update_value 
  125. * updates a value into the db 
  126. * @type action 
  127. * @date 23/01/13 
  128. * @param {mixed} $value the value to be saved 
  129. * @param {int} $post_id the post ID to save the value to 
  130. * @param {array} $field the field array 
  131. * @param {boolean} $exact allows the update_value filter to be skipped 
  132. * @return N/A 
  133. */ 
  134.  
  135. function update_value( $value, $post_id, $field ) 
  136.  
  137. // strip slashes 
  138. // - not needed? http://support.advancedcustomfields.com/discussion/3168/backslashes-stripped-in-wysiwyg-filed 
  139. //if( get_magic_quotes_gpc() ) 
  140. //{ 
  141. $value = stripslashes_deep($value); 
  142. //} 
  143.  
  144.  
  145. // apply filters  
  146. foreach( array('key', 'name', 'type') as $key ) 
  147. // run filters 
  148. $value = apply_filters('acf/update_value/' . $key . '=' . $field[ $key ], $value, $post_id, $field); // new filter 
  149.  
  150.  
  151. // if $post_id is a string, then it is used in the everything fields and can be found in the options table 
  152. if( is_numeric($post_id) ) 
  153. // allow ACF to save to revision! 
  154. update_metadata('post', $post_id, $field['name'], $value ); 
  155. update_metadata('post', $post_id, '_' . $field['name'], $field['key']); 
  156. elseif( strpos($post_id, 'user_') !== false ) 
  157. $user_id = str_replace('user_', '', $post_id); 
  158. update_metadata('user', $user_id, $field['name'], $value); 
  159. update_metadata('user', $user_id, '_' . $field['name'], $field['key']); 
  160. else 
  161. // for some reason, update_option does not use stripslashes_deep. 
  162. // update_metadata -> http://core.trac.wordpress.org/browser/tags/3.4.2/wp-includes/meta.php#L82: line 101 (does use stripslashes_deep) 
  163. // update_option -> http://core.trac.wordpress.org/browser/tags/3.5.1/wp-includes/option.php#L0: line 215 (does not use stripslashes_deep) 
  164. $value = stripslashes_deep($value); 
  165.  
  166. $this->update_option( $post_id . '_' . $field['name'], $value ); 
  167. $this->update_option( '_' . $post_id . '_' . $field['name'], $field['key'] ); 
  168.  
  169.  
  170. // update the cache 
  171. wp_cache_set( 'load_value/post_id=' . $post_id . '/name=' . $field['name'], $value, 'acf' ); 
  172.  
  173.  
  174.  
  175. /** 
  176. * update_option 
  177. * This function is a wrapper for the WP update_option but provides logic for a 'no' autoload 
  178. * @type function 
  179. * @date 4/01/2014 
  180. * @since 5.0.0 
  181. * @param $option (string) 
  182. * @param $value (mixed) 
  183. * @return (boolean) 
  184. */ 
  185.  
  186. function update_option( $option = '', $value = false, $autoload = 'no' ) { 
  187.  
  188. // vars 
  189. $deprecated = ''; 
  190. $return = false; 
  191.  
  192.  
  193. if( get_option($option) !== false ) 
  194. $return = update_option( $option, $value ); 
  195. else 
  196. $return = add_option( $option, $value, $deprecated, $autoload ); 
  197.  
  198.  
  199. // return 
  200. return $return; 
  201.  
  202.  
  203.  
  204. /** 
  205. * delete_value 
  206. * @description: deletes a value from the database 
  207. * @since: 3.6 
  208. * @created: 23/01/13 
  209. */ 
  210.  
  211. function delete_value( $post_id, $key ) 
  212. // if $post_id is a string, then it is used in the everything fields and can be found in the options table 
  213. if( is_numeric($post_id) ) 
  214. delete_post_meta( $post_id, $key ); 
  215. delete_post_meta( $post_id, '_' . $key ); 
  216. elseif( strpos($post_id, 'user_') !== false ) 
  217. $post_id = str_replace('user_', '', $post_id); 
  218. delete_user_meta( $post_id, $key ); 
  219. delete_user_meta( $post_id, '_' . $key ); 
  220. else 
  221. delete_option( $post_id . '_' . $key ); 
  222. delete_option( '_' . $post_id . '_' . $key ); 
  223.  
  224. wp_cache_delete( 'load_value/post_id=' . $post_id . '/name=' . $key, 'acf' ); 
  225.  
  226.  
  227. /** 
  228. * load_field 
  229. * @description: loads a field from the database 
  230. * @since 3.5.1 
  231. * @created: 14/10/12 
  232. */ 
  233.  
  234. function load_field( $field, $field_key, $post_id = false ) 
  235. // load cache 
  236. if( !$field ) 
  237. $field = wp_cache_get( 'load_field/key=' . $field_key, 'acf' ); 
  238.  
  239.  
  240. // load from DB 
  241. if( !$field ) 
  242. // vars 
  243. global $wpdb; 
  244.  
  245.  
  246. // get field from postmeta 
  247. $sql = $wpdb->prepare("SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = %s", $field_key); 
  248.  
  249. if( $post_id ) 
  250. $sql .= $wpdb->prepare("AND post_id = %d", $post_id); 
  251.  
  252. $rows = $wpdb->get_results( $sql, ARRAY_A ); 
  253.  
  254.  
  255.  
  256. // nothing found? 
  257. if( !empty($rows) ) 
  258. $row = $rows[0]; 
  259.  
  260.  
  261. /** 
  262. * WPML compatibility 
  263. * If WPML is active, and the $post_id (Field group ID) was not defined,  
  264. * it is assumed that the load_field functio has been called from the API (front end). 
  265. * In this case, the field group ID is never known and we can check for the correct translated field group 
  266. */ 
  267.  
  268. if( defined('ICL_LANGUAGE_CODE') && !$post_id ) 
  269. $wpml_post_id = icl_object_id($row['post_id'], 'acf', true, ICL_LANGUAGE_CODE); 
  270.  
  271. foreach( $rows as $r ) 
  272. if( $r['post_id'] == $wpml_post_id ) 
  273. // this row is a field from the translated field group 
  274. $row = $r; 
  275.  
  276.  
  277. // return field if it is not in a trashed field group 
  278. if( get_post_status( $row['post_id'] ) != "trash" ) 
  279. $field = $row['meta_value']; 
  280. $field = maybe_unserialize( $field ); 
  281. $field = maybe_unserialize( $field ); // run again for WPML 
  282.  
  283.  
  284. // add field_group ID 
  285. $field['field_group'] = $row['post_id']; 
  286.  
  287.  
  288.  
  289. // apply filters 
  290. $field = apply_filters('acf/load_field_defaults', $field); 
  291.  
  292.  
  293. // apply filters 
  294. foreach( array('type', 'name', 'key') as $key ) 
  295. // run filters 
  296. $field = apply_filters('acf/load_field/' . $key . '=' . $field[ $key ], $field); // new filter 
  297.  
  298.  
  299. // set cache 
  300. wp_cache_set( 'load_field/key=' . $field_key, $field, 'acf' ); 
  301.  
  302. return $field; 
  303.  
  304.  
  305. /** 
  306. * load_field_defaults 
  307. * @description: applies default values to the field after it has been loaded 
  308. * @since 3.5.1 
  309. * @created: 14/10/12 
  310. */ 
  311.  
  312. function load_field_defaults( $field ) 
  313. // validate $field 
  314. if( !is_array($field) ) 
  315. $field = array(); 
  316.  
  317.  
  318. // defaults 
  319. $defaults = array( 
  320. 'key' => '',  
  321. 'label' => '',  
  322. 'name' => '',  
  323. '_name' => '',  
  324. 'type' => 'text',  
  325. 'order_no' => 1,  
  326. 'instructions' => '',  
  327. 'required' => 0,  
  328. 'id' => '',  
  329. 'class' => '',  
  330. 'conditional_logic' => array( 
  331. 'status' => 0,  
  332. 'allorany' => 'all',  
  333. 'rules' => 0 
  334. ),  
  335. ); 
  336. $field = array_merge($defaults, $field); 
  337.  
  338.  
  339. // Parse Values 
  340. $field = apply_filters( 'acf/parse_types', $field ); 
  341.  
  342.  
  343. // field specific defaults 
  344. $field = apply_filters('acf/load_field_defaults/type=' . $field['type'] , $field); 
  345.  
  346.  
  347. // class 
  348. if( !$field['class'] ) 
  349. $field['class'] = $field['type']; 
  350.  
  351.  
  352. // id 
  353. if( !$field['id'] ) 
  354. $id = $field['name']; 
  355. $id = str_replace('][', '_', $id); 
  356. $id = str_replace('fields[', '', $id); 
  357. $id = str_replace('[', '-', $id); // location rules (select) does'nt have "fields[" in it 
  358. $id = str_replace(']', '', $id); 
  359.  
  360. $field['id'] = 'acf-field-' . $id; 
  361.  
  362.  
  363. // _name 
  364. if( !$field['_name'] ) 
  365. $field['_name'] = $field['name']; 
  366.  
  367.  
  368. // clean up conditional logic keys 
  369. if( !empty($field['conditional_logic']['rules']) ) 
  370. $field['conditional_logic']['rules'] = array_values($field['conditional_logic']['rules']); 
  371.  
  372.  
  373. // return 
  374. return $field; 
  375.  
  376.  
  377. /** 
  378. * update_field 
  379. * @description: updates a field in the database 
  380. * @since: 3.6 
  381. * @created: 24/01/13 
  382. */ 
  383.  
  384. function update_field( $field, $post_id ) 
  385. // sanitize field name 
  386. // - http://support.advancedcustomfields.com/discussion/5262/sanitize_title-on-field-name 
  387. // - issue with camel case! Replaced with JS 
  388. //$field['name'] = sanitize_title( $field['name'] ); 
  389.  
  390.  
  391. // filters 
  392. $field = apply_filters('acf/update_field/type=' . $field['type'], $field, $post_id ); // new filter 
  393.  
  394.  
  395. // clear cache 
  396. wp_cache_delete( 'load_field/key=' . $field['key'], 'acf' ); 
  397.  
  398.  
  399. // save 
  400. update_post_meta( $post_id, $field['key'], $field ); 
  401.  
  402.  
  403. /** 
  404. * delete_field 
  405. * @description: deletes a field in the database 
  406. * @since: 3.6 
  407. * @created: 24/01/13 
  408. */ 
  409.  
  410. function delete_field( $post_id, $field_key ) 
  411. // clear cache 
  412. wp_cache_delete( 'load_field/key=' . $field_key, 'acf' ); 
  413.  
  414.  
  415. // delete 
  416. delete_post_meta($post_id, $field_key); 
  417.  
  418.  
  419. /** 
  420. * create_field 
  421. * @description: renders a field into a HTML interface 
  422. * @since: 3.6 
  423. * @created: 23/01/13 
  424. */ 
  425.  
  426. function create_field( $field ) 
  427. // load defaults 
  428. // if field was loaded from db, these default will already be appield 
  429. // if field was written by hand, it may be missing keys 
  430. $field = apply_filters('acf/load_field_defaults', $field); 
  431.  
  432.  
  433. // create field specific html 
  434. do_action('acf/create_field/type=' . $field['type'], $field); 
  435.  
  436.  
  437. // conditional logic 
  438. if( $field['conditional_logic']['status'] ) 
  439. $field['conditional_logic']['field'] = $field['key']; 
  440.  
  441. ?> 
  442. <script type="text/javascript"> 
  443. (function($) { 
  444.  
  445. if( typeof acf !== 'undefined' ) 
  446. acf.conditional_logic.items.push(<?php echo json_encode($field['conditional_logic']); ?>); 
  447.  
  448. })(jQuery);  
  449. </script> 
  450. <?php 
  451.  
  452.  
  453.  
  454. /** 
  455. * create_field_options 
  456. * @description: renders a field into a HTML interface 
  457. * @since: 3.6 
  458. * @created: 23/01/13 
  459. */ 
  460.  
  461. function create_field_options($field) 
  462. // load standard + field specific defaults 
  463. $field = apply_filters('acf/load_field_defaults', $field); 
  464.  
  465. // render HTML 
  466. do_action('acf/create_field_options/type=' . $field['type'], $field); 
  467.  
  468.  
  469.