date.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. ** A base module for the following types of tags:
  4. ** [date] and [date*] # Date
  5. **/
  6. /* form_tag handler */
  7. add_action( 'wpcf7_init', 'wpcf7_add_form_tag_date', 10, 0 );
  8. function wpcf7_add_form_tag_date() {
  9. wpcf7_add_form_tag( array( 'date', 'date*' ),
  10. 'wpcf7_date_form_tag_handler', array( 'name-attr' => true ) );
  11. }
  12. function wpcf7_date_form_tag_handler( $tag ) {
  13. if ( empty( $tag->name ) ) {
  14. return '';
  15. }
  16. $validation_error = wpcf7_get_validation_error( $tag->name );
  17. $class = wpcf7_form_controls_class( $tag->type );
  18. $class .= ' wpcf7-validates-as-date';
  19. if ( $validation_error ) {
  20. $class .= ' wpcf7-not-valid';
  21. }
  22. $atts = array();
  23. $atts['class'] = $tag->get_class_option( $class );
  24. $atts['id'] = $tag->get_id_option();
  25. $atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true );
  26. $atts['min'] = $tag->get_date_option( 'min' );
  27. $atts['max'] = $tag->get_date_option( 'max' );
  28. $atts['step'] = $tag->get_option( 'step', 'int', true );
  29. if ( $tag->has_option( 'readonly' ) ) {
  30. $atts['readonly'] = 'readonly';
  31. }
  32. if ( $tag->is_required() ) {
  33. $atts['aria-required'] = 'true';
  34. }
  35. $atts['aria-invalid'] = $validation_error ? 'true' : 'false';
  36. $value = (string) reset( $tag->values );
  37. if ( $tag->has_option( 'placeholder' )
  38. or $tag->has_option( 'watermark' ) ) {
  39. $atts['placeholder'] = $value;
  40. $value = '';
  41. }
  42. $value = $tag->get_default_option( $value );
  43. $value = wpcf7_get_hangover( $tag->name, $value );
  44. $atts['value'] = $value;
  45. if ( wpcf7_support_html5() ) {
  46. $atts['type'] = $tag->basetype;
  47. } else {
  48. $atts['type'] = 'text';
  49. }
  50. $atts['name'] = $tag->name;
  51. $atts = wpcf7_format_atts( $atts );
  52. $html = sprintf(
  53. '<span class="wpcf7-form-control-wrap %1$s"><input %2$s />%3$s</span>',
  54. sanitize_html_class( $tag->name ), $atts, $validation_error );
  55. return $html;
  56. }
  57. /* Validation filter */
  58. add_filter( 'wpcf7_validate_date', 'wpcf7_date_validation_filter', 10, 2 );
  59. add_filter( 'wpcf7_validate_date*', 'wpcf7_date_validation_filter', 10, 2 );
  60. function wpcf7_date_validation_filter( $result, $tag ) {
  61. $name = $tag->name;
  62. $min = $tag->get_date_option( 'min' );
  63. $max = $tag->get_date_option( 'max' );
  64. $value = isset( $_POST[$name] )
  65. ? trim( strtr( (string) $_POST[$name], "\n", " " ) )
  66. : '';
  67. if ( $tag->is_required() and '' == $value ) {
  68. $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
  69. } elseif ( '' != $value and ! wpcf7_is_date( $value ) ) {
  70. $result->invalidate( $tag, wpcf7_get_message( 'invalid_date' ) );
  71. } elseif ( '' != $value and ! empty( $min ) and $value < $min ) {
  72. $result->invalidate( $tag, wpcf7_get_message( 'date_too_early' ) );
  73. } elseif ( '' != $value and ! empty( $max ) and $max < $value ) {
  74. $result->invalidate( $tag, wpcf7_get_message( 'date_too_late' ) );
  75. }
  76. return $result;
  77. }
  78. /* Messages */
  79. add_filter( 'wpcf7_messages', 'wpcf7_date_messages', 10, 1 );
  80. function wpcf7_date_messages( $messages ) {
  81. return array_merge( $messages, array(
  82. 'invalid_date' => array(
  83. 'description' => __( "Date format that the sender entered is invalid", 'contact-form-7' ),
  84. 'default' => __( "The date format is incorrect.", 'contact-form-7' )
  85. ),
  86. 'date_too_early' => array(
  87. 'description' => __( "Date is earlier than minimum limit", 'contact-form-7' ),
  88. 'default' => __( "The date is before the earliest one allowed.", 'contact-form-7' )
  89. ),
  90. 'date_too_late' => array(
  91. 'description' => __( "Date is later than maximum limit", 'contact-form-7' ),
  92. 'default' => __( "The date is after the latest one allowed.", 'contact-form-7' )
  93. ),
  94. ) );
  95. }
  96. /* Tag generator */
  97. add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_date', 19, 0 );
  98. function wpcf7_add_tag_generator_date() {
  99. $tag_generator = WPCF7_TagGenerator::get_instance();
  100. $tag_generator->add( 'date', __( 'date', 'contact-form-7' ),
  101. 'wpcf7_tag_generator_date' );
  102. }
  103. function wpcf7_tag_generator_date( $contact_form, $args = '' ) {
  104. $args = wp_parse_args( $args, array() );
  105. $type = 'date';
  106. $description = __( "Generate a form-tag for a date input field. For more details, see %s.", 'contact-form-7' );
  107. $desc_link = wpcf7_link( __( 'https://contactform7.com/date-field/', 'contact-form-7' ), __( 'Date Field', 'contact-form-7' ) );
  108. ?>
  109. <div class="control-box">
  110. <fieldset>
  111. <legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend>
  112. <table class="form-table">
  113. <tbody>
  114. <tr>
  115. <th scope="row"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></th>
  116. <td>
  117. <fieldset>
  118. <legend class="screen-reader-text"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></legend>
  119. <label><input type="checkbox" name="required" /> <?php echo esc_html( __( 'Required field', 'contact-form-7' ) ); ?></label>
  120. </fieldset>
  121. </td>
  122. </tr>
  123. <tr>
  124. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th>
  125. <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td>
  126. </tr>
  127. <tr>
  128. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-values' ); ?>"><?php echo esc_html( __( 'Default value', 'contact-form-7' ) ); ?></label></th>
  129. <td><input type="text" name="values" class="oneline" id="<?php echo esc_attr( $args['content'] . '-values' ); ?>" /><br />
  130. <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>
  131. </tr>
  132. <tr>
  133. <th scope="row"><?php echo esc_html( __( 'Range', 'contact-form-7' ) ); ?></th>
  134. <td>
  135. <fieldset>
  136. <legend class="screen-reader-text"><?php echo esc_html( __( 'Range', 'contact-form-7' ) ); ?></legend>
  137. <label>
  138. <?php echo esc_html( __( 'Min', 'contact-form-7' ) ); ?>
  139. <input type="date" name="min" class="date option" />
  140. </label>
  141. &ndash;
  142. <label>
  143. <?php echo esc_html( __( 'Max', 'contact-form-7' ) ); ?>
  144. <input type="date" name="max" class="date option" />
  145. </label>
  146. </fieldset>
  147. </td>
  148. </tr>
  149. <tr>
  150. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th>
  151. <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td>
  152. </tr>
  153. <tr>
  154. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th>
  155. <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td>
  156. </tr>
  157. </tbody>
  158. </table>
  159. </fieldset>
  160. </div>
  161. <div class="insert-box">
  162. <input type="text" name="<?php echo $type; ?>" class="tag code" readonly="readonly" onfocus="this.select()" />
  163. <div class="submitbox">
  164. <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" />
  165. </div>
  166. <br class="clear" />
  167. <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>
  168. </div>
  169. <?php
  170. }