bp_nouveau_ajax_joinleave_group()
Join or leave a group when clicking the “join/leave” button via a POST request.
Description
Return
(string) HTML
Source
File: bp-templates/bp-nouveau/includes/groups/ajax.php
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | function bp_nouveau_ajax_joinleave_group() { $response = array ( 'feedback' => sprintf( '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><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() || empty ( $_POST [ 'action' ] ) ) { wp_send_json_error( $response ); } if ( empty ( $_POST [ 'nonce' ] ) || empty ( $_POST [ 'item_id' ] ) || ! bp_is_active( 'groups' ) ) { wp_send_json_error( $response ); } // Use default nonce $nonce = $_POST [ 'nonce' ]; $check = 'bp_nouveau_groups' ; // 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 gid as integer. $group_id = (int) $_POST [ 'item_id' ]; $errors = array ( 'cannot' => sprintf( '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>' , esc_html__( 'You cannot join this group.' , 'buddyboss' ) ), 'member' => sprintf( '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>' , esc_html__( 'You are already a member of the group.' , 'buddyboss' ) ), ); if ( groups_is_user_banned( bp_loggedin_user_id(), $group_id ) ) { $response [ 'feedback' ] = $errors [ 'cannot' ]; wp_send_json_error( $response ); } // Validate and get the group $group = groups_get_group( array ( 'group_id' => $group_id ) ); if ( empty ( $group ->id ) ) { wp_send_json_error( $response ); } // Manage all button's possible actions here. switch ( $_POST [ 'action' ] ) { case 'groups_accept_invite' : if ( ! groups_accept_invite( bp_loggedin_user_id(), $group_id ) ) { $response = array ( 'feedback' => sprintf( '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>' , esc_html__( 'Group invitation could not be accepted.' , 'buddyboss' ) ), 'type' => 'error' , ); } else { if ( bp_is_active( 'activity' ) ) { groups_record_activity( array ( 'type' => 'joined_group' , 'item_id' => $group ->id, ) ); } // User is now a member of the group $group ->is_member = '1' ; $response = array ( 'feedback' => sprintf( '<div class="bp-feedback success"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>' , esc_html__( 'Group invite accepted.' , 'buddyboss' ) ), 'type' => 'success' , 'is_user' => bp_is_user(), 'contents' => bp_get_group_join_button( $group ), 'is_group' => bp_is_group(), ); } break ; case 'groups_reject_invite' : if ( ! groups_reject_invite( bp_loggedin_user_id(), $group_id ) ) { $response = array ( 'feedback' => sprintf( '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>' , esc_html__( 'Group invite could not be rejected' , 'buddyboss' ) ), 'type' => 'error' , ); } else { $response = array ( 'feedback' => sprintf( '<div class="bp-feedback success"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>' , esc_html__( 'Group invite rejected' , 'buddyboss' ) ), 'type' => 'success' , 'is_user' => bp_is_user(), ); } break ; case 'groups_join_group' : if ( groups_is_user_member( bp_loggedin_user_id(), $group ->id ) ) { $response = array ( 'feedback' => $errors [ 'member' ], 'type' => 'error' , ); } elseif ( 'public' !== $group ->status ) { $response = array ( 'feedback' => $errors [ 'cannot' ], 'type' => 'error' , ); } elseif ( ! groups_join_group( $group ->id ) ) { $response = array ( 'feedback' => sprintf( '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>' , esc_html__( 'Error joining this group.' , 'buddyboss' ) ), 'type' => 'error' , ); } else { // User is now a member of the group $group ->is_member = '1' ; $response = array ( 'contents' => bp_get_group_join_button( $group ), 'is_group' => bp_is_group(), 'type' => 'success' , ); } break ; case 'groups_request_membership' : if ( ! groups_send_membership_request( bp_loggedin_user_id(), $group ->id ) ) { $response = array ( 'feedback' => sprintf( '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>' , esc_html__( 'Error requesting membership.' , 'buddyboss' ) ), 'type' => 'error' , ); } else { // Request is pending $group ->is_pending = '1' ; $response = array ( 'contents' => bp_get_group_join_button( $group ), 'is_group' => bp_is_group(), 'type' => 'success' , ); } break ; case 'groups_leave_group' : if ( ! groups_leave_group( $group ->id ) ) { $response = array ( 'feedback' => sprintf( '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>' , esc_html__( 'Error leaving group.' , 'buddyboss' ) ), 'type' => 'error' , ); } else { // User is no more a member of the group $group ->is_member = '0' ; $bp = buddypress(); /** * When inside the group or in the loggedin user's group memberships screen * we need to reload the page. */ $bp_is_group = bp_is_group() || ( bp_is_user_groups() && bp_is_my_profile() ); $response = array ( 'contents' => bp_get_group_join_button( $group ), 'is_group' => $bp_is_group , 'type' => 'success' , ); // Reset the message if not in a Group or in a loggedin user's group memberships one! if ( ! $bp_is_group && isset( $bp ->template_message ) && isset( $bp ->template_message_type ) ) { unset( $bp ->template_message, $bp ->template_message_type ); @setcookie( 'bp-message' , false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() ); @setcookie( 'bp-message-type' , false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() ); } } break ; } if ( 'error' === $response [ 'type' ] ) { wp_send_json_error( $response ); } wp_send_json_success( $response ); } |
Changelog
Version | Description |
---|---|
BuddyPress 3.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.