class-acf-field-radio.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <?php
  2. if ( ! class_exists( 'acf_field_radio' ) ) :
  3. class acf_field_radio extends acf_field {
  4. /*
  5. * __construct
  6. *
  7. * This function will setup the field type data
  8. *
  9. * @type function
  10. * @date 5/03/2014
  11. * @since 5.0.0
  12. *
  13. * @param n/a
  14. * @return n/a
  15. */
  16. function initialize() {
  17. // vars
  18. $this->name = 'radio';
  19. $this->label = __( 'Radio Button', 'acf' );
  20. $this->category = 'choice';
  21. $this->defaults = array(
  22. 'layout' => 'vertical',
  23. 'choices' => array(),
  24. 'default_value' => '',
  25. 'other_choice' => 0,
  26. 'save_other_choice' => 0,
  27. 'allow_null' => 0,
  28. 'return_format' => 'value',
  29. );
  30. }
  31. /*
  32. * render_field()
  33. *
  34. * Create the HTML interface for your field
  35. *
  36. * @param $field (array) the $field being rendered
  37. *
  38. * @type action
  39. * @since 3.6
  40. * @date 23/01/13
  41. *
  42. * @param $field (array) the $field being edited
  43. * @return n/a
  44. */
  45. function render_field( $field ) {
  46. // vars
  47. $e = '';
  48. $ul = array(
  49. 'class' => 'acf-radio-list',
  50. 'data-allow_null' => $field['allow_null'],
  51. 'data-other_choice' => $field['other_choice'],
  52. );
  53. // append to class
  54. $ul['class'] .= ' ' . ( $field['layout'] == 'horizontal' ? 'acf-hl' : 'acf-bl' );
  55. $ul['class'] .= ' ' . $field['class'];
  56. // Determine selected value.
  57. $value = (string) $field['value'];
  58. // 1. Selected choice.
  59. if ( isset( $field['choices'][ $value ] ) ) {
  60. $checked = (string) $value;
  61. // 2. Custom choice.
  62. } elseif ( $field['other_choice'] && $value !== '' ) {
  63. $checked = 'other';
  64. // 3. Empty choice.
  65. } elseif ( $field['allow_null'] ) {
  66. $checked = '';
  67. // 4. Default to first choice.
  68. } else {
  69. $checked = (string) key( $field['choices'] );
  70. }
  71. // other choice
  72. $other_input = false;
  73. if ( $field['other_choice'] ) {
  74. // Define other input attrs.
  75. $other_input = array(
  76. 'type' => 'text',
  77. 'name' => $field['name'],
  78. 'value' => '',
  79. 'disabled' => 'disabled',
  80. 'class' => 'acf-disabled',
  81. );
  82. // Select other choice if value is not a valid choice.
  83. if ( $checked === 'other' ) {
  84. unset( $other_input['disabled'] );
  85. $other_input['value'] = $field['value'];
  86. }
  87. // Ensure an 'other' choice is defined.
  88. if ( ! isset( $field['choices']['other'] ) ) {
  89. $field['choices']['other'] = '';
  90. }
  91. }
  92. // Bail early if no choices.
  93. if ( empty( $field['choices'] ) ) {
  94. return;
  95. }
  96. // Hiden input.
  97. $e .= acf_get_hidden_input( array( 'name' => $field['name'] ) );
  98. // Open <ul>.
  99. $e .= '<ul ' . acf_esc_attr( $ul ) . '>';
  100. // Loop through choices.
  101. foreach ( $field['choices'] as $value => $label ) {
  102. $is_selected = false;
  103. // Ensure value is a string.
  104. $value = (string) $value;
  105. // Define input attrs.
  106. $attrs = array(
  107. 'type' => 'radio',
  108. 'id' => sanitize_title( $field['id'] . '-' . $value ),
  109. 'name' => $field['name'],
  110. 'value' => $value,
  111. );
  112. // Check if selected.
  113. if ( esc_attr( $value ) === esc_attr( $checked ) ) {
  114. $attrs['checked'] = 'checked';
  115. $is_selected = true;
  116. }
  117. // Check if is disabled.
  118. if ( isset( $field['disabled'] ) && acf_in_array( $value, $field['disabled'] ) ) {
  119. $attrs['disabled'] = 'disabled';
  120. }
  121. // Additional HTML (the "Other" input).
  122. $additional_html = '';
  123. if ( $value === 'other' && $other_input ) {
  124. $additional_html = ' ' . acf_get_text_input( $other_input );
  125. }
  126. // append
  127. $e .= '<li><label' . ( $is_selected ? ' class="selected"' : '' ) . '><input ' . acf_esc_attr( $attrs ) . '/>' . acf_esc_html( $label ) . '</label>' . $additional_html . '</li>';
  128. }
  129. // Close <ul>.
  130. $e .= '</ul>';
  131. // Output HTML.
  132. echo $e;
  133. }
  134. /*
  135. * render_field_settings()
  136. *
  137. * Create extra options for your field. This is rendered when editing a field.
  138. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  139. *
  140. * @type action
  141. * @since 3.6
  142. * @date 23/01/13
  143. *
  144. * @param $field - an array holding all the field's data
  145. */
  146. function render_field_settings( $field ) {
  147. // Encode choices (convert from array).
  148. $field['choices'] = acf_encode_choices( $field['choices'] );
  149. acf_render_field_setting(
  150. $field,
  151. array(
  152. 'label' => __( 'Choices', 'acf' ),
  153. '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>',
  154. 'type' => 'textarea',
  155. 'name' => 'choices',
  156. )
  157. );
  158. acf_render_field_setting(
  159. $field,
  160. array(
  161. 'label' => __( 'Default Value', 'acf' ),
  162. 'instructions' => __( 'Appears when creating a new post', 'acf' ),
  163. 'type' => 'text',
  164. 'name' => 'default_value',
  165. )
  166. );
  167. acf_render_field_setting(
  168. $field,
  169. array(
  170. 'label' => __( 'Return Value', 'acf' ),
  171. 'instructions' => __( 'Specify the returned value on front end', 'acf' ),
  172. 'type' => 'radio',
  173. 'name' => 'return_format',
  174. 'layout' => 'horizontal',
  175. 'choices' => array(
  176. 'value' => __( 'Value', 'acf' ),
  177. 'label' => __( 'Label', 'acf' ),
  178. 'array' => __( 'Both (Array)', 'acf' ),
  179. ),
  180. )
  181. );
  182. }
  183. /**
  184. * Renders the field settings used in the "Validation" tab.
  185. *
  186. * @since 6.0
  187. *
  188. * @param array $field The field settings array.
  189. * @return void
  190. */
  191. function render_field_validation_settings( $field ) {
  192. acf_render_field_setting(
  193. $field,
  194. array(
  195. 'label' => __( 'Allow Null?', 'acf' ),
  196. 'instructions' => '',
  197. 'name' => 'allow_null',
  198. 'type' => 'true_false',
  199. 'ui' => 1,
  200. )
  201. );
  202. acf_render_field_setting(
  203. $field,
  204. array(
  205. 'label' => __( 'Allow Other Choice', 'acf' ),
  206. 'name' => 'other_choice',
  207. 'type' => 'true_false',
  208. 'ui' => 1,
  209. 'instructions' => __( "Add 'other' choice to allow for custom values", 'acf' ),
  210. )
  211. );
  212. acf_render_field_setting(
  213. $field,
  214. array(
  215. 'label' => __( 'Save Other Choice', 'acf' ),
  216. 'name' => 'save_other_choice',
  217. 'type' => 'true_false',
  218. 'ui' => 1,
  219. 'instructions' => __( "Save 'other' values to the field's choices", 'acf' ),
  220. 'conditions' => array(
  221. 'field' => 'other_choice',
  222. 'operator' => '==',
  223. 'value' => 1,
  224. ),
  225. )
  226. );
  227. }
  228. /**
  229. * Renders the field settings used in the "Presentation" tab.
  230. *
  231. * @since 6.0
  232. *
  233. * @param array $field The field settings array.
  234. * @return void
  235. */
  236. function render_field_presentation_settings( $field ) {
  237. acf_render_field_setting(
  238. $field,
  239. array(
  240. 'label' => __( 'Layout', 'acf' ),
  241. 'instructions' => '',
  242. 'type' => 'radio',
  243. 'name' => 'layout',
  244. 'layout' => 'horizontal',
  245. 'choices' => array(
  246. 'vertical' => __( 'Vertical', 'acf' ),
  247. 'horizontal' => __( 'Horizontal', 'acf' ),
  248. ),
  249. )
  250. );
  251. }
  252. /*
  253. * update_field()
  254. *
  255. * This filter is appied to the $field before it is saved to the database
  256. *
  257. * @type filter
  258. * @since 3.6
  259. * @date 23/01/13
  260. *
  261. * @param $field - the field array holding all the field options
  262. * @param $post_id - the field group ID (post_type = acf)
  263. *
  264. * @return $field - the modified field
  265. */
  266. function update_field( $field ) {
  267. // decode choices (convert to array)
  268. $field['choices'] = acf_decode_choices( $field['choices'] );
  269. // return
  270. return $field;
  271. }
  272. /*
  273. * update_value()
  274. *
  275. * This filter is appied to the $value before it is updated in the db
  276. *
  277. * @type filter
  278. * @since 3.6
  279. * @date 23/01/13
  280. * @todo Fix bug where $field was found via json and has no ID
  281. *
  282. * @param $value - the value which will be saved in the database
  283. * @param $post_id - the $post_id of which the value will be saved
  284. * @param $field - the field array holding all the field options
  285. *
  286. * @return $value - the modified value
  287. */
  288. function update_value( $value, $post_id, $field ) {
  289. // bail early if no value (allow 0 to be saved)
  290. if ( ! $value && ! is_numeric( $value ) ) {
  291. return $value;
  292. }
  293. // save_other_choice
  294. if ( $field['save_other_choice'] ) {
  295. // value isn't in choices yet
  296. if ( ! isset( $field['choices'][ $value ] ) ) {
  297. // get raw $field (may have been changed via repeater field)
  298. // if field is local, it won't have an ID
  299. $selector = $field['ID'] ? $field['ID'] : $field['key'];
  300. $field = acf_get_field( $selector );
  301. // bail early if no ID (JSON only)
  302. if ( ! $field['ID'] ) {
  303. return $value;
  304. }
  305. // unslash (fixes serialize single quote issue)
  306. $value = wp_unslash( $value );
  307. // sanitize (remove tags)
  308. $value = sanitize_text_field( $value );
  309. // update $field
  310. $field['choices'][ $value ] = $value;
  311. // save
  312. acf_update_field( $field );
  313. }
  314. }
  315. // return
  316. return $value;
  317. }
  318. /*
  319. * load_value()
  320. *
  321. * This filter is appied to the $value after it is loaded from the db
  322. *
  323. * @type filter
  324. * @since 5.2.9
  325. * @date 23/01/13
  326. *
  327. * @param $value - the value found in the database
  328. * @param $post_id - the $post_id from which the value was loaded from
  329. * @param $field - the field array holding all the field options
  330. *
  331. * @return $value - the value to be saved in te database
  332. */
  333. function load_value( $value, $post_id, $field ) {
  334. // must be single value
  335. if ( is_array( $value ) ) {
  336. $value = array_pop( $value );
  337. }
  338. // return
  339. return $value;
  340. }
  341. /*
  342. * translate_field
  343. *
  344. * This function will translate field settings
  345. *
  346. * @type function
  347. * @date 8/03/2016
  348. * @since 5.3.2
  349. *
  350. * @param $field (array)
  351. * @return $field
  352. */
  353. function translate_field( $field ) {
  354. return acf_get_field_type( 'select' )->translate_field( $field );
  355. }
  356. /*
  357. * format_value()
  358. *
  359. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  360. *
  361. * @type filter
  362. * @since 3.6
  363. * @date 23/01/13
  364. *
  365. * @param $value (mixed) the value which was loaded from the database
  366. * @param $post_id (mixed) the $post_id from which the value was loaded
  367. * @param $field (array) the field array holding all the field options
  368. *
  369. * @return $value (mixed) the modified value
  370. */
  371. function format_value( $value, $post_id, $field ) {
  372. return acf_get_field_type( 'select' )->format_value( $value, $post_id, $field );
  373. }
  374. /**
  375. * Return the schema array for the REST API.
  376. *
  377. * @param array $field
  378. * @return array
  379. */
  380. function get_rest_schema( array $field ) {
  381. $schema = parent::get_rest_schema( $field );
  382. if ( isset( $field['default_value'] ) && '' !== $field['default_value'] ) {
  383. $schema['default'] = $field['default_value'];
  384. }
  385. // If other/custom choices are allowed, nothing else to do here.
  386. if ( ! empty( $field['other_choice'] ) ) {
  387. return $schema;
  388. }
  389. /**
  390. * If a user has defined keys for the radio options,
  391. * we should use the keys for the available options to POST to,
  392. * since they are what is displayed in GET requests.
  393. */
  394. $radio_keys = array_diff(
  395. array_keys( $field['choices'] ),
  396. array_values( $field['choices'] )
  397. );
  398. $schema['enum'] = empty( $radio_keys ) ? $field['choices'] : $radio_keys;
  399. if ( ! empty( $field['allow_null'] ) ) {
  400. $schema['enum'][] = null;
  401. }
  402. return $schema;
  403. }
  404. }
  405. // initialize
  406. acf_register_field_type( 'acf_field_radio' );
  407. endif; // class_exists check