akismet
1 year ago
constant-contact
1 year ago
recaptcha
1 year ago
sendinblue
1 year ago
stripe
1 year ago
acceptance.php
1 year ago
checkbox.php
1 year ago
count.php
1 year ago
date.php
1 year ago
disallowed-list.php
1 year ago
doi-helper.php
4 years ago
file.php
1 year ago
flamingo.php
1 year ago
hidden.php
7 years ago
listo.php
2 years ago
number.php
1 year ago
quiz.php
1 year ago
really-simple-captcha.php
1 year ago
reflection.php
3 years ago
response.php
6 years ago
select.php
1 year ago
submit.php
1 year ago
text.php
1 year ago
textarea.php
1 year ago
count.php
70 lines
| 1 | <?php |
| 2 | /** |
| 3 | ** A base module for [count], Twitter-like character count |
| 4 | **/ |
| 5 | |
| 6 | /* form_tag handler */ |
| 7 | |
| 8 | add_action( 'wpcf7_init', 'wpcf7_add_form_tag_count', 10, 0 ); |
| 9 | |
| 10 | function wpcf7_add_form_tag_count() { |
| 11 | wpcf7_add_form_tag( 'count', |
| 12 | 'wpcf7_count_form_tag_handler', |
| 13 | array( |
| 14 | 'name-attr' => true, |
| 15 | 'zero-controls-container' => true, |
| 16 | 'not-for-mail' => true, |
| 17 | ) |
| 18 | ); |
| 19 | } |
| 20 | |
| 21 | function wpcf7_count_form_tag_handler( $tag ) { |
| 22 | if ( empty( $tag->name ) ) { |
| 23 | return ''; |
| 24 | } |
| 25 | |
| 26 | $targets = wpcf7_scan_form_tags( array( 'name' => $tag->name ) ); |
| 27 | $maxlength = $minlength = null; |
| 28 | |
| 29 | while ( $targets ) { |
| 30 | $target = array_shift( $targets ); |
| 31 | |
| 32 | if ( 'count' !== $target->type ) { |
| 33 | $maxlength = $target->get_maxlength_option(); |
| 34 | $minlength = $target->get_minlength_option(); |
| 35 | break; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | if ( $maxlength and $minlength |
| 40 | and $maxlength < $minlength ) { |
| 41 | $maxlength = $minlength = null; |
| 42 | } |
| 43 | |
| 44 | if ( $tag->has_option( 'down' ) ) { |
| 45 | $value = (int) $maxlength; |
| 46 | $class = 'wpcf7-character-count down'; |
| 47 | } else { |
| 48 | $value = '0'; |
| 49 | $class = 'wpcf7-character-count up'; |
| 50 | } |
| 51 | |
| 52 | $atts = array(); |
| 53 | |
| 54 | $atts['id'] = $tag->get_id_option(); |
| 55 | $atts['class'] = $tag->get_class_option( $class ); |
| 56 | $atts['data-target-name'] = $tag->name; |
| 57 | $atts['data-starting-value'] = $value; |
| 58 | $atts['data-current-value'] = $value; |
| 59 | $atts['data-maximum-value'] = $maxlength; |
| 60 | $atts['data-minimum-value'] = $minlength; |
| 61 | |
| 62 | $html = sprintf( |
| 63 | '<span %1$s>%2$s</span>', |
| 64 | wpcf7_format_atts( $atts ), |
| 65 | $value |
| 66 | ); |
| 67 | |
| 68 | return $html; |
| 69 | } |
| 70 |