Index: bp-groups/bp-groups-forums.php
===================================================================
--- bp-groups/bp-groups-forums.php
+++ bp-groups/bp-groups-forums.php
@@ -14,14 +14,25 @@
 // Exit if accessed directly
 if ( !defined( 'ABSPATH' ) ) exit;
 
+/**
+ * Creates a new forum inside a specific BuddyPress group.
+ *
+ * Uses the bundled version of bbPress packaged with BuddyPress.
+ *
+ * @param int $group_id The group ID that the new forum should be attached to
+ * @param string $group_name The group name
+ * @param string $group_description The group description
+ *
+ * @since 1.0
+ */
 function groups_new_group_forum( $group_id = 0, $group_name = '', $group_desc = '' ) {
 	global $bp;
 
 	if ( empty( $group_id ) )
-		$group_id = $bp->groups->current_group->id;
+		$group_id = bp_get_current_group_id();
 
 	if ( empty( $group_name ) )
-		$group_name = $bp->groups->current_group->name;
+		$group_name = bp_get_current_group_name();
 
 	if ( empty( $group_desc ) )
 		$group_desc = $bp->groups->current_group->description;
@@ -40,6 +51,8 @@ function groups_new_group_forum( $group_id = 0, $group_name = '', $group_desc =
  * @subpackage Groups
  *
  * @param int $group_id Group id, passed from groups_details_updated
+ *
+ * @since 1.1
  */
 function groups_update_group_forum( $group_id ) {
 
@@ -66,9 +79,19 @@ function groups_update_group_forum( $group_id ) {
 }
 add_action( 'groups_details_updated', 'groups_update_group_forum' );
 
+/**
+ * Creates a new group forum post.
+ *
+ * Uses the bundled version of bbPress packaged with BuddyPress.
+ *
+ * @param string $post_text The text for the forum post
+ * @param int $topic_id The topic ID used so we can identify where the new forum post should reside
+ * @param mixed $page The page number where the new forum post should reside. Defaults to boolean false.
+ * @return mixed The new forum post ID on success. Boolean false on failure.
+ *
+ * @since 1.0
+ */
 function groups_new_group_forum_post( $post_text, $topic_id, $page = false ) {
-	global $bp;
-
 	if ( empty( $post_text ) )
 		return false;
 
@@ -78,24 +101,24 @@ function groups_new_group_forum_post( $post_text, $topic_id, $page = false ) {
 	if ( $post_id = bp_forums_insert_post( array( 'post_text' => $post_text, 'topic_id' => $topic_id ) ) ) {
 		$topic = bp_forums_get_topic_details( $topic_id );
 
-		$activity_action = sprintf( __( '%1$s replied to the forum topic %2$s in the group %3$s', 'buddypress'), bp_core_get_userlink( bp_loggedin_user_id() ), '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . 'forum/topic/' . $topic->topic_slug .'/">' . esc_attr( $topic->topic_title ) . '</a>', '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . '">' . esc_attr( $bp->groups->current_group->name ) . '</a>' );
+		$activity_action = sprintf( __( '%1$s replied to the forum topic %2$s in the group %3$s', 'buddypress'), bp_core_get_userlink( bp_loggedin_user_id() ), '<a href="' . bp_get_group_permalink( groups_get_current_group() ) . 'forum/topic/' . $topic->topic_slug .'/">' . esc_attr( $topic->topic_title ) . '</a>', '<a href="' . bp_get_group_permalink( groups_get_current_group() ) . '">' . esc_attr( bp_get_current_group_name() ) . '</a>' );
 		$activity_content = bp_create_excerpt( $post_text );
-		$primary_link = bp_get_group_permalink( $bp->groups->current_group ) . 'forum/topic/' . $topic->topic_slug . '/';
+		$primary_link = bp_get_group_permalink( groups_get_current_group() ) . 'forum/topic/' . $topic->topic_slug . '/';
 
 		if ( $page )
 			$primary_link .= "?topic_page=" . $page;
 
 		// Record this in activity streams
 		groups_record_activity( array(
-			'action'            => apply_filters_ref_array( 'groups_activity_new_forum_post_action',       array( $activity_action,  $post_id, $post_text, &$topic ) ),
-			'content'           => apply_filters_ref_array( 'groups_activity_new_forum_post_content',      array( $activity_content, $post_id, $post_text, &$topic ) ),
+			'action'            => apply_filters_ref_array( 'groups_activity_new_forum_post_action',  array( $activity_action,  $post_id, $post_text, &$topic ) ),
+			'content'           => apply_filters_ref_array( 'groups_activity_new_forum_post_content', array( $activity_content, $post_id, $post_text, &$topic ) ),
 			'primary_link'      => apply_filters( 'groups_activity_new_forum_post_primary_link', "{$primary_link}#post-{$post_id}" ),
 			'type'              => 'new_forum_post',
-			'item_id'           => $bp->groups->current_group->id,
+			'item_id'           => bp_get_current_group_id(),
 			'secondary_item_id' => $post_id
 		) );
 
-		do_action( 'groups_new_forum_topic_post', $bp->groups->current_group->id, $post_id );
+		do_action( 'groups_new_forum_topic_post', bp_get_current_group_id(), $post_id );
 
 		return $post_id;
 	}
@@ -103,9 +126,20 @@ function groups_new_group_forum_post( $post_text, $topic_id, $page = false ) {
 	return false;
 }
 
+/**
+ * Creates a new group forum topic.
+ *
+ * Uses the bundled version of bbPress packaged with BuddyPress.
+ *
+ * @param string $topic_title The title for the forum topic
+ * @param string $topic_text The text for the forum topic
+ * @param string $topic_tags A comma-delimited string of topic tags
+ * @param int $forum_id The forum ID this forum topic resides in
+ * @return mixed The new topic object on success. Boolean false on failure.
+ *
+ * @since 1.0
+ */
 function groups_new_group_forum_topic( $topic_title, $topic_text, $topic_tags, $forum_id ) {
-	global $bp;
-
 	if ( empty( $topic_title ) || empty( $topic_text ) )
 		return false;
 
@@ -117,20 +151,20 @@ function groups_new_group_forum_topic( $topic_title, $topic_text, $topic_tags, $
 	if ( $topic_id = bp_forums_new_topic( array( 'topic_title' => $topic_title, 'topic_text' => $topic_text, 'topic_tags' => $topic_tags, 'forum_id' => $forum_id ) ) ) {
 		$topic = bp_forums_get_topic_details( $topic_id );
 
-		$activity_action = sprintf( __( '%1$s started the forum topic %2$s in the group %3$s', 'buddypress'), bp_core_get_userlink( bp_loggedin_user_id() ), '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . 'forum/topic/' . $topic->topic_slug .'/">' . esc_attr( $topic->topic_title ) . '</a>', '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . '">' . esc_attr( $bp->groups->current_group->name ) . '</a>' );
+		$activity_action = sprintf( __( '%1$s started the forum topic %2$s in the group %3$s', 'buddypress'), bp_core_get_userlink( bp_loggedin_user_id() ), '<a href="' . bp_get_group_permalink( groups_get_current_group() ) . 'forum/topic/' . $topic->topic_slug .'/">' . esc_attr( $topic->topic_title ) . '</a>', '<a href="' . bp_get_group_permalink( groups_get_current_group() ) . '">' . esc_attr( bp_get_current_group_name() ) . '</a>' );
 		$activity_content = bp_create_excerpt( $topic_text );
 
 		// Record this in activity streams
 		groups_record_activity( array(
 			'action'            => apply_filters_ref_array( 'groups_activity_new_forum_topic_action',  array( $activity_action,  $topic_text, &$topic ) ),
 			'content'           => apply_filters_ref_array( 'groups_activity_new_forum_topic_content', array( $activity_content, $topic_text, &$topic ) ),
-			'primary_link'      => apply_filters( 'groups_activity_new_forum_topic_primary_link', bp_get_group_permalink( $bp->groups->current_group ) . 'forum/topic/' . $topic->topic_slug . '/' ),
+			'primary_link'      => apply_filters( 'groups_activity_new_forum_topic_primary_link', bp_get_group_permalink( groups_get_current_group() ) . 'forum/topic/' . $topic->topic_slug . '/' ),
 			'type'              => 'new_forum_topic',
-			'item_id'           => $bp->groups->current_group->id,
+			'item_id'           => bp_get_current_group_id(),
 			'secondary_item_id' => $topic->topic_id
 		) );
 
-	  do_action_ref_array( 'groups_new_forum_topic', array( $bp->groups->current_group->id, &$topic ) );
+		do_action_ref_array( 'groups_new_forum_topic', array( bp_get_current_group_id(), &$topic ) );
 
 		return $topic;
 	}
@@ -138,6 +172,19 @@ function groups_new_group_forum_topic( $topic_title, $topic_text, $topic_tags, $
 	return false;
 }
 
+/**
+ * Updates an existing group forum topic.
+ *
+ * Uses the bundled version of bbPress packaged with BuddyPress.
+ *
+ * @param int $topic_id The topic ID of the existing forum topic
+ * @param string $topic_title The title for the forum topic
+ * @param string $topic_text The text for the forum topic
+ * @param mixed $topic_tags A comma-delimited string of topic tags. Defaults to boolean false.
+ * @return mixed The topic object on success. Boolean false on failure.
+ *
+ * @since 1.1
+ */
 function groups_update_group_forum_topic( $topic_id, $topic_title, $topic_text, $topic_tags = false ) {
 	global $bp;
 
@@ -149,14 +196,14 @@ function groups_update_group_forum_topic( $topic_id, $topic_title, $topic_text,
 		// Get the corresponding activity item
 		if ( bp_is_active( 'activity' ) ) {
 			$id = bp_activity_get_activity_id( array(
-					'item_id'           => $bp->groups->current_group->id,
+					'item_id'           => bp_get_current_group_id(),
 					'secondary_item_id' => $topic_id,
 					'component'         => $bp->groups->id,
 					'type'              => 'new_forum_topic'
 			) );
 		}
 
-		$activity_action = sprintf( __( '%1$s started the forum topic %2$s in the group %3$s', 'buddypress'), bp_core_get_userlink( $topic->topic_poster ), '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . 'forum/topic/' . $topic->topic_slug .'/">' . esc_attr( $topic->topic_title ) . '</a>', '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . '">' . esc_attr( $bp->groups->current_group->name ) . '</a>' );
+		$activity_action = sprintf( __( '%1$s started the forum topic %2$s in the group %3$s', 'buddypress'), bp_core_get_userlink( $topic->topic_poster ), '<a href="' . bp_get_group_permalink( groups_get_current_group() ) . 'forum/topic/' . $topic->topic_slug .'/">' . esc_attr( $topic->topic_title ) . '</a>', '<a href="' . bp_get_group_permalink( groups_get_current_group() ) . '">' . esc_attr( bp_get_current_group_name() ) . '</a>' );
 		$activity_content = bp_create_excerpt( $topic_text );
 
 		// Record this in activity streams
@@ -164,9 +211,9 @@ function groups_update_group_forum_topic( $topic_id, $topic_title, $topic_text,
 			'id'                => $id,
 			'action'            => apply_filters_ref_array( 'groups_activity_new_forum_topic_action',  array( $activity_action,  $topic_text, &$topic ) ),
 			'content'           => apply_filters_ref_array( 'groups_activity_new_forum_topic_content', array( $activity_content, $topic_text, &$topic ) ),
-			'primary_link'      => apply_filters( 'groups_activity_new_forum_topic_primary_link', bp_get_group_permalink( $bp->groups->current_group ) . 'forum/topic/' . $topic->topic_slug . '/' ),
+			'primary_link'      => apply_filters( 'groups_activity_new_forum_topic_primary_link', bp_get_group_permalink( groups_get_current_group() ) . 'forum/topic/' . $topic->topic_slug . '/' ),
 			'type'              => 'new_forum_topic',
-			'item_id'           => (int) $bp->groups->current_group->id,
+			'item_id'           => (int) bp_get_current_group_id(),
 			'user_id'           => (int) $topic->topic_poster,
 			'secondary_item_id' => $topic->topic_id,
 			'recorded_time '    => $topic->topic_time
@@ -180,6 +227,19 @@ function groups_update_group_forum_topic( $topic_id, $topic_title, $topic_text,
 	return false;
 }
 
+/**
+ * Updates an existing group forum post.
+ *
+ * Uses the bundled version of bbPress packaged with BuddyPress.
+ *
+ * @param int $post_id The post ID of the existing forum post
+ * @param string $post_text The text for the forum post
+ * @param int $topic_id The topic ID of the existing forum topic
+ * @param mixed $page The page number where the new forum post should reside. Defaults to boolean false.
+ * @return mixed The forum post ID on success. Boolean false on failure.
+ *
+ * @since 1.1
+ */
 function groups_update_group_forum_post( $post_id, $post_text, $topic_id, $page = false ) {
 	global $bp;
 
@@ -190,25 +250,32 @@ function groups_update_group_forum_post( $post_id, $post_text, $topic_id, $page
 	if ( $post_id = bp_forums_insert_post( array( 'post_id' => $post_id, 'post_text' => $post_text, 'post_time' => $post->post_time, 'topic_id' => $topic_id, 'poster_id' => $post->poster_id ) ) ) {
 		$topic = bp_forums_get_topic_details( $topic_id );
 
-		$activity_action = sprintf( __( '%1$s replied to the forum topic %2$s in the group %3$s', 'buddypress'), bp_core_get_userlink( $post->poster_id ), '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . 'forum/topic/' . $topic->topic_slug .'">' . esc_attr( $topic->topic_title ) . '</a>', '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . '">' . esc_attr( $bp->groups->current_group->name ) . '</a>' );
+		$activity_action = sprintf( __( '%1$s replied to the forum topic %2$s in the group %3$s', 'buddypress'), bp_core_get_userlink( $post->poster_id ), '<a href="' . bp_get_group_permalink( groups_get_current_group() ) . 'forum/topic/' . $topic->topic_slug .'">' . esc_attr( $topic->topic_title ) . '</a>', '<a href="' . bp_get_group_permalink( groups_get_current_group() ) . '">' . esc_attr( bp_get_current_group_name() ) . '</a>' );
 		$activity_content = bp_create_excerpt( $post_text );
-		$primary_link = bp_get_group_permalink( $bp->groups->current_group ) . 'forum/topic/' . $topic->topic_slug . '/';
+		$primary_link = bp_get_group_permalink( groups_get_current_group() ) . 'forum/topic/' . $topic->topic_slug . '/';
 
 		if ( $page )
 			$primary_link .= "?topic_page=" . $page;
 
-		// Fetch an existing entry and update if one exists.
-		if ( bp_is_active( 'activity' ) )
-			$id = bp_activity_get_activity_id( array( 'user_id' => $post->poster_id, 'component' => $bp->groups->id, 'type' => 'new_forum_post', 'item_id' => $bp->groups->current_group->id, 'secondary_item_id' => $post_id ) );
+		// Get the corresponding activity item
+		if ( bp_is_active( 'activity' ) ) {
+			$id = bp_activity_get_activity_id( array(
+				'user_id'           => $post->poster_id,
+				'component'         => $bp->groups->id,
+				'type'              => 'new_forum_post',
+				'item_id'           => bp_get_current_group_id(),
+				'secondary_item_id' => $post_id
+			 ) );
+		}
 
 		// Update the entry in activity streams
 		groups_record_activity( array(
 			'id'                => $id,
-			'action'            => apply_filters_ref_array( 'groups_activity_new_forum_post_action',  array( $activity_action,  $post_text, &$topic, &$forum_post ) ),
-			'content'           => apply_filters_ref_array( 'groups_activity_new_forum_post_content', array( $activity_content, $post_text, &$topic, &$forum_post ) ),
+			'action'            => apply_filters_ref_array( 'groups_activity_new_forum_post_action',  array( $activity_action,  $post_text, &$topic, &$topic ) ),
+			'content'           => apply_filters_ref_array( 'groups_activity_new_forum_post_content', array( $activity_content, $post_text, &$topic, &$topic ) ),
 			'primary_link'      => apply_filters( 'groups_activity_new_forum_post_primary_link', $primary_link . "#post-" . $post_id ),
 			'type'              => 'new_forum_post',
-			'item_id'           => (int) $bp->groups->current_group->id,
+			'item_id'           => (int) bp_get_current_group_id(),
 			'user_id'           => (int) $post->poster_id,
 			'secondary_item_id' => $post_id,
 			'recorded_time'     => $post->post_time
@@ -223,7 +290,9 @@ function groups_update_group_forum_post( $post_id, $post_text, $topic_id, $page
 }
 
 /**
- * Handles the forum topic deletion routine
+ * Deletes a group forum topic and also any corresponding activity items.
+ *
+ * Uses the bundled version of bbPress packaged with BuddyPress.
  *
  * @package BuddyPress
  *
@@ -232,6 +301,8 @@ function groups_update_group_forum_post( $post_id, $post_text, $topic_id, $page
  * @uses bp_forums_delete_topic() to do the deletion itself
  * @param int $topic_id The id of the topic to be deleted
  * @return bool True if the delete routine went through properly
+ *
+ * @since 1.1
  */
 function groups_delete_group_forum_topic( $topic_id ) {
 	global $bp;
@@ -242,14 +313,24 @@ function groups_delete_group_forum_topic( $topic_id ) {
 	if ( bp_forums_delete_topic( array( 'topic_id' => $topic_id ) ) ) {
 		do_action( 'groups_before_delete_group_forum_topic', $topic_id );
 
-		// Delete the activity stream items
+		// Delete the corresponding activity stream items
 		if ( bp_is_active( 'activity' ) ) {
 			// The activity item for the initial topic
-			bp_activity_delete( array( 'item_id' => $bp->groups->current_group->id, 'secondary_item_id' => $topic_id, 'component' => $bp->groups->id, 'type' => 'new_forum_topic' ) );
+			bp_activity_delete( array(
+				'item_id'           => bp_get_current_group_id(),
+				'secondary_item_id' => $topic_id,
+				'component'         => $bp->groups->id,
+				'type'              => 'new_forum_topic'
+			) );
 
 			// The activity item for each post
 			foreach ( (array) $posts as $post ) {
-				bp_activity_delete( array( 'item_id' => $bp->groups->current_group->id, 'secondary_item_id' => $post->post_id, 'component' => $bp->groups->id, 'type' => 'new_forum_post' ) );
+				bp_activity_delete( array(
+					'item_id'           => bp_get_current_group_id(),
+					'secondary_item_id' => $post->post_id,
+					'component'         => $bp->groups->id,
+					'type'              => 'new_forum_post'
+				) );
 			}
 		}
 
@@ -262,7 +343,9 @@ function groups_delete_group_forum_topic( $topic_id ) {
 }
 
 /**
- * Delete a forum post
+ * Deletes a group forum post and its corresponding activity item.
+ *
+ * Uses the bundled version of bbPress packaged with BuddyPress.
  *
  * @package BuddyPress
  *
@@ -270,6 +353,8 @@ function groups_delete_group_forum_topic( $topic_id ) {
  * @param int $topic_id Optional. The topic to which the post belongs. This value isn't used in the
  *   function but is passed along to do_action() hooks.
  * @return bool True on success.
+ *
+ * @since 1.1
  */
 function groups_delete_group_forum_post( $post_id, $topic_id = false ) {
 	global $bp;
@@ -277,9 +362,14 @@ function groups_delete_group_forum_post( $post_id, $topic_id = false ) {
 	if ( bp_forums_delete_post( array( 'post_id' => $post_id ) ) ) {
 		do_action( 'groups_before_delete_group_forum_post', $post_id, $topic_id );
 
-		// Delete the activity stream item
+		// Delete the corresponding activity stream item
 		if ( bp_is_active( 'activity' ) )
-			bp_activity_delete( array( 'item_id' => $bp->groups->current_group->id, 'secondary_item_id' => $post_id, 'component' => $bp->groups->id, 'type' => 'new_forum_post' ) );
+			bp_activity_delete( array(
+				'item_id'           => bp_get_current_group_id(),
+				'secondary_item_id' => $post_id,
+				'component'         => $bp->groups->id,
+				'type'              => 'new_forum_post'
+			) );
 
 		do_action( 'groups_delete_group_forum_post', $post_id, $topic_id );
 
@@ -289,6 +379,15 @@ function groups_delete_group_forum_post( $post_id, $topic_id = false ) {
 	return false;
 }
 
+/**
+ * Get a total count of all public topics of a given type, across groups/forums
+ *
+ * @package BuddyPress
+ * @since 1.5
+ *
+ * @param string $type Either 'newest', 'popular', 'unreplied', 'tags'.  Defaults to 'newest'.
+ * @return int The topic count
+ */
 function groups_total_public_forum_topic_count( $type = 'newest' ) {
 	return apply_filters( 'groups_total_public_forum_topic_count', BP_Groups_Group::get_global_forum_topic_count( $type ) );
 }
@@ -297,7 +396,7 @@ function groups_total_public_forum_topic_count( $type = 'newest' ) {
  * Get a total count of all topics of a given status, across groups/forums
  *
  * @package BuddyPress
- * @since BuddyPress (1.5)
+ * @since 1.5
  *
  * @param str $status 'public', 'private', 'hidden', 'all' Which group types to count
  * @return int The topic count
