Skip to content

Instantly share code, notes, and snippets.

@KaineLabs
Created December 13, 2022 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KaineLabs/0abbfd07dce13018f14d4cb10235bfd6 to your computer and use it in GitHub Desktop.
Save KaineLabs/0abbfd07dce13018f14d4cb10235bfd6 to your computer and use it in GitHub Desktop.
Youzify - BuddyPress Activity Posts & Comments Letters Restrictions
<?php
/**
* Youzify - Activity Posts & Comments Letters Restrictions
*/
add_action( 'bp_activity_before_save', 'yzc_letters_restrictions', 10 );
function yzc_letters_restrictions( $activity ) {
$error_msg = 'Sorry, you have added too many letters.';
if ( $activity->type == 'activity_comment' ) {
$max_comment_letters = 5;
if ( yzc_is_max_letters( $max_comment_letters, $activity->content ) ) {
exit( '-1<div id="message" class="error bp-ajax-message"><p>' . esc_html( $error_msg ) . '</p></div>' );
}
} else {
$max_post_letters = 5;
// Check For Restrictions.
if ( yzc_is_max_letters( $max_post_letters, $activity->content ) ) {
wp_send_json_error( array( 'error' => $error_msg ) );
}
}
}
/**
* Check for Max Letters.
*/
function yzc_is_max_letters( $max_letters, $content ) {
// Delete Empty Spaces.
$content = str_replace( '&nbsp;', '', $content );
// Remove Spaces
$content = trim( $content );
// Remove All Links.
$content = preg_replace('/\b((https?|ftp|file):\/\/|www\.)[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $content );
if ( apply_filters( 'yzmr_exclude_spaces_on_max_letters', true ) ) {
$content = str_replace( ' ', '', $content );
}
// Get Letters Count.
$count = strlen( wp_strip_all_tags( $content ) );
return $count > $max_letters ? true : false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment