wpcf7_checkbox_form_tag_handler

The Contact Form 7 wpcf7 checkbox form tag handler function.

Description

wpcf7_checkbox_form_tag_handler( $tag ); 

Parameters (1)

0. $tag
The tag.

Usage

  1. if ( !function_exists( 'wpcf7_checkbox_form_tag_handler' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'contact-form-7/modules/checkbox.php'; 
  3.  
  4. // The tag. 
  5. $tag = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wpcf7_checkbox_form_tag_handler($tag); 
  9.  

Defined (1)

The function is defined in the following location(s).

/modules/checkbox.php  
  1. function wpcf7_checkbox_form_tag_handler( $tag ) { 
  2. $tag = new WPCF7_FormTag( $tag ); 
  3.  
  4. if ( empty( $tag->name ) ) { 
  5. return ''; 
  6.  
  7. $validation_error = wpcf7_get_validation_error( $tag->name ); 
  8.  
  9. $class = wpcf7_form_controls_class( $tag->type ); 
  10.  
  11. if ( $validation_error ) { 
  12. $class .= ' wpcf7-not-valid'; 
  13.  
  14. $label_first = $tag->has_option( 'label_first' ); 
  15. $use_label_element = $tag->has_option( 'use_label_element' ); 
  16. $exclusive = $tag->has_option( 'exclusive' ); 
  17. $free_text = $tag->has_option( 'free_text' ); 
  18. $multiple = false; 
  19.  
  20. if ( 'checkbox' == $tag->basetype ) { 
  21. $multiple = ! $exclusive; 
  22. } else { // radio 
  23. $exclusive = false; 
  24.  
  25. if ( $exclusive ) { 
  26. $class .= ' wpcf7-exclusive-checkbox'; 
  27.  
  28. $atts = array(); 
  29.  
  30. $atts['class'] = $tag->get_class_option( $class ); 
  31. $atts['id'] = $tag->get_id_option(); 
  32.  
  33. $tabindex = $tag->get_option( 'tabindex', 'int', true ); 
  34.  
  35. if ( false !== $tabindex ) { 
  36. $tabindex = absint( $tabindex ); 
  37.  
  38. $html = ''; 
  39. $count = 0; 
  40.  
  41. $values = (array) $tag->values; 
  42. $labels = (array) $tag->labels; 
  43.  
  44. if ( $data = (array) $tag->get_data_option() ) { 
  45. if ( $free_text ) { 
  46. $values = array_merge( 
  47. array_slice( $values, 0, -1 ),  
  48. array_values( $data ),  
  49. array_slice( $values, -1 ) ); 
  50. $labels = array_merge( 
  51. array_slice( $labels, 0, -1 ),  
  52. array_values( $data ),  
  53. array_slice( $labels, -1 ) ); 
  54. } else { 
  55. $values = array_merge( $values, array_values( $data ) ); 
  56. $labels = array_merge( $labels, array_values( $data ) ); 
  57.  
  58. $defaults = array(); 
  59.  
  60. $default_choice = $tag->get_default_option( null, 'multiple=1' ); 
  61.  
  62. foreach ( $default_choice as $value ) { 
  63. $key = array_search( $value, $values, true ); 
  64.  
  65. if ( false !== $key ) { 
  66. $defaults[] = (int) $key + 1; 
  67.  
  68. if ( $matches = $tag->get_first_match_option( '/^default:([0-9_]+)$/' ) ) { 
  69. $defaults = array_merge( $defaults, explode( '_', $matches[1] ) ); 
  70.  
  71. $defaults = array_unique( $defaults ); 
  72.  
  73. $hangover = wpcf7_get_hangover( $tag->name, $multiple ? array() : '' ); 
  74.  
  75. foreach ( $values as $key => $value ) { 
  76. $class = 'wpcf7-list-item'; 
  77.  
  78. $checked = false; 
  79.  
  80. if ( $hangover ) { 
  81. if ( $multiple ) { 
  82. $checked = in_array( esc_sql( $value ), (array) $hangover ); 
  83. } else { 
  84. $checked = ( $hangover == esc_sql( $value ) ); 
  85. } else { 
  86. $checked = in_array( $key + 1, (array) $defaults ); 
  87.  
  88. if ( isset( $labels[$key] ) ) 
  89. $label = $labels[$key]; 
  90. else 
  91. $label = $value; 
  92.  
  93. $item_atts = array( 
  94. 'type' => $tag->basetype,  
  95. 'name' => $tag->name . ( $multiple ? '[]' : '' ),  
  96. 'value' => $value,  
  97. 'checked' => $checked ? 'checked' : '',  
  98. 'tabindex' => $tabindex ? $tabindex : '' ); 
  99.  
  100. $item_atts = wpcf7_format_atts( $item_atts ); 
  101.  
  102. if ( $label_first ) { // put label first, input last 
  103. $item = sprintf( 
  104. '<span class="wpcf7-list-item-label">%1$s</span><input %2$s />',  
  105. esc_html( $label ), $item_atts ); 
  106. } else { 
  107. $item = sprintf( 
  108. '<input %2$s /><span class="wpcf7-list-item-label">%1$s</span>',  
  109. esc_html( $label ), $item_atts ); 
  110.  
  111. if ( $use_label_element ) 
  112. $item = '<label>' . $item . '</label>'; 
  113.  
  114. if ( false !== $tabindex ) 
  115. $tabindex += 1; 
  116.  
  117. $count += 1; 
  118.  
  119. if ( 1 == $count ) { 
  120. $class .= ' first'; 
  121.  
  122. if ( count( $values ) == $count ) { // last round 
  123. $class .= ' last'; 
  124.  
  125. if ( $free_text ) { 
  126. $free_text_name = sprintf( 
  127. '_wpcf7_%1$s_free_text_%2$s', $tag->basetype, $tag->name ); 
  128.  
  129. $free_text_atts = array( 
  130. 'name' => $free_text_name,  
  131. 'class' => 'wpcf7-free-text',  
  132. 'tabindex' => $tabindex ? $tabindex : '' ); 
  133.  
  134. if ( wpcf7_is_posted() && isset( $_POST[$free_text_name] ) ) { 
  135. $free_text_atts['value'] = wp_unslash( 
  136. $_POST[$free_text_name] ); 
  137.  
  138. $free_text_atts = wpcf7_format_atts( $free_text_atts ); 
  139.  
  140. $item .= sprintf( ' <input type="text" %s />', $free_text_atts ); 
  141.  
  142. $class .= ' has-free-text'; 
  143.  
  144. $item = '<span class="' . esc_attr( $class ) . '">' . $item . '</span>'; 
  145. $html .= $item; 
  146.  
  147. $atts = wpcf7_format_atts( $atts ); 
  148.  
  149. $html = sprintf( 
  150. '<span class="wpcf7-form-control-wrap %1$s"><span %2$s>%3$s</span>%4$s</span>',  
  151. sanitize_html_class( $tag->name ), $atts, $html, $validation_error ); 
  152.  
  153. return $html;