示例#1
1
/**
 * Follow button
 */
function get_follow_button()
{
    if (bp_follow_is_following(array('leader_id' => get_the_author_meta('ID'), 'follower_id' => bp_loggedin_user_id()))) {
        $link_text = __('Unfollow', 'artgorae');
    } else {
        $link_text = __('Follow', 'artgorae');
    }
    $args = array('leader_id' => get_the_author_meta('ID'), 'follower_id' => bp_loggedin_user_id(), 'link_text' => $link_text, 'link_class' => 'button alt', 'wrapper' => '');
    echo bp_follow_get_add_follow_button($args);
}
/**
 * AJAX callback when clicking on the "Unfollow" button to unfollow a user.
 *
 * @uses check_admin_referer() Checks to make sure the WP security nonce matches.
 * @uses bp_follow_stop_following() Stops a user following another user.
 * @uses bp_follow_is_following() Checks to see if a user is following another user already.
 */
function bp_follow_ajax_action_stop()
{
    check_admin_referer('stop_following');
    $link_class = !empty($_POST['link_class']) ? str_replace('unfollow ', '', $_POST['link_class']) : false;
    // successful unfollow
    if (bp_follow_stop_following(array('leader_id' => $_POST['uid'], 'follower_id' => bp_loggedin_user_id()))) {
        // output follow button
        $output = bp_follow_get_add_follow_button(array('leader_id' => $_POST['uid'], 'follower_id' => bp_loggedin_user_id(), 'wrapper' => false, 'link_class' => $link_class));
        // failed unfollow
    } else {
        // output fallback invalid button
        $args = array('id' => 'invalid', 'link_href' => 'javascript:;', 'component' => 'follow', 'wrapper' => false, 'link_class' => $link_class);
        if (!bp_follow_is_following(array('leader_id' => $_POST['uid'], 'follower_id' => bp_loggedin_user_id()))) {
            $output = bp_get_button(array_merge(array('link_text' => __('Not following', 'bp-follow')), $args));
        } else {
            $output = bp_get_button(array_merge(array('link_text' => __('Error unfollowing user', 'bp-follow')), $args));
        }
    }
    echo $output;
    exit;
}
/**
 * Output a follow / unfollow button for a given user depending on the follower status.
 *
 * @param mixed $args See bp_follow_get_add_follow_button() for full arguments.
 * @uses bp_follow_get_add_follow_button() Returns the follow / unfollow button
 * @author r-a-y
 * @since 1.1
 */
function bp_follow_add_follow_button($args = '')
{
    echo bp_follow_get_add_follow_button($args);
}