- fix count of words and letters for the bio field

This commit is contained in:
ashubawork
2023-07-27 14:39:08 +03:00
parent c9789b8462
commit 9cb4d5fa8f
3 changed files with 47 additions and 15 deletions
+6 -1
View File
@@ -110,7 +110,12 @@ jQuery(document).ready(function() {
jQuery( document.body ).on( 'change keyup', '#um-meta-bio', function() { jQuery( document.body ).on( 'change keyup', '#um-meta-bio', function() {
if ( typeof jQuery(this).val() !== 'undefined' ) { if ( typeof jQuery(this).val() !== 'undefined' ) {
var um_bio_limit = jQuery(this).data( 'character-limit' ); 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 ); jQuery( 'span.um-meta-bio-character span.um-bio-limit' ).text( remaining );
if ( remaining < 5 ) { if ( remaining < 5 ) {
+40 -14
View File
@@ -573,9 +573,14 @@ function um_submit_form_errors_hook_( $submitted_data, $form_data ) {
} }
if ( isset( $array['max_words'] ) && $array['max_words'] > 0 ) { if ( isset( $array['max_words'] ) && $array['max_words'] > 0 ) {
// count words without html tags if ( array_key_exists( 'html', $array ) && 1 === (int) $array['html'] ) {
$without_tags = wp_strip_all_tags( $submitted_data[ $key ] ); $text_value = wp_strip_all_tags( $submitted_data[ $key ] );
if ( str_word_count( $without_tags, 0, 'éèàôù' ) > $array['max_words'] ) { } 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. // 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'] ) ); 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'] ) ) { if ( empty( $array['validate'] ) ) {
continue; continue;
} }
@@ -933,17 +955,21 @@ function um_submit_form_errors_hook_( $submitted_data, $form_data ) {
} }
if ( isset( $submitted_data['description'] ) ) { // if ( isset( $submitted_data['description'] ) ) {
$max_chars = UM()->options()->get( 'profile_bio_maxchars' ); // echo '<pre>';
$profile_show_bio = UM()->options()->get( 'profile_show_bio' ); // print_r($submitted_data);
// echo '</pre>';
if ( $profile_show_bio ) { // exit();
if ( mb_strlen( str_replace( array( "\r\n", "\n", "\r\t", "\t" ), ' ', $submitted_data['description'] ) ) > $max_chars && $max_chars ) { // $max_chars = UM()->options()->get( 'profile_bio_maxchars' );
// translators: %s: max chars. // $profile_show_bio = UM()->options()->get( 'profile_show_bio' );
UM()->form()->add_error( 'description', sprintf( __( 'Your user description must contain less than %s characters', 'ultimate-member' ), $max_chars ) ); //
} // 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 ) } // end if ( isset in args array )
} }
add_action( 'um_submit_form_errors_hook_', 'um_submit_form_errors_hook_', 10, 2 ); add_action( 'um_submit_form_errors_hook_', 'um_submit_form_errors_hook_', 10, 2 );
+1
View File
@@ -1241,6 +1241,7 @@ function um_profile_header( $args ) {
<div class="um-meta-text"> <div class="um-meta-text">
<textarea id="um-meta-bio" <textarea id="um-meta-bio"
data-html="<?php echo esc_attr( UM()->options()->get( 'profile_show_html_bio' ) ); ?>"
data-character-limit="<?php echo esc_attr( UM()->options()->get( 'profile_bio_maxchars' ) ); ?>" data-character-limit="<?php echo esc_attr( UM()->options()->get( 'profile_bio_maxchars' ) ); ?>"
placeholder="<?php esc_attr_e( 'Tell us a bit about yourself...', 'ultimate-member' ); ?>" placeholder="<?php esc_attr_e( 'Tell us a bit about yourself...', 'ultimate-member' ); ?>"
name="<?php echo esc_attr( $description_key ); ?>"><?php echo UM()->fields()->field_value( $description_key ) ?></textarea> name="<?php echo esc_attr( $description_key ); ?>"><?php echo UM()->fields()->field_value( $description_key ) ?></textarea>