Sin descripción

class-acf-field-button-group.php 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. if ( ! class_exists( 'acf_field_button_group' ) ) :
  3. class acf_field_button_group extends acf_field {
  4. /**
  5. * initialize()
  6. *
  7. * This function will setup the field type data
  8. *
  9. * @date 18/9/17
  10. * @since 5.6.3
  11. *
  12. * @param n/a
  13. * @return n/a
  14. */
  15. function initialize() {
  16. // vars
  17. $this->name = 'button_group';
  18. $this->label = __( 'Button Group', 'acf' );
  19. $this->category = 'choice';
  20. $this->defaults = array(
  21. 'choices' => array(),
  22. 'default_value' => '',
  23. 'allow_null' => 0,
  24. 'return_format' => 'value',
  25. 'layout' => 'horizontal',
  26. );
  27. }
  28. /**
  29. * render_field()
  30. *
  31. * Creates the field's input HTML
  32. *
  33. * @date 18/9/17
  34. * @since 5.6.3
  35. *
  36. * @param array $field The field settings array
  37. * @return n/a
  38. */
  39. function render_field( $field ) {
  40. // vars
  41. $html = '';
  42. $selected = null;
  43. $buttons = array();
  44. $value = esc_attr( $field['value'] );
  45. // bail ealrly if no choices
  46. if ( empty( $field['choices'] ) ) {
  47. return;
  48. }
  49. // buttons
  50. foreach ( $field['choices'] as $_value => $_label ) {
  51. // checked
  52. $checked = ( $value === esc_attr( $_value ) );
  53. if ( $checked ) {
  54. $selected = true;
  55. }
  56. // append
  57. $buttons[] = array(
  58. 'name' => $field['name'],
  59. 'value' => $_value,
  60. 'label' => $_label,
  61. 'checked' => $checked,
  62. );
  63. }
  64. // maybe select initial value
  65. if ( ! $field['allow_null'] && $selected === null ) {
  66. $buttons[0]['checked'] = true;
  67. }
  68. // div
  69. $div = array( 'class' => 'acf-button-group' );
  70. if ( $field['layout'] == 'vertical' ) {
  71. $div['class'] .= ' -vertical'; }
  72. if ( $field['class'] ) {
  73. $div['class'] .= ' ' . $field['class']; }
  74. if ( $field['allow_null'] ) {
  75. $div['data-allow_null'] = 1; }
  76. // hdden input
  77. $html .= acf_get_hidden_input( array( 'name' => $field['name'] ) );
  78. // open
  79. $html .= '<div ' . acf_esc_attr( $div ) . '>';
  80. // loop
  81. foreach ( $buttons as $button ) {
  82. // checked
  83. if ( $button['checked'] ) {
  84. $button['checked'] = 'checked';
  85. } else {
  86. unset( $button['checked'] );
  87. }
  88. // append
  89. $html .= acf_get_radio_input( $button );
  90. }
  91. // close
  92. $html .= '</div>';
  93. // return
  94. echo $html;
  95. }
  96. /**
  97. * render_field_settings()
  98. *
  99. * Creates the field's settings HTML
  100. *
  101. * @date 18/9/17
  102. * @since 5.6.3
  103. *
  104. * @param array $field The field settings array
  105. * @return n/a
  106. */
  107. function render_field_settings( $field ) {
  108. // encode choices (convert from array)
  109. $field['choices'] = acf_encode_choices( $field['choices'] );
  110. // choices
  111. acf_render_field_setting(
  112. $field,
  113. array(
  114. 'label' => __( 'Choices', 'acf' ),
  115. 'instructions' => __( 'Enter each choice on a new line.', 'acf' ) . '<br /><br />' . __( 'For more control, you may specify both a value and label like this:', 'acf' ) . '<br /><br />' . __( 'red : Red', 'acf' ),
  116. 'type' => 'textarea',
  117. 'name' => 'choices',
  118. )
  119. );
  120. // allow_null
  121. acf_render_field_setting(
  122. $field,
  123. array(
  124. 'label' => __( 'Allow Null?', 'acf' ),
  125. 'instructions' => '',
  126. 'name' => 'allow_null',
  127. 'type' => 'true_false',
  128. 'ui' => 1,
  129. )
  130. );
  131. // default_value
  132. acf_render_field_setting(
  133. $field,
  134. array(
  135. 'label' => __( 'Default Value', 'acf' ),
  136. 'instructions' => __( 'Appears when creating a new post', 'acf' ),
  137. 'type' => 'text',
  138. 'name' => 'default_value',
  139. )
  140. );
  141. // layout
  142. acf_render_field_setting(
  143. $field,
  144. array(
  145. 'label' => __( 'Layout', 'acf' ),
  146. 'instructions' => '',
  147. 'type' => 'radio',
  148. 'name' => 'layout',
  149. 'layout' => 'horizontal',
  150. 'choices' => array(
  151. 'horizontal' => __( 'Horizontal', 'acf' ),
  152. 'vertical' => __( 'Vertical', 'acf' ),
  153. ),
  154. )
  155. );
  156. // return_format
  157. acf_render_field_setting(
  158. $field,
  159. array(
  160. 'label' => __( 'Return Value', 'acf' ),
  161. 'instructions' => __( 'Specify the returned value on front end', 'acf' ),
  162. 'type' => 'radio',
  163. 'name' => 'return_format',
  164. 'layout' => 'horizontal',
  165. 'choices' => array(
  166. 'value' => __( 'Value', 'acf' ),
  167. 'label' => __( 'Label', 'acf' ),
  168. 'array' => __( 'Both (Array)', 'acf' ),
  169. ),
  170. )
  171. );
  172. }
  173. /*
  174. * update_field()
  175. *
  176. * This filter is appied to the $field before it is saved to the database
  177. *
  178. * @date 18/9/17
  179. * @since 5.6.3
  180. *
  181. * @param array $field The field array holding all the field options
  182. * @return $field
  183. */
  184. function update_field( $field ) {
  185. return acf_get_field_type( 'radio' )->update_field( $field );
  186. }
  187. /*
  188. * load_value()
  189. *
  190. * This filter is appied to the $value after it is loaded from the db
  191. *
  192. * @date 18/9/17
  193. * @since 5.6.3
  194. *
  195. * @param mixed $value The value found in the database
  196. * @param mixed $post_id The post ID from which the value was loaded from
  197. * @param array $field The field array holding all the field options
  198. * @return $value
  199. */
  200. function load_value( $value, $post_id, $field ) {
  201. return acf_get_field_type( 'radio' )->load_value( $value, $post_id, $field );
  202. }
  203. /*
  204. * translate_field
  205. *
  206. * This function will translate field settings
  207. *
  208. * @date 18/9/17
  209. * @since 5.6.3
  210. *
  211. * @param array $field The field array holding all the field options
  212. * @return $field
  213. */
  214. function translate_field( $field ) {
  215. return acf_get_field_type( 'radio' )->translate_field( $field );
  216. }
  217. /*
  218. * format_value()
  219. *
  220. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  221. *
  222. * @date 18/9/17
  223. * @since 5.6.3
  224. *
  225. * @param mixed $value The value found in the database
  226. * @param mixed $post_id The post ID from which the value was loaded from
  227. * @param array $field The field array holding all the field options
  228. * @return $value
  229. */
  230. function format_value( $value, $post_id, $field ) {
  231. return acf_get_field_type( 'radio' )->format_value( $value, $post_id, $field );
  232. }
  233. /**
  234. * Return the schema array for the REST API.
  235. *
  236. * @param array $field
  237. * @return array
  238. */
  239. function get_rest_schema( array $field ) {
  240. $schema = parent::get_rest_schema( $field );
  241. if ( isset( $field['default_value'] ) && '' !== $field['default_value'] ) {
  242. $schema['default'] = $field['default_value'];
  243. }
  244. /**
  245. * If a user has defined keys for the buttons,
  246. * we should use the keys for the available options to POST to,
  247. * since they are what is displayed in GET requests.
  248. */
  249. $button_keys = array_diff(
  250. array_keys( $field['choices'] ),
  251. array_values( $field['choices'] )
  252. );
  253. $schema['enum'] = empty( $button_keys ) ? $field['choices'] : $button_keys;
  254. $schema['enum'][] = null;
  255. // Allow null via UI will value to empty string.
  256. if ( ! empty( $field['allow_null'] ) ) {
  257. $schema['enum'][] = '';
  258. }
  259. return $schema;
  260. }
  261. }
  262. // initialize
  263. acf_register_field_type( 'acf_field_button_group' );
  264. endif; // class_exists check