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.8.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 / quiz.php
contact-form-7 / modules Last commit date
acceptance.php 15 years ago akismet.php 14 years ago captcha.php 15 years ago checkbox.php 15 years ago file.php 14 years ago quiz.php 15 years ago response.php 15 years ago select.php 14 years ago special-mail-tags.php 14 years ago submit.php 15 years ago text.php 15 years ago textarea.php 14 years ago
quiz.php
214 lines
1 <?php
2 /**
3 ** A base module for [quiz]
4 **/
5
6 /* Shortcode handler */
7
8 wpcf7_add_shortcode( 'quiz', 'wpcf7_quiz_shortcode_handler', true );
9
10 function wpcf7_quiz_shortcode_handler( $tag ) {
11 if ( ! is_array( $tag ) )
12 return '';
13
14 $type = $tag['type'];
15 $name = $tag['name'];
16 $options = (array) $tag['options'];
17 $pipes = $tag['pipes'];
18
19 if ( empty( $name ) )
20 return '';
21
22 $atts = '';
23 $id_att = '';
24 $class_att = '';
25 $size_att = '';
26 $maxlength_att = '';
27 $tabindex_att = '';
28
29 $class_att .= ' wpcf7-quiz';
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( '%^([0-9]*)[/x]([0-9]*)$%', $option, $matches ) ) {
39 $size_att = (int) $matches[1];
40 $maxlength_att = (int) $matches[2];
41
42 } elseif ( preg_match( '%^tabindex:(\d+)$%', $option, $matches ) ) {
43 $tabindex_att = (int) $matches[1];
44
45 }
46 }
47
48 if ( $id_att )
49 $atts .= ' id="' . trim( $id_att ) . '"';
50
51 if ( $class_att )
52 $atts .= ' class="' . trim( $class_att ) . '"';
53
54 if ( $size_att )
55 $atts .= ' size="' . $size_att . '"';
56 else
57 $atts .= ' size="40"'; // default size
58
59 if ( $maxlength_att )
60 $atts .= ' maxlength="' . $maxlength_att . '"';
61
62 if ( '' !== $tabindex_att )
63 $atts .= sprintf( ' tabindex="%d"', $tabindex_att );
64
65 if ( is_a( $pipes, 'WPCF7_Pipes' ) && ! $pipes->zero() ) {
66 $pipe = $pipes->random_pipe();
67 $question = $pipe->before;
68 $answer = $pipe->after;
69 } else {
70 // default quiz
71 $question = '1+1=?';
72 $answer = '2';
73 }
74
75 $answer = wpcf7_canonicalize( $answer );
76
77 $html = '<span class="wpcf7-quiz-label">' . esc_html( $question ) . '</span>&nbsp;';
78 $html .= '<input type="text" name="' . $name . '"' . $atts . ' />';
79 $html .= '<input type="hidden" name="_wpcf7_quiz_answer_' . $name . '" value="' . wp_hash( $answer, 'wpcf7_quiz' ) . '" />';
80
81 $validation_error = wpcf7_get_validation_error( $name );
82
83 $html = '<span class="wpcf7-form-control-wrap ' . $name . '">' . $html . $validation_error . '</span>';
84
85 return $html;
86 }
87
88
89 /* Validation filter */
90
91 add_filter( 'wpcf7_validate_quiz', 'wpcf7_quiz_validation_filter', 10, 2 );
92
93 function wpcf7_quiz_validation_filter( $result, $tag ) {
94 $type = $tag['type'];
95 $name = $tag['name'];
96
97 $answer = wpcf7_canonicalize( $_POST[$name] );
98 $answer_hash = wp_hash( $answer, 'wpcf7_quiz' );
99 $expected_hash = $_POST['_wpcf7_quiz_answer_' . $name];
100 if ( $answer_hash != $expected_hash ) {
101 $result['valid'] = false;
102 $result['reason'][$name] = wpcf7_get_message( 'quiz_answer_not_correct' );
103 }
104
105 return $result;
106 }
107
108
109 /* Ajax echo filter */
110
111 add_filter( 'wpcf7_ajax_onload', 'wpcf7_quiz_ajax_refill' );
112 add_filter( 'wpcf7_ajax_json_echo', 'wpcf7_quiz_ajax_refill' );
113
114 function wpcf7_quiz_ajax_refill( $items ) {
115 if ( ! is_array( $items ) )
116 return $items;
117
118 $fes = wpcf7_scan_shortcode( array( 'type' => 'quiz' ) );
119
120 if ( empty( $fes ) )
121 return $items;
122
123 $refill = array();
124
125 foreach ( $fes as $fe ) {
126 $name = $fe['name'];
127 $pipes = $fe['pipes'];
128
129 if ( empty( $name ) )
130 continue;
131
132 if ( is_a( $pipes, 'WPCF7_Pipes' ) && ! $pipes->zero() ) {
133 $pipe = $pipes->random_pipe();
134 $question = $pipe->before;
135 $answer = $pipe->after;
136 } else {
137 // default quiz
138 $question = '1+1=?';
139 $answer = '2';
140 }
141
142 $answer = wpcf7_canonicalize( $answer );
143
144 $refill[$name] = array( $question, wp_hash( $answer, 'wpcf7_quiz' ) );
145 }
146
147 if ( ! empty( $refill ) )
148 $items['quiz'] = $refill;
149
150 return $items;
151 }
152
153
154 /* Messages */
155
156 add_filter( 'wpcf7_messages', 'wpcf7_quiz_messages' );
157
158 function wpcf7_quiz_messages( $messages ) {
159 return array_merge( $messages, array( 'quiz_answer_not_correct' => array(
160 'description' => __( "Sender doesn't enter the correct answer to the quiz", 'wpcf7' ),
161 'default' => __( 'Your answer is not correct.', 'wpcf7' )
162 ) ) );
163 }
164
165
166 /* Tag generator */
167
168 add_action( 'admin_init', 'wpcf7_add_tag_generator_quiz', 40 );
169
170 function wpcf7_add_tag_generator_quiz() {
171 wpcf7_add_tag_generator( 'quiz', __( 'Quiz', 'wpcf7' ),
172 'wpcf7-tg-pane-quiz', 'wpcf7_tg_pane_quiz' );
173 }
174
175 function wpcf7_tg_pane_quiz( &$contact_form ) {
176 ?>
177 <div id="wpcf7-tg-pane-quiz" class="hidden">
178 <form action="">
179 <table>
180 <tr><td><?php echo esc_html( __( 'Name', 'wpcf7' ) ); ?><br /><input type="text" name="name" class="tg-name oneline" /></td><td></td></tr>
181 </table>
182
183 <table>
184 <tr>
185 <td><code>id</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
186 <input type="text" name="id" class="idvalue oneline option" /></td>
187
188 <td><code>class</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
189 <input type="text" name="class" class="classvalue oneline option" /></td>
190 </tr>
191
192 <tr>
193 <td><code>size</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
194 <input type="text" name="size" class="numeric oneline option" /></td>
195
196 <td><code>maxlength</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
197 <input type="text" name="maxlength" class="numeric oneline option" /></td>
198 </tr>
199
200 <tr>
201 <td><?php echo esc_html( __( 'Quizzes', 'wpcf7' ) ); ?><br />
202 <textarea name="values"></textarea><br />
203 <span style="font-size: smaller"><?php echo esc_html( __( "* quiz|answer (e.g. 1+1=?|2)", 'wpcf7' ) ); ?></span>
204 </td>
205 </tr>
206 </table>
207
208 <div class="tg-tag"><?php echo esc_html( __( "Copy this code and paste it into the form left.", 'wpcf7' ) ); ?><br /><input type="text" name="quiz" class="tag" readonly="readonly" onfocus="this.select()" /></div>
209 </form>
210 </div>
211 <?php
212 }
213
214 ?>