/**
 * Filter the activity loop when we're on a "Following" page
 *
 * This is done:
 *   - On the activity directory and clicking on the "Following" tab
 *   - On a user's "Activity > Following" page
 *
 * @since 1.0.0
 *
 * @param string|array Current activity querystring
 * @param string $object The current object or component
 * @return array
 */
function bp_follow_add_activity_scope_filter($qs, $object)
{
    global $bp;
    // not on the activity object? stop now!
    if ($object != 'activity') {
        return $qs;
    }
    $set = false;
    // activity directory
    // can't use bp_is_activity_directory() yet since that's a BP 2.0 function
    if (!bp_displayed_user_id() && bp_is_activity_component() && !bp_current_action()) {
        // check if activity scope is following before manipulating
        if (isset($_COOKIE['bp-activity-scope']) && 'following' === $_COOKIE['bp-activity-scope']) {
            $set = true;
        }
        // user's activity following page
    } elseif (bp_is_user_activity() && bp_is_current_action('following')) {
        $set = true;
    }
    // not on a user page? stop now!
    if (!$set) {
        return $qs;
    }
    // set internal marker noting that our activity scope is applied
    $bp->follow->activity_scope_set = 1;
    $qs = wp_parse_args($qs);
    $following_ids = bp_get_following_ids(array('user_id' => bp_displayed_user_id() ? bp_displayed_user_id() : bp_loggedin_user_id()));
    // if $following_ids is empty, pass a negative number so no activity can be found
    $following_ids = empty($following_ids) ? -1 : $following_ids;
    $qs['user_id'] = $following_ids;
    return apply_filters('bp_follow_add_activity_scope_filter', $qs, false);
}
/**
 * Filter the activity loop.
 *
 * Specifically, when on the activity directory and clicking on the "Followed
 * Sites" tab.
 *
 * @param str $qs The querystring for the BP loop
 * @param str $object The current object for the querystring
 * @return str Modified querystring
 */
function bp_follow_blogs_add_activity_scope_filter($qs, $object)
{
    // not on the blogs object? stop now!
    if ($object != 'activity') {
        return $qs;
    }
    // parse querystring into an array
    $r = wp_parse_args($qs);
    if (bp_is_current_action(constant('BP_FOLLOW_BLOGS_USER_ACTIVITY_SLUG'))) {
        $r['scope'] = 'followblogs';
    }
    if (!isset($r['scope'])) {
        return $qs;
    }
    if ('followblogs' !== $r['scope']) {
        return $qs;
    }
    // get blog IDs that the user is following
    $following_ids = bp_get_following_ids(array('user_id' => bp_displayed_user_id() ? bp_displayed_user_id() : bp_loggedin_user_id(), 'follow_type' => 'blogs'));
    // if $following_ids is empty, pass a negative number so no blogs can be found
    $following_ids = empty($following_ids) ? -1 : $following_ids;
    $args = array('user_id' => 0, 'object' => 'blogs', 'primary_id' => $following_ids);
    // make sure we add a separator if we have an existing querystring
    if (!empty($qs)) {
        $qs .= '&';
    }
    // add our follow parameters to the end of the querystring
    $qs .= build_query($args);
    // support BP Groupblog
    // We need to filter the WHERE SQL conditions to do this
    if (function_exists('bp_groupblog_init')) {
        add_filter('bp_activity_get_where_conditions', 'bp_follow_blogs_groupblog_activity_where_conditions', 10, 2);
    }
    return $qs;
}
    /**
     * Displays the widget.
     */
    function widget($args, $instance)
    {
        // do not do anything if user isn't logged in
        if (!is_user_logged_in()) {
            return;
        }
        if (empty($instance['max_users'])) {
            $instance['max_users'] = 16;
        }
        // logged-in user isn't following anyone, so stop!
        if (!($following = bp_get_following_ids(array('user_id' => bp_loggedin_user_id())))) {
            return false;
        }
        // show the users the logged-in user is following
        if (bp_has_members(array('include' => $following, 'max' => $instance['max_users'], 'populate_extras' => false))) {
            do_action('bp_before_following_widget');
            echo $args['before_widget'];
            echo $args['before_title'] . $instance['title'] . $args['after_title'];
            ?>

			<div class="avatar-block">
				<?php 
            while (bp_members()) {
                bp_the_member();
                ?>
					<div class="item-avatar">
						<a href="<?php 
                bp_member_permalink();
                ?>
" title="<?php 
                bp_member_name();
                ?>
"><?php 
                bp_member_avatar();
                ?>
</a>
					</div>
				<?php 
            }
            ?>
			</div>

			<?php 
            echo $args['after_widget'];
            ?>

			<?php 
            do_action('bp_after_following_widget');
            ?>

	<?php 
        }
    }
 /**
  * RSS handler for a user's followed sites.
  *
  * When a user lands on /members/USERNAME/activity/followblogs/feed/, this
  * method generates the RSS feed for their followed sites.
  */
 public static function rss_handler()
 {
     // only available in BP 1.8+
     if (!class_exists('BP_Activity_Feed')) {
         return;
     }
     if (!bp_is_user_activity() || !bp_is_current_action(constant('BP_FOLLOW_BLOGS_USER_ACTIVITY_SLUG')) || !bp_is_action_variable('feed', 0)) {
         return;
     }
     // get blog IDs that the user is following
     $following_ids = bp_get_following_ids(array('follow_type' => 'blogs'));
     // if $following_ids is empty, pass a negative number so no blogs can be found
     $following_ids = empty($following_ids) ? -1 : $following_ids;
     $args = array('user_id' => 0, 'object' => 'blogs', 'primary_id' => $following_ids);
     // setup the feed
     buddypress()->activity->feed = new BP_Activity_Feed(array('id' => 'followedsites', 'title' => sprintf(__('%1$s | %2$s | Followed Site Activity', 'bp-follow'), bp_get_site_name(), bp_get_displayed_user_fullname()), 'link' => trailingslashit(bp_displayed_user_domain() . bp_get_activity_slug() . '/' . constant('BP_FOLLOW_BLOGS_USER_ACTIVITY_SLUG')), 'description' => sprintf(__("Activity feed for sites that %s is following.", 'buddypress'), bp_get_displayed_user_fullname()), 'activity_args' => $args));
 }
