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

https://bitbucket.org/rossberenson/michael-karas · PHP · 201 lines · 138 code · 55 blank · 8 comment · 23 complexity · cc255e6f97aa4e96512c89bfc7d00dfb MD5 · raw file

  1. <?php
  2. /**
  3. ** A base module for the following types of tags:
  4. ** [date] and [date*] # Date
  5. **/
  6. /* Shortcode handler */
  7. add_action( 'init', 'wpcf7_add_shortcode_date', 5 );
  8. function wpcf7_add_shortcode_date() {
  9. wpcf7_add_shortcode( array( 'date', 'date*' ),
  10. 'wpcf7_date_shortcode_handler', true );
  11. }
  12. function wpcf7_date_shortcode_handler( $tag ) {
  13. $tag = new WPCF7_Shortcode( $tag );
  14. if ( empty( $tag->name ) )
  15. return '';
  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. $atts = array();
  22. $atts['class'] = $tag->get_class_option( $class );
  23. $atts['id'] = $tag->get_option( 'id', 'id', true );
  24. $atts['tabindex'] = $tag->get_option( 'tabindex', 'int', true );
  25. $atts['min'] = $tag->get_option( 'min', 'date', true );
  26. $atts['max'] = $tag->get_option( 'max', 'date', true );
  27. $atts['step'] = $tag->get_option( 'step', 'int', true );
  28. if ( $tag->has_option( 'readonly' ) )
  29. $atts['readonly'] = 'readonly';
  30. if ( $tag->is_required() )
  31. $atts['aria-required'] = 'true';
  32. $value = (string) reset( $tag->values );
  33. if ( $tag->has_option( 'placeholder' ) || $tag->has_option( 'watermark' ) ) {
  34. $atts['placeholder'] = $value;
  35. $value = '';
  36. }
  37. if ( wpcf7_is_posted() && isset( $_POST[$tag->name] ) )
  38. $value = stripslashes_deep( $_POST[$tag->name] );
  39. $atts['value'] = $value;
  40. if ( wpcf7_support_html5() ) {
  41. $atts['type'] = $tag->basetype;
  42. } else {
  43. $atts['type'] = 'text';
  44. }
  45. $atts['name'] = $tag->name;
  46. $atts = wpcf7_format_atts( $atts );
  47. $html = sprintf(
  48. '<span class="wpcf7-form-control-wrap %1$s"><input %2$s />%3$s</span>',
  49. $tag->name, $atts, $validation_error );
  50. return $html;
  51. }
  52. /* Validation filter */
  53. add_filter( 'wpcf7_validate_date', 'wpcf7_date_validation_filter', 10, 2 );
  54. add_filter( 'wpcf7_validate_date*', 'wpcf7_date_validation_filter', 10, 2 );
  55. function wpcf7_date_validation_filter( $result, $tag ) {
  56. $tag = new WPCF7_Shortcode( $tag );
  57. $name = $tag->name;
  58. $min = $tag->get_option( 'min', 'date', true );
  59. $max = $tag->get_option( 'max', 'date', true );
  60. $value = isset( $_POST[$name] )
  61. ? trim( strtr( (string) $_POST[$name], "\n", " " ) )
  62. : '';
  63. if ( $tag->is_required() && '' == $value ) {
  64. $result['valid'] = false;
  65. $result['reason'][$name] = wpcf7_get_message( 'invalid_required' );
  66. } elseif ( '' != $value && ! wpcf7_is_date( $value ) ) {
  67. $result['valid'] = false;
  68. $result['reason'][$name] = wpcf7_get_message( 'invalid_date' );
  69. } elseif ( '' != $value && ! empty( $min ) && $value < $min ) {
  70. $result['valid'] = false;
  71. $result['reason'][$name] = wpcf7_get_message( 'date_too_early' );
  72. } elseif ( '' != $value && ! empty( $max ) && $max < $value ) {
  73. $result['valid'] = false;
  74. $result['reason'][$name] = wpcf7_get_message( 'date_too_late' );
  75. }
  76. return $result;
  77. }
  78. /* Messages */
  79. add_filter( 'wpcf7_messages', 'wpcf7_date_messages' );
  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", 'wpcf7' ),
  84. 'default' => __( 'Date format seems invalid.', 'wpcf7' )
  85. ),
  86. 'date_too_early' => array(
  87. 'description' => __( "Date is earlier than minimum limit", 'wpcf7' ),
  88. 'default' => __( 'This date is too early.', 'wpcf7' )
  89. ),
  90. 'date_too_late' => array(
  91. 'description' => __( "Date is later than maximum limit", 'wpcf7' ),
  92. 'default' => __( 'This date is too late.', 'wpcf7' )
  93. ) ) );
  94. }
  95. /* Tag generator */
  96. add_action( 'admin_init', 'wpcf7_add_tag_generator_date', 19 );
  97. function wpcf7_add_tag_generator_date() {
  98. if ( ! function_exists( 'wpcf7_add_tag_generator' ) )
  99. return;
  100. wpcf7_add_tag_generator( 'date', __( 'Date', 'wpcf7' ),
  101. 'wpcf7-tg-pane-date', 'wpcf7_tg_pane_date' );
  102. }
  103. function wpcf7_tg_pane_date( &$contact_form ) {
  104. wpcf7_tg_pane_date_and_relatives( 'date' );
  105. }
  106. function wpcf7_tg_pane_date_and_relatives( $type = 'date' ) {
  107. if ( ! in_array( $type, array() ) )
  108. $type = 'date';
  109. ?>
  110. <div id="wpcf7-tg-pane-<?php echo $type; ?>" class="hidden">
  111. <form action="">
  112. <table>
  113. <tr><td><input type="checkbox" name="required" />&nbsp;<?php echo esc_html( __( 'Required field?', 'wpcf7' ) ); ?></td></tr>
  114. <tr><td><?php echo esc_html( __( 'Name', 'wpcf7' ) ); ?><br /><input type="text" name="name" class="tg-name oneline" /></td><td></td></tr>
  115. </table>
  116. <table>
  117. <tr>
  118. <td><code>id</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
  119. <input type="text" name="id" class="idvalue oneline option" /></td>
  120. <td><code>class</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
  121. <input type="text" name="class" class="classvalue oneline option" /></td>
  122. </tr>
  123. <tr>
  124. <td><code>min</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
  125. <input type="date" name="min" class="date oneline option" /></td>
  126. <td><code>max</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
  127. <input type="date" name="max" class="date oneline option" /></td>
  128. </tr>
  129. <tr>
  130. <td><code>step</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
  131. <input type="number" name="step" class="numeric oneline option" min="1" /></td>
  132. </tr>
  133. <tr>
  134. <td><?php echo esc_html( __( 'Default value', 'wpcf7' ) ); ?> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br /><input type="text" name="values" class="oneline" /></td>
  135. <td>
  136. <br /><input type="checkbox" name="placeholder" class="option" />&nbsp;<?php echo esc_html( __( 'Use this text as placeholder?', 'wpcf7' ) ); ?>
  137. </td>
  138. </tr>
  139. </table>
  140. <div class="tg-tag"><?php echo esc_html( __( "Copy this code and paste it into the form left.", 'wpcf7' ) ); ?><br /><input type="text" name="<?php echo $type; ?>" class="tag" readonly="readonly" onfocus="this.select()" /></div>
  141. <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>
  142. </form>
  143. </div>
  144. <?php
  145. }
  146. ?>