1 | <?php
|
---|
2 | /**
|
---|
3 | ** A base module for [textarea] and [textarea*]
|
---|
4 | **/
|
---|
5 |
|
---|
6 | /* Shortcode handler */
|
---|
7 |
|
---|
8 | function wpcf7_textarea_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 | $content = $tag['content'];
|
---|
19 |
|
---|
20 | if ( empty( $name ) )
|
---|
21 | return '';
|
---|
22 |
|
---|
23 | $atts = '';
|
---|
24 | $id_att = '';
|
---|
25 | $class_att = '';
|
---|
26 | $cols_att = '';
|
---|
27 | $rows_att = '';
|
---|
28 |
|
---|
29 | if ( 'textarea*' == $type )
|
---|
30 | $class_att .= ' wpcf7-validates-as-required';
|
---|
31 |
|
---|
32 | foreach ( $options as $option ) {
|
---|
33 | if ( preg_match( '%^id:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
|
---|
34 | $id_att = $matches[1];
|
---|
35 |
|
---|
36 | } elseif ( preg_match( '%^class:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
|
---|
37 | $class_att .= ' ' . $matches[1];
|
---|
38 |
|
---|
39 | } elseif ( preg_match( '%^([0-9]*)[x/]([0-9]*)$%', $option, $matches ) ) {
|
---|
40 | $cols_att = (int) $matches[1];
|
---|
41 | $rows_att = (int) $matches[2];
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | if ( $id_att )
|
---|
46 | $atts .= ' id="' . trim( $id_att ) . '"';
|
---|
47 |
|
---|
48 | if ( $class_att )
|
---|
49 | $atts .= ' class="' . trim( $class_att ) . '"';
|
---|
50 |
|
---|
51 | if ( $cols_att )
|
---|
52 | $atts .= ' cols="' . $cols_att . '"';
|
---|
53 | else
|
---|
54 | $atts .= ' cols="40"'; // default size
|
---|
55 |
|
---|
56 | if ( $rows_att )
|
---|
57 | $atts .= ' rows="' . $rows_att . '"';
|
---|
58 | else
|
---|
59 | $atts .= ' rows="10"'; // default size
|
---|
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 | if ( ! empty( $content ) )
|
---|
71 | $value = $content;
|
---|
72 | }
|
---|
73 |
|
---|
74 | $html = '<textarea name="' . $name . '"' . $atts . '>' . esc_html( $value ) . '</textarea>';
|
---|
75 |
|
---|
76 | $validation_error = '';
|
---|
77 | if ( is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) )
|
---|
78 | $validation_error = $wpcf7_contact_form->validation_error( $name );
|
---|
79 |
|
---|
80 | $html = '<span class="wpcf7-form-control-wrap ' . $name . '">' . $html . $validation_error . '</span>';
|
---|
81 |
|
---|
82 | return $html;
|
---|
83 | }
|
---|
84 |
|
---|
85 | wpcf7_add_shortcode( 'textarea', 'wpcf7_textarea_shortcode_handler', true );
|
---|
86 | wpcf7_add_shortcode( 'textarea*', 'wpcf7_textarea_shortcode_handler', true );
|
---|
87 |
|
---|
88 |
|
---|
89 | /* Validation filter */
|
---|
90 |
|
---|
91 | function wpcf7_textarea_validation_filter( $result, $tag ) {
|
---|
92 | global $wpcf7_contact_form;
|
---|
93 |
|
---|
94 | $type = $tag['type'];
|
---|
95 | $name = $tag['name'];
|
---|
96 |
|
---|
97 | $_POST[$name] = (string) $_POST[$name];
|
---|
98 |
|
---|
99 | if ( 'textarea*' == $type ) {
|
---|
100 | if ( '' == $_POST[$name] ) {
|
---|
101 | $result['valid'] = false;
|
---|
102 | $result['reason'][$name] = $wpcf7_contact_form->message( 'invalid_required' );
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | return $result;
|
---|
107 | }
|
---|
108 |
|
---|
109 | add_filter( 'wpcf7_validate_textarea', 'wpcf7_textarea_validation_filter', 10, 2 );
|
---|
110 | add_filter( 'wpcf7_validate_textarea*', 'wpcf7_textarea_validation_filter', 10, 2 );
|
---|
111 |
|
---|
112 | ?>
|
---|