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.1 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 / checkbox.php
contact-form-7 / modules Last commit date
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
checkbox.php
162 lines
1 <?php
2 /**
3 ** A base module for [checkbox], [checkbox*], and [radio]
4 **/
5
6 /* Shortcode handler */
7
8 function wpcf7_checkbox_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 $label_first = false;
29 $use_label_element = false;
30
31 if ( 'checkbox*' == $type )
32 $class_att .= ' wpcf7-validates-as-required';
33
34 if ( 'checkbox' == $type || 'checkbox*' == $type )
35 $class_att .= ' wpcf7-checkbox';
36
37 if ( 'radio' == $type )
38 $class_att .= ' wpcf7-radio';
39
40 foreach ( $options as $option ) {
41 if ( preg_match( '%^id:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
42 $id_att = $matches[1];
43
44 } elseif ( preg_match( '%^class:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
45 $class_att .= ' ' . $matches[1];
46
47 } elseif ( preg_match( '/^default:([0-9_]+)$/', $option, $matches ) ) {
48 $defaults = explode( '_', $matches[1] );
49
50 } elseif ( preg_match( '%^label[_-]?first$%', $option ) ) {
51 $label_first = true;
52
53 } elseif ( preg_match( '%^use[_-]?label[_-]?element$%', $option ) ) {
54 $use_label_element = true;
55
56 }
57 }
58
59 if ( $id_att )
60 $atts .= ' id="' . trim( $id_att ) . '"';
61
62 if ( $class_att )
63 $atts .= ' class="' . trim( $class_att ) . '"';
64
65 $multiple = preg_match( '/^checkbox[*]?$/', $type ) && ! preg_grep( '%^exclusive$%', $options );
66
67 $html = '';
68
69 if ( preg_match( '/^checkbox[*]?$/', $type ) && ! $multiple && WPCF7_LOAD_JS )
70 $onclick = ' onclick="wpcf7ExclusiveCheckbox(this);"';
71
72 $input_type = rtrim( $type, '*' );
73
74 $posted = is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) && $wpcf7_contact_form->is_posted();
75
76 foreach ( $values as $key => $value ) {
77 $checked = false;
78
79 if ( in_array( $key + 1, (array) $defaults ) )
80 $checked = true;
81
82 if ( $posted) {
83 if ( $multiple && in_array( esc_sql( $value ), (array) $_POST[$name] ) )
84 $checked = true;
85 if ( ! $multiple && $_POST[$name] == esc_sql( $value ) )
86 $checked = true;
87 }
88
89 $checked = $checked ? ' checked="checked"' : '';
90
91 if ( is_array( $tag['labels'] ) && isset( $tag['labels'][$key] ) )
92 $label = $tag['labels'][$key];
93 else
94 $label = $value;
95
96 if ( $label_first ) { // put label first, input last
97 $item = '<span class="wpcf7-list-item-label">' . esc_html( $label ) . '</span>&nbsp;';
98 $item .= '<input type="' . $input_type . '" name="' . $name . ( $multiple ? '[]' : '' ) . '" value="' . esc_attr( $value ) . '"' . $checked . $onclick . ' />';
99 } else {
100 $item = '<input type="' . $input_type . '" name="' . $name . ( $multiple ? '[]' : '' ) . '" value="' . esc_attr( $value ) . '"' . $checked . $onclick . ' />';
101 $item .= '&nbsp;<span class="wpcf7-list-item-label">' . esc_html( $label ) . '</span>';
102 }
103
104 if ( $use_label_element )
105 $item = '<label>' . $item . '</label>';
106
107 $item = '<span class="wpcf7-list-item">' . $item . '</span>';
108 $html .= $item;
109 }
110
111 $html = '<span' . $atts . '>' . $html . '</span>';
112
113 $validation_error = '';
114 if ( is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) )
115 $validation_error = $wpcf7_contact_form->validation_error( $name );
116
117 $html = '<span class="wpcf7-form-control-wrap ' . $name . '">' . $html . $validation_error . '</span>';
118
119 return $html;
120 }
121
122 wpcf7_add_shortcode( 'checkbox', 'wpcf7_checkbox_shortcode_handler', true );
123 wpcf7_add_shortcode( 'checkbox*', 'wpcf7_checkbox_shortcode_handler', true );
124 wpcf7_add_shortcode( 'radio', 'wpcf7_checkbox_shortcode_handler', true );
125
126
127 /* Validation filter */
128
129 function wpcf7_checkbox_validation_filter( $result, $tag ) {
130 global $wpcf7_contact_form;
131
132 $type = $tag['type'];
133 $name = $tag['name'];
134 $values = $tag['values'];
135
136 if ( is_array( $_POST[$name] ) ) {
137 foreach ( $_POST[$name] as $key => $value ) {
138 $value = stripslashes( $value );
139 if ( ! in_array( $value, (array) $values ) ) // Not in given choices.
140 unset( $_POST[$name][$key] );
141 }
142 } else {
143 $value = stripslashes( $_POST[$name] );
144 if ( ! in_array( $value, (array) $values ) ) // Not in given choices.
145 $_POST[$name] = '';
146 }
147
148 if ( 'checkbox*' == $type ) {
149 if ( empty( $_POST[$name] ) ) {
150 $result['valid'] = false;
151 $result['reason'][$name] = $wpcf7_contact_form->message( 'invalid_required' );
152 }
153 }
154
155 return $result;
156 }
157
158 add_filter( 'wpcf7_validate_checkbox', 'wpcf7_checkbox_validation_filter', 10, 2 );
159 add_filter( 'wpcf7_validate_checkbox*', 'wpcf7_checkbox_validation_filter', 10, 2 );
160 add_filter( 'wpcf7_validate_radio', 'wpcf7_checkbox_validation_filter', 10, 2 );
161
162 ?>