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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. acf_render_field_setting(
  111. $field,
  112. array(
  113. 'label' => __( 'Choices', 'acf' ),
  114. 'instructions' => __( 'Enter each choice on a new line.', 'acf' ) . '<br />' . __( 'For more control, you may specify both a value and label like this:', 'acf' ) . '<br /><span class="acf-field-setting-example">' . __( 'red : Red', 'acf' ) . '</span>',
  115. 'type' => 'textarea',
  116. 'name' => 'choices',
  117. )
  118. );
  119. acf_render_field_setting(
  120. $field,
  121. array(
  122. 'label' => __( 'Default Value', 'acf' ),
  123. 'instructions' => __( 'Appears when creating a new post', 'acf' ),
  124. 'type' => 'text',
  125. 'name' => 'default_value',
  126. )
  127. );
  128. acf_render_field_setting(
  129. $field,
  130. array(
  131. 'label' => __( 'Return Value', 'acf' ),
  132. 'instructions' => __( 'Specify the returned value on front end', 'acf' ),
  133. 'type' => 'radio',
  134. 'name' => 'return_format',
  135. 'layout' => 'horizontal',
  136. 'choices' => array(
  137. 'value' => __( 'Value', 'acf' ),
  138. 'label' => __( 'Label', 'acf' ),
  139. 'array' => __( 'Both (Array)', 'acf' ),
  140. ),
  141. )
  142. );
  143. }
  144. /**
  145. * Renders the field settings used in the "Validation" tab.
  146. *
  147. * @since 6.0
  148. *
  149. * @param array $field The field settings array.
  150. * @return void
  151. */
  152. function render_field_validation_settings( $field ) {
  153. acf_render_field_setting(
  154. $field,
  155. array(
  156. 'label' => __( 'Allow Null?', 'acf' ),
  157. 'instructions' => '',
  158. 'name' => 'allow_null',
  159. 'type' => 'true_false',
  160. 'ui' => 1,
  161. )
  162. );
  163. }
  164. /**
  165. * Renders the field settings used in the "Presentation" tab.
  166. *
  167. * @since 6.0
  168. *
  169. * @param array $field The field settings array.
  170. * @return void
  171. */
  172. function render_field_presentation_settings( $field ) {
  173. acf_render_field_setting(
  174. $field,
  175. array(
  176. 'label' => __( 'Layout', 'acf' ),
  177. 'instructions' => '',
  178. 'type' => 'radio',
  179. 'name' => 'layout',
  180. 'layout' => 'horizontal',
  181. 'choices' => array(
  182. 'horizontal' => __( 'Horizontal', 'acf' ),
  183. 'vertical' => __( 'Vertical', 'acf' ),
  184. ),
  185. )
  186. );
  187. }
  188. /*
  189. * update_field()
  190. *
  191. * This filter is appied to the $field before it is saved to the database
  192. *
  193. * @date 18/9/17
  194. * @since 5.6.3
  195. *
  196. * @param array $field The field array holding all the field options
  197. * @return $field
  198. */
  199. function update_field( $field ) {
  200. return acf_get_field_type( 'radio' )->update_field( $field );
  201. }
  202. /*
  203. * load_value()
  204. *
  205. * This filter is appied to the $value after it is loaded from the db
  206. *
  207. * @date 18/9/17
  208. * @since 5.6.3
  209. *
  210. * @param mixed $value The value found in the database
  211. * @param mixed $post_id The post ID from which the value was loaded from
  212. * @param array $field The field array holding all the field options
  213. * @return $value
  214. */
  215. function load_value( $value, $post_id, $field ) {
  216. return acf_get_field_type( 'radio' )->load_value( $value, $post_id, $field );
  217. }
  218. /*
  219. * translate_field
  220. *
  221. * This function will translate field settings
  222. *
  223. * @date 18/9/17
  224. * @since 5.6.3
  225. *
  226. * @param array $field The field array holding all the field options
  227. * @return $field
  228. */
  229. function translate_field( $field ) {
  230. return acf_get_field_type( 'radio' )->translate_field( $field );
  231. }
  232. /*
  233. * format_value()
  234. *
  235. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  236. *
  237. * @date 18/9/17
  238. * @since 5.6.3
  239. *
  240. * @param mixed $value The value found in the database
  241. * @param mixed $post_id The post ID from which the value was loaded from
  242. * @param array $field The field array holding all the field options
  243. * @return $value
  244. */
  245. function format_value( $value, $post_id, $field ) {
  246. return acf_get_field_type( 'radio' )->format_value( $value, $post_id, $field );
  247. }
  248. /**
  249. * Return the schema array for the REST API.
  250. *
  251. * @param array $field
  252. * @return array
  253. */
  254. function get_rest_schema( array $field ) {
  255. $schema = parent::get_rest_schema( $field );
  256. if ( isset( $field['default_value'] ) && '' !== $field['default_value'] ) {
  257. $schema['default'] = $field['default_value'];
  258. }
  259. /**
  260. * If a user has defined keys for the buttons,
  261. * we should use the keys for the available options to POST to,
  262. * since they are what is displayed in GET requests.
  263. */
  264. $button_keys = array_diff(
  265. array_keys( $field['choices'] ),
  266. array_values( $field['choices'] )
  267. );
  268. $schema['enum'] = empty( $button_keys ) ? $field['choices'] : $button_keys;
  269. $schema['enum'][] = null;
  270. // Allow null via UI will value to empty string.
  271. if ( ! empty( $field['allow_null'] ) ) {
  272. $schema['enum'][] = '';
  273. }
  274. return $schema;
  275. }
  276. }
  277. // initialize
  278. acf_register_field_type( 'acf_field_button_group' );
  279. endif; // class_exists check