acceptance.php
12 years ago
akismet.php
13 years ago
captcha.php
12 years ago
checkbox.php
12 years ago
date.php
12 years ago
file.php
12 years ago
flamingo.php
12 years ago
jetpack.php
12 years ago
number.php
12 years ago
quiz.php
12 years ago
response.php
15 years ago
select.php
12 years ago
special-mail-tags.php
13 years ago
submit.php
12 years ago
text.php
12 years ago
textarea.php
12 years ago
checkbox.php
306 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_option( 'id', 'id', true ); |
| 46 | |
| 47 | $tabindex = $tag->get_option( 'tabindex', 'int', true ); |
| 48 | |
| 49 | if ( false !== $tabindex ) |
| 50 | $tabindex = absint( $tabindex ); |
| 51 | |
| 52 | $defaults = array(); |
| 53 | |
| 54 | if ( $matches = $tag->get_first_match_option( '/^default:([0-9_]+)$/' ) ) |
| 55 | $defaults = explode( '_', $matches[1] ); |
| 56 | |
| 57 | if ( isset( $_POST[$tag->name] ) ) |
| 58 | $post = $_POST[$tag->name]; |
| 59 | else |
| 60 | $post = $multiple ? array() : ''; |
| 61 | |
| 62 | $is_posted = wpcf7_is_posted(); |
| 63 | |
| 64 | $html = ''; |
| 65 | $count = 0; |
| 66 | |
| 67 | foreach ( (array) $tag->values as $key => $value ) { |
| 68 | $class = 'wpcf7-list-item'; |
| 69 | |
| 70 | $checked = false; |
| 71 | |
| 72 | if ( $is_posted && ! empty( $post ) ) { |
| 73 | if ( $multiple && in_array( esc_sql( $value ), (array) $post ) ) |
| 74 | $checked = true; |
| 75 | if ( ! $multiple && $post == esc_sql( $value ) ) |
| 76 | $checked = true; |
| 77 | } else { |
| 78 | if ( in_array( $key + 1, (array) $defaults ) ) |
| 79 | $checked = true; |
| 80 | } |
| 81 | |
| 82 | if ( isset( $tag->labels[$key] ) ) |
| 83 | $label = $tag->labels[$key]; |
| 84 | else |
| 85 | $label = $value; |
| 86 | |
| 87 | $item_atts = array( |
| 88 | 'type' => $tag->basetype, |
| 89 | 'name' => $tag->name . ( $multiple ? '[]' : '' ), |
| 90 | 'value' => $value, |
| 91 | 'checked' => $checked ? 'checked' : '', |
| 92 | 'tabindex' => $tabindex ? $tabindex : '' ); |
| 93 | |
| 94 | $item_atts = wpcf7_format_atts( $item_atts ); |
| 95 | |
| 96 | if ( $label_first ) { // put label first, input last |
| 97 | $item = sprintf( |
| 98 | '<span class="wpcf7-list-item-label">%1$s</span> <input %2$s />', |
| 99 | esc_html( $label ), $item_atts ); |
| 100 | } else { |
| 101 | $item = sprintf( |
| 102 | '<input %2$s /> <span class="wpcf7-list-item-label">%1$s</span>', |
| 103 | esc_html( $label ), $item_atts ); |
| 104 | } |
| 105 | |
| 106 | if ( $use_label_element ) |
| 107 | $item = '<label>' . $item . '</label>'; |
| 108 | |
| 109 | if ( false !== $tabindex ) |
| 110 | $tabindex += 1; |
| 111 | |
| 112 | $count += 1; |
| 113 | |
| 114 | if ( 1 == $count ) { |
| 115 | $class .= ' first'; |
| 116 | } |
| 117 | |
| 118 | if ( count( $tag->values ) == $count ) { // last round |
| 119 | $class .= ' last'; |
| 120 | |
| 121 | if ( $free_text ) { |
| 122 | $free_text_name = sprintf( |
| 123 | '_wpcf7_%1$s_free_text_%2$s', $tag->basetype, $tag->name ); |
| 124 | |
| 125 | $free_text_atts = array( |
| 126 | 'name' => $free_text_name, |
| 127 | 'class' => 'wpcf7-free-text', |
| 128 | 'tabindex' => $tabindex ? $tabindex : '' ); |
| 129 | |
| 130 | if ( wpcf7_is_posted() && isset( $_POST[$free_text_name] ) ) { |
| 131 | $free_text_atts['value'] = stripslashes_deep( |
| 132 | $_POST[$free_text_name] ); |
| 133 | } |
| 134 | |
| 135 | $free_text_atts = wpcf7_format_atts( $free_text_atts ); |
| 136 | |
| 137 | $item .= sprintf( ' <input type="text" %s />', $free_text_atts ); |
| 138 | |
| 139 | $class .= ' has-free-text'; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | $item = '<span class="' . esc_attr( $class ) . '">' . $item . '</span>'; |
| 144 | $html .= $item; |
| 145 | } |
| 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 | $tag->name, $atts, $html, $validation_error ); |
| 152 | |
| 153 | return $html; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /* Validation filter */ |
| 158 | |
| 159 | add_filter( 'wpcf7_validate_checkbox', 'wpcf7_checkbox_validation_filter', 10, 2 ); |
| 160 | add_filter( 'wpcf7_validate_checkbox*', 'wpcf7_checkbox_validation_filter', 10, 2 ); |
| 161 | add_filter( 'wpcf7_validate_radio', 'wpcf7_checkbox_validation_filter', 10, 2 ); |
| 162 | |
| 163 | function wpcf7_checkbox_validation_filter( $result, $tag ) { |
| 164 | $tag = new WPCF7_Shortcode( $tag ); |
| 165 | |
| 166 | $type = $tag->type; |
| 167 | $name = $tag->name; |
| 168 | |
| 169 | $value = isset( $_POST[$name] ) ? (array) $_POST[$name] : array(); |
| 170 | |
| 171 | if ( 'checkbox*' == $type ) { |
| 172 | if ( empty( $value ) ) { |
| 173 | $result['valid'] = false; |
| 174 | $result['reason'][$name] = wpcf7_get_message( 'invalid_required' ); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return $result; |
| 179 | } |
| 180 | |
| 181 | |
| 182 | /* Adding free text field */ |
| 183 | |
| 184 | add_filter( 'wpcf7_posted_data', 'wpcf7_checkbox_posted_data' ); |
| 185 | |
| 186 | function wpcf7_checkbox_posted_data( $posted_data ) { |
| 187 | $tags = wpcf7_scan_shortcode( |
| 188 | array( 'type' => array( 'checkbox', 'checkbox*', 'radio' ) ) ); |
| 189 | |
| 190 | if ( empty( $tags ) ) { |
| 191 | return $posted_data; |
| 192 | } |
| 193 | |
| 194 | foreach ( $tags as $tag ) { |
| 195 | $tag = new WPCF7_Shortcode( $tag ); |
| 196 | |
| 197 | if ( ! isset( $posted_data[$tag->name] ) ) { |
| 198 | continue; |
| 199 | } |
| 200 | |
| 201 | $posted_items = (array) $posted_data[$tag->name]; |
| 202 | |
| 203 | if ( $tag->has_option( 'free_text' ) ) { |
| 204 | if ( WPCF7_USE_PIPE ) { |
| 205 | $values = $tag->pipes->collect_afters(); |
| 206 | } else { |
| 207 | $values = $tag->values; |
| 208 | } |
| 209 | |
| 210 | $last = array_pop( $values ); |
| 211 | |
| 212 | if ( in_array( $last, $posted_items ) ) { |
| 213 | $posted_items = array_diff( $posted_items, array( $last ) ); |
| 214 | |
| 215 | $free_text_name = sprintf( |
| 216 | '_wpcf7_%1$s_free_text_%2$s', $tag->basetype, $tag->name ); |
| 217 | |
| 218 | $free_text = $posted_data[$free_text_name]; |
| 219 | |
| 220 | if ( ! empty( $free_text ) ) { |
| 221 | $posted_items[] = trim( $last . ' ' . $free_text ); |
| 222 | } else { |
| 223 | $posted_items[] = $last; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | $posted_data[$tag->name] = $posted_items; |
| 229 | } |
| 230 | |
| 231 | return $posted_data; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /* Tag generator */ |
| 236 | |
| 237 | add_action( 'admin_init', 'wpcf7_add_tag_generator_checkbox_and_radio', 30 ); |
| 238 | |
| 239 | function wpcf7_add_tag_generator_checkbox_and_radio() { |
| 240 | if ( ! function_exists( 'wpcf7_add_tag_generator' ) ) |
| 241 | return; |
| 242 | |
| 243 | wpcf7_add_tag_generator( 'checkbox', __( 'Checkboxes', 'contact-form-7' ), |
| 244 | 'wpcf7-tg-pane-checkbox', 'wpcf7_tg_pane_checkbox' ); |
| 245 | |
| 246 | wpcf7_add_tag_generator( 'radio', __( 'Radio buttons', 'contact-form-7' ), |
| 247 | 'wpcf7-tg-pane-radio', 'wpcf7_tg_pane_radio' ); |
| 248 | } |
| 249 | |
| 250 | function wpcf7_tg_pane_checkbox( &$contact_form ) { |
| 251 | wpcf7_tg_pane_checkbox_and_radio( 'checkbox' ); |
| 252 | } |
| 253 | |
| 254 | function wpcf7_tg_pane_radio( &$contact_form ) { |
| 255 | wpcf7_tg_pane_checkbox_and_radio( 'radio' ); |
| 256 | } |
| 257 | |
| 258 | function wpcf7_tg_pane_checkbox_and_radio( $type = 'checkbox' ) { |
| 259 | if ( 'radio' != $type ) |
| 260 | $type = 'checkbox'; |
| 261 | |
| 262 | ?> |
| 263 | <div id="wpcf7-tg-pane-<?php echo $type; ?>" class="hidden"> |
| 264 | <form action=""> |
| 265 | <table> |
| 266 | <?php if ( 'checkbox' == $type ) : ?> |
| 267 | <tr><td><input type="checkbox" name="required" /> <?php echo esc_html( __( 'Required field?', 'contact-form-7' ) ); ?></td></tr> |
| 268 | <?php endif; ?> |
| 269 | |
| 270 | <tr><td><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?><br /><input type="text" name="name" class="tg-name oneline" /></td><td></td></tr> |
| 271 | </table> |
| 272 | |
| 273 | <table> |
| 274 | <tr> |
| 275 | <td><code>id</code> (<?php echo esc_html( __( 'optional', 'contact-form-7' ) ); ?>)<br /> |
| 276 | <input type="text" name="id" class="idvalue oneline option" /></td> |
| 277 | |
| 278 | <td><code>class</code> (<?php echo esc_html( __( 'optional', 'contact-form-7' ) ); ?>)<br /> |
| 279 | <input type="text" name="class" class="classvalue oneline option" /></td> |
| 280 | </tr> |
| 281 | |
| 282 | <tr> |
| 283 | <td><?php echo esc_html( __( 'Choices', 'contact-form-7' ) ); ?><br /> |
| 284 | <textarea name="values"></textarea><br /> |
| 285 | <span style="font-size: smaller"><?php echo esc_html( __( "* One choice per line.", 'contact-form-7' ) ); ?></span> |
| 286 | </td> |
| 287 | |
| 288 | <td> |
| 289 | <br /><input type="checkbox" name="label_first" class="option" /> <?php echo esc_html( __( 'Put a label first, a checkbox last?', 'contact-form-7' ) ); ?> |
| 290 | <br /><input type="checkbox" name="use_label_element" class="option" /> <?php echo esc_html( __( 'Wrap each item with <label> tag?', 'contact-form-7' ) ); ?> |
| 291 | <?php if ( 'checkbox' == $type ) : ?> |
| 292 | <br /><input type="checkbox" name="exclusive" class="option" /> <?php echo esc_html( __( 'Make checkboxes exclusive?', 'contact-form-7' ) ); ?> |
| 293 | <?php endif; ?> |
| 294 | </td> |
| 295 | </tr> |
| 296 | </table> |
| 297 | |
| 298 | <div class="tg-tag"><?php echo esc_html( __( "Copy this code and paste it into the form left.", 'contact-form-7' ) ); ?><br /><input type="text" name="<?php echo $type; ?>" class="tag wp-ui-text-highlight code" readonly="readonly" onfocus="this.select()" /></div> |
| 299 | |
| 300 | <div class="tg-mail-tag"><?php echo esc_html( __( "And, put this code into the Mail fields below.", 'contact-form-7' ) ); ?><br /><input type="text" class="mail-tag wp-ui-text-highlight code" readonly="readonly" onfocus="this.select()" /></div> |
| 301 | </form> |
| 302 | </div> |
| 303 | <?php |
| 304 | } |
| 305 | |
| 306 | ?> |