source: trunk/www.guidonia.net/wp/wp-content/plugins/contact-form-7/modules/quiz.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 15 years ago
File size: 3.5 KB
Line 
1<?php
2/**
3** A base module for [quiz]
4**/
5
6/* Shortcode handler */
7
8function wpcf7_quiz_shortcode_handler( $tag ) {
9 global $wpcf7_contact_form;
10
11 if ( ! is_array( $tag ) )
12 return '';
13
14 $type = $tag['type'];
15 $name = $tag['name'];
16 $options = (array) $tag['options'];
17 $pipes = $tag['pipes'];
18
19 if ( empty( $name ) )
20 return '';
21
22 $atts = '';
23 $id_att = '';
24 $class_att = '';
25 $size_att = '';
26 $maxlength_att = '';
27
28 foreach ( $options as $option ) {
29 if ( preg_match( '%^id:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
30 $id_att = $matches[1];
31
32 } elseif ( preg_match( '%^class:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
33 $class_att .= ' ' . $matches[1];
34
35 } elseif ( preg_match( '%^([0-9]*)[/x]([0-9]*)$%', $option, $matches ) ) {
36 $size_att = (int) $matches[1];
37 $maxlength_att = (int) $matches[2];
38 }
39 }
40
41 if ( $id_att )
42 $atts .= ' id="' . trim( $id_att ) . '"';
43
44 if ( $class_att )
45 $atts .= ' class="' . trim( $class_att ) . '"';
46
47 if ( $size_att )
48 $atts .= ' size="' . $size_att . '"';
49 else
50 $atts .= ' size="40"'; // default size
51
52 if ( $maxlength_att )
53 $atts .= ' maxlength="' . $maxlength_att . '"';
54
55 if ( is_a( $pipes, 'WPCF7_Pipes' ) && ! $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 $html = '<span class="wpcf7-quiz-label">' . esc_html( $question ) . '</span>&nbsp;';
68 $html .= '<input type="text" name="' . $name . '"' . $atts . ' />';
69 $html .= '<input type="hidden" name="_wpcf7_quiz_answer_' . $name . '" value="' . wp_hash( $answer, 'wpcf7_quiz' ) . '" />';
70
71 $validation_error = '';
72 if ( is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) )
73 $validation_error = $wpcf7_contact_form->validation_error( $name );
74
75 $html = '<span class="wpcf7-form-control-wrap ' . $name . '">' . $html . $validation_error . '</span>';
76
77 return $html;
78}
79
80wpcf7_add_shortcode( 'quiz', 'wpcf7_quiz_shortcode_handler', true );
81
82
83/* Validation filter */
84
85function wpcf7_quiz_validation_filter( $result, $tag ) {
86 global $wpcf7_contact_form;
87
88 $type = $tag['type'];
89 $name = $tag['name'];
90
91 $answer = wpcf7_canonicalize( $_POST[$name] );
92 $answer_hash = wp_hash( $answer, 'wpcf7_quiz' );
93 $expected_hash = $_POST['_wpcf7_quiz_answer_' . $name];
94 if ( $answer_hash != $expected_hash ) {
95 $result['valid'] = false;
96 $result['reason'][$name] = $wpcf7_contact_form->message( 'quiz_answer_not_correct' );
97 }
98
99 return $result;
100}
101
102add_filter( 'wpcf7_validate_quiz', 'wpcf7_quiz_validation_filter', 10, 2 );
103
104
105/* Ajax echo filter */
106
107function wpcf7_quiz_ajax_echo_filter( $items ) {
108 global $wpcf7_contact_form;
109
110 if ( ! is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) )
111 return $items;
112
113 if ( ! is_array( $items ) )
114 return $items;
115
116 $fes = $wpcf7_contact_form->form_scan_shortcode(
117 array( 'type' => 'quiz' ) );
118
119 if ( empty( $fes ) )
120 return $items;
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 if ( is_a( $pipes, 'WPCF7_Pipes' ) && ! $pipes->zero() ) {
132 $pipe = $pipes->random_pipe();
133 $question = $pipe->before;
134 $answer = $pipe->after;
135 } else {
136 // default quiz
137 $question = '1+1=?';
138 $answer = '2';
139 }
140
141 $answer = wpcf7_canonicalize( $answer );
142
143 $refill[$name] = array( $question, wp_hash( $answer, 'wpcf7_quiz' ) );
144 }
145
146 if ( ! empty( $refill ) )
147 $items['quiz'] = $refill;
148
149 return $items;
150}
151
152add_filter( 'wpcf7_ajax_json_echo', 'wpcf7_quiz_ajax_echo_filter' );
153
154?>
Note: See TracBrowser for help on using the repository browser.