/**
  * @group delete
  */
 public function test_follow_and_delete_blog()
 {
     if (!is_multisite()) {
         return;
     }
     // create user and blog
     $u = $this->factory->user->create();
     $b = $this->factory->blog->create(array('title' => 'The Foo Bar Blog', 'user_id' => $u));
     // make blog creator follow own blog
     $f = bp_follow_start_following(array('leader_id' => $b, 'follower_id' => $u, 'follow_type' => 'blogs'));
     // assert that follow relationship worked
     $this->assertTrue($f);
     // prime cache
     new BP_Follow($b, $u, 'blogs');
     bp_follow_get_the_following_count(array('user_id' => $u, 'follow_type' => 'blogs'));
     bp_follow_get_the_followers_count(array('object_id' => $b, 'follow_type' => 'blogs'));
     // now delete blog
     wpmu_delete_blog($b);
     // check if cache was deleted
     $this->assertEmpty(wp_cache_get("{$b}:{$u}:blogs", 'bp_follow_data'));
     $this->assertEmpty(wp_cache_get($u, 'bp_follow_user_blogs_following_count'));
     $this->assertEmpty(wp_cache_get($b, 'bp_follow_blogs_followers_count'));
 }
/**
 * Setup profile / BuddyBar navigation.
 *
 * This function was moved from {@link BP_Follow_Component} in v1.3.0 due
 * to the users module being toggleable.
 *
 * @since 1.3.0
 */
function bp_follow_user_setup_nav($main_nav = array(), $sub_nav = array())
{
    global $bp;
    // If we're in the admin area and we're using the WP toolbar, we don't need
    // to run the rest of this method
    if (defined('WP_NETWORK_ADMIN') && bp_use_wp_admin_bar()) {
        return;
    }
    // Need to change the user ID, so if we're not on a member page, $counts variable is still calculated
    $user_id = bp_is_user() ? bp_displayed_user_id() : bp_loggedin_user_id();
    // BuddyBar compatibility
    $domain = bp_displayed_user_domain() ? bp_displayed_user_domain() : bp_loggedin_user_domain();
    /** FOLLOWERS NAV ************************************************/
    bp_core_new_nav_item(array('name' => sprintf(__('Following <span>%d</span>', 'bp-follow'), bp_follow_get_the_following_count(array('user_id' => $user_id))), 'slug' => $bp->follow->following->slug, 'position' => $bp->follow->params['adminbar_myaccount_order'], 'screen_function' => 'bp_follow_screen_following', 'default_subnav_slug' => 'following', 'item_css_id' => 'members-following'));
    /** FOLLOWING NAV ************************************************/
    bp_core_new_nav_item(array('name' => sprintf(__('Followers <span>%d</span>', 'bp-follow'), bp_follow_get_the_followers_count(array('user_id' => $user_id))), 'slug' => $bp->follow->followers->slug, 'position' => apply_filters('bp_follow_followers_nav_position', 62), 'screen_function' => 'bp_follow_screen_followers', 'default_subnav_slug' => 'followers', 'item_css_id' => 'members-followers'));
    /** ACTIVITY SUBNAV **********************************************/
    // Add activity sub nav item
    if (bp_is_active('activity') && apply_filters('bp_follow_show_activity_subnav', true)) {
        bp_core_new_subnav_item(array('name' => _x('Following', 'Activity subnav tab', 'bp-follow'), 'slug' => constant('BP_FOLLOWING_SLUG'), 'parent_url' => trailingslashit($domain . bp_get_activity_slug()), 'parent_slug' => bp_get_activity_slug(), 'screen_function' => 'bp_follow_screen_activity_following', 'position' => 21, 'item_css_id' => 'activity-following'));
    }
    // BuddyBar compatibility
    add_action('bp_adminbar_menus', 'bp_follow_group_buddybar_items');
}
/**
 * Output a 'Follow' activity button.
 *
 * @param $args {
 *      Array of arguments.  Also see other args via {@link BP_Button} class.
 *      @type int  $leader_id           Activity ID to follow.
 *      @type int  $follower_id         User ID initiating the follow request.
 *      @type bool $show_follower_count Should we show the follower count for this item? Default: false.
 * }
 */
