akismet
11 months ago
constant-contact
9 months ago
recaptcha
9 months ago
sendinblue
9 months ago
stripe
9 months ago
turnstile
9 months ago
acceptance.php
11 months ago
checkbox.php
9 months ago
count.php
1 year ago
date.php
9 months ago
disallowed-list.php
9 months ago
doi-helper.php
4 years ago
file.php
11 months ago
flamingo.php
9 months ago
hidden.php
7 years ago
listo.php
2 years ago
number.php
9 months ago
quiz.php
11 months ago
really-simple-captcha.php
9 months ago
reflection.php
3 years ago
response.php
6 years ago
select.php
9 months ago
submit.php
11 months ago
text.php
9 months ago
textarea.php
11 months ago
select.php
306 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 | $atts['autocomplete'] = $tag->get_option( |
| 40 | 'autocomplete', '[-0-9a-zA-Z]+', true |
| 41 | ); |
| 42 | |
| 43 | if ( $tag->is_required() ) { |
| 44 | $atts['aria-required'] = 'true'; |
| 45 | } |
| 46 | |
| 47 | if ( $validation_error ) { |
| 48 | $atts['aria-invalid'] = 'true'; |
| 49 | $atts['aria-describedby'] = wpcf7_get_validation_error_reference( |
| 50 | $tag->name |
| 51 | ); |
| 52 | } else { |
| 53 | $atts['aria-invalid'] = 'false'; |
| 54 | } |
| 55 | |
| 56 | $multiple = $tag->has_option( 'multiple' ); |
| 57 | $include_blank = $tag->has_option( 'include_blank' ); |
| 58 | $first_as_label = $tag->has_option( 'first_as_label' ); |
| 59 | |
| 60 | if ( $tag->has_option( 'size' ) ) { |
| 61 | $size = $tag->get_option( 'size', 'int', true ); |
| 62 | |
| 63 | if ( $size ) { |
| 64 | $atts['size'] = $size; |
| 65 | } elseif ( $multiple ) { |
| 66 | $atts['size'] = 4; |
| 67 | } else { |
| 68 | $atts['size'] = 1; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | if ( $data = (array) $tag->get_data_option() ) { |
| 73 | $tag->values = array_merge( $tag->values, array_values( $data ) ); |
| 74 | $tag->labels = array_merge( $tag->labels, array_values( $data ) ); |
| 75 | } |
| 76 | |
| 77 | $values = $tag->values; |
| 78 | $labels = $tag->labels; |
| 79 | |
| 80 | $default_choice = $tag->get_default_option( null, array( |
| 81 | 'multiple' => $multiple, |
| 82 | ) ); |
| 83 | |
| 84 | if ( $include_blank |
| 85 | or empty( $values ) ) { |
| 86 | array_unshift( |
| 87 | $labels, |
| 88 | __( '—Please choose an option—', 'contact-form-7' ) |
| 89 | ); |
| 90 | array_unshift( $values, '' ); |
| 91 | } elseif ( $first_as_label ) { |
| 92 | $values[0] = ''; |
| 93 | } |
| 94 | |
| 95 | $html = ''; |
| 96 | $hangover = wpcf7_get_hangover( $tag->name ); |
| 97 | |
| 98 | foreach ( $values as $key => $value ) { |
| 99 | if ( $hangover ) { |
| 100 | $selected = in_array( $value, (array) $hangover, true ); |
| 101 | } else { |
| 102 | $selected = in_array( $value, (array) $default_choice, true ); |
| 103 | } |
| 104 | |
| 105 | $item_atts = array( |
| 106 | 'value' => $value, |
| 107 | 'selected' => $selected, |
| 108 | ); |
| 109 | |
| 110 | $label = isset( $labels[$key] ) ? $labels[$key] : $value; |
| 111 | |
| 112 | $html .= sprintf( |
| 113 | '<option %1$s>%2$s</option>', |
| 114 | wpcf7_format_atts( $item_atts ), |
| 115 | esc_html( $label ) |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | $atts['multiple'] = (bool) $multiple; |
| 120 | $atts['name'] = $tag->name . ( $multiple ? '[]' : '' ); |
| 121 | |
| 122 | $html = sprintf( |
| 123 | '<span class="wpcf7-form-control-wrap" data-name="%1$s"><select %2$s>%3$s</select>%4$s</span>', |
| 124 | esc_attr( $tag->name ), |
| 125 | wpcf7_format_atts( $atts ), |
| 126 | $html, |
| 127 | $validation_error |
| 128 | ); |
| 129 | |
| 130 | return $html; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | add_action( |
| 135 | 'wpcf7_swv_create_schema', |
| 136 | 'wpcf7_swv_add_select_rules', |
| 137 | 10, 2 |
| 138 | ); |
| 139 | |
| 140 | function wpcf7_swv_add_select_rules( $schema, $contact_form ) { |
| 141 | $tags = $contact_form->scan_form_tags( array( |
| 142 | 'type' => array( 'select*' ), |
| 143 | ) ); |
| 144 | |
| 145 | foreach ( $tags as $tag ) { |
| 146 | $schema->add_rule( |
| 147 | wpcf7_swv_create_rule( 'required', array( |
| 148 | 'field' => $tag->name, |
| 149 | 'error' => wpcf7_get_message( 'invalid_required' ), |
| 150 | ) ) |
| 151 | ); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | |
| 156 | add_action( |
| 157 | 'wpcf7_swv_create_schema', |
| 158 | 'wpcf7_swv_add_select_enum_rules', |
| 159 | 20, 2 |
| 160 | ); |
| 161 | |
| 162 | function wpcf7_swv_add_select_enum_rules( $schema, $contact_form ) { |
| 163 | $tags = $contact_form->scan_form_tags( array( |
| 164 | 'basetype' => array( 'select' ), |
| 165 | ) ); |
| 166 | |
| 167 | $values = array_reduce( |
| 168 | $tags, |
| 169 | function ( $values, $tag ) { |
| 170 | if ( ! isset( $values[$tag->name] ) ) { |
| 171 | $values[$tag->name] = array(); |
| 172 | } |
| 173 | |
| 174 | $tag_values = array_merge( |
| 175 | (array) $tag->values, |
| 176 | (array) $tag->get_data_option() |
| 177 | ); |
| 178 | |
| 179 | if ( $tag->has_option( 'first_as_label' ) ) { |
| 180 | $tag_values = array_slice( $tag_values, 1 ); |
| 181 | } |
| 182 | |
| 183 | $values[$tag->name] = array_merge( |
| 184 | $values[$tag->name], |
| 185 | $tag_values |
| 186 | ); |
| 187 | |
| 188 | return $values; |
| 189 | }, |
| 190 | array() |
| 191 | ); |
| 192 | |
| 193 | foreach ( $values as $field => $field_values ) { |
| 194 | $field_values = array_map( |
| 195 | static function ( $value ) { |
| 196 | return html_entity_decode( |
| 197 | (string) $value, |
| 198 | ENT_QUOTES | ENT_HTML5, |
| 199 | 'UTF-8' |
| 200 | ); |
| 201 | }, |
| 202 | $field_values |
| 203 | ); |
| 204 | |
| 205 | $field_values = array_filter( |
| 206 | array_unique( $field_values ), |
| 207 | static function ( $value ) { |
| 208 | return '' !== $value; |
| 209 | } |
| 210 | ); |
| 211 | |
| 212 | $schema->add_rule( |
| 213 | wpcf7_swv_create_rule( 'enum', array( |
| 214 | 'field' => $field, |
| 215 | 'accept' => array_values( $field_values ), |
| 216 | 'error' => $contact_form->filter_message( |
| 217 | __( 'Undefined value was submitted through this field.', 'contact-form-7' ) |
| 218 | ), |
| 219 | ) ) |
| 220 | ); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
| 225 | /* Tag generator */ |
| 226 | |
| 227 | add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_menu', 25, 0 ); |
| 228 | |
| 229 | function wpcf7_add_tag_generator_menu() { |
| 230 | $tag_generator = WPCF7_TagGenerator::get_instance(); |
| 231 | |
| 232 | $tag_generator->add( 'menu', __( 'drop-down menu', 'contact-form-7' ), |
| 233 | 'wpcf7_tag_generator_menu', |
| 234 | array( 'version' => '2' ) |
| 235 | ); |
| 236 | } |
| 237 | |
| 238 | function wpcf7_tag_generator_menu( $contact_form, $options ) { |
| 239 | $field_types = array( |
| 240 | 'select' => array( |
| 241 | 'display_name' => __( 'Drop-down menu', 'contact-form-7' ), |
| 242 | 'heading' => __( 'Drop-down menu form-tag generator', 'contact-form-7' ), |
| 243 | 'description' => __( 'Generates a form-tag for a <a href="https://contactform7.com/checkboxes-radio-buttons-and-menus/">drop-down menu</a>.', 'contact-form-7' ), |
| 244 | ), |
| 245 | ); |
| 246 | |
| 247 | $tgg = new WPCF7_TagGeneratorGenerator( $options['content'] ); |
| 248 | |
| 249 | $formatter = new WPCF7_HTMLFormatter(); |
| 250 | |
| 251 | $formatter->append_start_tag( 'header', array( |
| 252 | 'class' => 'description-box', |
| 253 | ) ); |
| 254 | |
| 255 | $formatter->append_start_tag( 'h3' ); |
| 256 | |
| 257 | $formatter->append_preformatted( |
| 258 | esc_html( $field_types['select']['heading'] ) |
| 259 | ); |
| 260 | |
| 261 | $formatter->end_tag( 'h3' ); |
| 262 | |
| 263 | $formatter->append_start_tag( 'p' ); |
| 264 | |
| 265 | $formatter->append_preformatted( |
| 266 | wp_kses_data( $field_types['select']['description'] ) |
| 267 | ); |
| 268 | |
| 269 | $formatter->end_tag( 'header' ); |
| 270 | |
| 271 | $formatter->append_start_tag( 'div', array( |
| 272 | 'class' => 'control-box', |
| 273 | ) ); |
| 274 | |
| 275 | $formatter->call_user_func( static function () use ( $tgg, $field_types ) { |
| 276 | $tgg->print( 'field_type', array( |
| 277 | 'with_required' => true, |
| 278 | 'select_options' => array( |
| 279 | 'select' => $field_types['select']['display_name'], |
| 280 | ), |
| 281 | ) ); |
| 282 | |
| 283 | $tgg->print( 'field_name' ); |
| 284 | |
| 285 | $tgg->print( 'class_attr' ); |
| 286 | |
| 287 | $tgg->print( 'selectable_values', array( |
| 288 | 'first_as_label' => true, |
| 289 | ) ); |
| 290 | } ); |
| 291 | |
| 292 | $formatter->end_tag( 'div' ); |
| 293 | |
| 294 | $formatter->append_start_tag( 'footer', array( |
| 295 | 'class' => 'insert-box', |
| 296 | ) ); |
| 297 | |
| 298 | $formatter->call_user_func( static function () use ( $tgg, $field_types ) { |
| 299 | $tgg->print( 'insert_box_content' ); |
| 300 | |
| 301 | $tgg->print( 'mail_tag_tip' ); |
| 302 | } ); |
| 303 | |
| 304 | $formatter->print(); |
| 305 | } |
| 306 |