/wp-content/plugins/contact-form-7/modules/textarea.php

https://github.com/electricjesus/dragonfood · PHP · 172 lines · 119 code · 47 blank · 6 comment · 20 complexity · 12bd6b703e51f7dad957f100d070d882 MD5 · raw file

  1. <?php
  2. /**
  3. ** A base module for [textarea] and [textarea*]
  4. **/
  5. /* Shortcode handler */
  6. wpcf7_add_shortcode( 'textarea', 'wpcf7_textarea_shortcode_handler', true );
  7. wpcf7_add_shortcode( 'textarea*', 'wpcf7_textarea_shortcode_handler', true );
  8. function wpcf7_textarea_shortcode_handler( $tag ) {
  9. if ( ! is_array( $tag ) )
  10. return '';
  11. $type = $tag['type'];
  12. $name = $tag['name'];
  13. $options = (array) $tag['options'];
  14. $values = (array) $tag['values'];
  15. $content = $tag['content'];
  16. if ( empty( $name ) )
  17. return '';
  18. $atts = '';
  19. $id_att = '';
  20. $class_att = '';
  21. $cols_att = '';
  22. $rows_att = '';
  23. $tabindex_att = '';
  24. $title_att = '';
  25. if ( 'textarea*' == $type )
  26. $class_att .= ' wpcf7-validates-as-required';
  27. foreach ( $options as $option ) {
  28. if ( preg_match( '%^id:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
  29. $id_att = $matches[1];
  30. } elseif ( preg_match( '%^class:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
  31. $class_att .= ' ' . $matches[1];
  32. } elseif ( preg_match( '%^([0-9]*)[x/]([0-9]*)$%', $option, $matches ) ) {
  33. $cols_att = (int) $matches[1];
  34. $rows_att = (int) $matches[2];
  35. } elseif ( preg_match( '%^tabindex:(\d+)$%', $option, $matches ) ) {
  36. $tabindex_att = (int) $matches[1];
  37. }
  38. }
  39. $value = (string) reset( $values );
  40. if ( ! empty( $content ) )
  41. $value = $content;
  42. if ( wpcf7_script_is() && preg_grep( '%^watermark$%', $options ) ) {
  43. $class_att .= ' wpcf7-use-title-as-watermark';
  44. $title_att .= sprintf( ' %s', $value );
  45. $value = '';
  46. }
  47. if ( wpcf7_is_posted() && isset( $_POST[$name] ) )
  48. $value = stripslashes_deep( $_POST[$name] );
  49. if ( $id_att )
  50. $atts .= ' id="' . trim( $id_att ) . '"';
  51. if ( $class_att )
  52. $atts .= ' class="' . trim( $class_att ) . '"';
  53. if ( $cols_att )
  54. $atts .= ' cols="' . $cols_att . '"';
  55. else
  56. $atts .= ' cols="40"'; // default size
  57. if ( $rows_att )
  58. $atts .= ' rows="' . $rows_att . '"';
  59. else
  60. $atts .= ' rows="10"'; // default size
  61. if ( '' !== $tabindex_att )
  62. $atts .= sprintf( ' tabindex="%d"', $tabindex_att );
  63. if ( $title_att )
  64. $atts .= sprintf( ' title="%s"', trim( esc_attr( $title_att ) ) );
  65. $html = '<textarea name="' . $name . '"' . $atts . '>' . esc_textarea( $value ) . '</textarea>';
  66. $validation_error = wpcf7_get_validation_error( $name );
  67. $html = '<span class="wpcf7-form-control-wrap ' . $name . '">' . $html . $validation_error . '</span>';
  68. return $html;
  69. }
  70. /* Validation filter */
  71. add_filter( 'wpcf7_validate_textarea', 'wpcf7_textarea_validation_filter', 10, 2 );
  72. add_filter( 'wpcf7_validate_textarea*', 'wpcf7_textarea_validation_filter', 10, 2 );
  73. function wpcf7_textarea_validation_filter( $result, $tag ) {
  74. $type = $tag['type'];
  75. $name = $tag['name'];
  76. $_POST[$name] = (string) $_POST[$name];
  77. if ( 'textarea*' == $type ) {
  78. if ( '' == $_POST[$name] ) {
  79. $result['valid'] = false;
  80. $result['reason'][$name] = wpcf7_get_message( 'invalid_required' );
  81. }
  82. }
  83. return $result;
  84. }
  85. /* Tag generator */
  86. add_action( 'admin_init', 'wpcf7_add_tag_generator_textarea', 20 );
  87. function wpcf7_add_tag_generator_textarea() {
  88. wpcf7_add_tag_generator( 'textarea', __( 'Text area', 'wpcf7' ),
  89. 'wpcf7-tg-pane-textarea', 'wpcf7_tg_pane_textarea' );
  90. }
  91. function wpcf7_tg_pane_textarea( &$contact_form ) {
  92. ?>
  93. <div id="wpcf7-tg-pane-textarea" class="hidden">
  94. <form action="">
  95. <table>
  96. <tr><td><input type="checkbox" name="required" />&nbsp;<?php echo esc_html( __( 'Required field?', 'wpcf7' ) ); ?></td></tr>
  97. <tr><td><?php echo esc_html( __( 'Name', 'wpcf7' ) ); ?><br /><input type="text" name="name" class="tg-name oneline" /></td><td></td></tr>
  98. </table>
  99. <table>
  100. <tr>
  101. <td><code>id</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
  102. <input type="text" name="id" class="idvalue oneline option" /></td>
  103. <td><code>class</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
  104. <input type="text" name="class" class="classvalue oneline option" /></td>
  105. </tr>
  106. <tr>
  107. <td><code>cols</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
  108. <input type="text" name="cols" class="numeric oneline option" /></td>
  109. <td><code>rows</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
  110. <input type="text" name="rows" class="numeric oneline option" /></td>
  111. </tr>
  112. <tr>
  113. <td><?php echo esc_html( __( 'Default value', 'wpcf7' ) ); ?> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br /><input type="text" name="values" class="oneline" /></td>
  114. <td>
  115. <br /><input type="checkbox" name="watermark" class="option" />&nbsp;<?php echo esc_html( __( 'Use this text as watermark?', 'wpcf7' ) ); ?>
  116. </td>
  117. </tr>
  118. </table>
  119. <div class="tg-tag"><?php echo esc_html( __( "Copy this code and paste it into the form left.", 'wpcf7' ) ); ?><br /><input type="text" name="textarea" class="tag" readonly="readonly" onfocus="this.select()" /></div>
  120. <div class="tg-mail-tag"><?php echo esc_html( __( "And, put this code into the Mail fields below.", 'wpcf7' ) ); ?><br /><span class="arrow">&#11015;</span>&nbsp;<input type="text" class="mail-tag" readonly="readonly" onfocus="this.select()" /></div>
  121. </form>
  122. </div>
  123. <?php
  124. }
  125. ?>