Plugin Directory

source: contact-form-7/tags/5.9.6/modules/textarea.php

Last change on this file was 3103414, checked in by takayukister, 6 weeks ago

Merge changes on GitHub https://github.com/rocklobster-in/contact-form-7/

  • Property svn:eol-style set to native
File size: 6.5 KB
Line 
1<?php
2/**
3** A base module for [textarea] and [textarea*]
4**/
5
6/* form_tag handler */
7
8add_action( 'wpcf7_init', 'wpcf7_add_form_tag_textarea', 10, 0 );
9
10function wpcf7_add_form_tag_textarea() {
11        wpcf7_add_form_tag( array( 'textarea', 'textarea*' ),
12                'wpcf7_textarea_form_tag_handler', array( 'name-attr' => true )
13        );
14}
15
16function wpcf7_textarea_form_tag_handler( $tag ) {
17        if ( empty( $tag->name ) ) {
18                return '';
19        }
20
21        $validation_error = wpcf7_get_validation_error( $tag->name );
22
23        $class = wpcf7_form_controls_class( $tag->type );
24
25        if ( $validation_error ) {
26                $class .= ' wpcf7-not-valid';
27        }
28
29        $atts = array();
30
31        $atts['cols'] = $tag->get_cols_option( '40' );
32        $atts['rows'] = $tag->get_rows_option( '10' );
33        $atts['maxlength'] = $tag->get_maxlength_option( '400' );
34        $atts['minlength'] = $tag->get_minlength_option();
35
36        if ( $atts['maxlength'] and $atts['minlength']
37        and $atts['maxlength'] < $atts['minlength'] ) {
38                unset( $atts['maxlength'], $atts['minlength'] );
39        }
40
41        $atts['class'] = $tag->get_class_option( $class );
42        $atts['id'] = $tag->get_id_option();
43        $atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true );
44        $atts['readonly'] = $tag->has_option( 'readonly' );
45
46        $atts['autocomplete'] = $tag->get_option(
47                'autocomplete', '[-0-9a-zA-Z]+', true
48        );
49
50        if ( $tag->is_required() ) {
51                $atts['aria-required'] = 'true';
52        }
53
54        if ( $validation_error ) {
55                $atts['aria-invalid'] = 'true';
56                $atts['aria-describedby'] = wpcf7_get_validation_error_reference(
57                        $tag->name
58                );
59        } else {
60                $atts['aria-invalid'] = 'false';
61        }
62
63        $value = empty( $tag->content )
64                ? (string) reset( $tag->values )
65                : $tag->content;
66
67        if ( $tag->has_option( 'placeholder' )
68        or $tag->has_option( 'watermark' ) ) {
69                $atts['placeholder'] = $value;
70                $value = '';
71        }
72
73        $value = $tag->get_default_option( $value );
74
75        $value = wpcf7_get_hangover( $tag->name, $value );
76
77        $atts['name'] = $tag->name;
78
79        $html = sprintf(
80                '<span class="wpcf7-form-control-wrap" data-name="%1$s"><textarea %2$s>%3$s</textarea>%4$s</span>',
81                esc_attr( $tag->name ),
82                wpcf7_format_atts( $atts ),
83                esc_textarea( $value ),
84                $validation_error
85        );
86
87        return $html;
88}
89
90
91add_action(
92        'wpcf7_swv_create_schema',
93        'wpcf7_swv_add_textarea_rules',
94        10, 2
95);
96
97function wpcf7_swv_add_textarea_rules( $schema, $contact_form ) {
98        $tags = $contact_form->scan_form_tags( array(
99                'basetype' => array( 'textarea' ),
100        ) );
101
102        foreach ( $tags as $tag ) {
103                if ( $tag->is_required() ) {
104                        $schema->add_rule(
105                                wpcf7_swv_create_rule( 'required', array(
106                                        'field' => $tag->name,
107                                        'error' => wpcf7_get_message( 'invalid_required' ),
108                                ) )
109                        );
110                }
111
112                if ( $minlength = $tag->get_minlength_option() ) {
113                        $schema->add_rule(
114                                wpcf7_swv_create_rule( 'minlength', array(
115                                        'field' => $tag->name,
116                                        'threshold' => absint( $minlength ),
117                                        'error' => wpcf7_get_message( 'invalid_too_short' ),
118                                ) )
119                        );
120                }
121
122                if ( $maxlength = $tag->get_maxlength_option( '400' ) ) {
123                        $schema->add_rule(
124                                wpcf7_swv_create_rule( 'maxlength', array(
125                                        'field' => $tag->name,
126                                        'threshold' => absint( $maxlength ),
127                                        'error' => wpcf7_get_message( 'invalid_too_long' ),
128                                ) )
129                        );
130                }
131        }
132}
133
134
135/* Tag generator */
136
137add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_textarea', 20, 0 );
138
139function wpcf7_add_tag_generator_textarea() {
140        $tag_generator = WPCF7_TagGenerator::get_instance();
141        $tag_generator->add( 'textarea', __( 'text area', 'contact-form-7' ),
142                'wpcf7_tag_generator_textarea' );
143}
144
145function wpcf7_tag_generator_textarea( $contact_form, $args = '' ) {
146        $args = wp_parse_args( $args, array() );
147        $type = 'textarea';
148
149        $description = __( "Generate a form-tag for a multi-line text input field. For more details, see %s.", 'contact-form-7' );
150
151        $desc_link = wpcf7_link( __( 'https://contactform7.com/text-fields/', 'contact-form-7' ), __( 'Text fields', 'contact-form-7' ) );
152
153?>
154<div class="control-box">
155<fieldset>
156<legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend>
157
158<table class="form-table">
159<tbody>
160        <tr>
161        <th scope="row"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></th>
162        <td>
163                <fieldset>
164                <legend class="screen-reader-text"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></legend>
165                <label><input type="checkbox" name="required" /> <?php echo esc_html( __( 'Required field', 'contact-form-7' ) ); ?></label>
166                </fieldset>
167        </td>
168        </tr>
169
170        <tr>
171        <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th>
172        <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td>
173        </tr>
174
175        <tr>
176        <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-values' ); ?>"><?php echo esc_html( __( 'Default value', 'contact-form-7' ) ); ?></label></th>
177        <td><input type="text" name="values" class="oneline" id="<?php echo esc_attr( $args['content'] . '-values' ); ?>" /><br />
178        <label><input type="checkbox" name="placeholder" class="option" /> <?php echo esc_html( __( 'Use this text as the placeholder of the field', 'contact-form-7' ) ); ?></label></td>
179        </tr>
180
181        <tr>
182        <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th>
183        <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td>
184        </tr>
185
186        <tr>
187        <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th>
188        <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td>
189        </tr>
190
191</tbody>
192</table>
193</fieldset>
194</div>
195
196<div class="insert-box">
197        <input type="text" name="<?php echo $type; ?>" class="tag code" readonly="readonly" onfocus="this.select()" />
198
199        <div class="submitbox">
200        <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" />
201        </div>
202
203        <br class="clear" />
204
205        <p class="description mail-tag"><label for="<?php echo esc_attr( $args['content'] . '-mailtag' ); ?>"><?php echo sprintf( esc_html( __( "To use the value input through this field in a mail field, you need to insert the corresponding mail-tag (%s) into the field on the Mail tab.", 'contact-form-7' ) ), '<strong><span class="mail-tag"></span></strong>' ); ?><input type="text" class="mail-tag code hidden" readonly="readonly" id="<?php echo esc_attr( $args['content'] . '-mailtag' ); ?>" /></label></p>
206</div>
207<?php
208}
Note: See TracBrowser for help on using the repository browser.