quiz.php 6.5 KB

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