akismet
11 months ago
constant-contact
9 months ago
recaptcha
9 months ago
sendinblue
9 months ago
stripe
9 months ago
turnstile
9 months ago
acceptance.php
11 months ago
checkbox.php
9 months ago
count.php
1 year ago
date.php
9 months ago
disallowed-list.php
9 months ago
doi-helper.php
4 years ago
file.php
11 months ago
flamingo.php
9 months ago
hidden.php
7 years ago
listo.php
2 years ago
number.php
9 months ago
quiz.php
11 months ago
really-simple-captcha.php
9 months ago
reflection.php
3 years ago
response.php
6 years ago
select.php
9 months ago
submit.php
11 months ago
text.php
9 months ago
textarea.php
11 months ago
number.php
286 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 | |
| 204 | $tag_generator->add( 'number', __( 'number', 'contact-form-7' ), |
| 205 | 'wpcf7_tag_generator_number', |
| 206 | array( 'version' => '2' ) |
| 207 | ); |
| 208 | } |
| 209 | |
| 210 | function wpcf7_tag_generator_number( $contact_form, $options ) { |
| 211 | $field_types = array( |
| 212 | 'number' => array( |
| 213 | 'display_name' => __( 'Number field', 'contact-form-7' ), |
| 214 | 'heading' => __( 'Number field form-tag generator', 'contact-form-7' ), |
| 215 | 'description' => __( 'Generates a form-tag for a <a href="https://contactform7.com/number-fields/">number input field</a>.', 'contact-form-7' ), |
| 216 | ), |
| 217 | ); |
| 218 | |
| 219 | $tgg = new WPCF7_TagGeneratorGenerator( $options['content'] ); |
| 220 | |
| 221 | $formatter = new WPCF7_HTMLFormatter(); |
| 222 | |
| 223 | $formatter->append_start_tag( 'header', array( |
| 224 | 'class' => 'description-box', |
| 225 | ) ); |
| 226 | |
| 227 | $formatter->append_start_tag( 'h3' ); |
| 228 | |
| 229 | $formatter->append_preformatted( |
| 230 | esc_html( $field_types['number']['heading'] ) |
| 231 | ); |
| 232 | |
| 233 | $formatter->end_tag( 'h3' ); |
| 234 | |
| 235 | $formatter->append_start_tag( 'p' ); |
| 236 | |
| 237 | $formatter->append_preformatted( |
| 238 | wp_kses_data( $field_types['number']['description'] ) |
| 239 | ); |
| 240 | |
| 241 | $formatter->end_tag( 'header' ); |
| 242 | |
| 243 | $formatter->append_start_tag( 'div', array( |
| 244 | 'class' => 'control-box', |
| 245 | ) ); |
| 246 | |
| 247 | $formatter->call_user_func( static function () use ( $tgg, $field_types ) { |
| 248 | $tgg->print( 'field_type', array( |
| 249 | 'with_required' => true, |
| 250 | 'select_options' => array( |
| 251 | 'number' => __( 'Spinbox', 'contact-form-7' ), |
| 252 | 'range' => __( 'Slider', 'contact-form-7' ), |
| 253 | ), |
| 254 | ) ); |
| 255 | |
| 256 | $tgg->print( 'field_name' ); |
| 257 | |
| 258 | $tgg->print( 'class_attr' ); |
| 259 | |
| 260 | $tgg->print( 'min_max', array( |
| 261 | 'title' => __( 'Range', 'contact-form-7' ), |
| 262 | 'min_option' => 'min:', |
| 263 | 'max_option' => 'max:', |
| 264 | ) ); |
| 265 | |
| 266 | $tgg->print( 'default_value', array( |
| 267 | 'type' => 'number', |
| 268 | 'with_placeholder' => false, |
| 269 | ) ); |
| 270 | } ); |
| 271 | |
| 272 | $formatter->end_tag( 'div' ); |
| 273 | |
| 274 | $formatter->append_start_tag( 'footer', array( |
| 275 | 'class' => 'insert-box', |
| 276 | ) ); |
| 277 | |
| 278 | $formatter->call_user_func( static function () use ( $tgg, $field_types ) { |
| 279 | $tgg->print( 'insert_box_content' ); |
| 280 | |
| 281 | $tgg->print( 'mail_tag_tip' ); |
| 282 | } ); |
| 283 | |
| 284 | $formatter->print(); |
| 285 | } |
| 286 |