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