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> '; |
| 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 | ?> |