bp_dtheme_ajax_joinleave_group

Join or leave a group when clicking the "join/leave" button via a POST request.

Description

(string) bp_dtheme_ajax_joinleave_group(); 

Returns (string)

HTML


Usage

  1. if ( !function_exists( 'bp_dtheme_ajax_joinleave_group' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-themes/bp-default/_inc/ajax.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = bp_dtheme_ajax_joinleave_group(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/bp-themes/bp-default/_inc/ajax.php  
  1. function bp_dtheme_ajax_joinleave_group() { 
  2. // Bail if not a POST action 
  3. if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) 
  4. return; 
  5.  
  6. // Cast gid as integer 
  7. $group_id = (int) $_POST['gid']; 
  8.  
  9. if ( groups_is_user_banned( bp_loggedin_user_id(), $group_id ) ) 
  10. return; 
  11.  
  12. if ( ! $group = groups_get_group( array( 'group_id' => $group_id ) ) ) 
  13. return; 
  14.  
  15. if ( ! groups_is_user_member( bp_loggedin_user_id(), $group->id ) ) { 
  16. if ( 'public' == $group->status ) { 
  17. check_ajax_referer( 'groups_join_group' ); 
  18.  
  19. if ( ! groups_join_group( $group->id ) ) { 
  20. _e( 'Error joining group', buddypress ); 
  21. } else { 
  22. echo '<a id="group-' . esc_attr( $group->id ) . '" class="leave-group" rel="leave" title="' . __( 'Leave Group', buddypress ) . '" href="' . wp_nonce_url( bp_get_group_permalink( $group ) . 'leave-group', 'groups_leave_group' ) . '">' . __( 'Leave Group', buddypress ) . '</a>'; 
  23.  
  24. } elseif ( 'private' == $group->status ) { 
  25.  
  26. // If the user has already been invited, then this is 
  27. // an Accept Invitation button 
  28. check_ajax_referer( 'groups_accept_invite' ); 
  29.  
  30. if ( ! groups_accept_invite( bp_loggedin_user_id(), $group->id ) ) { 
  31. _e( 'Error requesting membership', buddypress ); 
  32. } else { 
  33. echo '<a id="group-' . esc_attr( $group->id ) . '" class="leave-group" rel="leave" title="' . __( 'Leave Group', buddypress ) . '" href="' . wp_nonce_url( bp_get_group_permalink( $group ) . 'leave-group', 'groups_leave_group' ) . '">' . __( 'Leave Group', buddypress ) . '</a>'; 
  34.  
  35. // Otherwise, it's a Request Membership button 
  36. } else { 
  37. check_ajax_referer( 'groups_request_membership' ); 
  38.  
  39. _e( 'Error requesting membership', buddypress ); 
  40. } else { 
  41. echo '<a id="group-' . esc_attr( $group->id ) . '" class="membership-requested" rel="membership-requested" title="' . __( 'Membership Requested', buddypress ) . '" href="' . bp_get_group_permalink( $group ) . '">' . __( 'Membership Requested', buddypress ) . '</a>'; 
  42.  
  43. } else { 
  44. check_ajax_referer( 'groups_leave_group' ); 
  45.  
  46. if ( ! groups_leave_group( $group->id ) ) { 
  47. _e( 'Error leaving group', buddypress ); 
  48. } elseif ( 'public' == $group->status ) { 
  49. echo '<a id="group-' . esc_attr( $group->id ) . '" class="join-group" rel="join" title="' . __( 'Join Group', buddypress ) . '" href="' . wp_nonce_url( bp_get_group_permalink( $group ) . 'join', 'groups_join_group' ) . '">' . __( 'Join Group', buddypress ) . '</a>'; 
  50. } elseif ( 'private' == $group->status ) { 
  51. echo '<a id="group-' . esc_attr( $group->id ) . '" class="request-membership" rel="join" title="' . __( 'Request Membership', buddypress ) . '" href="' . wp_nonce_url( bp_get_group_permalink( $group ) . 'request-membership', 'groups_send_membership_request' ) . '">' . __( 'Request Membership', buddypress ) . '</a>'; 
  52.  
  53. exit;