Support » Plugin: myCRED » Buddypress Messaging

  • Resolved PaulMRivera

    (@paulmrivera)


    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/

Viewing 15 replies - 1 through 15 (of 28 total)
  • Plugin Author Gabriel

    (@designbymerovingi)

    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.

    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.

    Plugin Author Gabriel

    (@designbymerovingi)

    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.

    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;
    }

    Plugin Author Gabriel

    (@designbymerovingi)

    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 );
    	}
    }

    still doesn’t work

    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' ); ?>

    Plugin Author Gabriel

    (@designbymerovingi)

    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.

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

    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' );
    		}

    Plugin Author Gabriel

    (@designbymerovingi)

    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' );
    }

    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.

    Plugin Author Gabriel

    (@designbymerovingi)

    Awesome!

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

    got it. Pay day is Wednesday 🙂

    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?

Viewing 15 replies - 1 through 15 (of 28 total)
  • The topic ‘Buddypress Messaging’ is closed to new replies.