Нет описания

class-acf-field.php 8.6KB

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