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