Skip to:
Content
Pages
Categories
Search
Top
Bottom

Forum Replies Created

Viewing 8 replies - 1 through 8 (of 8 total)
  • Avatar of P
    P
    Participant

    @patricksaad

    @bphelp Premium, not my cup of tea. I’m tackling the topic so throttling can be a feature in Buddypress itself (core feature), not simply as a plugin.

    Avatar of P
    P
    Participant

    @patricksaad

    The problem is I need to modify the core files and add a plugin at the same time so a user can change the Throttling settings in his WordPress backend.

    I really tried to simple write a plugin and skip the core files modifications, but it’s just not working out. Maybe you can help out here, I will post the 3 core file functions which I modified as well as the plugin code which I wrote. If you can figure out a way for me to not modify the core files and still get Throttling to work, share your code.

    Modified files:

    1- bp-activity\bp-activity-functions.php

    function bp_activity_post_update( $args = '' ) {
    	global $bp;
    
    	$defaults = array(
    		'content' => false,
    		'user_id' => bp_loggedin_user_id()
    	);
    	$r = wp_parse_args( $args, $defaults );
    	extract( $r, EXTR_SKIP );
    
    	if ( empty( $content ) || !strlen( trim( $content ) ) )
    		return false;
    
    	if ( bp_is_user_inactive( $user_id ) )
    		return false;
    
    	<strong>$floodTest = bp_core_check_for_flood ($user_id);
    	
    	if (!$floodTest)
    		return "flood";</strong>
    
    	// Record this on the user's profile
    	$from_user_link   = bp_core_get_userlink( $user_id );
    	$activity_action  = sprintf( __( '%s posted an update', 'buddypress' ), $from_user_link );
    	$activity_content = $content;
    	$primary_link     = bp_core_get_userlink( $user_id, false, true );
    
    	// Now write the values
    	$activity_id = bp_activity_add( array(
    		'user_id'      => $user_id,
    		'action'       => apply_filters( 'bp_activity_new_update_action', $activity_action ),
    		'content'      => apply_filters( 'bp_activity_new_update_content', $activity_content ),
    		'primary_link' => apply_filters( 'bp_activity_new_update_primary_link', $primary_link ),
    		'component'    => $bp->activity->id,
    		'type'         => 'activity_update'
    	) );
    
    	$activity_content = apply_filters( 'bp_activity_latest_update_content', $content );
    
    	// Add this update to the "latest update" usermeta so it can be fetched anywhere.
    	bp_update_user_meta( bp_loggedin_user_id(), 'bp_latest_update', array( 'id' => $activity_id, 'content' => $content ) );
    
    	do_action( 'bp_activity_posted_update', $content, $user_id, $activity_id );
    
    	return $activity_id;
    }
    

    2- bp-core\bp-core-moderation.php

    function bp_core_check_for_flood( $user_id = 0 )
    {	
    	<strong>// Option disabled. No flood checks.
    	if ( !$throttle_time = bp_get_option( 'bt_activity_time' ) )
    		return false;
    	
    	// Bail if no user ID passed
    	if ( !$user_id )
    		return false;
    
    	$last_posted = get_user_meta( $user_id, '_bp_last_posted', true );
    	
    	if ( !$last_posted )
    	{		
    		$last_posted = time();
    		add_user_meta( $user_id, '_bp_last_posted', $last_posted);
    		return true;
    	}
    	else
    	{	
    		if ( ( time() < ( $last_posted + $throttle_time ) ) && !current_user_can( 'throttle' ) )
    		{
    			update_user_meta($user_id,'_bp_last_posted',time());
    			return false;
    		}
    		else
    		{
    			update_user_meta($user_id,'_bp_last_posted',time());
    			return true;
    		}
    	}</strong>
    }

    3- bp-themes\bp-default\_inc\ajax.php

    function bp_dtheme_post_update() {
    	// Bail if not a POST action
    	if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
    		return;
    
    	// Check the nonce
    	check_admin_referer( 'post_update', '_wpnonce_post_update' );
    
    	if ( ! is_user_logged_in() )
    		exit( '-1' );
    
    	if ( empty( $_POST['content'] ) )
    		exit( '-1<div id="message" class="error"><p>' . __( 'Please enter some content to post.', 'buddypress' ) . '</p></div>' );
    
    	$activity_id = 0;
    	if ( empty( $_POST['object'] ) && bp_is_active( 'activity' ) ) {
    		$activity_id = bp_activity_post_update( array( 'content' => $_POST['content'] ) );
    
    	} elseif ( $_POST['object'] == 'groups' ) {
    		if ( ! empty( $_POST['item_id'] ) && bp_is_active( 'groups' ) )
    			$activity_id = groups_post_update( array( 'content' => $_POST['content'], 'group_id' => $_POST['item_id'] ) );
    
    	} else {
    		$activity_id = apply_filters( 'bp_activity_custom_update', $_POST['object'], $_POST['item_id'], $_POST['content'] );
    	}
    
    	if ($activity_id == "flood")
    	{
    		$bt_activity_throttle_time = bp_get_option ('bt_activity_time');
    		$bt_activity_message = bp_get_option( "bt_activity_message" );
    		
    		$msg = ( $bt_activity_message ) ? $bt_activity_message : "You have to wait to post again";
    		
    		exit( '-1<div id="message" class="error"><p>' . __( $msg, 'buddypress' ) . '</p></div>' );
    	}
    
    	if ( empty( $activity_id ) )
    		exit( '-1<div id="message" class="error"><p>' . __( 'There was a problem posting your update, please try again.', 'buddypress' ) . '</p></div>' );
    
    	if ( bp_has_activities ( 'include=' . $activity_id ) ) {
    		while ( bp_activities() ) {
    			bp_the_activity();
    			locate_template( array( 'activity/entry.php' ), true );
    		}
    	}
    
    	exit;
    }

    4- The Buddypress Throttling Plugin Code

    <?php
    add_action( 'admin_menu', 'plugin_menu' );
    
    function plugin_menu() {
    	// add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function);
    	add_options_page( 'Buddypress Throttling | Settings', 'Buddypress Throttling', 'manage_options', 'buddypress_throttling', 'buddypress_throttling_options' );
    }
    
    function buddypress_throttling_options() {
    
        //must check that the user has the required capability 
        if (!current_user_can('manage_options')) {
         	WP_die( __('You do not have sufficient permissions to access this page.') );
        }
    
        // variables for the field and option names 
        $hidden_field_name = 'submit_hidden';
    
        // See if the user has posted us some information
        // If they did, this hidden field will be set to 'Y'
        if( isset($_POST[ $hidden_field_name ]) && $_POST[ $hidden_field_name ] == 'Y' ) 
    	{
    		// Activity
    		$bp_activity_time_val =  ( intval($_POST["bt_activity_time"]) <= 0 ) ? 0 : $_POST ["bt_activity_time"];
    		update_option( "bt_activity_time", $bp_activity_time_val );
    		
    		$bp_activity_message_val =  ( isset($_POST["bt_activity_message"]) && $_POST["bt_activity_message"] != "" ) ? $_POST["bt_activity_message"] : "Please wait before posting again";
    		update_option( "bt_activity_message", $bp_activity_message_val );
    		?>
    		
    		<div class="updated"><p><strong><?php _e('Settings saved.', 'menu-test' ); ?></strong></p></div>
    		<?php
    	}
    	
    	// Activity read values
    	$bt_activity_time = get_option( "bt_activity_time" );
    	$bt_activity_time = (intval($bt_activity_time) <= 0) ? 0 : $bt_activity_time;
    	$bt_activity_message = get_option( "bt_activity_message" );
    	$bt_activity_message = ($bt_activity_message) ? $bt_activity_message : "Please wait before posting again";
    	
    	echo '<div class="wrap">';
    		echo "<h2>" . __( 'Buddypress Throttling Settings', 'menu-test' ) . "</h2>";
    		?>
    	
    		<form name="form1" method="post" action="">
    			<input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y">	
    			<div class="bt-plugin">
    				<style>
    					.bt-plugin span { display: inline-block; width: 120px; margin-left: 20px }
    					.bt-plugin textarea {width: 400px}
    				</style>
    				<h3><?php _e("On Activity Page: ", 'menu-test' ); ?></h3>
    				<p><span>Throttling Time:</span><input type="text" name="bt_activity_time" value="<?php echo $bt_activity_time; ?>" size="20"> (in seconds)</p>
    				<p><span>Message:</span><textarea name="bt_activity_message" rows="3"><?php echo $bt_activity_message; ?></textarea></p>
    			</div>
    			<p class="submit">
    			<input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" />
    			</p>
    		</form>
    	</div>
    <?php
    }

    Test it out locally, just replace the specified functions in these files with the functions I wrote, add the plugin code in a php file and save it under your Plugins directory (and activate it in the backend). The Plugin code will allow you to set the number of seconds for throttling as well as the message the user will see when he’s flooding.

    Put this puzzle pieces together and you got Flooding control (for the activity page for now), which can easily be done for friend requests @bphelp).

    Avatar of P
    P
    Participant

    @patricksaad

    @sooskriszta I agree that the plugin is a terrible idea. Throttling is one thing (telling the user “hey slow down a bit”) but setting an absolute limit for the number of friendships goes against the concept of a community.

    Going back to the ORIGINAL point of this topic, I said I will add throttling to the activity page and the friendship requests, but I ran into small trouble and thought maybe some of you people can help out. I opened another topic http://buddypress.org/support/topic/add-custom-setting-to-buddypress-settings/, I appreciate it if you can check it out and help out. @synaptic @sooskriszta

    Avatar of P
    P
    Participant

    @patricksaad

    @synaptic Will do asap, hopefully we’ll at least have Activity flooding control for Buddypress 1.8

    Avatar of P
    P
    Participant

    @patricksaad

    @Chouf1, that’s my blog you’re linking to. I am not sure if they added Throttling to the new Buddypress version. I opened a ticket months ago, the milestone has been set to: version 1.7 to Future Release http://buddypress.trac.wordpress.org/ticket/3732. This is a bit frustrating since Throttling makes for great spam control.

    @tifire Go to /bp-friends/bp-friends-functions.php in your Buddypress installation and change the friends_add_friend function to the following:

    function friends_add_friend( $initiator_userid, $friend_userid, $force_accept = false ) {
    	global $bp;
    
    	$friendship = new BP_Friends_Friendship;
    
    	if ( (int) $friendship->is_confirmed )
    		return true;
    
    	$friendship->initiator_user_id = $initiator_userid;
    	$friendship->friend_user_id    = $friend_userid;
    	$friendship->is_confirmed      = 0;
    	$friendship->is_limited        = 0;
    	$friendship->date_created      = bp_core_current_time();
    	
    	/**
    	 * BuddyPress Friend Request Throttling
    	 *
    	 * Set a throttle period for user friendship requests
    	 *
    	 * @author Patrick Saad
    	*/
    	
    	global $wpdb;
    	$qry = "SELECT date_created FROM wp_bp_friends where initiator_user_id = '".$initiator_userid."' order by date_created desc limit 1";
        $user_friend_requests = $wpdb->get_results( $qry );
    	
    	if ($user_friend_requests)
    	{
    		$latest_user_request = strtotime($user_friend_requests[0]->date_created, time());
    		$time_since_latest_request = time() - $latest_user_request;
    		
    		// that's 2 minutes
    		$throttle_period = 60 * 2;
    		
    		// if the last request was over 5 minutes ago, allow it
    		
    		if ($time_since_latest_request < $throttle_period)
    			return false;
    	}
    	// End of BuddyPress Friend Request Throttling //
    
    	if ( $force_accept )
    		$friendship->is_confirmed = 1;
    
    	if ( $friendship->save() ) {
    
    		if ( !$force_accept ) {
    			// Add the on screen notification
    			bp_core_add_notification( $friendship->initiator_user_id, $friendship->friend_user_id, $bp->friends->id, 'friendship_request' );
    
    			// Send the email notification
    			friends_notification_new_request( $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
    
    			do_action( 'friends_friendship_requested', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
    		} else {
    			// Update friend totals
    			friends_update_friend_totals( $friendship->initiator_user_id, $friendship->friend_user_id, 'add' );
    
    			do_action( 'friends_friendship_accepted', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
    		}
    
    		return true;
    	}
    
    	return false;
    }

    This will prevent a user from sending a friendship request to someone if their recent friendship request date was less than, say 2 minutes (change the $throttle_period variable to modify this time)

    Just gave this a fresh test, works in Buddypress 1.7.2. This same concept can be applied to comments, groups, etc, you just have to find the functions and play around.

    Avatar of P
    P
    Participant

    @patricksaad

    I don’t want to hijack this post, but I too am having the same problems. The Who’s Online and Recently Active Members widgets are showing empty results only on my search.php page (working perfectly on entire website pages).

    Working on homepage:
    http://www.lebmetal.com/

    Not working on search page:
    http://www.lebmetal.com/?s=test

    I have the exact same code in both my index and search files, I can’t seem to work this out.

    Avatar of P
    P
    Participant

    @patricksaad

    I reopened this ticket http://buddypress.trac.wordpress.org/ticket/3732 and DJPaul set the milestone to 1.7, and now I have no idea how to submit my patch (I am still not familiar with the process on buddypress.trax.org).

    Anyway, I fixed up a patch and tested it on Buddypress 1.6.3. Maybe you can check out the changes and submit a patch on my behalf if you can. You can download files I modified at http://we.tl/HpU6emIJs4 and check things out.

    Avatar of P
    P
    Participant

    @patricksaad

    Quoting from Version 1.6 Codex:

    Activity is now checked for blacklisted words and flooding protection has been added. (#3732)
    Add bp-core-moderation.php to provide basic functions for checking submitted content against illegal keys and flooding. (#3732)

    So basically any average perso ninstalling Buddypress will not have Flood Control, but a savvy developer can scratch his head and go fix Flood Control in more than one files:
    - bp-core/bp-moderation.php
    - bp-activity/bp-activity-functions.php
    - bp-themes/bp-default/_inc/ajax.php

    If you know what you’re doing and analyze / update code in these files, you can setup flood protection on your activity page, otherwise you’re fooled to think you have flood protection, at least in 1.6.2.

Viewing 8 replies - 1 through 8 (of 8 total)