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 3.9 3.9-beta 3.9.1 3.9.2 3.9.3 4.0 4.0.1 4.0.2 4.0.3 4.1 4.1-beta 4.1.1 4.1.2 4.2 4.2-beta 4.2.1 4.2.2 4.3 4.3.1 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6 4.6.1 4.7 4.8 4.8.1 4.9 4.9.1 4.9.2 5.0 5.0.1
contact-form-7 / modules / select.php
contact-form-7 / modules Last commit date
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 __( '&#8212;Please choose an option&#8212;', '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