/modules/checkbox.php

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