zero() ) { $pipe = $pipes->random_pipe(); $question = $pipe->before; $answer = $pipe->after; } else { // default quiz $question = '1+1=?'; $answer = '2'; } $answer = wpcf7_canonicalize( $answer ); $html = '' . esc_html( $question ) . ' '; $html .= ''; $html .= ''; $html = '' . $html . $validation_error . ''; return $html; } /* Validation filter */ add_filter( 'wpcf7_validate_quiz', 'wpcf7_quiz_validation_filter', 10, 2 ); function wpcf7_quiz_validation_filter( $result, $tag ) { $type = $tag['type']; $name = $tag['name']; $answer = wpcf7_canonicalize( $_POST[$name] ); $answer_hash = wp_hash( $answer, 'wpcf7_quiz' ); $expected_hash = $_POST['_wpcf7_quiz_answer_' . $name]; if ( $answer_hash != $expected_hash ) { $result['valid'] = false; $result['reason'][$name] = wpcf7_get_message( 'quiz_answer_not_correct' ); } return $result; } /* Ajax echo filter */ add_filter( 'wpcf7_ajax_onload', 'wpcf7_quiz_ajax_refill' ); add_filter( 'wpcf7_ajax_json_echo', 'wpcf7_quiz_ajax_refill' ); function wpcf7_quiz_ajax_refill( $items ) { if ( ! is_array( $items ) ) return $items; $fes = wpcf7_scan_shortcode( array( 'type' => 'quiz' ) ); if ( empty( $fes ) ) return $items; $refill = array(); foreach ( $fes as $fe ) { $name = $fe['name']; $pipes = $fe['pipes']; if ( empty( $name ) ) continue; if ( is_a( $pipes, 'WPCF7_Pipes' ) && ! $pipes->zero() ) { $pipe = $pipes->random_pipe(); $question = $pipe->before; $answer = $pipe->after; } else { // default quiz $question = '1+1=?'; $answer = '2'; } $answer = wpcf7_canonicalize( $answer ); $refill[$name] = array( $question, wp_hash( $answer, 'wpcf7_quiz' ) ); } if ( ! empty( $refill ) ) $items['quiz'] = $refill; return $items; } /* Messages */ add_filter( 'wpcf7_messages', 'wpcf7_quiz_messages' ); function wpcf7_quiz_messages( $messages ) { return array_merge( $messages, array( 'quiz_answer_not_correct' => array( 'description' => __( "Sender doesn't enter the correct answer to the quiz", 'wpcf7' ), 'default' => __( 'Your answer is not correct.', 'wpcf7' ) ) ) ); } /* Tag generator */ add_action( 'admin_init', 'wpcf7_add_tag_generator_quiz', 40 ); function wpcf7_add_tag_generator_quiz() { wpcf7_add_tag_generator( 'quiz', __( 'Quiz', 'wpcf7' ), 'wpcf7-tg-pane-quiz', 'wpcf7_tg_pane_quiz' ); } function wpcf7_tg_pane_quiz( &$contact_form ) { ?>