class-acf-field.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. if ( ! class_exists( 'acf_field' ) ) :
  3. class acf_field {
  4. // vars
  5. var $name = '',
  6. $label = '',
  7. $category = 'basic',
  8. $defaults = array(),
  9. $l10n = array(),
  10. $public = true;
  11. public $show_in_rest = true;
  12. /*
  13. * __construct
  14. *
  15. * This function will initialize the field type
  16. *
  17. * @type function
  18. * @date 5/03/2014
  19. * @since 5.0.0
  20. *
  21. * @param n/a
  22. * @return n/a
  23. */
  24. function __construct() {
  25. // initialize
  26. $this->initialize();
  27. // register info
  28. acf_register_field_type_info(
  29. array(
  30. 'label' => $this->label,
  31. 'name' => $this->name,
  32. 'category' => $this->category,
  33. 'public' => $this->public,
  34. )
  35. );
  36. // value
  37. $this->add_field_filter( 'acf/load_value', array( $this, 'load_value' ), 10, 3 );
  38. $this->add_field_filter( 'acf/update_value', array( $this, 'update_value' ), 10, 3 );
  39. $this->add_field_filter( 'acf/format_value', array( $this, 'format_value' ), 10, 3 );
  40. $this->add_field_filter( 'acf/validate_value', array( $this, 'validate_value' ), 10, 4 );
  41. $this->add_field_action( 'acf/delete_value', array( $this, 'delete_value' ), 10, 3 );
  42. // field
  43. $this->add_field_filter( 'acf/validate_rest_value', array( $this, 'validate_rest_value' ), 10, 3 );
  44. $this->add_field_filter( 'acf/validate_field', array( $this, 'validate_field' ), 10, 1 );
  45. $this->add_field_filter( 'acf/load_field', array( $this, 'load_field' ), 10, 1 );
  46. $this->add_field_filter( 'acf/update_field', array( $this, 'update_field' ), 10, 1 );
  47. $this->add_field_filter( 'acf/duplicate_field', array( $this, 'duplicate_field' ), 10, 1 );
  48. $this->add_field_action( 'acf/delete_field', array( $this, 'delete_field' ), 10, 1 );
  49. $this->add_field_action( 'acf/render_field', array( $this, 'render_field' ), 9, 1 );
  50. $this->add_field_action( 'acf/render_field_settings', array( $this, 'render_field_settings' ), 9, 1 );
  51. $this->add_field_action( 'acf/render_field_general_settings', array( $this, 'render_field_general_settings' ), 9, 1 );
  52. $this->add_field_action( 'acf/render_field_validation_settings', array( $this, 'render_field_validation_settings' ), 9, 1 );
  53. $this->add_field_action( 'acf/render_field_presentation_settings', array( $this, 'render_field_presentation_settings' ), 9, 1 );
  54. $this->add_field_action( 'acf/render_field_conditional_logic_settings', array( $this, 'render_field_conditional_logic_settings' ), 9, 1 );
  55. $this->add_field_filter( 'acf/prepare_field', array( $this, 'prepare_field' ), 10, 1 );
  56. $this->add_field_filter( 'acf/translate_field', array( $this, 'translate_field' ), 10, 1 );
  57. // input actions
  58. $this->add_action( 'acf/input/admin_enqueue_scripts', array( $this, 'input_admin_enqueue_scripts' ), 10, 0 );
  59. $this->add_action( 'acf/input/admin_head', array( $this, 'input_admin_head' ), 10, 0 );
  60. $this->add_action( 'acf/input/form_data', array( $this, 'input_form_data' ), 10, 1 );
  61. $this->add_filter( 'acf/input/admin_l10n', array( $this, 'input_admin_l10n' ), 10, 1 );
  62. $this->add_action( 'acf/input/admin_footer', array( $this, 'input_admin_footer' ), 10, 1 );
  63. // field group actions
  64. $this->add_action( 'acf/field_group/admin_enqueue_scripts', array( $this, 'field_group_admin_enqueue_scripts' ), 10, 0 );
  65. $this->add_action( 'acf/field_group/admin_head', array( $this, 'field_group_admin_head' ), 10, 0 );
  66. $this->add_action( 'acf/field_group/admin_footer', array( $this, 'field_group_admin_footer' ), 10, 0 );
  67. }
  68. /*
  69. * initialize
  70. *
  71. * This function will initialize the field type
  72. *
  73. * @type function
  74. * @date 27/6/17
  75. * @since 5.6.0
  76. *
  77. * @param n/a
  78. * @return n/a
  79. */
  80. function initialize() {
  81. /* do nothing */
  82. }
  83. /*
  84. * add_filter
  85. *
  86. * This function checks if the function is_callable before adding the filter
  87. *
  88. * @type function
  89. * @date 5/03/2014
  90. * @since 5.0.0
  91. *
  92. * @param $tag (string)
  93. * @param $function_to_add (string)
  94. * @param $priority (int)
  95. * @param $accepted_args (int)
  96. * @return n/a
  97. */
  98. function add_filter( $tag = '', $function_to_add = '', $priority = 10, $accepted_args = 1 ) {
  99. // bail early if no callable
  100. if ( ! is_callable( $function_to_add ) ) {
  101. return;
  102. }
  103. // add
  104. add_filter( $tag, $function_to_add, $priority, $accepted_args );
  105. }
  106. /*
  107. * add_field_filter
  108. *
  109. * This function will add a field type specific filter
  110. *
  111. * @type function
  112. * @date 29/09/2016
  113. * @since 5.4.0
  114. *
  115. * @param $tag (string)
  116. * @param $function_to_add (string)
  117. * @param $priority (int)
  118. * @param $accepted_args (int)
  119. * @return n/a
  120. */
  121. function add_field_filter( $tag = '', $function_to_add = '', $priority = 10, $accepted_args = 1 ) {
  122. // append
  123. $tag .= '/type=' . $this->name;
  124. // add
  125. $this->add_filter( $tag, $function_to_add, $priority, $accepted_args );
  126. }
  127. /*
  128. * add_action
  129. *
  130. * This function checks if the function is_callable before adding the action
  131. *
  132. * @type function
  133. * @date 5/03/2014
  134. * @since 5.0.0
  135. *
  136. * @param $tag (string)
  137. * @param $function_to_add (string)
  138. * @param $priority (int)
  139. * @param $accepted_args (int)
  140. * @return n/a
  141. */
  142. function add_action( $tag = '', $function_to_add = '', $priority = 10, $accepted_args = 1 ) {
  143. // bail early if no callable
  144. if ( ! is_callable( $function_to_add ) ) {
  145. return;
  146. }
  147. // add
  148. add_action( $tag, $function_to_add, $priority, $accepted_args );
  149. }
  150. /*
  151. * add_field_action
  152. *
  153. * This function will add a field type specific filter
  154. *
  155. * @type function
  156. * @date 29/09/2016
  157. * @since 5.4.0
  158. *
  159. * @param $tag (string)
  160. * @param $function_to_add (string)
  161. * @param $priority (int)
  162. * @param $accepted_args (int)
  163. * @return n/a
  164. */
  165. function add_field_action( $tag = '', $function_to_add = '', $priority = 10, $accepted_args = 1 ) {
  166. // append
  167. $tag .= '/type=' . $this->name;
  168. // add
  169. $this->add_action( $tag, $function_to_add, $priority, $accepted_args );
  170. }
  171. /*
  172. * validate_field
  173. *
  174. * This function will append default settings to a field
  175. *
  176. * @type filter ("acf/validate_field/type={$this->name}")
  177. * @since 3.6
  178. * @date 23/01/13
  179. *
  180. * @param $field (array)
  181. * @return $field (array)
  182. */
  183. function validate_field( $field ) {
  184. // bail early if no defaults
  185. if ( ! is_array( $this->defaults ) ) {
  186. return $field;
  187. }
  188. // merge in defaults but keep order of $field keys
  189. foreach ( $this->defaults as $k => $v ) {
  190. if ( ! isset( $field[ $k ] ) ) {
  191. $field[ $k ] = $v;
  192. }
  193. }
  194. // return
  195. return $field;
  196. }
  197. /*
  198. * admin_l10n
  199. *
  200. * This function will append l10n text translations to an array which is later passed to JS
  201. *
  202. * @type filter ("acf/input/admin_l10n")
  203. * @since 3.6
  204. * @date 23/01/13
  205. *
  206. * @param $l10n (array)
  207. * @return $l10n (array)
  208. */
  209. function input_admin_l10n( $l10n ) {
  210. // bail early if no defaults
  211. if ( empty( $this->l10n ) ) {
  212. return $l10n;
  213. }
  214. // append
  215. $l10n[ $this->name ] = $this->l10n;
  216. // return
  217. return $l10n;
  218. }
  219. /**
  220. * Add additional validation for fields being updated via the REST API.
  221. *
  222. * @param bool $valid
  223. * @param mixed $value
  224. * @param array $field
  225. *
  226. * @return bool|WP_Error
  227. */
  228. public function validate_rest_value( $valid, $value, $field ) {
  229. return $valid;
  230. }
  231. /**
  232. * Return the schema array for the REST API.
  233. *
  234. * @param array $field
  235. * @return array
  236. */
  237. public function get_rest_schema( array $field ) {
  238. $schema = array(
  239. 'type' => array( 'string', 'null' ),
  240. 'required' => ! empty( $field['required'] ),
  241. );
  242. if ( isset( $field['default_value'] ) && '' !== $field['default_value'] ) {
  243. $schema['default'] = $field['default_value'];
  244. }
  245. return $schema;
  246. }
  247. /**
  248. * Return an array of links for addition to the REST API response. Each link is an array and must have both `rel` and
  249. * `href` keys. The `href` key must be a REST API resource URL. If a link is marked as `embeddable`, the `_embed` URL
  250. * parameter will trigger WordPress to dispatch an internal sub request and load the object within the same request
  251. * under the `_embedded` response property.
  252. *
  253. * e.g;
  254. * [
  255. * [
  256. * 'rel' => 'acf:post',
  257. * 'href' => 'https://example.com/wp-json/wp/v2/posts/497',
  258. * 'embeddable' => true,
  259. * ],
  260. * [
  261. * 'rel' => 'acf:user',
  262. * 'href' => 'https://example.com/wp-json/wp/v2/users/2',
  263. * 'embeddable' => true,
  264. * ],
  265. * ]
  266. *
  267. * @param mixed $value The raw (unformatted) field value.
  268. * @param string|int $post_id
  269. * @param array $field
  270. * @return array
  271. */
  272. public function get_rest_links( $value, $post_id, array $field ) {
  273. return array();
  274. }
  275. /**
  276. * Apply basic formatting to prepare the value for default REST output.
  277. *
  278. * @param mixed $value
  279. * @param string|int $post_id
  280. * @param array $field
  281. * @return mixed
  282. */
  283. public function format_value_for_rest( $value, $post_id, array $field ) {
  284. return $value;
  285. }
  286. }
  287. endif; // class_exists check