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