/core/fields/_base.php

  1. <?php 
  2.  
  3. /** 
  4. * acf_field 
  5. * 
  6. * @description: This is the base class for all fields. 
  7. * @since: 3.6 
  8. * @created: 30/01/13 
  9. */ 
  10.  
  11. class acf_field 
  12. /** 
  13. * Vars 
  14. * 
  15. * @description:  
  16. * @since: 3.6 
  17. * @created: 30/01/13 
  18. */ 
  19.  
  20. var $name,  
  21. $title,  
  22. $category,  
  23. $defaults,  
  24. $l10n; 
  25.  
  26.  
  27. /** 
  28. * __construct() 
  29. * 
  30. * Adds neccessary Actions / Filters 
  31. * 
  32. * @since 3.6 
  33. * @date 30/01/13 
  34. */ 
  35.  
  36. function __construct() 
  37. // register field 
  38. add_filter('acf/registered_fields', array($this, 'registered_fields'), 10, 1); 
  39. add_filter('acf/load_field_defaults/type=' . $this->name, array($this, 'load_field_defaults'), 10, 1); 
  40.  
  41.  
  42. // value 
  43. $this->add_filter('acf/load_value/type=' . $this->name, array($this, 'load_value'), 10, 3); 
  44. $this->add_filter('acf/update_value/type=' . $this->name, array($this, 'update_value'), 10, 3); 
  45. $this->add_filter('acf/format_value/type=' . $this->name, array($this, 'format_value'), 10, 3); 
  46. $this->add_filter('acf/format_value_for_api/type=' . $this->name, array($this, 'format_value_for_api'), 10, 3); 
  47.  
  48.  
  49. // field 
  50. $this->add_filter('acf/load_field/type=' . $this->name, array($this, 'load_field'), 10, 3); 
  51. $this->add_filter('acf/update_field/type=' . $this->name, array($this, 'update_field'), 10, 2); 
  52. $this->add_action('acf/create_field/type=' . $this->name, array($this, 'create_field'), 10, 1); 
  53. $this->add_action('acf/create_field_options/type=' . $this->name, array($this, 'create_options'), 10, 1); 
  54.  
  55.  
  56. // input actions 
  57. $this->add_action('acf/input/admin_enqueue_scripts', array($this, 'input_admin_enqueue_scripts'), 10, 0); 
  58. $this->add_action('acf/input/admin_head', array($this, 'input_admin_head'), 10, 0); 
  59. $this->add_filter('acf/input/admin_l10n', array($this, 'input_admin_l10n'), 10, 1); 
  60.  
  61.  
  62. // field group actions 
  63. $this->add_action('acf/field_group/admin_enqueue_scripts', array($this, 'field_group_admin_enqueue_scripts'), 10, 0); 
  64. $this->add_action('acf/field_group/admin_head', array($this, 'field_group_admin_head'), 10, 0); 
  65.  
  66.  
  67.  
  68. /** 
  69. * add_filter 
  70. * 
  71. * @description: checks if the function is_callable before adding the filter 
  72. * @since: 3.6 
  73. * @created: 30/01/13 
  74. */ 
  75.  
  76. function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) 
  77. if( is_callable($function_to_add) ) 
  78. add_filter($tag, $function_to_add, $priority, $accepted_args); 
  79.  
  80.  
  81. /** 
  82. * add_action 
  83. * 
  84. * @description: checks if the function is_callable before adding the action 
  85. * @since: 3.6 
  86. * @created: 30/01/13 
  87. */ 
  88.  
  89. function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) 
  90. if( is_callable($function_to_add) ) 
  91. add_action($tag, $function_to_add, $priority, $accepted_args); 
  92.  
  93.  
  94. /** 
  95. * registered_fields() 
  96. * 
  97. * Adds this field to the select list when creating a new field 
  98. * 
  99. * @type filter 
  100. * @since 3.6 
  101. * @date 23/01/13 
  102. * 
  103. * @param $fields - the array of all registered fields 
  104. * 
  105. * @return $fields - the array of all registered fields 
  106. */ 
  107.  
  108. function registered_fields( $fields ) 
  109. // defaults 
  110. if( !$this->category ) 
  111. $this->category = __('Basic', 'acf'); 
  112.  
  113.  
  114. // add to array 
  115. $fields[ $this->category ][ $this->name ] = $this->label; 
  116.  
  117.  
  118. // return array 
  119. return $fields; 
  120.  
  121.  
  122. /** 
  123. * load_field_defaults 
  124. * 
  125. * action called when rendering the head of an admin screen. Used primarily for passing PHP to JS 
  126. * 
  127. * @type filer 
  128. * @date 1/06/13 
  129. * 
  130. * @param $field {array} 
  131. * @return $field {array} 
  132. */ 
  133.  
  134. function load_field_defaults( $field ) 
  135. if( !empty($this->defaults) ) 
  136. foreach( $this->defaults as $k => $v ) 
  137. if( !isset($field[ $k ]) ) 
  138. $field[ $k ] = $v; 
  139.  
  140. return $field; 
  141.  
  142.  
  143. /** 
  144. * admin_l10n 
  145. * 
  146. * filter is called to load all l10n text translations into the admin head script tag 
  147. * 
  148. * @type filer 
  149. * @date 1/06/13 
  150. * 
  151. * @param $field {array} 
  152. * @return $field {array} 
  153. */ 
  154.  
  155. function input_admin_l10n( $l10n ) 
  156. if( !empty($this->l10n) ) 
  157. $l10n[ $this->name ] = $this->l10n; 
  158.  
  159. return $l10n; 
  160.  
  161.  
  162.  
  163. ?> 
.