acceptance.php
13 years ago
akismet.php
13 years ago
captcha.php
13 years ago
checkbox.php
13 years ago
date.php
13 years ago
file.php
13 years ago
flamingo.php
13 years ago
jetpack.php
13 years ago
number.php
13 years ago
quiz.php
13 years ago
response.php
15 years ago
select.php
13 years ago
special-mail-tags.php
13 years ago
submit.php
13 years ago
text.php
13 years ago
textarea.php
13 years ago
checkbox.php
218 lines
| 1 | <?php |
| 2 | /** |
| 3 | ** A base module for [checkbox], [checkbox*], and [radio] |
| 4 | **/ |
| 5 | |
| 6 | /* Shortcode handler */ |
| 7 | |
| 8 | add_action( 'init', 'wpcf7_add_shortcode_checkbox', 5 ); |
| 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 | $multiple = false; |
| 32 | |
| 33 | if ( 'checkbox' == $tag->basetype ) |
| 34 | $multiple = ! $exclusive; |
| 35 | else // radio |
| 36 | $exclusive = false; |
| 37 | |
| 38 | if ( $exclusive ) |
| 39 | $class .= ' wpcf7-exclusive-checkbox'; |
| 40 | |
| 41 | $atts = array(); |
| 42 | |
| 43 | $atts['class'] = $tag->get_class_option( $class ); |
| 44 | $atts['id'] = $tag->get_option( 'id', 'id', true ); |
| 45 | |
| 46 | $tabindex = $tag->get_option( 'tabindex', 'int', true ); |
| 47 | |
| 48 | if ( false !== $tabindex ) |
| 49 | $tabindex = absint( $tabindex ); |
| 50 | |
| 51 | $defaults = array(); |
| 52 | |
| 53 | if ( $matches = $tag->get_first_match_option( '/^default:([0-9_]+)$/' ) ) |
| 54 | $defaults = explode( '_', $matches[1] ); |
| 55 | |
| 56 | if ( isset( $_POST[$tag->name] ) ) |
| 57 | $post = $_POST[$tag->name]; |
| 58 | else |
| 59 | $post = $multiple ? array() : ''; |
| 60 | |
| 61 | $is_posted = wpcf7_is_posted(); |
| 62 | |
| 63 | $html = ''; |
| 64 | |
| 65 | foreach ( (array) $tag->values as $key => $value ) { |
| 66 | $checked = false; |
| 67 | |
| 68 | if ( $is_posted && ! empty( $post ) ) { |
| 69 | if ( $multiple && in_array( esc_sql( $value ), (array) $post ) ) |
| 70 | $checked = true; |
| 71 | if ( ! $multiple && $post == esc_sql( $value ) ) |
| 72 | $checked = true; |
| 73 | } else { |
| 74 | if ( in_array( $key + 1, (array) $defaults ) ) |
| 75 | $checked = true; |
| 76 | } |
| 77 | |
| 78 | if ( isset( $tag->labels[$key] ) ) |
| 79 | $label = $tag->labels[$key]; |
| 80 | else |
| 81 | $label = $value; |
| 82 | |
| 83 | $item_atts = array( |
| 84 | 'type' => $tag->basetype, |
| 85 | 'name' => $tag->name . ( $multiple ? '[]' : '' ), |
| 86 | 'value' => $value, |
| 87 | 'checked' => $checked ? 'checked' : '', |
| 88 | 'tabindex' => $tabindex ? $tabindex : '' ); |
| 89 | |
| 90 | $item_atts = wpcf7_format_atts( $item_atts ); |
| 91 | |
| 92 | if ( $label_first ) { // put label first, input last |
| 93 | $item = sprintf( |
| 94 | '<span class="wpcf7-list-item-label">%1$s</span> <input %2$s />', |
| 95 | esc_html( $label ), $item_atts ); |
| 96 | } else { |
| 97 | $item = sprintf( |
| 98 | '<input %2$s /> <span class="wpcf7-list-item-label">%1$s</span>', |
| 99 | esc_html( $label ), $item_atts ); |
| 100 | } |
| 101 | |
| 102 | if ( $use_label_element ) |
| 103 | $item = '<label>' . $item . '</label>'; |
| 104 | |
| 105 | $item = '<span class="wpcf7-list-item">' . $item . '</span>'; |
| 106 | $html .= $item; |
| 107 | |
| 108 | if ( false !== $tabindex ) |
| 109 | $tabindex += 1; |
| 110 | } |
| 111 | |
| 112 | $atts = wpcf7_format_atts( $atts ); |
| 113 | |
| 114 | $html = sprintf( |
| 115 | '<span class="wpcf7-form-control-wrap %1$s"><span %2$s>%3$s</span>%4$s</span>', |
| 116 | $tag->name, $atts, $html, $validation_error ); |
| 117 | |
| 118 | return $html; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | /* Validation filter */ |
| 123 | |
| 124 | add_filter( 'wpcf7_validate_checkbox', 'wpcf7_checkbox_validation_filter', 10, 2 ); |
| 125 | add_filter( 'wpcf7_validate_checkbox*', 'wpcf7_checkbox_validation_filter', 10, 2 ); |
| 126 | add_filter( 'wpcf7_validate_radio', 'wpcf7_checkbox_validation_filter', 10, 2 ); |
| 127 | |
| 128 | function wpcf7_checkbox_validation_filter( $result, $tag ) { |
| 129 | $tag = new WPCF7_Shortcode( $tag ); |
| 130 | |
| 131 | $type = $tag->type; |
| 132 | $name = $tag->name; |
| 133 | |
| 134 | $value = isset( $_POST[$name] ) ? (array) $_POST[$name] : array(); |
| 135 | |
| 136 | if ( 'checkbox*' == $type ) { |
| 137 | if ( empty( $value ) ) { |
| 138 | $result['valid'] = false; |
| 139 | $result['reason'][$name] = wpcf7_get_message( 'invalid_required' ); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return $result; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /* Tag generator */ |
| 148 | |
| 149 | add_action( 'admin_init', 'wpcf7_add_tag_generator_checkbox_and_radio', 30 ); |
| 150 | |
| 151 | function wpcf7_add_tag_generator_checkbox_and_radio() { |
| 152 | if ( ! function_exists( 'wpcf7_add_tag_generator' ) ) |
| 153 | return; |
| 154 | |
| 155 | wpcf7_add_tag_generator( 'checkbox', __( 'Checkboxes', 'wpcf7' ), |
| 156 | 'wpcf7-tg-pane-checkbox', 'wpcf7_tg_pane_checkbox' ); |
| 157 | |
| 158 | wpcf7_add_tag_generator( 'radio', __( 'Radio buttons', 'wpcf7' ), |
| 159 | 'wpcf7-tg-pane-radio', 'wpcf7_tg_pane_radio' ); |
| 160 | } |
| 161 | |
| 162 | function wpcf7_tg_pane_checkbox( &$contact_form ) { |
| 163 | wpcf7_tg_pane_checkbox_and_radio( 'checkbox' ); |
| 164 | } |
| 165 | |
| 166 | function wpcf7_tg_pane_radio( &$contact_form ) { |
| 167 | wpcf7_tg_pane_checkbox_and_radio( 'radio' ); |
| 168 | } |
| 169 | |
| 170 | function wpcf7_tg_pane_checkbox_and_radio( $type = 'checkbox' ) { |
| 171 | if ( 'radio' != $type ) |
| 172 | $type = 'checkbox'; |
| 173 | |
| 174 | ?> |
| 175 | <div id="wpcf7-tg-pane-<?php echo $type; ?>" class="hidden"> |
| 176 | <form action=""> |
| 177 | <table> |
| 178 | <?php if ( 'checkbox' == $type ) : ?> |
| 179 | <tr><td><input type="checkbox" name="required" /> <?php echo esc_html( __( 'Required field?', 'wpcf7' ) ); ?></td></tr> |
| 180 | <?php endif; ?> |
| 181 | |
| 182 | <tr><td><?php echo esc_html( __( 'Name', 'wpcf7' ) ); ?><br /><input type="text" name="name" class="tg-name oneline" /></td><td></td></tr> |
| 183 | </table> |
| 184 | |
| 185 | <table> |
| 186 | <tr> |
| 187 | <td><code>id</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br /> |
| 188 | <input type="text" name="id" class="idvalue oneline option" /></td> |
| 189 | |
| 190 | <td><code>class</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br /> |
| 191 | <input type="text" name="class" class="classvalue oneline option" /></td> |
| 192 | </tr> |
| 193 | |
| 194 | <tr> |
| 195 | <td><?php echo esc_html( __( 'Choices', 'wpcf7' ) ); ?><br /> |
| 196 | <textarea name="values"></textarea><br /> |
| 197 | <span style="font-size: smaller"><?php echo esc_html( __( "* One choice per line.", 'wpcf7' ) ); ?></span> |
| 198 | </td> |
| 199 | |
| 200 | <td> |
| 201 | <br /><input type="checkbox" name="label_first" class="option" /> <?php echo esc_html( __( 'Put a label first, a checkbox last?', 'wpcf7' ) ); ?> |
| 202 | <br /><input type="checkbox" name="use_label_element" class="option" /> <?php echo esc_html( __( 'Wrap each item with <label> tag?', 'wpcf7' ) ); ?> |
| 203 | <?php if ( 'checkbox' == $type ) : ?> |
| 204 | <br /><input type="checkbox" name="exclusive" class="option" /> <?php echo esc_html( __( 'Make checkboxes exclusive?', 'wpcf7' ) ); ?> |
| 205 | <?php endif; ?> |
| 206 | </td> |
| 207 | </tr> |
| 208 | </table> |
| 209 | |
| 210 | <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> |
| 211 | |
| 212 | <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> |
| 213 | </form> |
| 214 | </div> |
| 215 | <?php |
| 216 | } |
| 217 | |
| 218 | ?> |