Advertisement
designbymerovingi

Limit BuddyPress Messages to users of same rank

Jan 22nd, 2015
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.33 KB | None | 0 0
  1. add_action( 'messages_message_before_save', 'mycred_pro_intercept_bp_message' );
  2. function mycred_pro_intercept_bp_message( $message ) {
  3.  
  4.     // We need recipients and myCRED
  5.     if ( empty( $message->recipients ) || ! function_exists( 'mycred_get_users_rank' ) ) return;
  6.  
  7.     // The message sender
  8.     $cui = bp_loggedin_user_id();
  9.  
  10.     // Get the senders Rank ID so we can get the ranks minimum balance requirement
  11.     $senders_rank_id = mycred_get_users_rank( $cui, 'ID' );
  12.     $senders_min = get_post_meta( $senders_rank_id, 'mycred_rank_min', true );
  13.  
  14.     // Now we loop through the recipients
  15.     $good_recipients = array();
  16.     foreach ( $message->recipients as $recipient ) {
  17.  
  18.         // Get the recipients rank ID so we can get the rank minimum balance requirement
  19.         $recipients_rank_id = mycred_get_users_rank( $recipient->user_id, 'ID' );
  20.         $recipients_min = get_post_meta( $recipients_rank_id, 'mycred_rank_min', true );
  21.  
  22.         // If recipient is on a higher rank, continue to the next
  23.         if ( $recipients_min > $senders_min ) continue;
  24.  
  25.         // Add recipient to the "good" list
  26.         $good_recipients[] = $recipient;
  27.  
  28.     }
  29.  
  30.     // Replace set recipients with the ones we have approved
  31.     // If this results in an empty array (all recipients declined)
  32.     // BuddyPress will show an error to the user and no message is sent.
  33.     $message->recipients = $good_recipients;
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement