akismet
11 months ago
constant-contact
9 months ago
recaptcha
9 months ago
sendinblue
9 months ago
stripe
9 months ago
turnstile
8 months ago
acceptance.php
11 months ago
checkbox.php
9 months ago
count.php
1 year ago
date.php
7 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
7 months ago
quiz.php
11 months ago
really-simple-captcha.php
6 months ago
reflection.php
3 years ago
response.php
6 years ago
select.php
7 months ago
submit.php
11 months ago
text.php
7 months ago
textarea.php
7 months ago
number.php
282 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 | $atts['autocomplete'] = $tag->get_autocomplete_option(); |
| 46 | |
| 47 | if ( $tag->is_required() ) { |
| 48 | $atts['aria-required'] = 'true'; |
| 49 | } |
| 50 | |
| 51 | if ( $validation_error ) { |
| 52 | $atts['aria-invalid'] = 'true'; |
| 53 | $atts['aria-describedby'] = wpcf7_get_validation_error_reference( |
| 54 | $tag->name |
| 55 | ); |
| 56 | } else { |
| 57 | $atts['aria-invalid'] = 'false'; |
| 58 | } |
| 59 | |
| 60 | $value = (string) reset( $tag->values ); |
| 61 | |
| 62 | if ( $tag->has_option( 'placeholder' ) or $tag->has_option( 'watermark' ) ) { |
| 63 | $atts['placeholder'] = $value; |
| 64 | $value = ''; |
| 65 | } |
| 66 | |
| 67 | $value = $tag->get_default_option( $value ); |
| 68 | |
| 69 | $value = wpcf7_get_hangover( $tag->name, $value ); |
| 70 | |
| 71 | $atts['value'] = $value; |
| 72 | |
| 73 | if ( 'range' === $tag->basetype ) { |
| 74 | if ( ! wpcf7_is_number( $atts['min'] ) ) { |
| 75 | $atts['min'] = '0'; |
| 76 | } |
| 77 | |
| 78 | if ( ! wpcf7_is_number( $atts['max'] ) ) { |
| 79 | $atts['max'] = '100'; |
| 80 | } |
| 81 | |
| 82 | if ( '' === $atts['value'] ) { |
| 83 | if ( $atts['min'] < $atts['max'] ) { |
| 84 | $atts['value'] = ( $atts['min'] + $atts['max'] ) / 2; |
| 85 | } else { |
| 86 | $atts['value'] = $atts['min']; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | $atts['type'] = $tag->basetype; |
| 92 | $atts['name'] = $tag->name; |
| 93 | |
| 94 | $html = sprintf( |
| 95 | '<span class="wpcf7-form-control-wrap" data-name="%1$s"><input %2$s />%3$s</span>', |
| 96 | esc_attr( $tag->name ), |
| 97 | wpcf7_format_atts( $atts ), |
| 98 | $validation_error |
| 99 | ); |
| 100 | |
| 101 | return $html; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | add_action( |
| 106 | 'wpcf7_swv_create_schema', |
| 107 | 'wpcf7_swv_add_number_rules', |
| 108 | 10, 2 |
| 109 | ); |
| 110 | |
| 111 | function wpcf7_swv_add_number_rules( $schema, $contact_form ) { |
| 112 | $tags = $contact_form->scan_form_tags( array( |
| 113 | 'basetype' => array( 'number', 'range' ), |
| 114 | ) ); |
| 115 | |
| 116 | foreach ( $tags as $tag ) { |
| 117 | if ( $tag->is_required() ) { |
| 118 | $schema->add_rule( |
| 119 | wpcf7_swv_create_rule( 'required', array( |
| 120 | 'field' => $tag->name, |
| 121 | 'error' => wpcf7_get_message( 'invalid_required' ), |
| 122 | ) ) |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | $schema->add_rule( |
| 127 | wpcf7_swv_create_rule( 'number', array( |
| 128 | 'field' => $tag->name, |
| 129 | 'error' => wpcf7_get_message( 'invalid_number' ), |
| 130 | ) ) |
| 131 | ); |
| 132 | |
| 133 | $min = $tag->get_option( 'min', 'signed_num', true ); |
| 134 | $max = $tag->get_option( 'max', 'signed_num', true ); |
| 135 | |
| 136 | if ( 'range' === $tag->basetype ) { |
| 137 | if ( ! wpcf7_is_number( $min ) ) { |
| 138 | $min = '0'; |
| 139 | } |
| 140 | |
| 141 | if ( ! wpcf7_is_number( $max ) ) { |
| 142 | $max = '100'; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if ( wpcf7_is_number( $min ) ) { |
| 147 | $schema->add_rule( |
| 148 | wpcf7_swv_create_rule( 'minnumber', array( |
| 149 | 'field' => $tag->name, |
| 150 | 'threshold' => $min, |
| 151 | 'error' => wpcf7_get_message( 'number_too_small' ), |
| 152 | ) ) |
| 153 | ); |
| 154 | } |
| 155 | |
| 156 | if ( wpcf7_is_number( $max ) ) { |
| 157 | $schema->add_rule( |
| 158 | wpcf7_swv_create_rule( 'maxnumber', array( |
| 159 | 'field' => $tag->name, |
| 160 | 'threshold' => $max, |
| 161 | 'error' => wpcf7_get_message( 'number_too_large' ), |
| 162 | ) ) |
| 163 | ); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /* Messages */ |
| 170 | |
| 171 | add_filter( 'wpcf7_messages', 'wpcf7_number_messages', 10, 1 ); |
| 172 | |
| 173 | function wpcf7_number_messages( $messages ) { |
| 174 | return array_merge( $messages, array( |
| 175 | 'invalid_number' => array( |
| 176 | 'description' => __( 'Number format that the sender entered is invalid', 'contact-form-7' ), |
| 177 | 'default' => __( 'Please enter a number.', 'contact-form-7' ), |
| 178 | ), |
| 179 | |
| 180 | 'number_too_small' => array( |
| 181 | 'description' => __( 'Number is smaller than minimum limit', 'contact-form-7' ), |
| 182 | 'default' => __( 'This field has a too small number.', 'contact-form-7' ), |
| 183 | ), |
| 184 | |
| 185 | 'number_too_large' => array( |
| 186 | 'description' => __( 'Number is larger than maximum limit', 'contact-form-7' ), |
| 187 | 'default' => __( 'This field has a too large number.', 'contact-form-7' ), |
| 188 | ), |
| 189 | ) ); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | /* Tag generator */ |
| 194 | |
| 195 | add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_number', 18, 0 ); |
| 196 | |
| 197 | function wpcf7_add_tag_generator_number() { |
| 198 | $tag_generator = WPCF7_TagGenerator::get_instance(); |
| 199 | |
| 200 | $tag_generator->add( 'number', __( 'number', 'contact-form-7' ), |
| 201 | 'wpcf7_tag_generator_number', |
| 202 | array( 'version' => '2' ) |
| 203 | ); |
| 204 | } |
| 205 | |
| 206 | function wpcf7_tag_generator_number( $contact_form, $options ) { |
| 207 | $field_types = array( |
| 208 | 'number' => array( |
| 209 | 'display_name' => __( 'Number field', 'contact-form-7' ), |
| 210 | 'heading' => __( 'Number field form-tag generator', 'contact-form-7' ), |
| 211 | 'description' => __( 'Generates a form-tag for a <a href="https://contactform7.com/number-fields/">number input field</a>.', 'contact-form-7' ), |
| 212 | ), |
| 213 | ); |
| 214 | |
| 215 | $tgg = new WPCF7_TagGeneratorGenerator( $options['content'] ); |
| 216 | |
| 217 | $formatter = new WPCF7_HTMLFormatter(); |
| 218 | |
| 219 | $formatter->append_start_tag( 'header', array( |
| 220 | 'class' => 'description-box', |
| 221 | ) ); |
| 222 | |
| 223 | $formatter->append_start_tag( 'h3' ); |
| 224 | |
| 225 | $formatter->append_preformatted( |
| 226 | esc_html( $field_types['number']['heading'] ) |
| 227 | ); |
| 228 | |
| 229 | $formatter->end_tag( 'h3' ); |
| 230 | |
| 231 | $formatter->append_start_tag( 'p' ); |
| 232 | |
| 233 | $formatter->append_preformatted( |
| 234 | wp_kses_data( $field_types['number']['description'] ) |
| 235 | ); |
| 236 | |
| 237 | $formatter->end_tag( 'header' ); |
| 238 | |
| 239 | $formatter->append_start_tag( 'div', array( |
| 240 | 'class' => 'control-box', |
| 241 | ) ); |
| 242 | |
| 243 | $formatter->call_user_func( static function () use ( $tgg, $field_types ) { |
| 244 | $tgg->print( 'field_type', array( |
| 245 | 'with_required' => true, |
| 246 | 'select_options' => array( |
| 247 | 'number' => __( 'Spinbox', 'contact-form-7' ), |
| 248 | 'range' => __( 'Slider', 'contact-form-7' ), |
| 249 | ), |
| 250 | ) ); |
| 251 | |
| 252 | $tgg->print( 'field_name' ); |
| 253 | |
| 254 | $tgg->print( 'class_attr' ); |
| 255 | |
| 256 | $tgg->print( 'min_max', array( |
| 257 | 'title' => __( 'Range', 'contact-form-7' ), |
| 258 | 'min_option' => 'min:', |
| 259 | 'max_option' => 'max:', |
| 260 | ) ); |
| 261 | |
| 262 | $tgg->print( 'default_value', array( |
| 263 | 'type' => 'number', |
| 264 | 'with_placeholder' => false, |
| 265 | ) ); |
| 266 | } ); |
| 267 | |
| 268 | $formatter->end_tag( 'div' ); |
| 269 | |
| 270 | $formatter->append_start_tag( 'footer', array( |
| 271 | 'class' => 'insert-box', |
| 272 | ) ); |
| 273 | |
| 274 | $formatter->call_user_func( static function () use ( $tgg, $field_types ) { |
| 275 | $tgg->print( 'insert_box_content' ); |
| 276 | |
| 277 | $tgg->print( 'mail_tag_tip' ); |
| 278 | } ); |
| 279 | |
| 280 | $formatter->print(); |
| 281 | } |
| 282 |