textarea.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. ** A base module for [textarea] and [textarea*]
  4. **/
  5. /* form_tag handler */
  6. add_action( 'wpcf7_init', 'wpcf7_add_form_tag_textarea', 10, 0 );
  7. function wpcf7_add_form_tag_textarea() {
  8. wpcf7_add_form_tag( array( 'textarea', 'textarea*' ),
  9. 'wpcf7_textarea_form_tag_handler', array( 'name-attr' => true ) );
  10. }
  11. function wpcf7_textarea_form_tag_handler( $tag ) {
  12. if ( empty( $tag->name ) ) {
  13. return '';
  14. }
  15. $validation_error = wpcf7_get_validation_error( $tag->name );
  16. $class = wpcf7_form_controls_class( $tag->type );
  17. if ( $validation_error ) {
  18. $class .= ' wpcf7-not-valid';
  19. }
  20. $atts = array();
  21. $atts['cols'] = $tag->get_cols_option( '40' );
  22. $atts['rows'] = $tag->get_rows_option( '10' );
  23. $atts['maxlength'] = $tag->get_maxlength_option();
  24. $atts['minlength'] = $tag->get_minlength_option();
  25. if ( $atts['maxlength'] and $atts['minlength']
  26. and $atts['maxlength'] < $atts['minlength'] ) {
  27. unset( $atts['maxlength'], $atts['minlength'] );
  28. }
  29. $atts['class'] = $tag->get_class_option( $class );
  30. $atts['id'] = $tag->get_id_option();
  31. $atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true );
  32. $atts['autocomplete'] = $tag->get_option( 'autocomplete',
  33. '[-0-9a-zA-Z]+', true );
  34. if ( $tag->has_option( 'readonly' ) ) {
  35. $atts['readonly'] = 'readonly';
  36. }
  37. if ( $tag->is_required() ) {
  38. $atts['aria-required'] = 'true';
  39. }
  40. $atts['aria-invalid'] = $validation_error ? 'true' : 'false';
  41. $value = empty( $tag->content )
  42. ? (string) reset( $tag->values )
  43. : $tag->content;
  44. if ( $tag->has_option( 'placeholder' )
  45. or $tag->has_option( 'watermark' ) ) {
  46. $atts['placeholder'] = $value;
  47. $value = '';
  48. }
  49. $value = $tag->get_default_option( $value );
  50. $value = wpcf7_get_hangover( $tag->name, $value );
  51. $atts['name'] = $tag->name;
  52. $atts = wpcf7_format_atts( $atts );
  53. $html = sprintf(
  54. '<span class="wpcf7-form-control-wrap %1$s"><textarea %2$s>%3$s</textarea>%4$s</span>',
  55. sanitize_html_class( $tag->name ), $atts,
  56. esc_textarea( $value ), $validation_error );
  57. return $html;
  58. }
  59. /* Validation filter */
  60. add_filter( 'wpcf7_validate_textarea',
  61. 'wpcf7_textarea_validation_filter', 10, 2 );
  62. add_filter( 'wpcf7_validate_textarea*',
  63. 'wpcf7_textarea_validation_filter', 10, 2 );
  64. function wpcf7_textarea_validation_filter( $result, $tag ) {
  65. $type = $tag->type;
  66. $name = $tag->name;
  67. $value = isset( $_POST[$name] ) ? (string) $_POST[$name] : '';
  68. if ( $tag->is_required() and '' == $value ) {
  69. $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
  70. }
  71. if ( '' !== $value ) {
  72. $maxlength = $tag->get_maxlength_option();
  73. $minlength = $tag->get_minlength_option();
  74. if ( $maxlength and $minlength
  75. and $maxlength < $minlength ) {
  76. $maxlength = $minlength = null;
  77. }
  78. $code_units = wpcf7_count_code_units( stripslashes( $value ) );
  79. if ( false !== $code_units ) {
  80. if ( $maxlength and $maxlength < $code_units ) {
  81. $result->invalidate( $tag, wpcf7_get_message( 'invalid_too_long' ) );
  82. } elseif ( $minlength and $code_units < $minlength ) {
  83. $result->invalidate( $tag, wpcf7_get_message( 'invalid_too_short' ) );
  84. }
  85. }
  86. }
  87. return $result;
  88. }
  89. /* Tag generator */
  90. add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_textarea', 20, 0 );
  91. function wpcf7_add_tag_generator_textarea() {
  92. $tag_generator = WPCF7_TagGenerator::get_instance();
  93. $tag_generator->add( 'textarea', __( 'text area', 'contact-form-7' ),
  94. 'wpcf7_tag_generator_textarea' );
  95. }
  96. function wpcf7_tag_generator_textarea( $contact_form, $args = '' ) {
  97. $args = wp_parse_args( $args, array() );
  98. $type = 'textarea';
  99. $description = __( "Generate a form-tag for a multi-line text input field. For more details, see %s.", 'contact-form-7' );
  100. $desc_link = wpcf7_link( __( 'https://contactform7.com/text-fields/', 'contact-form-7' ), __( 'Text Fields', 'contact-form-7' ) );
  101. ?>
  102. <div class="control-box">
  103. <fieldset>
  104. <legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend>
  105. <table class="form-table">
  106. <tbody>
  107. <tr>
  108. <th scope="row"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></th>
  109. <td>
  110. <fieldset>
  111. <legend class="screen-reader-text"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></legend>
  112. <label><input type="checkbox" name="required" /> <?php echo esc_html( __( 'Required field', 'contact-form-7' ) ); ?></label>
  113. </fieldset>
  114. </td>
  115. </tr>
  116. <tr>
  117. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th>
  118. <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td>
  119. </tr>
  120. <tr>
  121. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-values' ); ?>"><?php echo esc_html( __( 'Default value', 'contact-form-7' ) ); ?></label></th>
  122. <td><input type="text" name="values" class="oneline" id="<?php echo esc_attr( $args['content'] . '-values' ); ?>" /><br />
  123. <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>
  124. </tr>
  125. <tr>
  126. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th>
  127. <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td>
  128. </tr>
  129. <tr>
  130. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th>
  131. <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td>
  132. </tr>
  133. </tbody>
  134. </table>
  135. </fieldset>
  136. </div>
  137. <div class="insert-box">
  138. <input type="text" name="<?php echo $type; ?>" class="tag code" readonly="readonly" onfocus="this.select()" />
  139. <div class="submitbox">
  140. <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" />
  141. </div>
  142. <br class="clear" />
  143. <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>
  144. </div>
  145. <?php
  146. }