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
select.php
140 lines
| 1 | <?php |
| 2 | /** |
| 3 | ** A base module for [select] and [select*] |
| 4 | **/ |
| 5 | |
| 6 | /* Shortcode handler */ |
| 7 | |
| 8 | function wpcf7_select_shortcode_handler( $tag ) { |
| 9 | global $wpcf7_contact_form; |
| 10 | |
| 11 | if ( ! is_array( $tag ) ) |
| 12 | return ''; |
| 13 | |
| 14 | $type = $tag['type']; |
| 15 | $name = $tag['name']; |
| 16 | $options = (array) $tag['options']; |
| 17 | $values = (array) $tag['values']; |
| 18 | $labels = (array) $tag['labels']; |
| 19 | |
| 20 | if ( empty( $name ) ) |
| 21 | return ''; |
| 22 | |
| 23 | $atts = ''; |
| 24 | $id_att = ''; |
| 25 | $class_att = ''; |
| 26 | |
| 27 | $defaults = array(); |
| 28 | |
| 29 | if ( 'select*' == $type ) |
| 30 | $class_att .= ' wpcf7-validates-as-required'; |
| 31 | |
| 32 | foreach ( $options as $option ) { |
| 33 | if ( preg_match( '%^id:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) { |
| 34 | $id_att = $matches[1]; |
| 35 | |
| 36 | } elseif ( preg_match( '%^class:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) { |
| 37 | $class_att .= ' ' . $matches[1]; |
| 38 | |
| 39 | } elseif ( preg_match( '/^default:([0-9_]+)$/', $option, $matches ) ) { |
| 40 | $defaults = explode( '_', $matches[1] ); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | if ( $id_att ) |
| 45 | $atts .= ' id="' . trim( $id_att ) . '"'; |
| 46 | |
| 47 | if ( $class_att ) |
| 48 | $atts .= ' class="' . trim( $class_att ) . '"'; |
| 49 | |
| 50 | $multiple = (bool) preg_grep( '%^multiple$%', $options ); |
| 51 | $include_blank = (bool) preg_grep( '%^include_blank$%', $options ); |
| 52 | |
| 53 | $empty_select = empty( $values ); |
| 54 | if ( $empty_select || $include_blank ) { |
| 55 | array_unshift( $labels, '---' ); |
| 56 | array_unshift( $values, '---' ); |
| 57 | } |
| 58 | |
| 59 | $html = ''; |
| 60 | |
| 61 | $posted = is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) && $wpcf7_contact_form->is_posted(); |
| 62 | |
| 63 | foreach ( $values as $key => $value ) { |
| 64 | $selected = false; |
| 65 | |
| 66 | if ( ! $empty_select && in_array( $key + 1, (array) $defaults ) ) |
| 67 | $selected = true; |
| 68 | |
| 69 | if ( $posted ) { |
| 70 | if ( $multiple && in_array( esc_sql( $value ), (array) $_POST[$name] ) ) |
| 71 | $selected = true; |
| 72 | if ( ! $multiple && $_POST[$name] == esc_sql( $value ) ) |
| 73 | $selected = true; |
| 74 | } |
| 75 | |
| 76 | $selected = $selected ? ' selected="selected"' : ''; |
| 77 | |
| 78 | if ( isset( $labels[$key] ) ) |
| 79 | $label = $labels[$key]; |
| 80 | else |
| 81 | $label = $value; |
| 82 | |
| 83 | $html .= '<option value="' . esc_attr( $value ) . '"' . $selected . '>' . esc_html( $label ) . '</option>'; |
| 84 | } |
| 85 | |
| 86 | if ( $multiple ) |
| 87 | $atts .= ' multiple="multiple"'; |
| 88 | |
| 89 | $html = '<select name="' . $name . ( $multiple ? '[]' : '' ) . '"' . $atts . '>' . $html . '</select>'; |
| 90 | |
| 91 | $validation_error = ''; |
| 92 | if ( is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) ) |
| 93 | $validation_error = $wpcf7_contact_form->validation_error( $name ); |
| 94 | |
| 95 | $html = '<span class="wpcf7-form-control-wrap ' . $name . '">' . $html . $validation_error . '</span>'; |
| 96 | |
| 97 | return $html; |
| 98 | } |
| 99 | |
| 100 | wpcf7_add_shortcode( 'select', 'wpcf7_select_shortcode_handler', true ); |
| 101 | wpcf7_add_shortcode( 'select*', 'wpcf7_select_shortcode_handler', true ); |
| 102 | |
| 103 | |
| 104 | /* Validation filter */ |
| 105 | |
| 106 | function wpcf7_select_validation_filter( $result, $tag ) { |
| 107 | global $wpcf7_contact_form; |
| 108 | |
| 109 | $type = $tag['type']; |
| 110 | $name = $tag['name']; |
| 111 | $values = $tag['values']; |
| 112 | |
| 113 | if ( is_array( $_POST[$name] ) ) { |
| 114 | foreach ( $_POST[$name] as $key => $value ) { |
| 115 | $value = stripslashes( $value ); |
| 116 | if ( ! in_array( $value, (array) $values ) ) // Not in given choices. |
| 117 | unset( $_POST[$name][$key] ); |
| 118 | } |
| 119 | } else { |
| 120 | $value = stripslashes( $_POST[$name] ); |
| 121 | if ( ! in_array( $value, (array) $values ) ) // Not in given choices. |
| 122 | $_POST[$name] = ''; |
| 123 | } |
| 124 | |
| 125 | if ( 'select*' == $type ) { |
| 126 | if ( empty( $_POST[$name] ) || |
| 127 | ! is_array( $_POST[$name] ) && '---' == $_POST[$name] || |
| 128 | is_array( $_POST[$name] ) && 1 == count( $_POST[$name] ) && '---' == $_POST[$name][0] ) { |
| 129 | $result['valid'] = false; |
| 130 | $result['reason'][$name] = $wpcf7_contact_form->message( 'invalid_required' ); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return $result; |
| 135 | } |
| 136 | |
| 137 | add_filter( 'wpcf7_validate_select', 'wpcf7_select_validation_filter', 10, 2 ); |
| 138 | add_filter( 'wpcf7_validate_select*', 'wpcf7_select_validation_filter', 10, 2 ); |
| 139 | |
| 140 | ?> |