/**
 * Add RSS feed support for a user's following activity.
 *
 * eg. example.com/members/USERNAME/activity/following/feed/
 *
 * Only available in BuddyPress 1.8+.
 *
 * @since 1.2.1
 * @author r-a-y
 */
function bp_follow_my_following_feed()
{
    // only available in BP 1.8+
    if (!class_exists('BP_Activity_Feed')) {
        return;
    }
    if (!bp_is_user_activity() || !bp_is_current_action(constant('BP_FOLLOWING_SLUG')) || !bp_is_action_variable('feed', 0)) {
        return false;
    }
    global $bp;
    // setup the feed
    $bp->activity->feed = new BP_Activity_Feed(array('id' => 'myfollowing', 'title' => sprintf(__('%1$s | %2$s | Following Activity', 'bp-follow'), bp_get_site_name(), bp_get_displayed_user_fullname()), 'link' => trailingslashit(bp_displayed_user_domain() . bp_get_activity_slug() . '/' . constant('BP_FOLLOWING_SLUG')), 'description' => sprintf(__("Activity feed for people that %s is following.", 'buddypress'), bp_get_displayed_user_fullname()), 'activity_args' => array('user_id' => bp_get_following_ids(), 'display_comments' => 'threaded')));
}
/**
 * Filter the members loop on a follow page.
 *
 * This is done so we can return the users that:
 *   - the current user is following (on a user page or member directory); or
 *   - are following the displayed user on the displayed user's followers page
 *
 * @author r-a-y
 * @since 1.2
 *
 * @param array|string $qs The querystring for the BP loop
 * @param str $object The current object for the querystring
 * @return array|string Modified querystring
 */
function bp_follow_add_member_scope_filter($qs, $object)
{
    // not on the members object? stop now!
    if ($object != 'members') {
        return $qs;
    }
    $set = false;
    // members directory
    // can't use bp_is_members_directory() yet since that's a BP 2.0 function
    if (!bp_is_user() && bp_is_members_component()) {
        // check if members scope is following before manipulating
        if (isset($_COOKIE['bp-members-scope']) && 'following' === $_COOKIE['bp-members-scope']) {
            $set = true;
            $action = 'following';
        }
        // user page
    } elseif (bp_is_user()) {
        $set = true;
        $action = bp_current_action();
    }
    // not on a user page? stop now!
    if (!$set) {
        return $qs;
    }
    // filter the members loop based on the current page
    switch ($action) {
        case 'following':
            // parse querystring into an array
            $qs = wp_parse_args($qs);
            $qs['include'] = bp_get_following_ids(array('user_id' => bp_displayed_user_id() ? bp_displayed_user_id() : bp_loggedin_user_id()));
            $qs['per_page'] = apply_filters('bp_follow_per_page', 20);
            return $qs;
            break;
        case 'followers':
            // parse querystring into an array
            $qs = wp_parse_args($qs);
            $qs['include'] = bp_get_follower_ids();
            $qs['per_page'] = apply_filters('bp_follow_per_page', 20);
            return $qs;
            break;
        default:
            return $qs;
            break;
    }
}
/**
 * Output a comma-separated list of user_ids for a given user's following.
 *
 * @param array $args See bp_get_following_ids().
 */
function bp_following_ids($args = '')
{
    echo bp_get_following_ids($args);
}
/**
 * Filter the members loop on a user's "Following" or "Followers" page.
 *
 * This is done so we can return the users that:
 *   - the current user is following; or
 *   - the users that are following the current user
 *
 * @author r-a-y
 * @since 1.2
 *
 * @param str $qs The querystring for the BP loop
 * @param str $object The current object for the querystring
 * @return str Modified querystring
 */
function bp_follow_add_member_scope_filter($qs, $object)
{
    // not on the members object? stop now!
    if ($object != 'members') {
        return $qs;
    }
    // not on a user page? stop now!
    if (!bp_is_user()) {
        return $qs;
    }
    // filter the members loop based on the current page
    switch (bp_current_action()) {
        // 'following' page
        case constant('BP_FOLLOWING_SLUG'):
            $args = array('include' => bp_get_following_ids(), 'per_page' => apply_filters('bp_follow_per_page', 20));
            // make sure we add a separator if we have an existing querystring
            if (!empty($qs)) {
                $qs .= '&';
            }
            // add our follow parameters to the end of the querystring
            $qs .= build_query($args);
            return $qs;
            break;
            // 'followers' page
        // 'followers' page
        case constant('BP_FOLLOWERS_SLUG'):
            $args = array('include' => bp_get_follower_ids(), 'per_page' => apply_filters('bp_follow_per_page', 20));
            // make sure we add a separator if we have an existing querystring
            if (!empty($qs)) {
                $qs .= '&';
            }
            // add our follow parameters to the end of the querystring
            $qs .= build_query($args);
            return $qs;
            break;
        default:
            return $qs;
            break;
    }
}