Skip to:
Content
Pages
Categories
Search
Top
Bottom

group details updated is not posting in activity stream. Or comments on blog

  • @aljo1985

    Participant

    Currently when updating group details, it is not posting in sitewide activity stream or the groups home activity stream.

    I have created a function for adding custom fields to create and edit groups and have currently added, record this to activity stream, for when group details are edited. Isn’t this supposed to be functioning anyway?

    I am a very fast learner and have kinda got used to editing buddypress how I would like to without touching any source files.

    Here is my current code I am using to insert my edits into the groups meta and insert the update into the activity stream.

    add_action( 'groups_group_details_edited', 'group_details_update' );
    function group_details_update( $group_id ) {
    	global $bp, $wpdb;
    	
    	if ( !bp_current_user_can( 'bp_moderate' ) && !groups_is_user_member( bp_loggedin_user_id(), $group_id ))
    		return false;
    	
    	$plain_fields = array(
    		'server',
    		'country'
    	);
    
    	foreach( $plain_fields as $field ) {
    		$key = 'group-' . $field;
    		if ( isset( $_POST[$key] ) ) {
    			$value = $_POST[$key];
    			
    			if ($value == 'null')
    				bp_core_add_message( __( 'There was an error updating group details. Please try again.', 'buddypress' ), 'error' );
    			else
    				groups_update_groupmeta( $group_id, 'h1z1_' . $field, $value );
     		}
    	}
    	
    	// Record this in activity streams
    	$activity_action  = sprintf( __( '%1$s has modified group %2$s', 'buddypress'), bp_core_get_userlink( bp_loggedin_user_id() ), '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . '">' . esc_attr( $bp->groups->current_group->name ) . '</a>' );
    	$activity_content = $content;
     
    	$activity_id = groups_record_activity( array(
    		'user_id' => bp_loggedin_user_id(),
    		'action'  => apply_filters( 'groups_activity_new_update_action',  $activity_action  ),
    		'content' => apply_filters( 'groups_activity_new_update_content', $activity_content ),
    		'type'    => 'group_details_updated',
    		'item_id' => $group_id
    	) );
    
    	groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );
    	do_action( 'bp_groups_posted_update', $content, $user_id, $group_id, $activity_id );
    	
    }

    This works when I edit the details of a group, but no activity is showing when I edit photo or settings.

    I am assuming this is supposed to work out of the box, but doesn’t seem to be.

    A few bugs I have found.
    It posts when a member joins a group, but does nothing when they leave the group, they can keep joining and leaving and it will just post member has joined group multiple times. If the member is still in the group and I remove them from the group as an admin, it will then delete the streams that said the user joined, even if there is multiple it will remove them all.

    If I kick/ban the user, it will remove the streams also. If I unban, it will not post that a member has joined the group again. There are many functionality issues within the groups.

    Are the problems above supposed to work and do work with the latest version of buddypress or do I have to hook bp_groups for each part that is not working as desired to add actions in my functions.php?

    Thanks for reading my long post, hope someone can clear this up.

