Last active
February 21, 2024 21:34
-
-
Save igorbenic/7b3c4a81e91b924e80e858ae20a4edcc to your computer and use it in GitHub Desktop.
How to Limit BuddyPress Activity Messages | https://www.ibenic.com/limit-buddypress-activity-messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// .../wp-content/plugins/buddypress/bp-activity/classes/class-bp-activity-activity.php | |
// ... | |
/** | |
* Database interaction class for the BuddyPress activity component. | |
* Instance methods are available for creating/editing an activity, | |
* static methods for querying activities. | |
* | |
* @since 1.0.0 | |
*/ | |
class BP_Activity_Activity { | |
// ... | |
/** | |
* Save the activity item to the database. | |
* | |
* @since 1.0.0 | |
* | |
* @return WP_Error|bool True on success. | |
*/ | |
public function save() { | |
global $wpdb; | |
$bp = buddypress(); | |
$this->id = apply_filters_ref_array( 'bp_activity_id_before_save', array( $this->id, &$this ) ); | |
$this->item_id = apply_filters_ref_array( 'bp_activity_item_id_before_save', array( $this->item_id, &$this ) ); | |
$this->secondary_item_id = apply_filters_ref_array( 'bp_activity_secondary_item_id_before_save', array( $this->secondary_item_id, &$this ) ); | |
$this->user_id = apply_filters_ref_array( 'bp_activity_user_id_before_save', array( $this->user_id, &$this ) ); | |
$this->primary_link = apply_filters_ref_array( 'bp_activity_primary_link_before_save', array( $this->primary_link, &$this ) ); | |
$this->component = apply_filters_ref_array( 'bp_activity_component_before_save', array( $this->component, &$this ) ); | |
$this->type = apply_filters_ref_array( 'bp_activity_type_before_save', array( $this->type, &$this ) ); | |
$this->action = apply_filters_ref_array( 'bp_activity_action_before_save', array( $this->action, &$this ) ); | |
$this->content = apply_filters_ref_array( 'bp_activity_content_before_save', array( $this->content, &$this ) ); | |
$this->date_recorded = apply_filters_ref_array( 'bp_activity_date_recorded_before_save', array( $this->date_recorded, &$this ) ); | |
$this->hide_sitewide = apply_filters_ref_array( 'bp_activity_hide_sitewide_before_save', array( $this->hide_sitewide, &$this ) ); | |
$this->mptt_left = apply_filters_ref_array( 'bp_activity_mptt_left_before_save', array( $this->mptt_left, &$this ) ); | |
$this->mptt_right = apply_filters_ref_array( 'bp_activity_mptt_right_before_save', array( $this->mptt_right, &$this ) ); | |
$this->is_spam = apply_filters_ref_array( 'bp_activity_is_spam_before_save', array( $this->is_spam, &$this ) ); | |
/** | |
* Fires before the current activity item gets saved. | |
* | |
* Please use this hook to filter the properties above. Each part will be passed in. | |
* | |
* @since 1.0.0 | |
* | |
* @param BP_Activity_Activity $this Current instance of the activity item being saved. Passed by reference. | |
*/ | |
do_action_ref_array( 'bp_activity_before_save', array( &$this ) ); | |
if ( 'wp_error' === $this->error_type && $this->errors->get_error_code() ) { | |
return $this->errors; | |
} | |
// ... | |
} | |
// ... | |
} | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* We will return false if we're trying to mention ourselves. | |
*/ | |
function can_this_user_be_mentioned( $user_id ) { | |
/* This function can contain various checks such as: | |
- user roles | |
- user levels | |
- capabilities | |
- and a lot more... | |
*/ | |
$current_user_id = get_current_user_id(); | |
if ( $current_user_id !== $user_id ) { | |
return true; | |
} | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'bp_activity_before_save', 'yourplugin_disable_activity_on_save' ); | |
/** | |
* If a user can't be mentioned, add an error to Activity object. | |
* | |
* @param BP_Activity_Activity $activity Activity object. | |
* | |
* @return void | |
*/ | |
function yourplugin_disable_activity_on_save( $activity ) { | |
$unavailable_username = ''; | |
$user_ids = bp_activity_find_mentions( $activity->content ); | |
if ( ! $user_ids ) { | |
return; | |
} | |
foreach( $user_ids as $user_id => $username ) { | |
if ( ! can_this_user_be_mentioned( $user_id ) ) { | |
$unavailable_username = $username; | |
break; | |
} | |
} | |
if ( $unavailable_username ) { | |
$activity->error_type = 'wp_error'; | |
$activity->errors = new WP_Error( 'no-user-mention', sprintf( __( 'You can\'t mention %s.', 'your_textdomain' ), $unavailable_username ) ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'bp_activity_post_update_content', 'myplugin_disable_activity_mention_user' ); | |
/** | |
* If a username can't be mentioned, we will return an empty content and a message. | |
* | |
* @param string $content Content. | |
* | |
* @return string | |
*/ | |
function myplugin_disable_activity_mention_user( $content ) { | |
$user_ids = bp_activity_find_mentions( $content ); | |
if ( ! $user_ids ) { | |
return $content; | |
} | |
foreach( $user_ids as $user_id => $username ) { | |
if ( ! can_this_user_be_mentioned( $user_id ) ) { | |
$content = ''; | |
bp_core_add_message( sprintf( __( 'You can\t mention %s.', 'your_textdomain' ), $username ), 'error' ); | |
break; | |
} | |
} | |
return $content; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// .../wp-content/plugins/buddypress/bp-activity/actions/post.php | |
// ... | |
function bp_activity_action_post_update() { | |
// Do not proceed if user is not logged in, not viewing activity, or not posting. | |
if ( !is_user_logged_in() || !bp_is_activity_component() || !bp_is_current_action( 'post' ) ) | |
return false; | |
// Check the nonce. | |
check_admin_referer( 'post_update', '_wpnonce_post_update' ); | |
/** | |
* Filters the content provided in the activity input field. | |
* | |
* @since 1.2.0 | |
* | |
* @param string $value Activity message being posted. | |
*/ | |
$content = apply_filters( 'bp_activity_post_update_content', $_POST['whats-new'] ); | |
// ... | |
} | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment