No Description

class-checkbox.php 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. <?php
  2. /**
  3. * Checkbox field.
  4. *
  5. * @since 1.0.0
  6. */
  7. class WPForms_Field_Checkbox extends WPForms_Field {
  8. /**
  9. * Primary class constructor.
  10. *
  11. * @since 1.0.0
  12. */
  13. public function init() {
  14. // Define field type information.
  15. $this->name = esc_html__( 'Checkboxes', 'wpforms-lite' );
  16. $this->type = 'checkbox';
  17. $this->icon = 'fa-check-square-o';
  18. $this->order = 110;
  19. $this->defaults = array(
  20. 1 => array(
  21. 'label' => esc_html__( 'First Choice', 'wpforms-lite' ),
  22. 'value' => '',
  23. 'image' => '',
  24. 'default' => '',
  25. ),
  26. 2 => array(
  27. 'label' => esc_html__( 'Second Choice', 'wpforms-lite' ),
  28. 'value' => '',
  29. 'image' => '',
  30. 'default' => '',
  31. ),
  32. 3 => array(
  33. 'label' => esc_html__( 'Third Choice', 'wpforms-lite' ),
  34. 'value' => '',
  35. 'image' => '',
  36. 'default' => '',
  37. ),
  38. );
  39. // Customize HTML field values.
  40. add_filter( 'wpforms_html_field_value', array( $this, 'field_html_value' ), 10, 4 );
  41. // Define additional field properties.
  42. add_filter( 'wpforms_field_properties_checkbox', array( $this, 'field_properties' ), 5, 3 );
  43. }
  44. /**
  45. * Return images, if any, for HTML supported values.
  46. *
  47. * @since 1.4.5
  48. *
  49. * @param string $value Field value.
  50. * @param array $field Field settings.
  51. * @param array $form_data Form data and settings.
  52. * @param string $context Value display context.
  53. *
  54. * @return string
  55. */
  56. public function field_html_value( $value, $field, $form_data = array(), $context = '' ) {
  57. // Only use HTML formatting for checkbox fields, with image choices
  58. // enabled, and exclude the entry table display. Lastly, provides a
  59. // filter to disable fancy display.
  60. if (
  61. ! empty( $field['value'] ) &&
  62. $this->type === $field['type'] &&
  63. ! empty( $field['images'] ) &&
  64. 'entry-table' !== $context &&
  65. apply_filters( 'wpforms_checkbox_field_html_value_images', true, $context )
  66. ) {
  67. $items = array();
  68. $values = explode( "\n", $field['value'] );
  69. foreach ( $values as $key => $val ) {
  70. if ( ! empty( $field['images'][ $key ] ) ) {
  71. $items[] = sprintf(
  72. '<span style="max-width:200px;display:block;margin:0 0 5px 0;"><img src="%s" style="max-width:100%%;display:block;margin:0;"></span>%s',
  73. esc_url( $field['images'][ $key ] ),
  74. $val
  75. );
  76. } else {
  77. $items[] = $val;
  78. }
  79. }
  80. return implode( '<br><br>', $items );
  81. }
  82. return $value;
  83. }
  84. /**
  85. * Define additional field properties.
  86. *
  87. * @since 1.4.5
  88. *
  89. * @param array $properties Field properties.
  90. * @param array $field Field settings.
  91. * @param array $form_data Form data and settings.
  92. *
  93. * @return array
  94. */
  95. public function field_properties( $properties, $field, $form_data ) {
  96. // Define data.
  97. $form_id = absint( $form_data['id'] );
  98. $field_id = absint( $field['id'] );
  99. $choices = $field['choices'];
  100. $dynamic = wpforms_get_field_dynamic_choices( $field, $form_id, $form_data );
  101. if ( $dynamic ) {
  102. $choices = $dynamic;
  103. $field['show_values'] = true;
  104. }
  105. // Remove primary input.
  106. unset( $properties['inputs']['primary'] );
  107. // Set input container (ul) properties.
  108. $properties['input_container'] = array(
  109. 'class' => array( ! empty( $field['random'] ) ? 'wpforms-randomize' : '' ),
  110. 'data' => array(),
  111. 'attr' => array(),
  112. 'id' => "wpforms-{$form_id}-field_{$field_id}",
  113. );
  114. $field['choice_limit'] = empty( $field['choice_limit'] ) ? 0 : (int) $field['choice_limit'];
  115. if ( $field['choice_limit'] > 0 ) {
  116. $properties['input_container']['data']['choice-limit'] = $field['choice_limit'];
  117. }
  118. // Set input properties.
  119. foreach ( $choices as $key => $choice ) {
  120. // Used for dynamic choices.
  121. $depth = isset( $choice['depth'] ) ? absint( $choice['depth'] ) : 1;
  122. // Choice labels should not be left blank, but if they are we
  123. // provide a basic value.
  124. $value = isset( $field['show_values'] ) ? $choice['value'] : $choice['label'];
  125. if ( '' === $value ) {
  126. if ( 1 === count( $choices ) ) {
  127. $value = esc_html__( 'Checked', 'wpforms-lite' );
  128. } else {
  129. /* translators: %s - choice number. */
  130. $value = sprintf( esc_html__( 'Choice %s', 'wpforms-lite' ), $key );
  131. }
  132. }
  133. $properties['inputs'][ $key ] = array(
  134. 'container' => array(
  135. 'attr' => array(),
  136. 'class' => array( "choice-{$key}", "depth-{$depth}" ),
  137. 'data' => array(),
  138. 'id' => '',
  139. ),
  140. 'label' => array(
  141. 'attr' => array(
  142. 'for' => "wpforms-{$form_id}-field_{$field_id}_{$key}",
  143. ),
  144. 'class' => array( 'wpforms-field-label-inline' ),
  145. 'data' => array(),
  146. 'id' => '',
  147. 'text' => $choice['label'],
  148. ),
  149. 'attr' => array(
  150. 'name' => "wpforms[fields][{$field_id}][]",
  151. 'value' => $value,
  152. ),
  153. 'class' => array(),
  154. 'data' => array(),
  155. 'id' => "wpforms-{$form_id}-field_{$field_id}_{$key}",
  156. 'image' => isset( $choice['image'] ) ? $choice['image'] : '',
  157. 'required' => ! empty( $field['required'] ) ? 'required' : '',
  158. 'default' => isset( $choice['default'] ),
  159. );
  160. // Rule for validator only if needed.
  161. if ( $field['choice_limit'] > 0 ) {
  162. $properties['inputs'][ $key ]['data']['rule-check-limit'] = 'true';
  163. }
  164. }
  165. // Required class for pagebreak validation.
  166. if ( ! empty( $field['required'] ) ) {
  167. $properties['input_container']['class'][] = 'wpforms-field-required';
  168. }
  169. // Custom properties if image choices is enabled.
  170. if ( ! $dynamic && ! empty( $field['choices_images'] ) ) {
  171. $properties['input_container']['class'][] = 'wpforms-image-choices';
  172. $properties['input_container']['class'][] = 'wpforms-image-choices-' . sanitize_html_class( $field['choices_images_style'] );
  173. foreach ( $properties['inputs'] as $key => $inputs ) {
  174. $properties['inputs'][ $key ]['container']['class'][] = 'wpforms-image-choices-item';
  175. if ( in_array( $field['choices_images_style'], array( 'modern', 'classic' ), true ) ) {
  176. $properties['inputs'][ $key ]['class'][] = 'wpforms-screen-reader-element';
  177. }
  178. }
  179. }
  180. // Custom properties for disclaimer format display.
  181. if ( ! empty( $field['disclaimer_format'] ) ) {
  182. $properties['description']['class'][] = 'wpforms-disclaimer-description';
  183. $properties['description']['value'] = nl2br( $properties['description']['value'] );
  184. }
  185. // Add selected class for choices with defaults.
  186. foreach ( $properties['inputs'] as $key => $inputs ) {
  187. if ( ! empty( $inputs['default'] ) ) {
  188. $properties['inputs'][ $key ]['container']['class'][] = 'wpforms-selected';
  189. }
  190. }
  191. return $properties;
  192. }
  193. /**
  194. * Field options panel inside the builder.
  195. *
  196. * @since 1.0.0
  197. *
  198. * @param array $field Field settings.
  199. */
  200. public function field_options( $field ) {
  201. /*
  202. * Basic field options.
  203. */
  204. // Options open markup.
  205. $this->field_option(
  206. 'basic-options',
  207. $field,
  208. array(
  209. 'markup' => 'open',
  210. )
  211. );
  212. // Label.
  213. $this->field_option( 'label', $field );
  214. // Choices.
  215. $this->field_option( 'choices', $field );
  216. // Choices Images.
  217. $this->field_option( 'choices_images', $field );
  218. // Description.
  219. $this->field_option( 'description', $field );
  220. // Required toggle.
  221. $this->field_option( 'required', $field );
  222. // Options close markup.
  223. $this->field_option(
  224. 'basic-options',
  225. $field,
  226. array(
  227. 'markup' => 'close',
  228. )
  229. );
  230. /*
  231. * Advanced field options
  232. */
  233. // Options open markup.
  234. $this->field_option(
  235. 'advanced-options',
  236. $field,
  237. array(
  238. 'markup' => 'open',
  239. )
  240. );
  241. // Randomize order of choices.
  242. $this->field_element(
  243. 'row',
  244. $field,
  245. array(
  246. 'slug' => 'random',
  247. 'content' => $this->field_element(
  248. 'toggle',
  249. $field,
  250. array(
  251. 'slug' => 'random',
  252. 'value' => isset( $field['random'] ) ? '1' : '0',
  253. 'desc' => esc_html__( 'Randomize Choices', 'wpforms-lite' ),
  254. 'tooltip' => esc_html__( 'Check this option to randomize the order of the choices.', 'wpforms-lite' ),
  255. ),
  256. false
  257. ),
  258. )
  259. );
  260. // Show Values toggle option. This option will only show if already used
  261. // or if manually enabled by a filter.
  262. if ( ! empty( $field['show_values'] ) || wpforms_show_fields_options_setting() ) {
  263. $this->field_element(
  264. 'row',
  265. $field,
  266. array(
  267. 'slug' => 'show_values',
  268. 'content' => $this->field_element(
  269. 'toggle',
  270. $field,
  271. array(
  272. 'slug' => 'show_values',
  273. 'value' => isset( $field['show_values'] ) ? $field['show_values'] : '0',
  274. 'desc' => esc_html__( 'Show Values', 'wpforms-lite' ),
  275. 'tooltip' => esc_html__( 'Check this option to manually set form field values.', 'wpforms-lite' ),
  276. ),
  277. false
  278. ),
  279. )
  280. );
  281. }
  282. // Choices Images Style (theme).
  283. $this->field_option( 'choices_images_style', $field );
  284. // Display format.
  285. $this->field_option( 'input_columns', $field );
  286. // Choice Limit.
  287. $field['choice_limit'] = empty( $field['choice_limit'] ) ? 0 : (int) $field['choice_limit'];
  288. $this->field_element(
  289. 'row',
  290. $field,
  291. array(
  292. 'slug' => 'choice_limit',
  293. 'content' =>
  294. $this->field_element(
  295. 'label',
  296. $field,
  297. array(
  298. 'slug' => 'choice_limit',
  299. 'value' => esc_html__( 'Choice Limit', 'wpforms-lite' ),
  300. 'tooltip' => esc_html__( 'Limit the number of checkboxes a user can select. Leave empty for unlimited.', 'wpforms-lite' ),
  301. ),
  302. false
  303. ) . $this->field_element(
  304. 'text',
  305. $field,
  306. array(
  307. 'slug' => 'choice_limit',
  308. 'value' => $field['choice_limit'] > 0 ? $field['choice_limit'] : '',
  309. 'type' => 'number',
  310. ),
  311. false
  312. ),
  313. )
  314. );
  315. // Dynamic choice auto-populating toggle.
  316. $this->field_option( 'dynamic_choices', $field );
  317. // Dynamic choice source.
  318. $this->field_option( 'dynamic_choices_source', $field );
  319. // Custom CSS classes.
  320. $this->field_option( 'css', $field );
  321. // Hide label.
  322. $this->field_option( 'label_hide', $field );
  323. // Enable Disclaimer formatting.
  324. $this->field_element(
  325. 'row',
  326. $field,
  327. [
  328. 'slug' => 'disclaimer_format',
  329. 'content' => $this->field_element(
  330. 'toggle',
  331. $field,
  332. [
  333. 'slug' => 'disclaimer_format',
  334. 'value' => isset( $field['disclaimer_format'] ) ? '1' : '0',
  335. 'desc' => esc_html__( 'Enable Disclaimer / Terms of Service Display', 'wpforms-lite' ),
  336. 'tooltip' => esc_html__( 'Check this option to adjust the field styling to support Disclaimers and Terms of Service type agreements.', 'wpforms-lite' ),
  337. ],
  338. false
  339. ),
  340. ]
  341. );
  342. // Options close markup.
  343. $this->field_option(
  344. 'advanced-options',
  345. $field,
  346. [
  347. 'markup' => 'close',
  348. ]
  349. );
  350. }
  351. /**
  352. * Field preview inside the builder.
  353. *
  354. * @since 1.0.0
  355. *
  356. * @param array $field Field settings.
  357. */
  358. public function field_preview( $field ) {
  359. // Label.
  360. $this->field_preview_option( 'label', $field );
  361. // Choices.
  362. $this->field_preview_option( 'choices', $field );
  363. // Description.
  364. $this->field_preview_option(
  365. 'description',
  366. $field,
  367. array(
  368. 'class' => ! empty( $field['disclaimer_format'] ) ? 'disclaimer nl2br' : false,
  369. )
  370. );
  371. }
  372. /**
  373. * Field display on the form front-end.
  374. *
  375. * @since 1.0.0
  376. *
  377. * @param array $field Field settings.
  378. * @param array $deprecated Deprecated array.
  379. * @param array $form_data Form data and settings.
  380. */
  381. public function field_display( $field, $deprecated, $form_data ) {
  382. $using_image_choices = empty( $field['dynamic_choices'] ) && ! empty( $field['choices_images'] );
  383. // Define data.
  384. $container = $field['properties']['input_container'];
  385. $choices = $field['properties']['inputs'];
  386. $amp_state_id = '';
  387. if ( wpforms_is_amp() && $using_image_choices ) {
  388. $amp_state_id = str_replace( '-', '_', sanitize_key( $container['id'] ) ) . '_state';
  389. $state = array();
  390. foreach ( $choices as $key => $choice ) {
  391. $state[ $choice['id'] ] = ! empty( $choice['default'] );
  392. }
  393. printf(
  394. '<amp-state id="%s"><script type="application/json">%s</script></amp-state>',
  395. esc_attr( $amp_state_id ),
  396. wp_json_encode( $state )
  397. );
  398. }
  399. printf(
  400. '<ul %s>',
  401. wpforms_html_attributes( $container['id'], $container['class'], $container['data'], $container['attr'] )
  402. );
  403. foreach ( $choices as $key => $choice ) {
  404. if ( wpforms_is_amp() && $using_image_choices ) {
  405. $choice['container']['attr']['[class]'] = sprintf(
  406. '%s + ( %s[%s] ? " wpforms-selected" : "")',
  407. wp_json_encode( implode( ' ', $choice['container']['class'] ) ),
  408. $amp_state_id,
  409. wp_json_encode( $choice['id'] )
  410. );
  411. }
  412. // If the field is required, has the label hidden, and has
  413. // disclaimer mode enabled, so the required status in choice
  414. // label.
  415. $required = '';
  416. if ( ! empty( $field['disclaimer_format'] ) && ! empty( $choice['required'] ) && ! empty( $field['label_hide'] ) ) {
  417. $required = wpforms_get_field_required_label();
  418. }
  419. printf(
  420. '<li %s>',
  421. wpforms_html_attributes( $choice['container']['id'], $choice['container']['class'], $choice['container']['data'], $choice['container']['attr'] )
  422. );
  423. // The required constraint in HTML5 form validation does not work with checkbox groups, so omit in AMP.
  424. $required_attr = wpforms_is_amp() && count( $choices ) > 1 ? '' : $choice['required'];
  425. if ( $using_image_choices ) {
  426. // Make sure the image choices are keyboard-accessible.
  427. $choice['label']['attr']['tabindex'] = 0;
  428. if ( wpforms_is_amp() ) {
  429. $choice['label']['attr']['on'] = sprintf(
  430. 'tap:AMP.setState({ %s: { %s: ! %s[%s] } })',
  431. wp_json_encode( $amp_state_id ),
  432. wp_json_encode( $choice['id'] ),
  433. $amp_state_id,
  434. wp_json_encode( $choice['id'] )
  435. );
  436. $choice['label']['attr']['role'] = 'button';
  437. }
  438. // Image choices.
  439. printf(
  440. '<label %s>',
  441. wpforms_html_attributes( $choice['label']['id'], $choice['label']['class'], $choice['label']['data'], $choice['label']['attr'] )
  442. );
  443. if ( ! empty( $choice['image'] ) ) {
  444. printf(
  445. '<span class="wpforms-image-choices-image"><img src="%s" alt="%s"%s></span>',
  446. esc_url( $choice['image'] ),
  447. esc_attr( $choice['label']['text'] ),
  448. ! empty( $choice['label']['text'] ) ? ' title="' . esc_attr( $choice['label']['text'] ) . '"' : ''
  449. );
  450. }
  451. if ( 'none' === $field['choices_images_style'] ) {
  452. echo '<br>';
  453. }
  454. $choice['attr']['tabindex'] = '-1';
  455. if ( wpforms_is_amp() ) {
  456. $choice['attr']['[checked]'] = sprintf(
  457. '%s[%s]',
  458. $amp_state_id,
  459. wp_json_encode( $choice['id'] )
  460. );
  461. }
  462. printf(
  463. '<input type="checkbox" %s %s %s>',
  464. wpforms_html_attributes( $choice['id'], $choice['class'], $choice['data'], $choice['attr'] ),
  465. esc_attr( $required_attr ),
  466. checked( '1', $choice['default'], false )
  467. );
  468. echo '<span class="wpforms-image-choices-label">' . wp_kses_post( $choice['label']['text'] ) . '</span>';
  469. echo '</label>';
  470. } else {
  471. // Normal display.
  472. printf(
  473. '<input type="checkbox" %s %s %s>',
  474. wpforms_html_attributes( $choice['id'], $choice['class'], $choice['data'], $choice['attr'] ),
  475. esc_attr( $required_attr ),
  476. checked( '1', $choice['default'], false )
  477. );
  478. printf(
  479. '<label %s>%s%s</label>',
  480. wpforms_html_attributes( $choice['label']['id'], $choice['label']['class'], $choice['label']['data'], $choice['label']['attr'] ),
  481. wp_kses_post( $choice['label']['text'] ),
  482. $required
  483. ); // phpcs:ignore
  484. }
  485. echo '</li>';
  486. }
  487. echo '</ul>';
  488. }
  489. /**
  490. * Validate field on form submit.
  491. *
  492. * @since 1.5.2
  493. *
  494. * @param int $field_id field ID.
  495. * @param array $field_submit submitted data.
  496. * @param array $form_data form data.
  497. */
  498. public function validate( $field_id, $field_submit, $form_data ) {
  499. $field_submit = (array) $field_submit;
  500. $choice_limit = empty( $form_data['fields'][ $field_id ]['choice_limit'] ) ? 0 : (int) $form_data['fields'][ $field_id ]['choice_limit'];
  501. $count_choices = count( $field_submit );
  502. if ( $choice_limit > 0 && $count_choices > $choice_limit ) {
  503. // Generating the error.
  504. $error = wpforms_setting( 'validation-check-limit', esc_html__( 'You have exceeded the number of allowed selections: {#}.', 'wpforms-lite' ) );
  505. $error = str_replace( '{#}', $choice_limit, $error );
  506. }
  507. // Basic required check - If field is marked as required, check for entry data.
  508. if (
  509. ! empty( $form_data['fields'][ $field_id ]['required'] ) &&
  510. (
  511. empty( $field_submit ) ||
  512. (
  513. count( $field_submit ) === 1 &&
  514. ( ! isset( $field_submit[0] ) || (string) $field_submit[0] === '' )
  515. )
  516. )
  517. ) {
  518. $error = wpforms_get_required_label();
  519. }
  520. if ( ! empty( $error ) ) {
  521. wpforms()->process->errors[ $form_data['id'] ][ $field_id ] = $error;
  522. }
  523. }
  524. /**
  525. * Format and sanitize field.
  526. *
  527. * @since 1.0.2
  528. *
  529. * @param int $field_id Field ID.
  530. * @param array $field_submit Submitted form data.
  531. * @param array $form_data Form data and settings.
  532. */
  533. public function format( $field_id, $field_submit, $form_data ) {
  534. $field_submit = (array) $field_submit;
  535. $field = $form_data['fields'][ $field_id ];
  536. $dynamic = ! empty( $field['dynamic_choices'] ) ? $field['dynamic_choices'] : false;
  537. $name = sanitize_text_field( $field['label'] );
  538. $value_raw = wpforms_sanitize_array_combine( $field_submit );
  539. $data = array(
  540. 'name' => $name,
  541. 'value' => '',
  542. 'value_raw' => $value_raw,
  543. 'id' => absint( $field_id ),
  544. 'type' => $this->type,
  545. );
  546. if ( 'post_type' === $dynamic && ! empty( $field['dynamic_post_type'] ) ) {
  547. // Dynamic population is enabled using post type.
  548. $value_raw = implode( ',', array_map( 'absint', $field_submit ) );
  549. $data['value_raw'] = $value_raw;
  550. $data['dynamic'] = 'post_type';
  551. $data['dynamic_items'] = $value_raw;
  552. $data['dynamic_post_type'] = $field['dynamic_post_type'];
  553. $posts = array();
  554. foreach ( $field_submit as $id ) {
  555. $post = get_post( $id );
  556. if ( ! is_wp_error( $post ) && ! empty( $post ) && $data['dynamic_post_type'] === $post->post_type ) {
  557. $posts[] = esc_html( $post->post_title );
  558. }
  559. }
  560. $data['value'] = ! empty( $posts ) ? wpforms_sanitize_array_combine( $posts ) : '';
  561. }
  562. elseif ( 'taxonomy' === $dynamic && ! empty( $field['dynamic_taxonomy'] ) ) {
  563. // Dynamic population is enabled using taxonomy.
  564. $value_raw = implode( ',', array_map( 'absint', $field_submit ) );
  565. $data['value_raw'] = $value_raw;
  566. $data['dynamic'] = 'taxonomy';
  567. $data['dynamic_items'] = $value_raw;
  568. $data['dynamic_taxonomy'] = $field['dynamic_taxonomy'];
  569. $terms = array();
  570. foreach ( $field_submit as $id ) {
  571. $term = get_term( $id, $field['dynamic_taxonomy'] );
  572. if ( ! is_wp_error( $term ) && ! empty( $term ) ) {
  573. $terms[] = esc_html( $term->name );
  574. }
  575. }
  576. $data['value'] = ! empty( $terms ) ? wpforms_sanitize_array_combine( $terms ) : '';
  577. } else {
  578. // Normal processing, dynamic population is off.
  579. $choice_keys = [];
  580. // If show_values is true, that means values posted are the raw values
  581. // and not the labels. So we need to set label values. Also store
  582. // the choice keys.
  583. if ( ! empty( $field['show_values'] ) && (int) $field['show_values'] === 1 ) {
  584. foreach ( $field_submit as $item ) {
  585. foreach ( $field['choices'] as $key => $choice ) {
  586. if ( $item === $choice['value'] || ( empty( $choice['value'] ) && (int) str_replace( 'Choice ', '', $item ) === $key ) ) {
  587. $value[] = $choice['label'];
  588. $choice_keys[] = $key;
  589. break;
  590. }
  591. }
  592. }
  593. $data['value'] = ! empty( $value ) ? wpforms_sanitize_array_combine( $value ) : '';
  594. } else {
  595. $data['value'] = $value_raw;
  596. // Determine choices keys, this is needed for image choices.
  597. foreach ( $field_submit as $item ) {
  598. foreach ( $field['choices'] as $key => $choice ) {
  599. /* translators: %s - choice number. */
  600. if ( $item === $choice['label'] || $item === sprintf( esc_html__( 'Choice %s', 'wpforms-lite' ), $key ) ) {
  601. $choice_keys[] = $key;
  602. break;
  603. }
  604. }
  605. }
  606. }
  607. // Images choices are enabled, lookup and store image URLs.
  608. if ( ! empty( $choice_keys ) && ! empty( $field['choices_images'] ) ) {
  609. $data['images'] = [];
  610. foreach ( $choice_keys as $key ) {
  611. $data['images'][] = ! empty( $field['choices'][ $key ]['image'] ) ? esc_url_raw( $field['choices'][ $key ]['image'] ) : '';
  612. }
  613. }
  614. }
  615. // Push field details to be saved.
  616. wpforms()->process->fields[ $field_id ] = $data;
  617. }
  618. }
  619. new WPForms_Field_Checkbox();