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

https://github.com/Canuckaholic/Pop-Digital · PHP · 206 lines · 141 code · 57 blank · 8 comment · 23 complexity · 78b2854cf41ba5436e4f93293c036fc6 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( 'wpcf7_init', 'wpcf7_add_shortcode_date' );
  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_id_option();
  24. $atts['tabindex'] = $tag->get_option( 'tabindex', 'int', true );
  25. $atts['min'] = $tag->get_date_option( 'min' );
  26. $atts['max'] = $tag->get_date_option( 'max' );
  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. $atts['aria-invalid'] = $validation_error ? 'true' : 'false';
  33. $value = (string) reset( $tag->values );
  34. if ( $tag->has_option( 'placeholder' ) || $tag->has_option( 'watermark' ) ) {
  35. $atts['placeholder'] = $value;
  36. $value = '';
  37. }
  38. $value = wpcf7_get_hangover( $tag->name, $value );
  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. sanitize_html_class( $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_date_option( 'min' );
  59. $max = $tag->get_date_option( 'max' );
  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. if ( isset( $result['reason'][$name] ) && $id = $tag->get_id_option() ) {
  77. $result['idref'][$name] = $id;
  78. }
  79. return $result;
  80. }
  81. /* Messages */
  82. add_filter( 'wpcf7_messages', 'wpcf7_date_messages' );
  83. function wpcf7_date_messages( $messages ) {
  84. return array_merge( $messages, array(
  85. 'invalid_date' => array(
  86. 'description' => __( "Date format that the sender entered is invalid", 'contact-form-7' ),
  87. 'default' => __( 'Date format seems invalid.', 'contact-form-7' )
  88. ),
  89. 'date_too_early' => array(
  90. 'description' => __( "Date is earlier than minimum limit", 'contact-form-7' ),
  91. 'default' => __( 'This date is too early.', 'contact-form-7' )
  92. ),
  93. 'date_too_late' => array(
  94. 'description' => __( "Date is later than maximum limit", 'contact-form-7' ),
  95. 'default' => __( 'This date is too late.', 'contact-form-7' )
  96. ) ) );
  97. }
  98. /* Tag generator */
  99. add_action( 'admin_init', 'wpcf7_add_tag_generator_date', 19 );
  100. function wpcf7_add_tag_generator_date() {
  101. if ( ! function_exists( 'wpcf7_add_tag_generator' ) )
  102. return;
  103. wpcf7_add_tag_generator( 'date', __( 'Date', 'contact-form-7' ),
  104. 'wpcf7-tg-pane-date', 'wpcf7_tg_pane_date' );
  105. }
  106. function wpcf7_tg_pane_date( $contact_form ) {
  107. wpcf7_tg_pane_date_and_relatives( 'date' );
  108. }
  109. function wpcf7_tg_pane_date_and_relatives( $type = 'date' ) {
  110. if ( ! in_array( $type, array() ) )
  111. $type = 'date';
  112. ?>
  113. <div id="wpcf7-tg-pane-<?php echo $type; ?>" class="hidden">
  114. <form action="">
  115. <table>
  116. <tr><td><input type="checkbox" name="required" />&nbsp;<?php echo esc_html( __( 'Required field?', 'contact-form-7' ) ); ?></td></tr>
  117. <tr><td><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?><br /><input type="text" name="name" class="tg-name oneline" /></td><td></td></tr>
  118. </table>
  119. <table>
  120. <tr>
  121. <td><code>id</code> (<?php echo esc_html( __( 'optional', 'contact-form-7' ) ); ?>)<br />
  122. <input type="text" name="id" class="idvalue oneline option" /></td>
  123. <td><code>class</code> (<?php echo esc_html( __( 'optional', 'contact-form-7' ) ); ?>)<br />
  124. <input type="text" name="class" class="classvalue oneline option" /></td>
  125. </tr>
  126. <tr>
  127. <td><code>min</code> (<?php echo esc_html( __( 'optional', 'contact-form-7' ) ); ?>)<br />
  128. <input type="date" name="min" class="date oneline option" /></td>
  129. <td><code>max</code> (<?php echo esc_html( __( 'optional', 'contact-form-7' ) ); ?>)<br />
  130. <input type="date" name="max" class="date oneline option" /></td>
  131. </tr>
  132. <tr>
  133. <td><code>step</code> (<?php echo esc_html( __( 'optional', 'contact-form-7' ) ); ?>)<br />
  134. <input type="number" name="step" class="numeric oneline option" min="1" /></td>
  135. </tr>
  136. <tr>
  137. <td><?php echo esc_html( __( 'Default value', 'contact-form-7' ) ); ?> (<?php echo esc_html( __( 'optional', 'contact-form-7' ) ); ?>)<br /><input type="text" name="values" class="oneline" /></td>
  138. <td>
  139. <br /><input type="checkbox" name="placeholder" class="option" />&nbsp;<?php echo esc_html( __( 'Use this text as placeholder?', 'contact-form-7' ) ); ?>
  140. </td>
  141. </tr>
  142. </table>
  143. <div class="tg-tag"><?php echo esc_html( __( "Copy this code and paste it into the form left.", 'contact-form-7' ) ); ?><br /><input type="text" name="<?php echo $type; ?>" class="tag wp-ui-text-highlight code" readonly="readonly" onfocus="this.select()" /></div>
  144. <div class="tg-mail-tag"><?php echo esc_html( __( "And, put this code into the Mail fields below.", 'contact-form-7' ) ); ?><br /><input type="text" class="mail-tag wp-ui-text-highlight code" readonly="readonly" onfocus="this.select()" /></div>
  145. </form>
  146. </div>
  147. <?php
  148. }
  149. ?>