akismet
2 years ago
constant-contact
2 years ago
recaptcha
2 years ago
sendinblue
2 years ago
stripe
2 years ago
acceptance.php
3 years ago
checkbox.php
3 years ago
count.php
3 years ago
date.php
3 years ago
disallowed-list.php
4 years ago
doi-helper.php
4 years ago
file.php
3 years ago
flamingo.php
3 years ago
hidden.php
7 years ago
listo.php
3 years ago
number.php
3 years ago
quiz.php
4 years ago
really-simple-captcha.php
3 years ago
reflection.php
3 years ago
response.php
6 years ago
select.php
3 years ago
submit.php
4 years ago
text.php
3 years ago
textarea.php
3 years ago
number.php
293 lines
| 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 | function wpcf7_add_form_tag_number() { |
| 13 | wpcf7_add_form_tag( array( 'number', 'number*', 'range', 'range*' ), |
| 14 | 'wpcf7_number_form_tag_handler', |
| 15 | array( |
| 16 | 'name-attr' => true, |
| 17 | ) |
| 18 | ); |
| 19 | } |
| 20 | |
| 21 | function wpcf7_number_form_tag_handler( $tag ) { |
| 22 | if ( empty( $tag->name ) ) { |
| 23 | return ''; |
| 24 | } |
| 25 | |
| 26 | $validation_error = wpcf7_get_validation_error( $tag->name ); |
| 27 | |
| 28 | $class = wpcf7_form_controls_class( $tag->type ); |
| 29 | |
| 30 | $class .= ' wpcf7-validates-as-number'; |
| 31 | |
| 32 | if ( $validation_error ) { |
| 33 | $class .= ' wpcf7-not-valid'; |
| 34 | } |
| 35 | |
| 36 | $atts = array(); |
| 37 | |
| 38 | $atts['class'] = $tag->get_class_option( $class ); |
| 39 | $atts['id'] = $tag->get_id_option(); |
| 40 | $atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true ); |
| 41 | $atts['min'] = $tag->get_option( 'min', 'signed_num', true ); |
| 42 | $atts['max'] = $tag->get_option( 'max', 'signed_num', true ); |
| 43 | $atts['step'] = $tag->get_option( 'step', 'num', true ); |
| 44 | $atts['readonly'] = $tag->has_option( 'readonly' ); |
| 45 | |
| 46 | $atts['autocomplete'] = $tag->get_option( |
| 47 | 'autocomplete', '[-0-9a-zA-Z]+', true |
| 48 | ); |
| 49 | |
| 50 | if ( $tag->is_required() ) { |
| 51 | $atts['aria-required'] = 'true'; |
| 52 | } |
| 53 | |
| 54 | if ( $validation_error ) { |
| 55 | $atts['aria-invalid'] = 'true'; |
| 56 | $atts['aria-describedby'] = wpcf7_get_validation_error_reference( |
| 57 | $tag->name |
| 58 | ); |
| 59 | } else { |
| 60 | $atts['aria-invalid'] = 'false'; |
| 61 | } |
| 62 | |
| 63 | $value = (string) reset( $tag->values ); |
| 64 | |
| 65 | if ( $tag->has_option( 'placeholder' ) |
| 66 | or $tag->has_option( 'watermark' ) ) { |
| 67 | $atts['placeholder'] = $value; |
| 68 | $value = ''; |
| 69 | } |
| 70 | |
| 71 | $value = $tag->get_default_option( $value ); |
| 72 | |
| 73 | $value = wpcf7_get_hangover( $tag->name, $value ); |
| 74 | |
| 75 | $atts['value'] = $value; |
| 76 | |
| 77 | if ( 'range' === $tag->basetype ) { |
| 78 | if ( ! wpcf7_is_number( $atts['min'] ) ) { |
| 79 | $atts['min'] = '0'; |
| 80 | } |
| 81 | |
| 82 | if ( ! wpcf7_is_number( $atts['max'] ) ) { |
| 83 | $atts['max'] = '100'; |
| 84 | } |
| 85 | |
| 86 | if ( '' === $atts['value'] ) { |
| 87 | if ( $atts['min'] < $atts['max'] ) { |
| 88 | $atts['value'] = ( $atts['min'] + $atts['max'] ) / 2; |
| 89 | } else { |
| 90 | $atts['value'] = $atts['min']; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | $atts['type'] = $tag->basetype; |
| 96 | $atts['name'] = $tag->name; |
| 97 | |
| 98 | $html = sprintf( |
| 99 | '<span class="wpcf7-form-control-wrap" data-name="%1$s"><input %2$s />%3$s</span>', |
| 100 | esc_attr( $tag->name ), |
| 101 | wpcf7_format_atts( $atts ), |
| 102 | $validation_error |
| 103 | ); |
| 104 | |
| 105 | return $html; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | add_action( |
| 110 | 'wpcf7_swv_create_schema', |
| 111 | 'wpcf7_swv_add_number_rules', |
| 112 | 10, 2 |
| 113 | ); |
| 114 | |
| 115 | function wpcf7_swv_add_number_rules( $schema, $contact_form ) { |
| 116 | $tags = $contact_form->scan_form_tags( array( |
| 117 | 'basetype' => array( 'number', 'range' ), |
| 118 | ) ); |
| 119 | |
| 120 | foreach ( $tags as $tag ) { |
| 121 | if ( $tag->is_required() ) { |
| 122 | $schema->add_rule( |
| 123 | wpcf7_swv_create_rule( 'required', array( |
| 124 | 'field' => $tag->name, |
| 125 | 'error' => wpcf7_get_message( 'invalid_required' ), |
| 126 | ) ) |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | $schema->add_rule( |
| 131 | wpcf7_swv_create_rule( 'number', array( |
| 132 | 'field' => $tag->name, |
| 133 | 'error' => wpcf7_get_message( 'invalid_number' ), |
| 134 | ) ) |
| 135 | ); |
| 136 | |
| 137 | $min = $tag->get_option( 'min', 'signed_num', true ); |
| 138 | $max = $tag->get_option( 'max', 'signed_num', true ); |
| 139 | |
| 140 | if ( 'range' === $tag->basetype ) { |
| 141 | if ( ! wpcf7_is_number( $min ) ) { |
| 142 | $min = '0'; |
| 143 | } |
| 144 | |
| 145 | if ( ! wpcf7_is_number( $max ) ) { |
| 146 | $max = '100'; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | if ( wpcf7_is_number( $min ) ) { |
| 151 | $schema->add_rule( |
| 152 | wpcf7_swv_create_rule( 'minnumber', array( |
| 153 | 'field' => $tag->name, |
| 154 | 'threshold' => $min, |
| 155 | 'error' => wpcf7_get_message( 'number_too_small' ), |
| 156 | ) ) |
| 157 | ); |
| 158 | } |
| 159 | |
| 160 | if ( wpcf7_is_number( $max ) ) { |
| 161 | $schema->add_rule( |
| 162 | wpcf7_swv_create_rule( 'maxnumber', array( |
| 163 | 'field' => $tag->name, |
| 164 | 'threshold' => $max, |
| 165 | 'error' => wpcf7_get_message( 'number_too_large' ), |
| 166 | ) ) |
| 167 | ); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | |
| 173 | /* Messages */ |
| 174 | |
| 175 | add_filter( 'wpcf7_messages', 'wpcf7_number_messages', 10, 1 ); |
| 176 | |
| 177 | function wpcf7_number_messages( $messages ) { |
| 178 | return array_merge( $messages, array( |
| 179 | 'invalid_number' => array( |
| 180 | 'description' => __( "Number format that the sender entered is invalid", 'contact-form-7' ), |
| 181 | 'default' => __( "Please enter a number.", 'contact-form-7' ), |
| 182 | ), |
| 183 | |
| 184 | 'number_too_small' => array( |
| 185 | 'description' => __( "Number is smaller than minimum limit", 'contact-form-7' ), |
| 186 | 'default' => __( "This field has a too small number.", 'contact-form-7' ), |
| 187 | ), |
| 188 | |
| 189 | 'number_too_large' => array( |
| 190 | 'description' => __( "Number is larger than maximum limit", 'contact-form-7' ), |
| 191 | 'default' => __( "This field has a too large number.", 'contact-form-7' ), |
| 192 | ), |
| 193 | ) ); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | /* Tag generator */ |
| 198 | |
| 199 | add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_number', 18, 0 ); |
| 200 | |
| 201 | function wpcf7_add_tag_generator_number() { |
| 202 | $tag_generator = WPCF7_TagGenerator::get_instance(); |
| 203 | $tag_generator->add( 'number', __( 'number', 'contact-form-7' ), |
| 204 | 'wpcf7_tag_generator_number' ); |
| 205 | } |
| 206 | |
| 207 | function wpcf7_tag_generator_number( $contact_form, $args = '' ) { |
| 208 | $args = wp_parse_args( $args, array() ); |
| 209 | $type = 'number'; |
| 210 | |
| 211 | $description = __( "Generate a form-tag for a field for numeric value input. For more details, see %s.", 'contact-form-7' ); |
| 212 | |
| 213 | $desc_link = wpcf7_link( __( 'https://contactform7.com/number-fields/', 'contact-form-7' ), __( 'Number fields', 'contact-form-7' ) ); |
| 214 | |
| 215 | ?> |
| 216 | <div class="control-box"> |
| 217 | <fieldset> |
| 218 | <legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend> |
| 219 | |
| 220 | <table class="form-table"> |
| 221 | <tbody> |
| 222 | <tr> |
| 223 | <th scope="row"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></th> |
| 224 | <td> |
| 225 | <fieldset> |
| 226 | <legend class="screen-reader-text"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></legend> |
| 227 | <select name="tagtype"> |
| 228 | <option value="number" selected="selected"><?php echo esc_html( __( 'Spinbox', 'contact-form-7' ) ); ?></option> |
| 229 | <option value="range"><?php echo esc_html( __( 'Slider', 'contact-form-7' ) ); ?></option> |
| 230 | </select> |
| 231 | <br /> |
| 232 | <label><input type="checkbox" name="required" /> <?php echo esc_html( __( 'Required field', 'contact-form-7' ) ); ?></label> |
| 233 | </fieldset> |
| 234 | </td> |
| 235 | </tr> |
| 236 | |
| 237 | <tr> |
| 238 | <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th> |
| 239 | <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td> |
| 240 | </tr> |
| 241 | |
| 242 | <tr> |
| 243 | <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-values' ); ?>"><?php echo esc_html( __( 'Default value', 'contact-form-7' ) ); ?></label></th> |
| 244 | <td><input type="text" name="values" class="oneline" id="<?php echo esc_attr( $args['content'] . '-values' ); ?>" /><br /> |
| 245 | <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> |
| 246 | </tr> |
| 247 | |
| 248 | <tr> |
| 249 | <th scope="row"><?php echo esc_html( __( 'Range', 'contact-form-7' ) ); ?></th> |
| 250 | <td> |
| 251 | <fieldset> |
| 252 | <legend class="screen-reader-text"><?php echo esc_html( __( 'Range', 'contact-form-7' ) ); ?></legend> |
| 253 | <label> |
| 254 | <?php echo esc_html( __( 'Min', 'contact-form-7' ) ); ?> |
| 255 | <input type="number" name="min" class="numeric option" /> |
| 256 | </label> |
| 257 | – |
| 258 | <label> |
| 259 | <?php echo esc_html( __( 'Max', 'contact-form-7' ) ); ?> |
| 260 | <input type="number" name="max" class="numeric option" /> |
| 261 | </label> |
| 262 | </fieldset> |
| 263 | </td> |
| 264 | </tr> |
| 265 | |
| 266 | <tr> |
| 267 | <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th> |
| 268 | <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td> |
| 269 | </tr> |
| 270 | |
| 271 | <tr> |
| 272 | <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th> |
| 273 | <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td> |
| 274 | </tr> |
| 275 | </tbody> |
| 276 | </table> |
| 277 | </fieldset> |
| 278 | </div> |
| 279 | |
| 280 | <div class="insert-box"> |
| 281 | <input type="text" name="<?php echo $type; ?>" class="tag code" readonly="readonly" onfocus="this.select()" /> |
| 282 | |
| 283 | <div class="submitbox"> |
| 284 | <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" /> |
| 285 | </div> |
| 286 | |
| 287 | <br class="clear" /> |
| 288 | |
| 289 | <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> |
| 290 | </div> |
| 291 | <?php |
| 292 | } |
| 293 |