bp_nouveau_ajax_followunfollow_member()

Follow/Unfollow a user via a POST request.

Description

Return

(string) HTML

Source

File: bp-templates/bp-nouveau/includes/follow/ajax.php

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
function bp_nouveau_ajax_followunfollow_member() {
    $response = array(
        'feedback' => sprintf(
            '<div class="bp-feedback error bp-ajax-message"><p>%s</p></div>',
            esc_html__( 'There was a problem performing this action. Please try again.', 'buddyboss' )
        ),
    );
 
    // Bail if not a POST action.
    if ( ! bp_is_post_request() ) {
        wp_send_json_error( $response );
    }
 
    if ( empty( $_POST['_wpnonce'] ) || empty( $_POST['item_id'] ) ) {
        wp_send_json_error( $response );
    }
 
    // Use default nonce
    $nonce = $_POST['_wpnonce'];
    $check = 'bp_nouveau_follow';
 
    // Use a specific one for actions needed it
    if ( ! empty( $_POST['_wpnonce'] ) && ! empty( $_POST['action'] ) ) {
        $nonce = $_POST['_wpnonce'];
        $check = $_POST['action'];
    }
 
    // Nonce check!
    if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, $check ) ) {
        wp_send_json_error( $response );
    }
 
    // Cast fid as an integer.
    $leader_id = (int) $_POST['item_id'];
 
    // Check if the user exists
    if ( isset( $_POST['action'] ) ) {
        $user = get_user_by( 'id', $leader_id );
        if ( ! $user ) {
            wp_send_json_error(
                array(
                    'feedback' => sprintf(
                        '<div class="bp-feedback error">%s</div>',
                        esc_html__( 'No member found with that ID.', 'buddyboss' )
                    ),
                )
            );
        }
    }
 
    $is_following = bp_is_following( array( 'leader_id' => $leader_id, 'follower_id' => bp_loggedin_user_id() ) );
 
    // Trying to unfollow.
    if ( $is_following ) {
        if ( ! bp_stop_following( array( 'leader_id' => $leader_id, 'follower_id' => bp_loggedin_user_id() ) ) ) {
 
            $response['feedback'] = sprintf(
                '<div class="bp-feedback error">%s</div>',
                esc_html__( 'There was a problem when trying to unfollow this user.', 'buddyboss' )
            );
 
            wp_send_json_error( $response );
        } else {
 
            if ( bp_has_members( 'include=' . $leader_id ) ) {
                while ( bp_members() ) {
                    bp_the_member();
                    wp_send_json_success( array(
                        'contents' => bp_get_add_follow_button( $leader_id, bp_loggedin_user_id(), array(
                            'parent_element' => 'li',
                            'button_element' => 'button'
                        ) )
                    ) );
                }
            } else {
                wp_send_json_success( array( 'contents' => '' ) );
            }
        }
 
        // Trying to follow.
    } elseif ( ! $is_following ) {
        if ( ! bp_start_following( array( 'leader_id' => $leader_id, 'follower_id' => bp_loggedin_user_id() ) ) ) {
 
            $response['feedback'] = sprintf(
                '<div class="bp-feedback error">%s</div>',
                esc_html__( 'There was a problem when trying to follow this user.', 'buddyboss' )
            );
 
            wp_send_json_error( $response );
        } else {
            if ( bp_has_members( 'include=' . $leader_id ) ) {
                while ( bp_members() ) {
                    bp_the_member();
                    wp_send_json_success( array(
                        'contents' => bp_get_add_follow_button( $leader_id, bp_loggedin_user_id(), array(
                            'parent_element' => 'li',
                            'button_element' => 'button'
                        ) )
                    ) );
                }
            } else {
                wp_send_json_success( array( 'contents' => '' ) );
            }
        }
    } else {
        $response['feedback'] = sprintf(
            '<div class="bp-feedback error">%s</div>',
            esc_html__( 'Request Pending', 'buddyboss' )
        );
 
        wp_send_json_error( $response );
    }
}

Changelog

Changelog
Version Description
BuddyBoss 1.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.