count.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. ** A base module for [count], Twitter-like character count
  4. **/
  5. /* form_tag handler */
  6. add_action( 'wpcf7_init', 'wpcf7_add_form_tag_count', 10, 0 );
  7. function wpcf7_add_form_tag_count() {
  8. wpcf7_add_form_tag( 'count',
  9. 'wpcf7_count_form_tag_handler',
  10. array(
  11. 'name-attr' => true,
  12. 'zero-controls-container' => true,
  13. 'not-for-mail' => true,
  14. )
  15. );
  16. }
  17. function wpcf7_count_form_tag_handler( $tag ) {
  18. if ( empty( $tag->name ) ) {
  19. return '';
  20. }
  21. $targets = wpcf7_scan_form_tags( array( 'name' => $tag->name ) );
  22. $maxlength = $minlength = null;
  23. while ( $targets ) {
  24. $target = array_shift( $targets );
  25. if ( 'count' != $target->type ) {
  26. $maxlength = $target->get_maxlength_option();
  27. $minlength = $target->get_minlength_option();
  28. break;
  29. }
  30. }
  31. if ( $maxlength and $minlength
  32. and $maxlength < $minlength ) {
  33. $maxlength = $minlength = null;
  34. }
  35. if ( $tag->has_option( 'down' ) ) {
  36. $value = (int) $maxlength;
  37. $class = 'wpcf7-character-count down';
  38. } else {
  39. $value = '0';
  40. $class = 'wpcf7-character-count up';
  41. }
  42. $atts = array();
  43. $atts['id'] = $tag->get_id_option();
  44. $atts['class'] = $tag->get_class_option( $class );
  45. $atts['data-target-name'] = $tag->name;
  46. $atts['data-starting-value'] = $value;
  47. $atts['data-current-value'] = $value;
  48. $atts['data-maximum-value'] = $maxlength;
  49. $atts['data-minimum-value'] = $minlength;
  50. $atts = wpcf7_format_atts( $atts );
  51. $html = sprintf( '<span %1$s>%2$s</span>', $atts, $value );
  52. return $html;
  53. }