/**
  * Action handler when a follow activity button is clicked.
  */
 public function action_listener()
 {
     if (!bp_is_activity_component()) {
         return;
     }
     if (!bp_is_current_action('follow') && !bp_is_current_action('unfollow')) {
         return false;
     }
     if (empty($activity_id) && bp_action_variable(0)) {
         $activity_id = (int) bp_action_variable(0);
     }
     // Not viewing a specific activity item.
     if (empty($activity_id)) {
         return;
     }
     $action = bp_is_current_action('follow') ? 'follow' : 'unfollow';
     // Check the nonce.
     check_admin_referer("bp_follow_activity_{$action}");
     $save = bp_is_current_action('follow') ? 'bp_follow_start_following' : 'bp_follow_stop_following';
     $follow_type = bp_follow_activity_get_type($activity_id);
     // Failure on action
     if (!$save(array('leader_id' => $activity_id, 'follower_id' => bp_loggedin_user_id(), 'follow_type' => $follow_type))) {
         $message_type = 'error';
         if ('follow' === $action) {
             $message = __('You are already following that item.', 'bp-follow');
         } else {
             $message = __('You were not following that item.', 'bp-follow');
         }
         // Success!
     } else {
         $message_type = 'success';
         if ('follow' === $action) {
             $message = __('You are now following that item.', 'bp-follow');
         } else {
             $message = __('You are no longer following that item.', 'bp-follow');
         }
     }
     /**
      * Dynamic filter for the message displayed after the follow button is clicked.
      *
      * Default filter name is 'bp_follow_activity_message_activity'.
      *
      * Handy for plugin devs.
      *
      * @since 1.3.0
      *
      * @param string $message      Message that gets displayed after a follow action.
      * @param string $action       Either 'follow' or 'unfollow'.
      * @param int    $activity_id  Activity ID.
      * @param string $message_type Either 'success' or 'error'.
      */
     $message = apply_filters("bp_follow_activity_message_{$follow_type}", $message, $action, $activity_id, $message_type);
     bp_core_add_message($message, $message_type);
     // Redirect
     $redirect = wp_get_referer() ? wp_get_referer() : bp_get_activity_directory_permalink();
     bp_core_redirect($redirect);
     die;
 }
/**
 * 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));
}