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