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 .= '';
$validation_error = '';
if ( is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) )
$validation_error = $wpcf7_contact_form->validation_error( $name );
$html = '' . $html . $validation_error . '';
return $html;
}
wpcf7_add_shortcode( 'quiz', 'wpcf7_quiz_shortcode_handler', true );
/* Validation filter */
function wpcf7_quiz_validation_filter( $result, $tag ) {
global $wpcf7_contact_form;
$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_contact_form->message( 'quiz_answer_not_correct' );
}
return $result;
}
add_filter( 'wpcf7_validate_quiz', 'wpcf7_quiz_validation_filter', 10, 2 );
/* Ajax echo filter */
function wpcf7_quiz_ajax_echo_filter( $items ) {
global $wpcf7_contact_form;
if ( ! is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) )
return $items;
if ( ! is_array( $items ) )
return $items;
$fes = $wpcf7_contact_form->form_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;
}
add_filter( 'wpcf7_ajax_json_echo', 'wpcf7_quiz_ajax_echo_filter' );
?>