Ei kuvausta

class-acf-field-radio.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. // choices
  150. acf_render_field_setting(
  151. $field,
  152. array(
  153. 'label' => __( 'Choices', 'acf' ),
  154. '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' ),
  155. 'type' => 'textarea',
  156. 'name' => 'choices',
  157. )
  158. );
  159. // allow_null
  160. acf_render_field_setting(
  161. $field,
  162. array(
  163. 'label' => __( 'Allow Null?', 'acf' ),
  164. 'instructions' => '',
  165. 'name' => 'allow_null',
  166. 'type' => 'true_false',
  167. 'ui' => 1,
  168. )
  169. );
  170. // other_choice
  171. acf_render_field_setting(
  172. $field,
  173. array(
  174. 'label' => __( 'Other', 'acf' ),
  175. 'instructions' => '',
  176. 'name' => 'other_choice',
  177. 'type' => 'true_false',
  178. 'ui' => 1,
  179. 'message' => __( "Add 'other' choice to allow for custom values", 'acf' ),
  180. )
  181. );
  182. // save_other_choice
  183. acf_render_field_setting(
  184. $field,
  185. array(
  186. 'label' => __( 'Save Other', 'acf' ),
  187. 'instructions' => '',
  188. 'name' => 'save_other_choice',
  189. 'type' => 'true_false',
  190. 'ui' => 1,
  191. 'message' => __( "Save 'other' values to the field's choices", 'acf' ),
  192. 'conditions' => array(
  193. 'field' => 'other_choice',
  194. 'operator' => '==',
  195. 'value' => 1,
  196. ),
  197. )
  198. );
  199. // default_value
  200. acf_render_field_setting(
  201. $field,
  202. array(
  203. 'label' => __( 'Default Value', 'acf' ),
  204. 'instructions' => __( 'Appears when creating a new post', 'acf' ),
  205. 'type' => 'text',
  206. 'name' => 'default_value',
  207. )
  208. );
  209. // layout
  210. acf_render_field_setting(
  211. $field,
  212. array(
  213. 'label' => __( 'Layout', 'acf' ),
  214. 'instructions' => '',
  215. 'type' => 'radio',
  216. 'name' => 'layout',
  217. 'layout' => 'horizontal',
  218. 'choices' => array(
  219. 'vertical' => __( 'Vertical', 'acf' ),
  220. 'horizontal' => __( 'Horizontal', 'acf' ),
  221. ),
  222. )
  223. );
  224. // return_format
  225. acf_render_field_setting(
  226. $field,
  227. array(
  228. 'label' => __( 'Return Value', 'acf' ),
  229. 'instructions' => __( 'Specify the returned value on front end', 'acf' ),
  230. 'type' => 'radio',
  231. 'name' => 'return_format',
  232. 'layout' => 'horizontal',
  233. 'choices' => array(
  234. 'value' => __( 'Value', 'acf' ),
  235. 'label' => __( 'Label', 'acf' ),
  236. 'array' => __( 'Both (Array)', 'acf' ),
  237. ),
  238. )
  239. );
  240. }
  241. /*
  242. * update_field()
  243. *
  244. * This filter is appied to the $field before it is saved to the database
  245. *
  246. * @type filter
  247. * @since 3.6
  248. * @date 23/01/13
  249. *
  250. * @param $field - the field array holding all the field options
  251. * @param $post_id - the field group ID (post_type = acf)
  252. *
  253. * @return $field - the modified field
  254. */
  255. function update_field( $field ) {
  256. // decode choices (convert to array)
  257. $field['choices'] = acf_decode_choices( $field['choices'] );
  258. // return
  259. return $field;
  260. }
  261. /*
  262. * update_value()
  263. *
  264. * This filter is appied to the $value before it is updated in the db
  265. *
  266. * @type filter
  267. * @since 3.6
  268. * @date 23/01/13
  269. * @todo Fix bug where $field was found via json and has no ID
  270. *
  271. * @param $value - the value which will be saved in the database
  272. * @param $post_id - the $post_id of which the value will be saved
  273. * @param $field - the field array holding all the field options
  274. *
  275. * @return $value - the modified value
  276. */
  277. function update_value( $value, $post_id, $field ) {
  278. // bail early if no value (allow 0 to be saved)
  279. if ( ! $value && ! is_numeric( $value ) ) {
  280. return $value;
  281. }
  282. // save_other_choice
  283. if ( $field['save_other_choice'] ) {
  284. // value isn't in choices yet
  285. if ( ! isset( $field['choices'][ $value ] ) ) {
  286. // get raw $field (may have been changed via repeater field)
  287. // if field is local, it won't have an ID
  288. $selector = $field['ID'] ? $field['ID'] : $field['key'];
  289. $field = acf_get_field( $selector, true );
  290. // bail early if no ID (JSON only)
  291. if ( ! $field['ID'] ) {
  292. return $value;
  293. }
  294. // unslash (fixes serialize single quote issue)
  295. $value = wp_unslash( $value );
  296. // sanitize (remove tags)
  297. $value = sanitize_text_field( $value );
  298. // update $field
  299. $field['choices'][ $value ] = $value;
  300. // save
  301. acf_update_field( $field );
  302. }
  303. }
  304. // return
  305. return $value;
  306. }
  307. /*
  308. * load_value()
  309. *
  310. * This filter is appied to the $value after it is loaded from the db
  311. *
  312. * @type filter
  313. * @since 5.2.9
  314. * @date 23/01/13
  315. *
  316. * @param $value - the value found in the database
  317. * @param $post_id - the $post_id from which the value was loaded from
  318. * @param $field - the field array holding all the field options
  319. *
  320. * @return $value - the value to be saved in te database
  321. */
  322. function load_value( $value, $post_id, $field ) {
  323. // must be single value
  324. if ( is_array( $value ) ) {
  325. $value = array_pop( $value );
  326. }
  327. // return
  328. return $value;
  329. }
  330. /*
  331. * translate_field
  332. *
  333. * This function will translate field settings
  334. *
  335. * @type function
  336. * @date 8/03/2016
  337. * @since 5.3.2
  338. *
  339. * @param $field (array)
  340. * @return $field
  341. */
  342. function translate_field( $field ) {
  343. return acf_get_field_type( 'select' )->translate_field( $field );
  344. }
  345. /*
  346. * format_value()
  347. *
  348. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  349. *
  350. * @type filter
  351. * @since 3.6
  352. * @date 23/01/13
  353. *
  354. * @param $value (mixed) the value which was loaded from the database
  355. * @param $post_id (mixed) the $post_id from which the value was loaded
  356. * @param $field (array) the field array holding all the field options
  357. *
  358. * @return $value (mixed) the modified value
  359. */
  360. function format_value( $value, $post_id, $field ) {
  361. return acf_get_field_type( 'select' )->format_value( $value, $post_id, $field );
  362. }
  363. /**
  364. * Return the schema array for the REST API.
  365. *
  366. * @param array $field
  367. * @return array
  368. */
  369. function get_rest_schema( array $field ) {
  370. $schema = parent::get_rest_schema( $field );
  371. if ( isset( $field['default_value'] ) && '' !== $field['default_value'] ) {
  372. $schema['default'] = $field['default_value'];
  373. }
  374. // If other/custom choices are allowed, nothing else to do here.
  375. if ( ! empty( $field['other_choice'] ) ) {
  376. return $schema;
  377. }
  378. /**
  379. * If a user has defined keys for the radio options,
  380. * we should use the keys for the available options to POST to,
  381. * since they are what is displayed in GET requests.
  382. */
  383. $radio_keys = array_diff(
  384. array_keys( $field['choices'] ),
  385. array_values( $field['choices'] )
  386. );
  387. $schema['enum'] = empty( $radio_keys ) ? $field['choices'] : $radio_keys;
  388. if ( ! empty( $field['allow_null'] ) ) {
  389. $schema['enum'][] = null;
  390. }
  391. return $schema;
  392. }
  393. }
  394. // initialize
  395. acf_register_field_type( 'acf_field_radio' );
  396. endif; // class_exists check