1 | <?php
|
---|
2 | /**
|
---|
3 | ** A base module for [text], [text*], [email], and [email*]
|
---|
4 | **/
|
---|
5 |
|
---|
6 | /* Shortcode handler */
|
---|
7 |
|
---|
8 | function wpcf7_text_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 | $values = (array) $tag['values'];
|
---|
18 |
|
---|
19 | if ( empty( $name ) )
|
---|
20 | return '';
|
---|
21 |
|
---|
22 | $atts = '';
|
---|
23 | $id_att = '';
|
---|
24 | $class_att = '';
|
---|
25 | $size_att = '';
|
---|
26 | $maxlength_att = '';
|
---|
27 |
|
---|
28 | if ( 'email' == $type || 'email*' == $type )
|
---|
29 | $class_att .= ' wpcf7-validates-as-email';
|
---|
30 |
|
---|
31 | if ( 'text*' == $type || 'email*' == $type )
|
---|
32 | $class_att .= ' wpcf7-validates-as-required';
|
---|
33 |
|
---|
34 | foreach ( $options as $option ) {
|
---|
35 | if ( preg_match( '%^id:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
|
---|
36 | $id_att = $matches[1];
|
---|
37 |
|
---|
38 | } elseif ( preg_match( '%^class:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
|
---|
39 | $class_att .= ' ' . $matches[1];
|
---|
40 |
|
---|
41 | } elseif ( preg_match( '%^([0-9]*)[/x]([0-9]*)$%', $option, $matches ) ) {
|
---|
42 | $size_att = (int) $matches[1];
|
---|
43 | $maxlength_att = (int) $matches[2];
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | if ( $id_att )
|
---|
48 | $atts .= ' id="' . trim( $id_att ) . '"';
|
---|
49 |
|
---|
50 | if ( $class_att )
|
---|
51 | $atts .= ' class="' . trim( $class_att ) . '"';
|
---|
52 |
|
---|
53 | if ( $size_att )
|
---|
54 | $atts .= ' size="' . $size_att . '"';
|
---|
55 | else
|
---|
56 | $atts .= ' size="40"'; // default size
|
---|
57 |
|
---|
58 | if ( $maxlength_att )
|
---|
59 | $atts .= ' maxlength="' . $maxlength_att . '"';
|
---|
60 |
|
---|
61 | // Value
|
---|
62 | if ( is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) && $wpcf7_contact_form->is_posted() ) {
|
---|
63 | if ( isset( $_POST['_wpcf7_mail_sent'] ) && $_POST['_wpcf7_mail_sent']['ok'] )
|
---|
64 | $value = '';
|
---|
65 | else
|
---|
66 | $value = $_POST[$name];
|
---|
67 | } else {
|
---|
68 | $value = $values[0];
|
---|
69 | }
|
---|
70 |
|
---|
71 | $html = '<input type="text" name="' . $name . '" value="' . esc_attr( $value ) . '"' . $atts . ' />';
|
---|
72 |
|
---|
73 | $validation_error = '';
|
---|
74 | if ( is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) )
|
---|
75 | $validation_error = $wpcf7_contact_form->validation_error( $name );
|
---|
76 |
|
---|
77 | $html = '<span class="wpcf7-form-control-wrap ' . $name . '">' . $html . $validation_error . '</span>';
|
---|
78 |
|
---|
79 | return $html;
|
---|
80 | }
|
---|
81 |
|
---|
82 | wpcf7_add_shortcode( 'text', 'wpcf7_text_shortcode_handler', true );
|
---|
83 | wpcf7_add_shortcode( 'text*', 'wpcf7_text_shortcode_handler', true );
|
---|
84 | wpcf7_add_shortcode( 'email', 'wpcf7_text_shortcode_handler', true );
|
---|
85 | wpcf7_add_shortcode( 'email*', 'wpcf7_text_shortcode_handler', true );
|
---|
86 |
|
---|
87 |
|
---|
88 | /* Validation filter */
|
---|
89 |
|
---|
90 | function wpcf7_text_validation_filter( $result, $tag ) {
|
---|
91 | global $wpcf7_contact_form;
|
---|
92 |
|
---|
93 | $type = $tag['type'];
|
---|
94 | $name = $tag['name'];
|
---|
95 |
|
---|
96 | $_POST[$name] = trim( strtr( (string) $_POST[$name], "\n", " " ) );
|
---|
97 |
|
---|
98 | if ( 'text*' == $type ) {
|
---|
99 | if ( '' == $_POST[$name] ) {
|
---|
100 | $result['valid'] = false;
|
---|
101 | $result['reason'][$name] = $wpcf7_contact_form->message( 'invalid_required' );
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | if ( 'email' == $type || 'email*' == $type ) {
|
---|
106 | if ( 'email*' == $type && '' == $_POST[$name] ) {
|
---|
107 | $result['valid'] = false;
|
---|
108 | $result['reason'][$name] = $wpcf7_contact_form->message( 'invalid_required' );
|
---|
109 | } elseif ( '' != $_POST[$name] && ! is_email( $_POST[$name] ) ) {
|
---|
110 | $result['valid'] = false;
|
---|
111 | $result['reason'][$name] = $wpcf7_contact_form->message( 'invalid_email' );
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | return $result;
|
---|
116 | }
|
---|
117 |
|
---|
118 | add_filter( 'wpcf7_validate_text', 'wpcf7_text_validation_filter', 10, 2 );
|
---|
119 | add_filter( 'wpcf7_validate_text*', 'wpcf7_text_validation_filter', 10, 2 );
|
---|
120 | add_filter( 'wpcf7_validate_email', 'wpcf7_text_validation_filter', 10, 2 );
|
---|
121 | add_filter( 'wpcf7_validate_email*', 'wpcf7_text_validation_filter', 10, 2 );
|
---|
122 |
|
---|
123 | ?>
|
---|