function bp_follow_activity_button($args = array())
{
    global $activities_template;
    $r = bp_parse_args($args, array('leader_id' => !empty($activities_template->in_the_loop) ? bp_get_activity_id() : 0, 'follower_id' => bp_loggedin_user_id(), 'link_text' => '', 'link_title' => '', 'wrapper_class' => '', 'link_class' => 'button bp-primary-action', 'wrapper' => false, 'show_follower_count' => false), 'follow_activity_button');
    if (!$r['leader_id'] || !$r['follower_id']) {
        return;
    }
    $follow_type = bp_follow_activity_get_type($r['leader_id']);
    // if we're checking during an activity loop, then follow status is already
    // queried via bulk_inject_follow_activity_status()
    if (!empty($activities_template->in_the_loop) && $r['follower_id'] == bp_loggedin_user_id() && $r['leader_id'] == bp_get_activity_id() && 'activity' === $follow_type) {
        $is_following = $activities_template->activity->is_following;
        // else we manually query the follow status
    } else {
        $is_following = bp_follow_is_following(array('leader_id' => $r['leader_id'], 'follower_id' => $r['follower_id'], 'follow_type' => $follow_type));
    }
    // setup some variables
    if ($is_following) {
        $id = 'following';
        $action = 'unfollow';
        /* @todo Maybe bring back the count for the 'unfollow' button?
        		$count  = bp_follow_get_the_followers_count( array(
        			'object_id'   => $r['leader_id'],
        			'follow_type' => $follow_type
        		) );
        		*/
        $count = 0;
        if (empty($count)) {
            $link_text = _x('Unfollow', 'Follow activity button', 'bp-follow');
        } else {
            $link_text = sprintf(_x('Unfollow %s', 'Follow activity button', 'bp-follow'), '<span>' . $count . '</span>');
        }
        if (empty($r['link_text'])) {
            $r['link_text'] = $link_text;
        }
    } else {
        $id = 'not-following';
        $action = 'follow';
        $count = 0;
        if (true === $r['show_follower_count']) {
            $count = bp_follow_get_the_followers_count(array('object_id' => $r['leader_id'], 'follow_type' => $follow_type));
        }
        if (empty($count)) {
            $link_text = _x('Follow', 'Follow activity button', 'bp-follow');
        } else {
            $link_text = sprintf(_x('Follow %s', 'Follow activity button', 'bp-follow'), '<span>' . $count . '</span>');
        }
        if (empty($r['link_text'])) {
            $r['link_text'] = $link_text;
        }
    }
    $wrapper_class = 'follow-button ' . $id;
    if (!empty($r['wrapper_class'])) {
        $wrapper_class .= ' ' . esc_attr($r['wrapper_class']);
    }
    $link_class = $action;
    if (!empty($r['link_class'])) {
        $link_class .= ' ' . esc_attr($r['link_class']);
    }
    // setup the button arguments
    $button = array('id' => $id, 'component' => 'follow', 'must_be_logged_in' => true, 'block_self' => false, 'wrapper_class' => $wrapper_class, 'wrapper_id' => 'follow-button-' . (int) $r['leader_id'], 'link_href' => wp_nonce_url(trailingslashit(bp_get_activity_directory_permalink() . $action . '/' . esc_attr($r['leader_id'])), "bp_follow_activity_{$action}"), 'link_text' => $r['link_text'], 'link_title' => esc_attr($r['link_title']), 'link_id' => $action . '-' . (int) $r['leader_id'], 'link_class' => $link_class, 'wrapper' => !empty($r['wrapper']) ? esc_attr($r['wrapper']) : false);
    // Filter and output the HTML button
    bp_button(apply_filters('bp_follow_activity_get_follow_button', $button, $r, $is_following));
}
/**
 * Get the total followers and total following counts for a user.
 *
 * You shouldn't really use this function any more.
 *
 * @see bp_follow_get_the_following_count() To grab the following count.
 * @see bp_follow_get_the_followers_count() To grab the followers count.
 *
 * @since 1.0.0
 *
 * @param array $args {
 *     Array of arguments.
 *     @type int    $user_id     The user ID to grab follow counts for.
 *     @type string $follow_type The follow type. Default to '', which will query follow counts for users.
 *                               Passing a follow type such as 'blogs' will only return a 'following'
 *                               key and integer zero for the 'followers' key since a user can only follow
 *                               blogs.
 * }
 * @return array [ followers => int, following => int ]
 */
function bp_follow_total_follow_counts($args = '')
{
    $r = wp_parse_args($args, array('user_id' => bp_loggedin_user_id(), 'follow_type' => ''));
    $retval = array();
    $retval['following'] = bp_follow_get_the_following_count(array('user_id' => $r['user_id'], 'follow_type' => $r['follow_type']));
    /**
     * Passing a follow type such as 'blogs' will only return a 'following'
     * key and integer zero for the 'followers' key since a user can only follow
     * blogs.
     */
    if (!empty($r['follow_type'])) {
        $retval['followers'] = 0;
    } else {
        $retval['followers'] = bp_follow_get_the_followers_count(array('user_id' => $r['user_id'], 'follow_type' => $r['follow_type']));
    }
    if (empty($r['follow_type'])) {
        /**
         * Filter the total follow counts for a user.
         *
         * @since 1.0.0
         *
         * @param array $retval  Array consisting of 'following' and 'followers' counts.
         * @param int   $user_id The user ID. Defaults to logged-in user ID.
         */
        $retval = apply_filters('bp_follow_total_follow_counts', $retval, $r['user_id']);
    } else {
        /**
         * Filter the total follow counts for a user given a specific follow type.
         *
         * @since 1.3.0
         *
         * @param array $retval  Array consisting of 'following' and 'followers' counts. Note: 'followers'
         *                       is always going to be 0, since a user can only follow a given follow type.
         * @param int   $user_id The user ID. Defaults to logged-in user ID.
         */
        $retval = apply_filters('bp_follow_total_follow_' . $r['follow_type'] . '_counts', $retval, $r['user_id']);
    }
    return $retval;
}