acceptance.php
16 years ago
captcha.php
16 years ago
checkbox.php
16 years ago
file.php
16 years ago
icl.php
16 years ago
quiz.php
16 years ago
select.php
16 years ago
submit.php
16 years ago
text.php
16 years ago
textarea.php
16 years ago
checkbox.php
232 lines
| 1 | <?php |
| 2 | /** |
| 3 | ** A base module for [checkbox], [checkbox*], and [radio] |
| 4 | **/ |
| 5 | |
| 6 | /* Shortcode handler */ |
| 7 | |
| 8 | wpcf7_add_shortcode( 'checkbox', 'wpcf7_checkbox_shortcode_handler', true ); |
| 9 | wpcf7_add_shortcode( 'checkbox*', 'wpcf7_checkbox_shortcode_handler', true ); |
| 10 | wpcf7_add_shortcode( 'radio', 'wpcf7_checkbox_shortcode_handler', true ); |
| 11 | |
| 12 | function wpcf7_checkbox_shortcode_handler( $tag ) { |
| 13 | global $wpcf7_contact_form; |
| 14 | |
| 15 | if ( ! is_array( $tag ) ) |
| 16 | return ''; |
| 17 | |
| 18 | $type = $tag['type']; |
| 19 | $name = $tag['name']; |
| 20 | $options = (array) $tag['options']; |
| 21 | $values = (array) $tag['values']; |
| 22 | $labels = (array) $tag['labels']; |
| 23 | |
| 24 | if ( empty( $name ) ) |
| 25 | return ''; |
| 26 | |
| 27 | $atts = ''; |
| 28 | $id_att = ''; |
| 29 | $class_att = ''; |
| 30 | |
| 31 | $defaults = array(); |
| 32 | |
| 33 | $label_first = false; |
| 34 | $use_label_element = false; |
| 35 | |
| 36 | if ( 'checkbox*' == $type ) |
| 37 | $class_att .= ' wpcf7-validates-as-required'; |
| 38 | |
| 39 | if ( 'checkbox' == $type || 'checkbox*' == $type ) |
| 40 | $class_att .= ' wpcf7-checkbox'; |
| 41 | |
| 42 | if ( 'radio' == $type ) |
| 43 | $class_att .= ' wpcf7-radio'; |
| 44 | |
| 45 | foreach ( $options as $option ) { |
| 46 | if ( preg_match( '%^id:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) { |
| 47 | $id_att = $matches[1]; |
| 48 | |
| 49 | } elseif ( preg_match( '%^class:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) { |
| 50 | $class_att .= ' ' . $matches[1]; |
| 51 | |
| 52 | } elseif ( preg_match( '/^default:([0-9_]+)$/', $option, $matches ) ) { |
| 53 | $defaults = explode( '_', $matches[1] ); |
| 54 | |
| 55 | } elseif ( preg_match( '%^label[_-]?first$%', $option ) ) { |
| 56 | $label_first = true; |
| 57 | |
| 58 | } elseif ( preg_match( '%^use[_-]?label[_-]?element$%', $option ) ) { |
| 59 | $use_label_element = true; |
| 60 | |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if ( $id_att ) |
| 65 | $atts .= ' id="' . trim( $id_att ) . '"'; |
| 66 | |
| 67 | if ( $class_att ) |
| 68 | $atts .= ' class="' . trim( $class_att ) . '"'; |
| 69 | |
| 70 | $multiple = preg_match( '/^checkbox[*]?$/', $type ) && ! preg_grep( '%^exclusive$%', $options ); |
| 71 | |
| 72 | $html = ''; |
| 73 | |
| 74 | if ( preg_match( '/^checkbox[*]?$/', $type ) && ! $multiple && WPCF7_LOAD_JS ) |
| 75 | $onclick = ' onclick="wpcf7ExclusiveCheckbox(this);"'; |
| 76 | |
| 77 | $input_type = rtrim( $type, '*' ); |
| 78 | |
| 79 | $posted = is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) && $wpcf7_contact_form->is_posted(); |
| 80 | |
| 81 | foreach ( $values as $key => $value ) { |
| 82 | $checked = false; |
| 83 | |
| 84 | if ( in_array( $key + 1, (array) $defaults ) ) |
| 85 | $checked = true; |
| 86 | |
| 87 | if ( $posted) { |
| 88 | if ( $multiple && in_array( esc_sql( $value ), (array) $_POST[$name] ) ) |
| 89 | $checked = true; |
| 90 | if ( ! $multiple && $_POST[$name] == esc_sql( $value ) ) |
| 91 | $checked = true; |
| 92 | } |
| 93 | |
| 94 | $checked = $checked ? ' checked="checked"' : ''; |
| 95 | |
| 96 | if ( isset( $labels[$key] ) ) |
| 97 | $label = $labels[$key]; |
| 98 | else |
| 99 | $label = $value; |
| 100 | |
| 101 | if ( $label_first ) { // put label first, input last |
| 102 | $item = '<span class="wpcf7-list-item-label">' . esc_html( $label ) . '</span> '; |
| 103 | $item .= '<input type="' . $input_type . '" name="' . $name . ( $multiple ? '[]' : '' ) . '" value="' . esc_attr( $value ) . '"' . $checked . $onclick . ' />'; |
| 104 | } else { |
| 105 | $item = '<input type="' . $input_type . '" name="' . $name . ( $multiple ? '[]' : '' ) . '" value="' . esc_attr( $value ) . '"' . $checked . $onclick . ' />'; |
| 106 | $item .= ' <span class="wpcf7-list-item-label">' . esc_html( $label ) . '</span>'; |
| 107 | } |
| 108 | |
| 109 | if ( $use_label_element ) |
| 110 | $item = '<label>' . $item . '</label>'; |
| 111 | |
| 112 | $item = '<span class="wpcf7-list-item">' . $item . '</span>'; |
| 113 | $html .= $item; |
| 114 | } |
| 115 | |
| 116 | $html = '<span' . $atts . '>' . $html . '</span>'; |
| 117 | |
| 118 | $validation_error = ''; |
| 119 | if ( is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) ) |
| 120 | $validation_error = $wpcf7_contact_form->validation_error( $name ); |
| 121 | |
| 122 | $html = '<span class="wpcf7-form-control-wrap ' . $name . '">' . $html . $validation_error . '</span>'; |
| 123 | |
| 124 | return $html; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /* Validation filter */ |
| 129 | |
| 130 | add_filter( 'wpcf7_validate_checkbox', 'wpcf7_checkbox_validation_filter', 10, 2 ); |
| 131 | add_filter( 'wpcf7_validate_checkbox*', 'wpcf7_checkbox_validation_filter', 10, 2 ); |
| 132 | add_filter( 'wpcf7_validate_radio', 'wpcf7_checkbox_validation_filter', 10, 2 ); |
| 133 | |
| 134 | function wpcf7_checkbox_validation_filter( $result, $tag ) { |
| 135 | global $wpcf7_contact_form; |
| 136 | |
| 137 | $type = $tag['type']; |
| 138 | $name = $tag['name']; |
| 139 | $values = $tag['values']; |
| 140 | |
| 141 | if ( is_array( $_POST[$name] ) ) { |
| 142 | foreach ( $_POST[$name] as $key => $value ) { |
| 143 | $value = stripslashes( $value ); |
| 144 | if ( ! in_array( $value, (array) $values ) ) // Not in given choices. |
| 145 | unset( $_POST[$name][$key] ); |
| 146 | } |
| 147 | } else { |
| 148 | $value = stripslashes( $_POST[$name] ); |
| 149 | if ( ! in_array( $value, (array) $values ) ) // Not in given choices. |
| 150 | $_POST[$name] = ''; |
| 151 | } |
| 152 | |
| 153 | if ( 'checkbox*' == $type ) { |
| 154 | if ( empty( $_POST[$name] ) ) { |
| 155 | $result['valid'] = false; |
| 156 | $result['reason'][$name] = $wpcf7_contact_form->message( 'invalid_required' ); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return $result; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | /* Tag generator */ |
| 165 | |
| 166 | add_action( 'admin_init', 'wpcf7_add_tag_generator_checkbox_and_radio', 30 ); |
| 167 | |
| 168 | function wpcf7_add_tag_generator_checkbox_and_radio() { |
| 169 | wpcf7_add_tag_generator( 'checkbox', __( 'Checkboxes', 'wpcf7' ), |
| 170 | 'wpcf7-tg-pane-checkbox', 'wpcf7_tg_pane_checkbox' ); |
| 171 | |
| 172 | wpcf7_add_tag_generator( 'radio', __( 'Radio buttons', 'wpcf7' ), |
| 173 | 'wpcf7-tg-pane-radio', 'wpcf7_tg_pane_radio' ); |
| 174 | } |
| 175 | |
| 176 | function wpcf7_tg_pane_checkbox( &$contact_form ) { |
| 177 | wpcf7_tg_pane_checkbox_and_radio( 'checkbox' ); |
| 178 | } |
| 179 | |
| 180 | function wpcf7_tg_pane_radio( &$contact_form ) { |
| 181 | wpcf7_tg_pane_checkbox_and_radio( 'radio' ); |
| 182 | } |
| 183 | |
| 184 | function wpcf7_tg_pane_checkbox_and_radio( $type = 'checkbox' ) { |
| 185 | if ( 'radio' != $type ) |
| 186 | $type = 'checkbox'; |
| 187 | |
| 188 | ?> |
| 189 | <div id="wpcf7-tg-pane-<?php echo $type; ?>" class="hidden"> |
| 190 | <form action=""> |
| 191 | <table> |
| 192 | <?php if ( 'checkbox' == $type ) : ?> |
| 193 | <tr><td><input type="checkbox" name="required" /> <?php echo esc_html( __( 'Required field?', 'wpcf7' ) ); ?></td></tr> |
| 194 | <?php endif; ?> |
| 195 | |
| 196 | <tr><td><?php echo esc_html( __( 'Name', 'wpcf7' ) ); ?><br /><input type="text" name="name" class="tg-name oneline" /></td><td></td></tr> |
| 197 | </table> |
| 198 | |
| 199 | <table> |
| 200 | <tr> |
| 201 | <td><code>id</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br /> |
| 202 | <input type="text" name="id" class="idvalue oneline option" /></td> |
| 203 | |
| 204 | <td><code>class</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br /> |
| 205 | <input type="text" name="class" class="classvalue oneline option" /></td> |
| 206 | </tr> |
| 207 | |
| 208 | <tr> |
| 209 | <td><?php echo esc_html( __( 'Choices', 'wpcf7' ) ); ?><br /> |
| 210 | <textarea name="values"></textarea><br /> |
| 211 | <span style="font-size: smaller"><?php echo esc_html( __( "* One choice per line.", 'wpcf7' ) ); ?></span> |
| 212 | </td> |
| 213 | |
| 214 | <td> |
| 215 | <br /><input type="checkbox" name="label_first" class="option" /> <?php echo esc_html( __( 'Put a label first, a checkbox last?', 'wpcf7' ) ); ?> |
| 216 | <br /><input type="checkbox" name="use_label_element" class="option" /> <?php echo esc_html( __( 'Wrap each item with <label> tag?', 'wpcf7' ) ); ?> |
| 217 | <?php if ( 'checkbox' == $type ) : ?> |
| 218 | <br /><input type="checkbox" name="exclusive" class="option" /> <?php echo esc_html( __( 'Make checkboxes exclusive?', 'wpcf7' ) ); ?> |
| 219 | <?php endif; ?> |
| 220 | </td> |
| 221 | </tr> |
| 222 | </table> |
| 223 | |
| 224 | <div class="tg-tag"><?php echo esc_html( __( "Copy this code and paste it into the form left.", 'wpcf7' ) ); ?><br /><input type="text" name="<?php echo $type; ?>" class="tag" readonly="readonly" onfocus="this.select()" /></div> |
| 225 | |
| 226 | <div class="tg-mail-tag"><?php echo esc_html( __( "And, put this code into the Mail fields below.", 'wpcf7' ) ); ?><br /><span class="arrow">⬇</span> <input type="text" class="mail-tag" readonly="readonly" onfocus="this.select()" /></div> |
| 227 | </form> |
| 228 | </div> |
| 229 | <?php |
| 230 | } |
| 231 | |
| 232 | ?> |