fields.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'acf_fields' ) ) :
  6. class acf_fields {
  7. /** @var array Contains an array of field type instances */
  8. var $types = array();
  9. /*
  10. * __construct
  11. *
  12. * This function will setup the class functionality
  13. *
  14. * @type function
  15. * @date 5/03/2014
  16. * @since 5.4.0
  17. *
  18. * @param n/a
  19. * @return n/a
  20. */
  21. function __construct() {
  22. /* do nothing */
  23. }
  24. /*
  25. * register_field_type
  26. *
  27. * This function will register a field type instance
  28. *
  29. * @type function
  30. * @date 6/07/2016
  31. * @since 5.4.0
  32. *
  33. * @param $class (string)
  34. * @return n/a
  35. */
  36. function register_field_type( $class ) {
  37. // allow instance
  38. if ( $class instanceof acf_field ) {
  39. $this->types[ $class->name ] = $class;
  40. // allow class name
  41. } else {
  42. $instance = new $class();
  43. $this->types[ $instance->name ] = $instance;
  44. }
  45. }
  46. /*
  47. * get_field_type
  48. *
  49. * This function will return a field type instance
  50. *
  51. * @type function
  52. * @date 6/07/2016
  53. * @since 5.4.0
  54. *
  55. * @param $name (string)
  56. * @return (mixed)
  57. */
  58. function get_field_type( $name ) {
  59. return isset( $this->types[ $name ] ) ? $this->types[ $name ] : null;
  60. }
  61. /*
  62. * is_field_type
  63. *
  64. * This function will return true if a field type exists
  65. *
  66. * @type function
  67. * @date 6/07/2016
  68. * @since 5.4.0
  69. *
  70. * @param $name (string)
  71. * @return (mixed)
  72. */
  73. function is_field_type( $name ) {
  74. return isset( $this->types[ $name ] );
  75. }
  76. /*
  77. * register_field_type_info
  78. *
  79. * This function will store a basic array of info about the field type
  80. * to later be overriden by the above register_field_type function
  81. *
  82. * @type function
  83. * @date 29/5/17
  84. * @since 5.6.0
  85. *
  86. * @param $info (array)
  87. * @return n/a
  88. */
  89. function register_field_type_info( $info ) {
  90. // convert to object
  91. $instance = (object) $info;
  92. $this->types[ $instance->name ] = $instance;
  93. }
  94. /*
  95. * get_field_types
  96. *
  97. * This function will return an array of all field types
  98. *
  99. * @type function
  100. * @date 6/07/2016
  101. * @since 5.4.0
  102. *
  103. * @param $name (string)
  104. * @return (mixed)
  105. */
  106. function get_field_types() {
  107. return $this->types;
  108. }
  109. }
  110. // initialize
  111. acf()->fields = new acf_fields();
  112. endif; // class_exists check
  113. /*
  114. * acf_register_field_type
  115. *
  116. * alias of acf()->fields->register_field_type()
  117. *
  118. * @type function
  119. * @date 31/5/17
  120. * @since 5.6.0
  121. *
  122. * @param n/a
  123. * @return n/a
  124. */
  125. function acf_register_field_type( $class ) {
  126. return acf()->fields->register_field_type( $class );
  127. }
  128. /*
  129. * acf_register_field_type_info
  130. *
  131. * alias of acf()->fields->register_field_type_info()
  132. *
  133. * @type function
  134. * @date 31/5/17
  135. * @since 5.6.0
  136. *
  137. * @param n/a
  138. * @return n/a
  139. */
  140. function acf_register_field_type_info( $info ) {
  141. return acf()->fields->register_field_type_info( $info );
  142. }
  143. /*
  144. * acf_get_field_type
  145. *
  146. * alias of acf()->fields->get_field_type()
  147. *
  148. * @type function
  149. * @date 31/5/17
  150. * @since 5.6.0
  151. *
  152. * @param n/a
  153. * @return n/a
  154. */
  155. function acf_get_field_type( $name ) {
  156. return acf()->fields->get_field_type( $name );
  157. }
  158. /*
  159. * acf_get_field_types
  160. *
  161. * alias of acf()->fields->get_field_types()
  162. *
  163. * @type function
  164. * @date 31/5/17
  165. * @since 5.6.0
  166. *
  167. * @param n/a
  168. * @return n/a
  169. */
  170. function acf_get_field_types( $args = array() ) {
  171. // default
  172. $args = wp_parse_args(
  173. $args,
  174. array(
  175. 'public' => true, // true, false
  176. )
  177. );
  178. // get field types
  179. $field_types = acf()->fields->get_field_types();
  180. // filter
  181. return wp_filter_object_list( $field_types, $args );
  182. }
  183. /**
  184. * acf_get_field_types_info
  185. *
  186. * Returns an array containing information about each field type
  187. *
  188. * @date 18/6/18
  189. * @since 5.6.9
  190. *
  191. * @param type $var Description. Default.
  192. * @return type Description.
  193. */
  194. function acf_get_field_types_info( $args = array() ) {
  195. // vars
  196. $data = array();
  197. $field_types = acf_get_field_types();
  198. // loop
  199. foreach ( $field_types as $type ) {
  200. $data[ $type->name ] = array(
  201. 'label' => $type->label,
  202. 'name' => $type->name,
  203. 'category' => $type->category,
  204. 'public' => $type->public,
  205. );
  206. }
  207. // return
  208. return $data;
  209. }
  210. /*
  211. * acf_is_field_type
  212. *
  213. * alias of acf()->fields->is_field_type()
  214. *
  215. * @type function
  216. * @date 31/5/17
  217. * @since 5.6.0
  218. *
  219. * @param n/a
  220. * @return n/a
  221. */
  222. function acf_is_field_type( $name = '' ) {
  223. return acf()->fields->is_field_type( $name );
  224. }
  225. /*
  226. * acf_get_field_type_prop
  227. *
  228. * This function will return a field type's property
  229. *
  230. * @type function
  231. * @date 1/10/13
  232. * @since 5.0.0
  233. *
  234. * @param n/a
  235. * @return (array)
  236. */
  237. function acf_get_field_type_prop( $name = '', $prop = '' ) {
  238. $type = acf_get_field_type( $name );
  239. return ( $type && isset( $type->$prop ) ) ? $type->$prop : null;
  240. }
  241. /*
  242. * acf_get_field_type_label
  243. *
  244. * This function will return the label of a field type
  245. *
  246. * @type function
  247. * @date 1/10/13
  248. * @since 5.0.0
  249. *
  250. * @param n/a
  251. * @return (array)
  252. */
  253. function acf_get_field_type_label( $name = '' ) {
  254. $label = acf_get_field_type_prop( $name, 'label' );
  255. return $label ? $label : '<span class="acf-tooltip-js" title="' . __( 'Field type does not exist', 'acf' ) . '">' . __( 'Unknown', 'acf' ) . '</span>';
  256. }
  257. /*
  258. * acf_field_type_exists (deprecated)
  259. *
  260. * deprecated in favour of acf_is_field_type()
  261. *
  262. * @type function
  263. * @date 1/10/13
  264. * @since 5.0.0
  265. *
  266. * @param $type (string)
  267. * @return (boolean)
  268. */
  269. function acf_field_type_exists( $type = '' ) {
  270. return acf_is_field_type( $type );
  271. }
  272. /*
  273. * acf_get_grouped_field_types
  274. *
  275. * Returns an multi-dimentional array of field types "name => label" grouped by category
  276. *
  277. * @type function
  278. * @date 1/10/13
  279. * @since 5.0.0
  280. *
  281. * @param n/a
  282. * @return (array)
  283. */
  284. function acf_get_grouped_field_types() {
  285. // vars
  286. $types = acf_get_field_types();
  287. $groups = array();
  288. $l10n = array(
  289. 'basic' => __( 'Basic', 'acf' ),
  290. 'content' => __( 'Content', 'acf' ),
  291. 'choice' => __( 'Choice', 'acf' ),
  292. 'relational' => __( 'Relational', 'acf' ),
  293. 'jquery' => __( 'jQuery', 'acf' ),
  294. 'layout' => __( 'Layout', 'acf' ),
  295. );
  296. // loop
  297. foreach ( $types as $type ) {
  298. // translate
  299. $cat = $type->category;
  300. $cat = isset( $l10n[ $cat ] ) ? $l10n[ $cat ] : $cat;
  301. // append
  302. $groups[ $cat ][ $type->name ] = $type->label;
  303. }
  304. // filter
  305. $groups = apply_filters( 'acf/get_field_types', $groups );
  306. // return
  307. return $groups;
  308. }