/wp-content/plugins/advanced-custom-fields/core/api.php

https://github.com/meanlumberjack/First-Estate · PHP · 353 lines · 161 code · 82 blank · 110 comment · 20 complexity · 78f8ef49c4c941d414da8ae14a4de3a5 MD5 · raw file

  1. <?php
  2. // set some globals
  3. reset_the_repeater_field();
  4. /*--------------------------------------------------------------------------------------
  5. *
  6. * get_fields
  7. *
  8. * @author Elliot Condon
  9. * @since 1.0.3
  10. *
  11. *-------------------------------------------------------------------------------------*/
  12. function get_fields($post_id = false)
  13. {
  14. // vars
  15. global $post;
  16. if(!$post_id)
  17. {
  18. $post_id = $post->ID;
  19. }
  20. elseif($post_id == "options")
  21. {
  22. $post_id = 999999999;
  23. }
  24. // default
  25. $value = array();
  26. $keys = get_post_custom_keys($post_id);
  27. if($keys)
  28. {
  29. foreach($keys as $key)
  30. {
  31. if(substr($key, 0, 1) != "_")
  32. {
  33. $value[$key] = get_field($key, $post_id);
  34. }
  35. }
  36. }
  37. // no value
  38. if(empty($value))
  39. {
  40. return false;
  41. }
  42. return $value;
  43. }
  44. /*--------------------------------------------------------------------------------------
  45. *
  46. * get_field
  47. *
  48. * @author Elliot Condon
  49. * @since 1.0.3
  50. *
  51. *-------------------------------------------------------------------------------------*/
  52. function get_field($field_name, $post_id = false)
  53. {
  54. global $post, $acf;
  55. if(!$post_id)
  56. {
  57. $post_id = $post->ID;
  58. }
  59. elseif($post_id == "options")
  60. {
  61. $post_id = 999999999;
  62. }
  63. // default
  64. $value = "";
  65. // get value
  66. $field_key = get_post_meta($post_id, '_' . $field_name, true);
  67. if($field_key != "")
  68. {
  69. // we can load the field properly!
  70. $field = $acf->get_acf_field($field_key);
  71. $value = $acf->get_value_for_api($post_id, $field);
  72. }
  73. else
  74. {
  75. // just load the text version
  76. $value = get_post_meta($post_id, $field_name, true);
  77. }
  78. // no value?
  79. if($value == "") $value = false;
  80. return $value;
  81. }
  82. /*--------------------------------------------------------------------------------------
  83. *
  84. * the_field
  85. *
  86. * @author Elliot Condon
  87. * @since 1.0.3
  88. *
  89. *-------------------------------------------------------------------------------------*/
  90. function the_field($field_name, $post_id = false)
  91. {
  92. $value = get_field($field_name, $post_id);
  93. if(is_array($value))
  94. {
  95. $value = @implode(', ',$value);
  96. }
  97. echo $value;
  98. }
  99. /*--------------------------------------------------------------------------------------
  100. *
  101. * the_repeater_field
  102. *
  103. * @author Elliot Condon
  104. * @since 1.0.3
  105. *
  106. *-------------------------------------------------------------------------------------*/
  107. function the_repeater_field($field_name, $post_id = false)
  108. {
  109. // if no field, create field + reset count
  110. if(!$GLOBALS['acf_field'])
  111. {
  112. reset_the_repeater_field();
  113. $GLOBALS['acf_field'] = get_field($field_name, $post_id);
  114. }
  115. // increase order_no
  116. $GLOBALS['acf_count']++;
  117. // vars
  118. $field = $GLOBALS['acf_field'];
  119. $i = $GLOBALS['acf_count'];
  120. if(isset($field[$i]))
  121. {
  122. return true;
  123. }
  124. // no row, reset the global values
  125. reset_the_repeater_field();
  126. return false;
  127. }
  128. function the_flexible_field($field_name, $post_id = false)
  129. {
  130. return the_repeater_field($field_name, $post_id);
  131. }
  132. /*--------------------------------------------------------------------------------------
  133. *
  134. * reset_the_repeater_field
  135. *
  136. * @author Elliot Condon
  137. * @since 1.0.3
  138. *
  139. *-------------------------------------------------------------------------------------*/
  140. function reset_the_repeater_field()
  141. {
  142. $GLOBALS['acf_field'] = false;
  143. $GLOBALS['acf_count'] = -1;
  144. }
  145. /*--------------------------------------------------------------------------------------
  146. *
  147. * get_sub_field
  148. *
  149. * @author Elliot Condon
  150. * @since 1.0.3
  151. *
  152. *-------------------------------------------------------------------------------------*/
  153. function get_sub_field($field_name)
  154. {
  155. // vars
  156. $field = $GLOBALS['acf_field'];
  157. $i = $GLOBALS['acf_count'];
  158. // no value
  159. if(!$field) return false;
  160. if(!isset($field[$i][$field_name])) return false;
  161. return $field[$i][$field_name];
  162. }
  163. /*--------------------------------------------------------------------------------------
  164. *
  165. * the_sub_field
  166. *
  167. * @author Elliot Condon
  168. * @since 1.0.3
  169. *
  170. *-------------------------------------------------------------------------------------*/
  171. function the_sub_field($field_name, $field = false)
  172. {
  173. $value = get_sub_field($field_name, $field);
  174. if(is_array($value))
  175. {
  176. $value = implode(', ',$value);
  177. }
  178. echo $value;
  179. }
  180. /*--------------------------------------------------------------------------------------
  181. *
  182. * register_field
  183. *
  184. * @author Elliot Condon
  185. * @since 3.0.0
  186. *
  187. *-------------------------------------------------------------------------------------*/
  188. $GLOBALS['acf_register_field'] = array();
  189. function register_field($class = "", $url = "")
  190. {
  191. $GLOBALS['acf_register_field'][] = array(
  192. 'url' => $url,
  193. 'class' => $class,
  194. );
  195. }
  196. function acf_register_field($array)
  197. {
  198. $array = array_merge($array, $GLOBALS['acf_register_field']);
  199. return $array;
  200. }
  201. add_filter('acf_register_field', 'acf_register_field');
  202. /*--------------------------------------------------------------------------------------
  203. *
  204. * register_field_group
  205. *
  206. * @author Elliot Condon
  207. * @since 3.0.6
  208. *
  209. *-------------------------------------------------------------------------------------*/
  210. $GLOBALS['acf_register_field_group'] = array();
  211. function register_field_group($array)
  212. {
  213. // add id
  214. $array['id'] = uniqid();
  215. $GLOBALS['acf_register_field_group'][] = $array;
  216. }
  217. function acf_register_field_group($array)
  218. {
  219. $array = array_merge($array, $GLOBALS['acf_register_field_group']);
  220. // order field groups based on menu_order
  221. // Obtain a list of columns
  222. foreach ($array as $key => $row) {
  223. $menu_order[$key] = $row['menu_order'];
  224. }
  225. // Sort the array with menu_order ascending
  226. // Add $array as the last parameter, to sort by the common key
  227. array_multisort($menu_order, SORT_ASC, $array);
  228. return $array;
  229. }
  230. add_filter('acf_register_field_group', 'acf_register_field_group');
  231. /*--------------------------------------------------------------------------------------
  232. *
  233. * register_options_page
  234. *
  235. * @author Elliot Condon
  236. * @since 3.0.0
  237. *
  238. *-------------------------------------------------------------------------------------*/
  239. $GLOBALS['acf_register_options_page'] = array();
  240. function register_options_page($title = "")
  241. {
  242. $GLOBALS['acf_register_options_page'][] = array(
  243. 'title' => $title,
  244. 'slug' => 'options-' . sanitize_title_with_dashes( $title ),
  245. );
  246. }
  247. function acf_register_options_page($array)
  248. {
  249. $array = array_merge($array, $GLOBALS['acf_register_options_page']);
  250. return $array;
  251. }
  252. add_filter('acf_register_options_page', 'acf_register_options_page');
  253. /*--------------------------------------------------------------------------------------
  254. *
  255. * get_sub_field
  256. *
  257. * @author Elliot Condon
  258. * @since 1.0.3
  259. *
  260. *-------------------------------------------------------------------------------------*/
  261. function get_row_layout()
  262. {
  263. // vars
  264. $field = $GLOBALS['acf_field'];
  265. $i = $GLOBALS['acf_count'];
  266. // no value
  267. if(!$field) return false;
  268. if(!isset($field[$i]['acf_fc_layout'])) return false;
  269. return $field[$i]['acf_fc_layout'];
  270. }
  271. ?>