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).