暂无描述

class-radio.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. <?php
  2. /**
  3. * Multiple Choice field.
  4. *
  5. * @since 1.0.0
  6. */
  7. class WPForms_Field_Radio 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__( 'Multiple Choice', 'wpforms-lite' );
  16. $this->type = 'radio';
  17. $this->icon = 'fa-dot-circle-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_radio', 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 radio 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. 'radio' === $field['type'] &&
  63. ! empty( $field['image'] ) &&
  64. 'entry-table' !== $context &&
  65. apply_filters( 'wpforms_radio_field_html_value_images', true, $context )
  66. ) {
  67. if ( ! empty( $field['image'] ) ) {
  68. return sprintf(
  69. '<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',
  70. esc_url( $field['image'] ),
  71. $value
  72. );
  73. }
  74. }
  75. return $value;
  76. }
  77. /**
  78. * Define additional field properties.
  79. *
  80. * @since 1.4.5
  81. *
  82. * @param array $properties Field properties.
  83. * @param array $field Field settings.
  84. * @param array $form_data Form data and settings.
  85. *
  86. * @return array
  87. */
  88. public function field_properties( $properties, $field, $form_data ) {
  89. // Remove primary input.
  90. unset( $properties['inputs']['primary'] );
  91. // Define data.
  92. $form_id = absint( $form_data['id'] );
  93. $field_id = absint( $field['id'] );
  94. $choices = $field['choices'];
  95. $dynamic = wpforms_get_field_dynamic_choices( $field, $form_id, $form_data );
  96. if ( $dynamic ) {
  97. $choices = $dynamic;
  98. $field['show_values'] = true;
  99. }
  100. // Set input container (ul) properties.
  101. $properties['input_container'] = array(
  102. 'class' => array( ! empty( $field['random'] ) ? 'wpforms-randomize' : '' ),
  103. 'data' => array(),
  104. 'attr' => array(),
  105. 'id' => "wpforms-{$form_id}-field_{$field_id}",
  106. );
  107. // Set input properties.
  108. foreach ( $choices as $key => $choice ) {
  109. // Used for dynamic choices.
  110. $depth = isset( $choice['depth'] ) ? absint( $choice['depth'] ) : 1;
  111. $value = isset( $field['show_values'] ) ? $choice['value'] : $choice['label'];
  112. /* translators: %s - choice number. */
  113. $value = ( '' === $value ) ? sprintf( esc_html__( 'Choice %s', 'wpforms-lite' ), $key ) : $value;
  114. $properties['inputs'][ $key ] = array(
  115. 'container' => array(
  116. 'attr' => array(),
  117. 'class' => array( "choice-{$key}", "depth-{$depth}" ),
  118. 'data' => array(),
  119. 'id' => '',
  120. ),
  121. 'label' => array(
  122. 'attr' => array(
  123. 'for' => "wpforms-{$form_id}-field_{$field_id}_{$key}",
  124. ),
  125. 'class' => array( 'wpforms-field-label-inline' ),
  126. 'data' => array(),
  127. 'id' => '',
  128. 'text' => $choice['label'],
  129. ),
  130. 'attr' => array(
  131. 'name' => "wpforms[fields][{$field_id}]",
  132. 'value' => $value,
  133. ),
  134. 'class' => array(),
  135. 'data' => array(),
  136. 'id' => "wpforms-{$form_id}-field_{$field_id}_{$key}",
  137. 'image' => isset( $choice['image'] ) ? $choice['image'] : '',
  138. 'required' => ! empty( $field['required'] ) ? 'required' : '',
  139. 'default' => isset( $choice['default'] ),
  140. );
  141. }
  142. // Required class for pagebreak validation.
  143. if ( ! empty( $field['required'] ) ) {
  144. $properties['input_container']['class'][] = 'wpforms-field-required';
  145. }
  146. // Custom properties if image choices is enabled.
  147. if ( ! $dynamic && ! empty( $field['choices_images'] ) ) {
  148. $properties['input_container']['class'][] = 'wpforms-image-choices';
  149. $properties['input_container']['class'][] = 'wpforms-image-choices-' . sanitize_html_class( $field['choices_images_style'] );
  150. foreach ( $properties['inputs'] as $key => $inputs ) {
  151. $properties['inputs'][ $key ]['container']['class'][] = 'wpforms-image-choices-item';
  152. if ( in_array( $field['choices_images_style'], array( 'modern', 'classic' ), true ) ) {
  153. $properties['inputs'][ $key ]['class'][] = 'wpforms-screen-reader-element';
  154. }
  155. }
  156. }
  157. // Add selected class for choices with defaults.
  158. foreach ( $properties['inputs'] as $key => $inputs ) {
  159. if ( ! empty( $inputs['default'] ) ) {
  160. $properties['inputs'][ $key ]['container']['class'][] = 'wpforms-selected';
  161. }
  162. }
  163. return $properties;
  164. }
  165. /**
  166. * Field options panel inside the builder.
  167. *
  168. * @since 1.0.0
  169. *
  170. * @param array $field Field settings.
  171. */
  172. public function field_options( $field ) {
  173. /*
  174. * Basic field options.
  175. */
  176. // Options open markup.
  177. $this->field_option(
  178. 'basic-options',
  179. $field,
  180. array(
  181. 'markup' => 'open',
  182. )
  183. );
  184. // Label.
  185. $this->field_option( 'label', $field );
  186. // Choices.
  187. $this->field_option( 'choices', $field );
  188. // Choices Images.
  189. $this->field_option( 'choices_images', $field );
  190. // Description.
  191. $this->field_option( 'description', $field );
  192. // Required toggle.
  193. $this->field_option( 'required', $field );
  194. // Options close markup.
  195. $this->field_option(
  196. 'basic-options',
  197. $field,
  198. array(
  199. 'markup' => 'close',
  200. )
  201. );
  202. /*
  203. * Advanced field options.
  204. */
  205. // Options open markup.
  206. $this->field_option(
  207. 'advanced-options',
  208. $field,
  209. array(
  210. 'markup' => 'open',
  211. )
  212. );
  213. // Randomize order of choices.
  214. $this->field_element(
  215. 'row',
  216. $field,
  217. array(
  218. 'slug' => 'random',
  219. 'content' => $this->field_element(
  220. 'toggle',
  221. $field,
  222. array(
  223. 'slug' => 'random',
  224. 'value' => isset( $field['random'] ) ? '1' : '0',
  225. 'desc' => esc_html__( 'Randomize Choices', 'wpforms-lite' ),
  226. 'tooltip' => esc_html__( 'Check this option to randomize the order of the choices.', 'wpforms-lite' ),
  227. ),
  228. false
  229. ),
  230. )
  231. );
  232. // Show Values toggle option. This option will only show if already used
  233. // or if manually enabled by a filter.
  234. if ( ! empty( $field['show_values'] ) || wpforms_show_fields_options_setting() ) {
  235. $this->field_element(
  236. 'row',
  237. $field,
  238. array(
  239. 'slug' => 'show_values',
  240. 'content' => $this->field_element(
  241. 'toggle',
  242. $field,
  243. array(
  244. 'slug' => 'show_values',
  245. 'value' => isset( $field['show_values'] ) ? $field['show_values'] : '0',
  246. 'desc' => esc_html__( 'Show Values', 'wpforms-lite' ),
  247. 'tooltip' => esc_html__( 'Check this option to manually set form field values.', 'wpforms-lite' ),
  248. ),
  249. false
  250. ),
  251. )
  252. );
  253. }
  254. // Choices Images Style (theme).
  255. $this->field_option( 'choices_images_style', $field );
  256. // Display format.
  257. $this->field_option( 'input_columns', $field );
  258. // Dynamic choice auto-populating toggle.
  259. $this->field_option( 'dynamic_choices', $field );
  260. // Dynamic choice source.
  261. $this->field_option( 'dynamic_choices_source', $field );
  262. // Custom CSS classes.
  263. $this->field_option( 'css', $field );
  264. // Hide label.
  265. $this->field_option( 'label_hide', $field );
  266. // Options close markup.
  267. $this->field_option(
  268. 'advanced-options',
  269. $field,
  270. [
  271. 'markup' => 'close',
  272. ]
  273. );
  274. }
  275. /**
  276. * Field preview inside the builder.
  277. *
  278. * @since 1.0.0
  279. *
  280. * @param array $field Field settings.
  281. */
  282. public function field_preview( $field ) {
  283. // Label.
  284. $this->field_preview_option( 'label', $field );
  285. // Choices.
  286. $this->field_preview_option( 'choices', $field );
  287. // Description.
  288. $this->field_preview_option( 'description', $field );
  289. }
  290. /**
  291. * Field display on the form front-end.
  292. *
  293. * @since 1.0.0
  294. *
  295. * @param array $field Field settings.
  296. * @param array $deprecated Deprecated array.
  297. * @param array $form_data Form data and settings.
  298. */
  299. public function field_display( $field, $deprecated, $form_data ) {
  300. $using_image_choices = empty( $field['dynamic_choices'] ) && ! empty( $field['choices_images'] );
  301. // Define data.
  302. $container = $field['properties']['input_container'];
  303. $choices = $field['properties']['inputs'];
  304. $amp_state_id = '';
  305. if ( wpforms_is_amp() && $using_image_choices ) {
  306. $amp_state_id = str_replace( '-', '_', sanitize_key( $container['id'] ) ) . '_state';
  307. $state = array(
  308. 'selected' => null,
  309. );
  310. foreach ( $choices as $key => $choice ) {
  311. if ( $choice['default'] ) {
  312. $state['selected'] = $choice['attr']['value'];
  313. break;
  314. }
  315. }
  316. printf(
  317. '<amp-state id="%s"><script type="application/json">%s</script></amp-state>',
  318. esc_attr( $amp_state_id ),
  319. wp_json_encode( $state )
  320. );
  321. }
  322. printf(
  323. '<ul %s>',
  324. wpforms_html_attributes( $container['id'], $container['class'], $container['data'], $container['attr'] )
  325. ); // WPCS: XSS ok.
  326. foreach ( $choices as $key => $choice ) {
  327. if ( wpforms_is_amp() && $using_image_choices ) {
  328. $choice['container']['attr']['[class]'] = sprintf(
  329. '%s + ( %s == %s ? " wpforms-selected" : "")',
  330. wp_json_encode( implode( ' ', $choice['container']['class'] ) ),
  331. $amp_state_id,
  332. wp_json_encode( $choice['attr']['value'] )
  333. );
  334. }
  335. printf(
  336. '<li %s>',
  337. wpforms_html_attributes( $choice['container']['id'], $choice['container']['class'], $choice['container']['data'], $choice['container']['attr'] )
  338. ); // WPCS: XSS ok.
  339. if ( $using_image_choices ) {
  340. // Make sure the image choices are keyboard-accessible.
  341. $choice['label']['attr']['tabindex'] = 0;
  342. if ( wpforms_is_amp() ) {
  343. $choice['label']['attr']['on'] = sprintf(
  344. 'tap:AMP.setState(%s)',
  345. wp_json_encode( array( $amp_state_id => $choice['attr']['value'] ) )
  346. );
  347. $choice['label']['attr']['role'] = 'button';
  348. }
  349. // Image choices.
  350. printf(
  351. '<label %s>',
  352. wpforms_html_attributes( $choice['label']['id'], $choice['label']['class'], $choice['label']['data'], $choice['label']['attr'] )
  353. ); // WPCS: XSS ok.
  354. if ( ! empty( $choice['image'] ) ) {
  355. printf(
  356. '<span class="wpforms-image-choices-image"><img src="%s" alt="%s"%s></span>',
  357. esc_url( $choice['image'] ),
  358. esc_attr( $choice['label']['text'] ),
  359. ! empty( $choice['label']['text'] ) ? ' title="' . esc_attr( $choice['label']['text'] ) . '"' : ''
  360. );
  361. }
  362. if ( 'none' === $field['choices_images_style'] ) {
  363. echo '<br>';
  364. }
  365. $choice['attr']['tabindex'] = '-1';
  366. if ( wpforms_is_amp() ) {
  367. $choice['attr']['[checked]'] = sprintf(
  368. '%s == %s',
  369. $amp_state_id,
  370. wp_json_encode( $choice['attr']['value'] )
  371. );
  372. }
  373. printf(
  374. '<input type="radio" %s %s %s>',
  375. wpforms_html_attributes( $choice['id'], $choice['class'], $choice['data'], $choice['attr'] ),
  376. esc_attr( $choice['required'] ),
  377. checked( '1', $choice['default'], false )
  378. ); // WPCS: XSS ok.
  379. echo '<span class="wpforms-image-choices-label">' . wp_kses_post( $choice['label']['text'] ) . '</span>';
  380. echo '</label>';
  381. } else {
  382. // Normal display.
  383. printf(
  384. '<input type="radio" %s %s %s>',
  385. wpforms_html_attributes( $choice['id'], $choice['class'], $choice['data'], $choice['attr'] ),
  386. esc_attr( $choice['required'] ),
  387. checked( '1', $choice['default'], false )
  388. ); // WPCS: XSS ok.
  389. printf(
  390. '<label %s>%s</label>',
  391. wpforms_html_attributes( $choice['label']['id'], $choice['label']['class'], $choice['label']['data'], $choice['label']['attr'] ),
  392. wp_kses_post( $choice['label']['text'] )
  393. ); // WPCS: XSS ok.
  394. }
  395. echo '</li>';
  396. }
  397. echo '</ul>';
  398. }
  399. /**
  400. * Format and sanitize field.
  401. *
  402. * @since 1.0.2
  403. *
  404. * @param int $field_id Field ID.
  405. * @param string $field_submit Submitted form data.
  406. * @param array $form_data Form data and settings.
  407. */
  408. public function format( $field_id, $field_submit, $form_data ) {
  409. $field = $form_data['fields'][ $field_id ];
  410. $dynamic = ! empty( $field['dynamic_choices'] ) ? $field['dynamic_choices'] : false;
  411. $name = sanitize_text_field( $field['label'] );
  412. $value_raw = sanitize_text_field( $field_submit );
  413. $data = array(
  414. 'name' => $name,
  415. 'value' => '',
  416. 'value_raw' => $value_raw,
  417. 'id' => absint( $field_id ),
  418. 'type' => $this->type,
  419. );
  420. if ( 'post_type' === $dynamic && ! empty( $field['dynamic_post_type'] ) ) {
  421. // Dynamic population is enabled using post type.
  422. $data['dynamic'] = 'post_type';
  423. $data['dynamic_items'] = absint( $value_raw );
  424. $data['dynamic_post_type'] = $field['dynamic_post_type'];
  425. $post = get_post( $value_raw );
  426. if ( ! empty( $post ) && ! is_wp_error( $post ) && $data['dynamic_post_type'] === $post->post_type ) {
  427. $data['value'] = esc_html( $post->post_title );
  428. }
  429. } elseif ( 'taxonomy' === $dynamic && ! empty( $field['dynamic_taxonomy'] ) ) {
  430. // Dynamic population is enabled using taxonomy.
  431. $data['dynamic'] = 'taxonomy';
  432. $data['dynamic_items'] = absint( $value_raw );
  433. $data['dynamic_taxonomy'] = $field['dynamic_taxonomy'];
  434. $term = get_term( $value_raw, $data['dynamic_taxonomy'] );
  435. if ( ! empty( $term ) && ! is_wp_error( $term ) ) {
  436. $data['value'] = esc_html( $term->name );
  437. }
  438. } else {
  439. // Normal processing, dynamic population is off.
  440. $choice_key = '';
  441. // If show_values is true, that means value posted is the raw value
  442. // and not the label. So we need to set label value. Also store
  443. // the choice key.
  444. if ( ! empty( $field['show_values'] ) ) {
  445. foreach ( $field['choices'] as $key => $choice ) {
  446. if ( $choice['value'] === $field_submit ) {
  447. $data['value'] = sanitize_text_field( $choice['label'] );
  448. $choice_key = $key;
  449. break;
  450. }
  451. }
  452. } else {
  453. $data['value'] = $value_raw;
  454. // Determine choice key, this is needed for image choices.
  455. foreach ( $field['choices'] as $key => $choice ) {
  456. /* translators: %s - choice number. */
  457. if ( $value_raw === $choice['label'] || $value_raw === sprintf( esc_html__( 'Choice %s', 'wpforms-lite' ), $key ) ) {
  458. $choice_key = $key;
  459. break;
  460. }
  461. }
  462. }
  463. // Images choices are enabled, lookup and store image URL.
  464. if ( ! empty( $choice_key ) && ! empty( $field['choices_images'] ) ) {
  465. $data['image'] = ! empty( $field['choices'][ $choice_key ]['image'] ) ? esc_url_raw( $field['choices'][ $choice_key ]['image'] ) : '';
  466. }
  467. }
  468. // Push field details to be saved.
  469. wpforms()->process->fields[ $field_id ] = $data;
  470. }
  471. }
  472. new WPForms_Field_Radio();