checkbox.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. /**
  3. ** A base module for [checkbox], [checkbox*], and [radio]
  4. **/
  5. /* form_tag handler */
  6. add_action( 'wpcf7_init', 'wpcf7_add_form_tag_checkbox', 10, 0 );
  7. function wpcf7_add_form_tag_checkbox() {
  8. wpcf7_add_form_tag( array( 'checkbox', 'checkbox*', 'radio' ),
  9. 'wpcf7_checkbox_form_tag_handler',
  10. array(
  11. 'name-attr' => true,
  12. 'selectable-values' => true,
  13. 'multiple-controls-container' => true,
  14. )
  15. );
  16. }
  17. function wpcf7_checkbox_form_tag_handler( $tag ) {
  18. if ( empty( $tag->name ) ) {
  19. return '';
  20. }
  21. $validation_error = wpcf7_get_validation_error( $tag->name );
  22. $class = wpcf7_form_controls_class( $tag->type );
  23. if ( $validation_error ) {
  24. $class .= ' wpcf7-not-valid';
  25. }
  26. $label_first = $tag->has_option( 'label_first' );
  27. $use_label_element = $tag->has_option( 'use_label_element' );
  28. $exclusive = $tag->has_option( 'exclusive' );
  29. $free_text = $tag->has_option( 'free_text' );
  30. $multiple = false;
  31. if ( 'checkbox' == $tag->basetype ) {
  32. $multiple = ! $exclusive;
  33. } else { // radio
  34. $exclusive = false;
  35. }
  36. if ( $exclusive ) {
  37. $class .= ' wpcf7-exclusive-checkbox';
  38. }
  39. $atts = array();
  40. $atts['class'] = $tag->get_class_option( $class );
  41. $atts['id'] = $tag->get_id_option();
  42. $tabindex = $tag->get_option( 'tabindex', 'signed_int', true );
  43. if ( false !== $tabindex ) {
  44. $tabindex = (int) $tabindex;
  45. }
  46. $html = '';
  47. $count = 0;
  48. if ( $data = (array) $tag->get_data_option() ) {
  49. if ( $free_text ) {
  50. $tag->values = array_merge(
  51. array_slice( $tag->values, 0, -1 ),
  52. array_values( $data ),
  53. array_slice( $tag->values, -1 ) );
  54. $tag->labels = array_merge(
  55. array_slice( $tag->labels, 0, -1 ),
  56. array_values( $data ),
  57. array_slice( $tag->labels, -1 ) );
  58. } else {
  59. $tag->values = array_merge( $tag->values, array_values( $data ) );
  60. $tag->labels = array_merge( $tag->labels, array_values( $data ) );
  61. }
  62. }
  63. $values = $tag->values;
  64. $labels = $tag->labels;
  65. $default_choice = $tag->get_default_option( null, array(
  66. 'multiple' => $multiple,
  67. ) );
  68. $hangover = wpcf7_get_hangover( $tag->name, $multiple ? array() : '' );
  69. foreach ( $values as $key => $value ) {
  70. if ( $hangover ) {
  71. $checked = in_array( $value, (array) $hangover, true );
  72. } else {
  73. $checked = in_array( $value, (array) $default_choice, true );
  74. }
  75. if ( isset( $labels[$key] ) ) {
  76. $label = $labels[$key];
  77. } else {
  78. $label = $value;
  79. }
  80. $item_atts = array(
  81. 'type' => $tag->basetype,
  82. 'name' => $tag->name . ( $multiple ? '[]' : '' ),
  83. 'value' => $value,
  84. 'checked' => $checked ? 'checked' : '',
  85. 'tabindex' => false !== $tabindex ? $tabindex : '',
  86. );
  87. $item_atts = wpcf7_format_atts( $item_atts );
  88. if ( $label_first ) { // put label first, input last
  89. $item = sprintf(
  90. '<span class="wpcf7-list-item-label">%1$s</span><input %2$s />',
  91. esc_html( $label ), $item_atts );
  92. } else {
  93. $item = sprintf(
  94. '<input %2$s /><span class="wpcf7-list-item-label">%1$s</span>',
  95. esc_html( $label ), $item_atts );
  96. }
  97. if ( $use_label_element ) {
  98. $item = '<label>' . $item . '</label>';
  99. }
  100. if ( false !== $tabindex
  101. and 0 < $tabindex ) {
  102. $tabindex += 1;
  103. }
  104. $class = 'wpcf7-list-item';
  105. $count += 1;
  106. if ( 1 == $count ) {
  107. $class .= ' first';
  108. }
  109. if ( count( $values ) == $count ) { // last round
  110. $class .= ' last';
  111. if ( $free_text ) {
  112. $free_text_name = sprintf(
  113. '_wpcf7_%1$s_free_text_%2$s', $tag->basetype, $tag->name );
  114. $free_text_atts = array(
  115. 'name' => $free_text_name,
  116. 'class' => 'wpcf7-free-text',
  117. 'tabindex' => false !== $tabindex ? $tabindex : '',
  118. );
  119. if ( wpcf7_is_posted()
  120. and isset( $_POST[$free_text_name] ) ) {
  121. $free_text_atts['value'] = wp_unslash(
  122. $_POST[$free_text_name] );
  123. }
  124. $free_text_atts = wpcf7_format_atts( $free_text_atts );
  125. $item .= sprintf( ' <input type="text" %s />', $free_text_atts );
  126. $class .= ' has-free-text';
  127. }
  128. }
  129. $item = '<span class="' . esc_attr( $class ) . '">' . $item . '</span>';
  130. $html .= $item;
  131. }
  132. $atts = wpcf7_format_atts( $atts );
  133. $html = sprintf(
  134. '<span class="wpcf7-form-control-wrap %1$s"><span %2$s>%3$s</span>%4$s</span>',
  135. sanitize_html_class( $tag->name ), $atts, $html, $validation_error );
  136. return $html;
  137. }
  138. /* Validation filter */
  139. add_filter( 'wpcf7_validate_checkbox',
  140. 'wpcf7_checkbox_validation_filter', 10, 2 );
  141. add_filter( 'wpcf7_validate_checkbox*',
  142. 'wpcf7_checkbox_validation_filter', 10, 2 );
  143. add_filter( 'wpcf7_validate_radio',
  144. 'wpcf7_checkbox_validation_filter', 10, 2 );
  145. function wpcf7_checkbox_validation_filter( $result, $tag ) {
  146. $name = $tag->name;
  147. $is_required = $tag->is_required() || 'radio' == $tag->type;
  148. $value = isset( $_POST[$name] ) ? (array) $_POST[$name] : array();
  149. if ( $is_required and empty( $value ) ) {
  150. $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
  151. }
  152. return $result;
  153. }
  154. /* Adding free text field */
  155. add_filter( 'wpcf7_posted_data', 'wpcf7_checkbox_posted_data', 10, 1 );
  156. function wpcf7_checkbox_posted_data( $posted_data ) {
  157. $tags = wpcf7_scan_form_tags(
  158. array( 'type' => array( 'checkbox', 'checkbox*', 'radio' ) ) );
  159. if ( empty( $tags ) ) {
  160. return $posted_data;
  161. }
  162. foreach ( $tags as $tag ) {
  163. if ( ! isset( $posted_data[$tag->name] ) ) {
  164. continue;
  165. }
  166. $posted_items = (array) $posted_data[$tag->name];
  167. if ( $tag->has_option( 'free_text' ) ) {
  168. if ( WPCF7_USE_PIPE ) {
  169. $values = $tag->pipes->collect_afters();
  170. } else {
  171. $values = $tag->values;
  172. }
  173. $last = array_pop( $values );
  174. $last = html_entity_decode( $last, ENT_QUOTES, 'UTF-8' );
  175. if ( in_array( $last, $posted_items ) ) {
  176. $posted_items = array_diff( $posted_items, array( $last ) );
  177. $free_text_name = sprintf(
  178. '_wpcf7_%1$s_free_text_%2$s', $tag->basetype, $tag->name );
  179. $free_text = $posted_data[$free_text_name];
  180. if ( ! empty( $free_text ) ) {
  181. $posted_items[] = trim( $last . ' ' . $free_text );
  182. } else {
  183. $posted_items[] = $last;
  184. }
  185. }
  186. }
  187. $posted_data[$tag->name] = $posted_items;
  188. }
  189. return $posted_data;
  190. }
  191. /* Tag generator */
  192. add_action( 'wpcf7_admin_init',
  193. 'wpcf7_add_tag_generator_checkbox_and_radio', 30, 0 );
  194. function wpcf7_add_tag_generator_checkbox_and_radio() {
  195. $tag_generator = WPCF7_TagGenerator::get_instance();
  196. $tag_generator->add( 'checkbox', __( 'checkboxes', 'contact-form-7' ),
  197. 'wpcf7_tag_generator_checkbox' );
  198. $tag_generator->add( 'radio', __( 'radio buttons', 'contact-form-7' ),
  199. 'wpcf7_tag_generator_checkbox' );
  200. }
  201. function wpcf7_tag_generator_checkbox( $contact_form, $args = '' ) {
  202. $args = wp_parse_args( $args, array() );
  203. $type = $args['id'];
  204. if ( 'radio' != $type ) {
  205. $type = 'checkbox';
  206. }
  207. if ( 'checkbox' == $type ) {
  208. $description = __( "Generate a form-tag for a group of checkboxes. For more details, see %s.", 'contact-form-7' );
  209. } elseif ( 'radio' == $type ) {
  210. $description = __( "Generate a form-tag for a group of radio buttons. For more details, see %s.", 'contact-form-7' );
  211. }
  212. $desc_link = wpcf7_link( __( 'https://contactform7.com/checkboxes-radio-buttons-and-menus/', 'contact-form-7' ), __( 'Checkboxes, Radio Buttons and Menus', 'contact-form-7' ) );
  213. ?>
  214. <div class="control-box">
  215. <fieldset>
  216. <legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend>
  217. <table class="form-table">
  218. <tbody>
  219. <?php if ( 'checkbox' == $type ) : ?>
  220. <tr>
  221. <th scope="row"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></th>
  222. <td>
  223. <fieldset>
  224. <legend class="screen-reader-text"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></legend>
  225. <label><input type="checkbox" name="required" /> <?php echo esc_html( __( 'Required field', 'contact-form-7' ) ); ?></label>
  226. </fieldset>
  227. </td>
  228. </tr>
  229. <?php endif; ?>
  230. <tr>
  231. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th>
  232. <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td>
  233. </tr>
  234. <tr>
  235. <th scope="row"><?php echo esc_html( __( 'Options', 'contact-form-7' ) ); ?></th>
  236. <td>
  237. <fieldset>
  238. <legend class="screen-reader-text"><?php echo esc_html( __( 'Options', 'contact-form-7' ) ); ?></legend>
  239. <textarea name="values" class="values" id="<?php echo esc_attr( $args['content'] . '-values' ); ?>"></textarea>
  240. <label for="<?php echo esc_attr( $args['content'] . '-values' ); ?>"><span class="description"><?php echo esc_html( __( "One option per line.", 'contact-form-7' ) ); ?></span></label><br />
  241. <label><input type="checkbox" name="label_first" class="option" /> <?php echo esc_html( __( 'Put a label first, a checkbox last', 'contact-form-7' ) ); ?></label><br />
  242. <label><input type="checkbox" name="use_label_element" class="option" /> <?php echo esc_html( __( 'Wrap each item with label element', 'contact-form-7' ) ); ?></label>
  243. <?php if ( 'checkbox' == $type ) : ?>
  244. <br /><label><input type="checkbox" name="exclusive" class="option" /> <?php echo esc_html( __( 'Make checkboxes exclusive', 'contact-form-7' ) ); ?></label>
  245. <?php endif; ?>
  246. </fieldset>
  247. </td>
  248. </tr>
  249. <tr>
  250. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th>
  251. <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td>
  252. </tr>
  253. <tr>
  254. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th>
  255. <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td>
  256. </tr>
  257. </tbody>
  258. </table>
  259. </fieldset>
  260. </div>
  261. <div class="insert-box">
  262. <input type="text" name="<?php echo $type; ?>" class="tag code" readonly="readonly" onfocus="this.select()" />
  263. <div class="submitbox">
  264. <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" />
  265. </div>
  266. <br class="clear" />
  267. <p class="description mail-tag"><label for="<?php echo esc_attr( $args['content'] . '-mailtag' ); ?>"><?php echo sprintf( esc_html( __( "To use the value input through this field in a mail field, you need to insert the corresponding mail-tag (%s) into the field on the Mail tab.", 'contact-form-7' ) ), '<strong><span class="mail-tag"></span></strong>' ); ?><input type="text" class="mail-tag code hidden" readonly="readonly" id="<?php echo esc_attr( $args['content'] . '-mailtag' ); ?>" /></label></p>
  268. </div>
  269. <?php
  270. }