acceptance.php
7 years ago
akismet.php
7 years ago
checkbox.php
7 years ago
constant-contact.php
7 years ago
count.php
7 years ago
date.php
7 years ago
file.php
7 years ago
flamingo.php
7 years ago
hidden.php
7 years ago
listo.php
9 years ago
number.php
7 years ago
quiz.php
7 years ago
really-simple-captcha.php
7 years ago
recaptcha.php
7 years ago
response.php
7 years ago
select.php
7 years ago
submit.php
7 years ago
text.php
7 years ago
textarea.php
7 years ago
quiz.php
239 lines
| 1 | <?php |
| 2 | /** |
| 3 | ** A base module for [quiz] |
| 4 | **/ |
| 5 | |
| 6 | /* form_tag handler */ |
| 7 | |
| 8 | add_action( 'wpcf7_init', 'wpcf7_add_form_tag_quiz', 10, 0 ); |
| 9 | |
| 10 | function wpcf7_add_form_tag_quiz() { |
| 11 | wpcf7_add_form_tag( 'quiz', |
| 12 | 'wpcf7_quiz_form_tag_handler', |
| 13 | array( |
| 14 | 'name-attr' => true, |
| 15 | 'do-not-store' => true, |
| 16 | 'not-for-mail' => true, |
| 17 | ) |
| 18 | ); |
| 19 | } |
| 20 | |
| 21 | function wpcf7_quiz_form_tag_handler( $tag ) { |
| 22 | if ( empty( $tag->name ) ) { |
| 23 | return ''; |
| 24 | } |
| 25 | |
| 26 | $validation_error = wpcf7_get_validation_error( $tag->name ); |
| 27 | |
| 28 | $class = wpcf7_form_controls_class( $tag->type ); |
| 29 | |
| 30 | if ( $validation_error ) { |
| 31 | $class .= ' wpcf7-not-valid'; |
| 32 | } |
| 33 | |
| 34 | $atts = array(); |
| 35 | |
| 36 | $atts['size'] = $tag->get_size_option( '40' ); |
| 37 | $atts['maxlength'] = $tag->get_maxlength_option(); |
| 38 | $atts['minlength'] = $tag->get_minlength_option(); |
| 39 | |
| 40 | if ( $atts['maxlength'] and $atts['minlength'] |
| 41 | and $atts['maxlength'] < $atts['minlength'] ) { |
| 42 | unset( $atts['maxlength'], $atts['minlength'] ); |
| 43 | } |
| 44 | |
| 45 | $atts['class'] = $tag->get_class_option( $class ); |
| 46 | $atts['id'] = $tag->get_id_option(); |
| 47 | $atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true ); |
| 48 | $atts['autocomplete'] = 'off'; |
| 49 | $atts['aria-required'] = 'true'; |
| 50 | $atts['aria-invalid'] = $validation_error ? 'true' : 'false'; |
| 51 | |
| 52 | $pipes = $tag->pipes; |
| 53 | |
| 54 | if ( $pipes instanceof WPCF7_Pipes |
| 55 | and ! $pipes->zero() ) { |
| 56 | $pipe = $pipes->random_pipe(); |
| 57 | $question = $pipe->before; |
| 58 | $answer = $pipe->after; |
| 59 | } else { |
| 60 | // default quiz |
| 61 | $question = '1+1=?'; |
| 62 | $answer = '2'; |
| 63 | } |
| 64 | |
| 65 | $answer = wpcf7_canonicalize( $answer ); |
| 66 | |
| 67 | $atts['type'] = 'text'; |
| 68 | $atts['name'] = $tag->name; |
| 69 | |
| 70 | $atts = wpcf7_format_atts( $atts ); |
| 71 | |
| 72 | $html = sprintf( |
| 73 | '<span class="wpcf7-form-control-wrap %1$s"><label><span class="wpcf7-quiz-label">%2$s</span> <input %3$s /></label><input type="hidden" name="_wpcf7_quiz_answer_%4$s" value="%5$s" />%6$s</span>', |
| 74 | sanitize_html_class( $tag->name ), |
| 75 | esc_html( $question ), $atts, $tag->name, |
| 76 | wp_hash( $answer, 'wpcf7_quiz' ), $validation_error ); |
| 77 | |
| 78 | return $html; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /* Validation filter */ |
| 83 | |
| 84 | add_filter( 'wpcf7_validate_quiz', 'wpcf7_quiz_validation_filter', 10, 2 ); |
| 85 | |
| 86 | function wpcf7_quiz_validation_filter( $result, $tag ) { |
| 87 | $name = $tag->name; |
| 88 | |
| 89 | $answer = isset( $_POST[$name] ) ? wpcf7_canonicalize( $_POST[$name] ) : ''; |
| 90 | $answer = wp_unslash( $answer ); |
| 91 | |
| 92 | $answer_hash = wp_hash( $answer, 'wpcf7_quiz' ); |
| 93 | |
| 94 | $expected_hash = isset( $_POST['_wpcf7_quiz_answer_' . $name] ) |
| 95 | ? (string) $_POST['_wpcf7_quiz_answer_' . $name] |
| 96 | : ''; |
| 97 | |
| 98 | if ( $answer_hash != $expected_hash ) { |
| 99 | $result->invalidate( $tag, wpcf7_get_message( 'quiz_answer_not_correct' ) ); |
| 100 | } |
| 101 | |
| 102 | return $result; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /* Ajax echo filter */ |
| 107 | |
| 108 | add_filter( 'wpcf7_ajax_onload', 'wpcf7_quiz_ajax_refill', 10, 1 ); |
| 109 | add_filter( 'wpcf7_ajax_json_echo', 'wpcf7_quiz_ajax_refill', 10, 1 ); |
| 110 | |
| 111 | function wpcf7_quiz_ajax_refill( $items ) { |
| 112 | if ( ! is_array( $items ) ) { |
| 113 | return $items; |
| 114 | } |
| 115 | |
| 116 | $fes = wpcf7_scan_form_tags( array( 'type' => 'quiz' ) ); |
| 117 | |
| 118 | if ( empty( $fes ) ) { |
| 119 | return $items; |
| 120 | } |
| 121 | |
| 122 | $refill = array(); |
| 123 | |
| 124 | foreach ( $fes as $fe ) { |
| 125 | $name = $fe['name']; |
| 126 | $pipes = $fe['pipes']; |
| 127 | |
| 128 | if ( empty( $name ) ) { |
| 129 | continue; |
| 130 | } |
| 131 | |
| 132 | if ( $pipes instanceof WPCF7_Pipes |
| 133 | and ! $pipes->zero() ) { |
| 134 | $pipe = $pipes->random_pipe(); |
| 135 | $question = $pipe->before; |
| 136 | $answer = $pipe->after; |
| 137 | } else { |
| 138 | // default quiz |
| 139 | $question = '1+1=?'; |
| 140 | $answer = '2'; |
| 141 | } |
| 142 | |
| 143 | $answer = wpcf7_canonicalize( $answer ); |
| 144 | |
| 145 | $refill[$name] = array( $question, wp_hash( $answer, 'wpcf7_quiz' ) ); |
| 146 | } |
| 147 | |
| 148 | if ( ! empty( $refill ) ) { |
| 149 | $items['quiz'] = $refill; |
| 150 | } |
| 151 | |
| 152 | return $items; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | /* Messages */ |
| 157 | |
| 158 | add_filter( 'wpcf7_messages', 'wpcf7_quiz_messages', 10, 1 ); |
| 159 | |
| 160 | function wpcf7_quiz_messages( $messages ) { |
| 161 | $messages = array_merge( $messages, array( |
| 162 | 'quiz_answer_not_correct' => array( |
| 163 | 'description' => |
| 164 | __( "Sender doesn't enter the correct answer to the quiz", 'contact-form-7' ), |
| 165 | 'default' => |
| 166 | __( "The answer to the quiz is incorrect.", 'contact-form-7' ), |
| 167 | ), |
| 168 | ) ); |
| 169 | |
| 170 | return $messages; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | /* Tag generator */ |
| 175 | |
| 176 | add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_quiz', 40, 0 ); |
| 177 | |
| 178 | function wpcf7_add_tag_generator_quiz() { |
| 179 | $tag_generator = WPCF7_TagGenerator::get_instance(); |
| 180 | $tag_generator->add( 'quiz', __( 'quiz', 'contact-form-7' ), |
| 181 | 'wpcf7_tag_generator_quiz' ); |
| 182 | } |
| 183 | |
| 184 | function wpcf7_tag_generator_quiz( $contact_form, $args = '' ) { |
| 185 | $args = wp_parse_args( $args, array() ); |
| 186 | $type = 'quiz'; |
| 187 | |
| 188 | $description = __( "Generate a form-tag for a question-answer pair. For more details, see %s.", 'contact-form-7' ); |
| 189 | |
| 190 | $desc_link = wpcf7_link( __( 'https://contactform7.com/quiz/', 'contact-form-7' ), __( 'Quiz', 'contact-form-7' ) ); |
| 191 | |
| 192 | ?> |
| 193 | <div class="control-box"> |
| 194 | <fieldset> |
| 195 | <legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend> |
| 196 | |
| 197 | <table class="form-table"> |
| 198 | <tbody> |
| 199 | <tr> |
| 200 | <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th> |
| 201 | <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td> |
| 202 | </tr> |
| 203 | |
| 204 | <tr> |
| 205 | <th scope="row"><?php echo esc_html( __( 'Questions and answers', 'contact-form-7' ) ); ?></th> |
| 206 | <td> |
| 207 | <fieldset> |
| 208 | <legend class="screen-reader-text"><?php echo esc_html( __( 'Questions and answers', 'contact-form-7' ) ); ?></legend> |
| 209 | <textarea name="values" class="values" id="<?php echo esc_attr( $args['content'] . '-values' ); ?>"></textarea><br /> |
| 210 | <label for="<?php echo esc_attr( $args['content'] . '-values' ); ?>"><span class="description"><?php echo esc_html( __( "One pipe-separated question-answer pair (e.g. The capital of Brazil?|Rio) per line.", 'contact-form-7' ) ); ?></span></label> |
| 211 | </fieldset> |
| 212 | </td> |
| 213 | </tr> |
| 214 | |
| 215 | <tr> |
| 216 | <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th> |
| 217 | <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td> |
| 218 | </tr> |
| 219 | |
| 220 | <tr> |
| 221 | <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th> |
| 222 | <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td> |
| 223 | </tr> |
| 224 | |
| 225 | </tbody> |
| 226 | </table> |
| 227 | </fieldset> |
| 228 | </div> |
| 229 | |
| 230 | <div class="insert-box"> |
| 231 | <input type="text" name="<?php echo $type; ?>" class="tag code" readonly="readonly" onfocus="this.select()" /> |
| 232 | |
| 233 | <div class="submitbox"> |
| 234 | <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" /> |
| 235 | </div> |
| 236 | </div> |
| 237 | <?php |
| 238 | } |
| 239 |