constant-contact
4 years ago
recaptcha
4 years ago
sendinblue
4 years ago
stripe
4 years ago
acceptance.php
5 years ago
akismet.php
4 years ago
checkbox.php
4 years ago
count.php
7 years ago
date.php
5 years ago
disallowed-list.php
4 years ago
doi-helper.php
4 years ago
file.php
5 years ago
flamingo.php
4 years ago
hidden.php
7 years ago
listo.php
9 years ago
number.php
5 years ago
quiz.php
4 years ago
really-simple-captcha.php
4 years ago
response.php
6 years ago
select.php
4 years ago
submit.php
4 years ago
text.php
4 years ago
textarea.php
4 years ago
select.php
237 lines
| 1 | <?php |
| 2 | /** |
| 3 | ** A base module for [select] and [select*] |
| 4 | **/ |
| 5 | |
| 6 | /* form_tag handler */ |
| 7 | |
| 8 | add_action( 'wpcf7_init', 'wpcf7_add_form_tag_select', 10, 0 ); |
| 9 | |
| 10 | function wpcf7_add_form_tag_select() { |
| 11 | wpcf7_add_form_tag( array( 'select', 'select*' ), |
| 12 | 'wpcf7_select_form_tag_handler', |
| 13 | array( |
| 14 | 'name-attr' => true, |
| 15 | 'selectable-values' => true, |
| 16 | ) |
| 17 | ); |
| 18 | } |
| 19 | |
| 20 | function wpcf7_select_form_tag_handler( $tag ) { |
| 21 | if ( empty( $tag->name ) ) { |
| 22 | return ''; |
| 23 | } |
| 24 | |
| 25 | $validation_error = wpcf7_get_validation_error( $tag->name ); |
| 26 | |
| 27 | $class = wpcf7_form_controls_class( $tag->type ); |
| 28 | |
| 29 | if ( $validation_error ) { |
| 30 | $class .= ' wpcf7-not-valid'; |
| 31 | } |
| 32 | |
| 33 | $atts = array(); |
| 34 | |
| 35 | $atts['class'] = $tag->get_class_option( $class ); |
| 36 | $atts['id'] = $tag->get_id_option(); |
| 37 | $atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true ); |
| 38 | |
| 39 | if ( $tag->is_required() ) { |
| 40 | $atts['aria-required'] = 'true'; |
| 41 | } |
| 42 | |
| 43 | if ( $validation_error ) { |
| 44 | $atts['aria-invalid'] = 'true'; |
| 45 | $atts['aria-describedby'] = wpcf7_get_validation_error_reference( |
| 46 | $tag->name |
| 47 | ); |
| 48 | } else { |
| 49 | $atts['aria-invalid'] = 'false'; |
| 50 | } |
| 51 | |
| 52 | $multiple = $tag->has_option( 'multiple' ); |
| 53 | $include_blank = $tag->has_option( 'include_blank' ); |
| 54 | $first_as_label = $tag->has_option( 'first_as_label' ); |
| 55 | |
| 56 | if ( $tag->has_option( 'size' ) ) { |
| 57 | $size = $tag->get_option( 'size', 'int', true ); |
| 58 | |
| 59 | if ( $size ) { |
| 60 | $atts['size'] = $size; |
| 61 | } elseif ( $multiple ) { |
| 62 | $atts['size'] = 4; |
| 63 | } else { |
| 64 | $atts['size'] = 1; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if ( $data = (array) $tag->get_data_option() ) { |
| 69 | $tag->values = array_merge( $tag->values, array_values( $data ) ); |
| 70 | $tag->labels = array_merge( $tag->labels, array_values( $data ) ); |
| 71 | } |
| 72 | |
| 73 | $values = $tag->values; |
| 74 | $labels = $tag->labels; |
| 75 | |
| 76 | $default_choice = $tag->get_default_option( null, array( |
| 77 | 'multiple' => $multiple, |
| 78 | ) ); |
| 79 | |
| 80 | if ( $include_blank |
| 81 | or empty( $values ) ) { |
| 82 | array_unshift( $labels, '---' ); |
| 83 | array_unshift( $values, '' ); |
| 84 | } elseif ( $first_as_label ) { |
| 85 | $values[0] = ''; |
| 86 | } |
| 87 | |
| 88 | $html = ''; |
| 89 | $hangover = wpcf7_get_hangover( $tag->name ); |
| 90 | |
| 91 | foreach ( $values as $key => $value ) { |
| 92 | if ( $hangover ) { |
| 93 | $selected = in_array( $value, (array) $hangover, true ); |
| 94 | } else { |
| 95 | $selected = in_array( $value, (array) $default_choice, true ); |
| 96 | } |
| 97 | |
| 98 | $item_atts = array( |
| 99 | 'value' => $value, |
| 100 | 'selected' => $selected ? 'selected' : '', |
| 101 | ); |
| 102 | |
| 103 | $item_atts = wpcf7_format_atts( $item_atts ); |
| 104 | |
| 105 | $label = isset( $labels[$key] ) ? $labels[$key] : $value; |
| 106 | |
| 107 | $html .= sprintf( |
| 108 | '<option %1$s>%2$s</option>', |
| 109 | $item_atts, |
| 110 | esc_html( $label ) |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | if ( $multiple ) { |
| 115 | $atts['multiple'] = 'multiple'; |
| 116 | } |
| 117 | |
| 118 | $atts['name'] = $tag->name . ( $multiple ? '[]' : '' ); |
| 119 | |
| 120 | $atts = wpcf7_format_atts( $atts ); |
| 121 | |
| 122 | $html = sprintf( |
| 123 | '<span class="wpcf7-form-control-wrap %1$s"><select %2$s>%3$s</select>%4$s</span>', |
| 124 | sanitize_html_class( $tag->name ), $atts, $html, $validation_error |
| 125 | ); |
| 126 | |
| 127 | return $html; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /* Validation filter */ |
| 132 | |
| 133 | add_filter( 'wpcf7_validate_select', 'wpcf7_select_validation_filter', 10, 2 ); |
| 134 | add_filter( 'wpcf7_validate_select*', 'wpcf7_select_validation_filter', 10, 2 ); |
| 135 | |
| 136 | function wpcf7_select_validation_filter( $result, $tag ) { |
| 137 | $name = $tag->name; |
| 138 | |
| 139 | $has_value = isset( $_POST[$name] ) && '' !== $_POST[$name]; |
| 140 | |
| 141 | if ( $has_value and $tag->has_option( 'multiple' ) ) { |
| 142 | $vals = array_filter( (array) $_POST[$name], function( $val ) { |
| 143 | return '' !== $val; |
| 144 | } ); |
| 145 | |
| 146 | $has_value = ! empty( $vals ); |
| 147 | } |
| 148 | |
| 149 | if ( $tag->is_required() and ! $has_value ) { |
| 150 | $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) ); |
| 151 | } |
| 152 | |
| 153 | return $result; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /* Tag generator */ |
| 158 | |
| 159 | add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_menu', 25, 0 ); |
| 160 | |
| 161 | function wpcf7_add_tag_generator_menu() { |
| 162 | $tag_generator = WPCF7_TagGenerator::get_instance(); |
| 163 | $tag_generator->add( 'menu', __( 'drop-down menu', 'contact-form-7' ), |
| 164 | 'wpcf7_tag_generator_menu' ); |
| 165 | } |
| 166 | |
| 167 | function wpcf7_tag_generator_menu( $contact_form, $args = '' ) { |
| 168 | $args = wp_parse_args( $args, array() ); |
| 169 | |
| 170 | $description = __( "Generate a form-tag for a drop-down menu. For more details, see %s.", 'contact-form-7' ); |
| 171 | |
| 172 | $desc_link = wpcf7_link( __( 'https://contactform7.com/checkboxes-radio-buttons-and-menus/', 'contact-form-7' ), __( 'Checkboxes, radio buttons and menus', 'contact-form-7' ) ); |
| 173 | |
| 174 | ?> |
| 175 | <div class="control-box"> |
| 176 | <fieldset> |
| 177 | <legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend> |
| 178 | |
| 179 | <table class="form-table"> |
| 180 | <tbody> |
| 181 | <tr> |
| 182 | <th scope="row"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></th> |
| 183 | <td> |
| 184 | <fieldset> |
| 185 | <legend class="screen-reader-text"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></legend> |
| 186 | <label><input type="checkbox" name="required" /> <?php echo esc_html( __( 'Required field', 'contact-form-7' ) ); ?></label> |
| 187 | </fieldset> |
| 188 | </td> |
| 189 | </tr> |
| 190 | |
| 191 | <tr> |
| 192 | <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th> |
| 193 | <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td> |
| 194 | </tr> |
| 195 | |
| 196 | <tr> |
| 197 | <th scope="row"><?php echo esc_html( __( 'Options', 'contact-form-7' ) ); ?></th> |
| 198 | <td> |
| 199 | <fieldset> |
| 200 | <legend class="screen-reader-text"><?php echo esc_html( __( 'Options', 'contact-form-7' ) ); ?></legend> |
| 201 | <textarea name="values" class="values" id="<?php echo esc_attr( $args['content'] . '-values' ); ?>"></textarea> |
| 202 | <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 /> |
| 203 | <label><input type="checkbox" name="multiple" class="option" /> <?php echo esc_html( __( 'Allow multiple selections', 'contact-form-7' ) ); ?></label><br /> |
| 204 | <label><input type="checkbox" name="include_blank" class="option" /> <?php echo esc_html( __( 'Insert a blank item as the first option', 'contact-form-7' ) ); ?></label> |
| 205 | </fieldset> |
| 206 | </td> |
| 207 | </tr> |
| 208 | |
| 209 | <tr> |
| 210 | <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th> |
| 211 | <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td> |
| 212 | </tr> |
| 213 | |
| 214 | <tr> |
| 215 | <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th> |
| 216 | <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td> |
| 217 | </tr> |
| 218 | |
| 219 | </tbody> |
| 220 | </table> |
| 221 | </fieldset> |
| 222 | </div> |
| 223 | |
| 224 | <div class="insert-box"> |
| 225 | <input type="text" name="select" class="tag code" readonly="readonly" onfocus="this.select()" /> |
| 226 | |
| 227 | <div class="submitbox"> |
| 228 | <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" /> |
| 229 | </div> |
| 230 | |
| 231 | <br class="clear" /> |
| 232 | |
| 233 | <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> |
| 234 | </div> |
| 235 | <?php |
| 236 | } |
| 237 |