Viewing 1 replies (of 1 total)
  • @aljo1985

    Participant

    Okay so I realised that it would only send the activity to stream when you have the email option checked. I also noticed that upon doing that you post 2 pieces of information into the database that are exactly the same.. Well they are not structured the same but the information holds the same data.

    This is inside bp_groups_groupmeta and bp_activity
    So I have optimized my code to remove duplicate data, well just removed it from posting into groupsmeta really, as it doesn’t need to be in there… Maybe you can optimize your code to post the action in activity only and read from there, rather than have it posted in both. The system is great, don’t get me wrong I am just making suggestions.

    So I have removed one of your functions on remove action and copied it into my own function with edits to that function. I have added 2 extra fields, drop down boxs to be exact.

    Here is my code with a lot of comments as I was trying out many different things, then decided I don’t want it to do that lol. EDIT

    <?php
    // Add custom group fields.
    // Removed the default add activity stream. file /public_html/wp-content/plugins/buddypress/bp-groups/bp-groups-activity.php for reference.
    remove_action( 'groups_details_updated', 'bp_groups_group_details_updated_add_activity' );
    // On group update.
    add_action( 'groups_details_updated', 'group_details_update', 10, 3 );
    // Updated group details.
    // Updated group details.
    function group_details_update( $group_id, $old_group, $notify_members ) {
    	global $bp, $wpdb;
    	// Custom fields.
    	$plain_fields = array(
    		'server',
    		'country'
    		//'activity'
    	);
    	foreach( $plain_fields as $field ) {
    		$key = 'group-' . $field;
    		$metakey = 'h1z1_' . $field;
    		if ( isset( $_POST[$key] ) ) {
    			$value = $_POST[$key];
    			
    			// Do they want the update notification posted on their activity stream?
    			/*if ($value == '1')
    				$post_activity = true;
    			*/
    			//Make sure they selected an item from the required dropdown boxs.
    			if ($value == 'null')
    				return bp_core_add_message( __( 'There was an error updating group details. Please try again.', 'buddypress' ), 'error' );
    			else {
    				// changed1 is empty by default, not declared. If empty, get groupmeta.
    				if (empty($changed))
    					$changed = groups_get_groupmeta( $group_id, $metakey );
    				//if groupmeta(old value) == posted value changed is empty again, so we can check the next custom field to see if that also has the same old value.
    				if ($changed == $value)
    					$changed = '';
    				else
    					groups_update_groupmeta( $group_id, $metakey, $value );
    			}
     		}
    	}
    	// Optional removed checkbox for now.. Might use later
    	/*if ($post_activity == false)
    		return false;
    	*/
    	// Taken from /public_html/wp-content/plugins/buddypress/bp-groups/bp-groups-activity.php
    	// We removed the email notification check so that it will post an activity stream regardless.
    	// Bail if Activity is not active.
    	if ( ! bp_is_active( 'activity' ) )
    		return false;
    
    	if ( ! isset( $old_group->name ) || ! isset( $old_group->description ) )
    		return false;
    
    	// If the admin has opted not to notify members, don't post an activity item either
    	// Removed, I want updated posted if it sends and email or not.
    	/*if ( empty( $notify_members ) ) {
    		return;
    	}*/
    
    	$group = groups_get_group( array(
    		'group_id' => $group_id,
    	) );
    
    	/*
    	 * Store the changed data, which will be used to generate the activity
    	 * action. Since we haven't yet created the activity item, we store the
    	 * old group data in groupmeta, keyed by the timestamp that we'll put
    	 * on the activity item.
    	 */
    	
    	if ( $group->name !== $old_group->name || $group->description !== $old_group->description )
    		$changed = 'changed';
    
    	// If there are no changes, don't post an activity item.
    	if ( empty( $changed ) )
    		return;
    
    	$time = bp_core_current_time();
    	// Don't want a long description of what has been changed inside the details. Also reduces information posted in groupmeta table.
    	//groups_update_groupmeta( $group_id, 'updated_details_' . $time, $changed );
    	
    	// And finally, update groups last activity.. Currently doesn't in standard.
    	groups_update_groupmeta( $group_id, 'last_activity', $time );
    	
    	// Since we have removed the information from meta, we will record it directly into action on activity..
    	// You do not need the same information recorded twice in the database. This needs optimizing. Hence removing update groupmeta..
    	/*
    	$user_link = bp_core_get_userlink( bp_loggedin_user_id() );
    	$group_link = '<a href="' . esc_url( bp_get_group_permalink( $group ) ) . '">' . esc_html( $group->name ) . '</a>';
    	$action = sprintf( __( '%1$s changed the description of the group %2$s from "%3$s" to "%4$s"', 'buddypress' ), $user_link, $group_link, esc_html( $changed['description']['old'] ), esc_html( $changed['description']['new'] ) );
    	*/
    	// Record in activity streams.
    	return groups_record_activity( array(
    		'type'          => 'group_details_updated',
    		'item_id'       => $group_id,
    		'user_id'       => bp_loggedin_user_id(),
    		'recorded_time' => $time,
    	) );
    }

    If there is bugs in any code, I will find them and fix them.. That’s one of the downsides to being a perfectionist..

    I have not fixed the

    A few bugs I have found.

    From the first post yet, but will be doing after I have had my dinner 🙂
    My idea is to have a notification saying that the user has left the group when they leave/kicked/banned/ obviously if its after the 5 minutes mark lol.

Viewing 1 replies (of 1 total)
  • The topic ‘group details updated is not posting in activity stream. Or comments on blog’ is closed to new replies.
Skip to toolbar