From 9cb4d5fa8fb561266e3ffee041628efe5ac8b639 Mon Sep 17 00:00:00 2001 From: ashubawork Date: Thu, 27 Jul 2023 14:39:08 +0300 Subject: [PATCH] - fix count of words and letters for the bio field --- assets/js/um-profile.js | 7 +++- includes/core/um-actions-form.php | 54 ++++++++++++++++++++-------- includes/core/um-actions-profile.php | 1 + 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/assets/js/um-profile.js b/assets/js/um-profile.js index 5929df97..8f0f3381 100644 --- a/assets/js/um-profile.js +++ b/assets/js/um-profile.js @@ -110,7 +110,12 @@ jQuery(document).ready(function() { jQuery( document.body ).on( 'change keyup', '#um-meta-bio', function() { if ( typeof jQuery(this).val() !== 'undefined' ) { var um_bio_limit = jQuery(this).data( 'character-limit' ); - var remaining = um_bio_limit - jQuery(this).val().length; + var bio_html = jQuery(this).attr('data-html'); + if ( parseInt( bio_html ) === 1 ){ + var remaining = um_bio_limit - jQuery(this).val().replace(/(<([^>]+)>)/ig,'').length; + } else { + var remaining = um_bio_limit - jQuery(this).val().length; + } jQuery( 'span.um-meta-bio-character span.um-bio-limit' ).text( remaining ); if ( remaining < 5 ) { diff --git a/includes/core/um-actions-form.php b/includes/core/um-actions-form.php index 0d847b2e..3222faa1 100644 --- a/includes/core/um-actions-form.php +++ b/includes/core/um-actions-form.php @@ -573,9 +573,14 @@ function um_submit_form_errors_hook_( $submitted_data, $form_data ) { } if ( isset( $array['max_words'] ) && $array['max_words'] > 0 ) { - // count words without html tags - $without_tags = wp_strip_all_tags( $submitted_data[ $key ] ); - if ( str_word_count( $without_tags, 0, 'éèàôù' ) > $array['max_words'] ) { + if ( array_key_exists( 'html', $array ) && 1 === (int) $array['html'] ) { + $text_value = wp_strip_all_tags( $submitted_data[ $key ] ); + } else { + // count words without html tags + $text_value = $submitted_data[ $key ]; + } + + if ( str_word_count( $text_value, 0, '0123456789éèàôù' ) > $array['max_words'] ) { // translators: %s: max words. UM()->form()->add_error( $key, sprintf( __( 'You are only allowed to enter a maximum of %s words', 'ultimate-member' ), $array['max_words'] ) ); } @@ -668,6 +673,23 @@ function um_submit_form_errors_hook_( $submitted_data, $form_data ) { } } + if ( isset( $submitted_data['description'] ) ) { + $max_chars = UM()->options()->get( 'profile_bio_maxchars' ); + $profile_show_bio = UM()->options()->get( 'profile_show_bio' ); + if ( ! UM()->options()->get( 'profile_show_html_bio' ) ) { + $description_value = $submitted_data['description']; + } else { + $description_value = wp_strip_all_tags( $submitted_data['description'] ); + } + + if ( $profile_show_bio ) { + if ( mb_strlen( str_replace( array( "\r\n", "\n", "\r\t", "\t" ), ' ', $description_value ) ) > $max_chars && $max_chars ) { + // translators: %s: max chars. + UM()->form()->add_error( 'description', sprintf( __( 'Your user description must contain less than %s characters', 'ultimate-member' ), $max_chars ) ); + } + } + } + if ( empty( $array['validate'] ) ) { continue; } @@ -933,17 +955,21 @@ function um_submit_form_errors_hook_( $submitted_data, $form_data ) { } - if ( isset( $submitted_data['description'] ) ) { - $max_chars = UM()->options()->get( 'profile_bio_maxchars' ); - $profile_show_bio = UM()->options()->get( 'profile_show_bio' ); - - if ( $profile_show_bio ) { - if ( mb_strlen( str_replace( array( "\r\n", "\n", "\r\t", "\t" ), ' ', $submitted_data['description'] ) ) > $max_chars && $max_chars ) { - // translators: %s: max chars. - UM()->form()->add_error( 'description', sprintf( __( 'Your user description must contain less than %s characters', 'ultimate-member' ), $max_chars ) ); - } - } - } +// if ( isset( $submitted_data['description'] ) ) { +// echo '
';
+//			print_r($submitted_data);
+//			echo '
'; +// exit(); +// $max_chars = UM()->options()->get( 'profile_bio_maxchars' ); +// $profile_show_bio = UM()->options()->get( 'profile_show_bio' ); +// +// if ( $profile_show_bio ) { +// if ( mb_strlen( str_replace( array( "\r\n", "\n", "\r\t", "\t" ), ' ', $submitted_data['description'] ) ) > $max_chars && $max_chars ) { +// // translators: %s: max chars. +// UM()->form()->add_error( 'description', sprintf( __( 'Your user description must contain less than %s characters', 'ultimate-member' ), $max_chars ) ); +// } +// } +// } } // end if ( isset in args array ) } add_action( 'um_submit_form_errors_hook_', 'um_submit_form_errors_hook_', 10, 2 ); diff --git a/includes/core/um-actions-profile.php b/includes/core/um-actions-profile.php index 625994b4..b2a99eae 100644 --- a/includes/core/um-actions-profile.php +++ b/includes/core/um-actions-profile.php @@ -1241,6 +1241,7 @@ function um_profile_header( $args ) {