WordPress.org

Ready to get started?Download WordPress

Forums

myCRED
[resolved] Buddypress Messaging (27 posts)

  1. PaulMRivera
    Member
    Posted 8 months ago #

    Is there a way to not allow a private message to be sent of the user does not have enough points? (i.e if User A has 0 points and wants to send a message to User B, the message will not go through on send and deliver a notification of failure due to not enough points)

    http://wordpress.org/plugins/mycred/

  2. Gabriel
    Member
    Plugin Author

    Posted 8 months ago #

    BuddyPress is not my strongest skill but you could for example strip off email recipients when users have zero balance which will cause BP to fail to send the message. Maybe someone out there who knows BuddyPress better could offer a better solution.

    add_action( 'messages_message_before_save', 'mycred_restrict_messages' );
    function mycred_restrict_messages( $message ) {
    	if ( empty( $message->recipients ) || ! function_exists( 'mycred_get_users_cred' ) ) return;
    	$cui = bp_loggedin_user_id();
    	$balance = mycred_get_users_cred( $cui );
    	if ( $balance == 0 )
    		$message->recipients = array();
    }

    The above code goes in your bp themes functions.php file or bp-custom.php file.

  3. PaulMRivera
    Member
    Posted 8 months ago #

    that code works great for what i need done unless someone else has another way. is there any way call a notification at that point with custom text? I bought notification plus.

  4. Gabriel
    Member
    Plugin Author

    Posted 8 months ago #

    The myCRED Notifications (and Notifications plus) add-on only shows users point gains and loses, thats what it was built for.

    There is the bp_core_add_message BuddyPress function which is used by BuddyPress to add messages which you might be able to use (have not tested this):

    add_action( 'messages_message_before_save', 'mycred_restrict_messages' );
    function mycred_restrict_messages( $message ) {
    	if ( empty( $message->recipients ) || ! function_exists( 'mycred_get_users_cred' ) ) return;
    	$cui = bp_loggedin_user_id();
    	$balance = mycred_get_users_cred( $cui );
    	if ( $balance == 0 ) {
    		$message->recipients = array();
    		bp_core_add_message( 'You do not have enough points to send messages', 'error' );
    	}
    }

    But you might also want to include information on the messages page informing users that if they do not have enough points, they can not send messages.

  5. PaulMRivera
    Member
    Posted 8 months ago #

    that "add_message" function doesn't work but i did find the following in the bp_core file. I believe this tells how to do it, I'm just not sure what to add.

    /**
     * Checks if there is a feedback message in the WP cookie, if so, adds a
     * "template_notices" action so that the message can be parsed into the template
     * and displayed to the user.
     *
     * After the message is displayed, it removes the message vars from the cookie
     * so that the message is not shown to the user multiple times.
     *
     * @package BuddyPress Core
     * @global $bp_message The message text
     * @global $bp_message_type The type of message (error/success)
     * @uses setcookie() Sets a cookie value for the user.
     */
    function bp_core_setup_message() {
    	global $bp;
    
    	if ( empty( $bp->template_message ) && isset( $_COOKIE['bp-message'] ) )
    		$bp->template_message = stripslashes( $_COOKIE['bp-message'] );
    
    	if ( empty( $bp->template_message_type ) && isset( $_COOKIE['bp-message-type'] ) )
    		$bp->template_message_type = stripslashes( $_COOKIE['bp-message-type'] );
    
    	add_action( 'template_notices', 'bp_core_render_message' );
    
    	if ( isset( $_COOKIE['bp-message'] ) )
    		@setcookie( 'bp-message', false, time() - 1000, COOKIEPATH );
    	if ( isset( $_COOKIE['bp-message-type'] ) )
    		@setcookie( 'bp-message-type', false, time() - 1000, COOKIEPATH );
    }
    add_action( 'bp_actions', 'bp_core_setup_message', 5 );
    
    /**
     * Renders a feedback message (either error or success message) to the theme template.
     * The hook action 'template_notices' is used to call this function, it is not called directly.
     *
     * @package BuddyPress Core
     * @global BuddyPress $bp The one true BuddyPress instance
     */
    function bp_core_render_message() {
    	global $bp;
    
    	if ( !empty( $bp->template_message ) ) :
    		$type    = ( 'success' == $bp->template_message_type ) ? 'updated' : 'error';
    		$content = apply_filters( 'bp_core_render_message_content', $bp->template_message, $type ); ?>
    
    		<div id="message" class="bp-template-notice <?php echo $type; ?>">
    
    			<?php echo $content; ?>
    
    		</div>
    
    	<?php
    
    		do_action( 'bp_core_render_message' );
    
    	endif;
    }
  6. Gabriel
    Member
    Plugin Author

    Posted 8 months ago #

    Based on that code try this:

    add_action( 'messages_message_before_save', 'mycred_restrict_messages' );
    function mycred_restrict_messages( $message ) {
    	if ( empty( $message->recipients ) || ! function_exists( 'mycred_get_users_cred' ) ) return;
    	$cui = bp_loggedin_user_id();
    	$balance = mycred_get_users_cred( $cui );
    	if ( $balance == 0 ) {
    		$message->recipients = array();
    		@setcookie( 'bp-message', 'You do not have enough points to send messages.', 3600, COOKIEPATH );
    		@setcookie( 'bp-message-type', 'error', 3600, COOKIEPATH );
    	}
    }
  7. PaulMRivera
    Member
    Posted 8 months ago #

    still doesn't work

  8. PaulMRivera
    Member
    Posted 8 months ago #

    I did find this in my theme. possibly something with this?

    <?php do_action( 'bp_before_notices_loop' ); ?>
    
    <?php if ( bp_has_message_threads() ) : ?>
    
    	<div class="pagination no-ajax" id="user-pag">
    
    		<div class="pag-count" id="messages-dir-count">
    			<?php bp_messages_pagination_count(); ?>
    		</div>
    
    		<div class="pagination-links" id="messages-dir-pag">
    			<?php bp_messages_pagination(); ?>
    		</div>
    
    	</div><!-- .pagination -->
    
    	<?php do_action( 'bp_after_notices_pagination' ); ?>
    	<?php do_action( 'bp_before_notices' ); ?>
    
    	<table id="message-threads" class="messages-notices">
    		<?php while ( bp_message_threads() ) : bp_message_thread(); ?>
    			<tr id="notice-<?php bp_message_notice_id(); ?>" class="<?php bp_message_css_class(); ?>">
    				<td width="1%"></td>
    				<td width="38%">
    					<strong><?php bp_message_notice_subject(); ?></strong>
    					<?php bp_message_notice_text(); ?>
    				</td>
    				<td width="21%">
    
    					<?php if ( bp_messages_is_active_notice() ) : ?>
    
    						<strong><?php bp_messages_is_active_notice(); ?></strong>
    
    					<?php endif; ?>
    
    					<span class="activity"><?php _e( 'Sent:', 'buddypress' ); ?> <?php bp_message_notice_post_date(); ?></span>
    				</td>
    
    				<?php do_action( 'bp_notices_list_item' ); ?>
    
    				<td width="20%">
    					<a class="button tiny" href="<?php bp_message_activate_deactivate_link(); ?>" class="confirm"><?php bp_message_activate_deactivate_text(); ?></a>
    					<a class="button tiny" href="<?php bp_message_notice_delete_link(); ?>" class="confirm" title="<?php _e( "Delete Message", "buddypress" ); ?>">x</a>
    				</td>
    			</tr>
    		<?php endwhile; ?>
    	</table><!-- #message-threads -->
    
    	<?php do_action( 'bp_after_notices' ); ?>
    
    <?php else: ?>
    
    	<div id="message" class="info">
    		<p><?php _e( 'Sorry, no notices were found.', 'buddypress' ); ?></p>
    	</div>
    
    <?php endif;?>
    
    <?php do_action( 'bp_after_notices_loop' ); ?>
  9. Gabriel
    Member
    Plugin Author

    Posted 8 months ago #

    Not sure what the issue can be, BuddyPress documentation seems to be limited to what is discussed in their support forum as I cant seem to find any helpful information.

  10. PaulMRivera
    Member
    Posted 8 months ago #

    k, I will keep trying. Thanks for your all your help

  11. PaulMRivera
    Member
    Posted 8 months ago #

    Could it be that since we are removing the recipient to get the error that it's pulling the recipient error rather than the "no balance" error? Something like "if recipient=false then get error message" (sorry I'm not great at this php stuff)

    I've seen a lot of places that to call a message would be

    bp_core_add_message( __( 'Your message was sent successfully', 'buddypress' ) );
    	} else {
    		bp_core_add_message( __( 'you're message was not sent', 'buddypress' ), 'error' );
    		}
  12. Gabriel
    Member
    Plugin Author

    Posted 8 months ago #

    Sounds logical as it seems it can only handle one message at a time.
    You could try this:

    // Restrict messages
    add_action( 'messages_message_before_save', 'mycred_restrict_messages' );
    function mycred_restrict_messages( $message ) {
    	if ( empty( $message->recipients ) || ! function_exists( 'mycred_get_users_cred' ) ) return;
    
    	global $mycred_message_blocked;
    	$cui = bp_loggedin_user_id();
    	$balance = mycred_get_users_cred( $cui );
    
    	$mycred_message_blocked = false;
    	if ( $balance == 0 ) {
    		$message->recipients = array();
    		$mycred_message_blocked = true;
    	}
    }
    
    // Add message after BuddyPress adds it's own
    add_action( 'messages_screen_compose', 'mycred_add_message_block_notice' );
    function mycred_add_message_block_notice() {
    	global $mycred_message_blocked;
    
    	if ( ! isset( $_POST['send'] ) || $mycred_message_blocked == false ) return;
    	bp_core_add_message( 'You do not have enough points to send messages', 'error' );
    }
  13. PaulMRivera
    Member
    Posted 8 months ago #

    Success! Thank you so much for helping me out. Do you have a donation page? This plugin is exactly what I needed, and I always want to help developers out.

  14. Gabriel
    Member
    Plugin Author

    Posted 8 months ago #

    Awesome!

    You can either donate here, or visit the myCRED store.

  15. PaulMRivera
    Member
    Posted 8 months ago #

    got it. Pay day is Wednesday :)

  16. PaulMRivera
    Member
    Posted 7 months ago #

    So I've run into a problem (not sure if it's to do with an update of some sort or maybe i just didn't notice it). The code above still works by blocking a user to send a private message if their credits are zero.

    What's happening now though is that if Person A has 1 credit and sends a message to Person B it will set the credits to 0 for person A and start the conversation, but since the credits are 0 for Person A, Person A can't send a reply message in that same conversation. If the conversation is started and Person A has 1 or more credits, Person A can reply as much as they want within that conversation without being deducted a credit.

    I know the problem is related to:

    $mycred_message_blocked = false;
    	if ( $balance == 0 ) {
    		$message->recipients = array();
    		$mycred_message_blocked = true;
    	}

    Is there a way to modify this code so that if Person A has 0 credits they can still reply within private messages (conversations) already initiated?

  17. Gabriel
    Member
    Plugin Author

    Posted 7 months ago #

    Hey.

    Another way to restrict creating new messages for those who do not have enough points is to override the compose.php template file in your BP theme by wrapping the entire form with a balance check. So if the user does not have enough points, the form is not loaded and users can not create new messages, however they can reply to messages already started.

    It's what I do on the myCRED.me website where you must have a minimum balance and a specific myCRED rank to start new conversations. You can however reply to already existing messages without any restrictions.

  18. PaulMRivera
    Member
    Posted 7 months ago #

    Would I wrap the entire compose.php with mycred_get_users_cred? I'm not great at code so not exactly sure how to wrap it.

    And is there a way to show a message when they have a 0 balance when the form is not shown?

    Thanks

  19. PaulMRivera
    Member
    Posted 7 months ago #

    I found this on one of your example pages:

    // Example
    $user_id = get_current_user_id();
    $balance = mycred_get_users_cred( $user_id );
    if ( $balance > 0 ) {
    // balance is over zero show the form
    }
    else {
    // balance is lower then zero show something else
    }

    Would I use this? I am trying to input this by putting the <form></form> in the "show form" section but can't seem to get it right

  20. Gabriel
    Member
    Plugin Author

    Posted 7 months ago #

    Hey.

    This is how I would do it.

    In the top of the compose.php file I insert as the very first thing:

    <?php if ( function_exists( 'mycred_get_users_cred' ) && mycred_get_users_cred( get_current_user_id() ) == 0  ) : ?>
    
    	<h3>Access Denied</h3>
    	<p>You must have more then zero points to send messages.</p>
    
    <?php else : ?>

    Then on the bottom of the page as the very last thing I insert:

    <?php endif; ?>

    This will "wrap" the form in a balance check and show the example message I am using if the user has zero points.

  21. PaulMRivera
    Member
    Posted 7 months ago #

    Worked great. I worked with you before to exclude a specific membership from mycred with the code.

    /* Exclude gold from credits */
    add_filter('bp_member_name','kleo_member_username');
    
    add_filter( 'mycred_exclude_user', 'check_membership_exclusion', 10, 2 );
    function check_membership_exclusion( $result, $user_id ) {
    	// Make sure Paid Memberships Pro is installed
    	if ( ! function_exists( 'pmpro_hasMembershipLevel' ) ) return $result;
    
    	// If the user has membership "gold exclude
    	if ( pmpro_hasMembershipLevel( 'gold', $user_id ) ) return true;
    	return $result;
    }

    The code you posted above also blocks even the excluded membership. Is there a way to allow the excluded membership to message with a 0 balance?

    Thank you

  22. PaulMRivera
    Member
    Posted 7 months ago #

    It will work exactly how I need it to if there is a way for that code to bypass the excluded members.

    Thanks again.

  23. Gabriel
    Member
    Plugin Author

    Posted 7 months ago #

    Replace the very first code snippet with the following:

    <?php
    $user_id = get_current_user_id();
    // all the reasons to deny access
    if (
    	( function_exists( 'mycred_get_users_cred' ) && mycred_get_users_cred( $user_id ) == 0 ) ||
    	( function_exists( 'pmpro_hasMembershipLevel' ) && pmpro_hasMembershipLevel( 'gold', $user_id ) )
    ) : ?>

    You would need to change "gold" to whatever level you want to deny access to the file.

  24. PaulMRivera
    Member
    Posted 7 months ago #

    Doesn't appear to work with that code. Still blocks the "gold" membership from sending messages with 0 balance. I will keep trying, and let you know if I come up with anything.

  25. PaulMRivera
    Member
    Posted 7 months ago #

    Figured it out. Switched the code to this and it worked:

    <?php
    $user_id = get_current_user_id();
    // all the reasons to deny access
    if (
    	( function_exists( 'mycred_get_users_cred' ) && mycred_get_users_cred( $user_id ) == 0 && function_exists( 'pmpro_hasMembershipLevel' ) && pmpro_hasMembershipLevel( 'gold', $user_id ) )
    ) : ?>

    Thank you for your help!

  26. Gabriel
    Member
    Plugin Author

    Posted 7 months ago #

    Ah sorry I misunderstood. I thought you wanted either or.

  27. PaulMRivera
    Member
    Posted 7 months ago #

    No problem. Thanks for helping!

Reply

You must log in to post.

About this Plugin

About this Topic