text.php 11 KB

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