PageRenderTime 26ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

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

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