[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-groups/actions/ -> leave-group.php (source)

   1  <?php
   2  /**
   3   * Groups: Leave action
   4   *
   5   * @package BuddyPress
   6   * @subpackage GroupActions
   7   * @since 3.0.0
   8   */
   9  
  10  /**
  11   * Catch and process "Leave Group" button clicks.
  12   *
  13   * When a group member clicks on the "Leave Group" button from a group's page,
  14   * this function is run.
  15   *
  16   * Note: When leaving a group from the group directory, AJAX is used and
  17   * another function handles this. See {@link bp_legacy_theme_ajax_joinleave_group()}.
  18   *
  19   * @since 1.2.4
  20   *
  21   * @return bool
  22   */
  23  function groups_action_leave_group() {
  24      if ( ! bp_is_single_item() || ! bp_is_groups_component() || ! bp_is_current_action( 'leave-group' ) ) {
  25          return false;
  26      }
  27  
  28      // Nonce check.
  29      if ( ! check_admin_referer( 'groups_leave_group' ) ) {
  30          return false;
  31      }
  32  
  33      // User wants to leave any group.
  34      if ( groups_is_user_member( bp_loggedin_user_id(), bp_get_current_group_id() ) ) {
  35          $bp = buddypress();
  36  
  37          // Stop sole admins from abandoning their group.
  38          $group_admins = groups_get_group_admins( bp_get_current_group_id() );
  39  
  40          if ( 1 == count( $group_admins ) && $group_admins[0]->user_id == bp_loggedin_user_id() ) {
  41              bp_core_add_message( __( 'This group must have at least one admin', 'buddypress' ), 'error' );
  42          } elseif ( ! groups_leave_group( $bp->groups->current_group->id ) ) {
  43              bp_core_add_message( __( 'There was an error leaving the group.', 'buddypress' ), 'error' );
  44          } else {
  45              bp_core_add_message( __( 'You successfully left the group.', 'buddypress' ) );
  46          }
  47  
  48          $group = groups_get_current_group();
  49          $redirect = bp_get_group_permalink( $group );
  50  
  51          if ( ! $group->is_visible ) {
  52              $redirect = trailingslashit( bp_loggedin_user_domain() . bp_get_groups_slug() );
  53          }
  54  
  55          bp_core_redirect( $redirect );
  56      }
  57  
  58      /** This filter is documented in bp-groups/bp-groups-actions.php */
  59      bp_core_load_template( apply_filters( 'groups_template_group_home', 'groups/single/home' ) );
  60  }
  61  add_action( 'bp_actions', 'groups_action_leave_group' );
  62  
  63  /**
  64   * Clean up requests/invites when a member leaves a group.
  65   *
  66   * @since 5.0.0
  67   */
  68  function groups_action_clean_up_invites_requests( $user_id, $group_id ) {
  69  
  70      $invites_class = new BP_Groups_Invitation_Manager();
  71      // Remove invitations/requests where the deleted user is the receiver.
  72      $invites_class->delete( array(
  73          'user_id' => $user_id,
  74          'item_id' => $group_id,
  75          'type'    => 'all'
  76      ) );
  77      /**
  78       * Remove invitations where the deleted user is the sender.
  79       * We'll use groups_uninvite_user() so that notifications will be cleaned up.
  80       */ 
  81      $pending_invites = groups_get_invites( array(
  82          'inviter_id' => $user_id,
  83          'item_id'    => $group_id,
  84      ) );
  85  
  86      if ( $pending_invites ) {
  87          foreach ( $pending_invites as $invite ) {
  88              groups_uninvite_user( $invite->user_id, $group_id, $user_id );
  89          }
  90      }
  91  }
  92  add_action( 'bp_groups_member_after_delete', 'groups_action_clean_up_invites_requests', 10, 2 );


Generated: Fri Oct 18 01:01:36 2019 Cross-referenced by PHPXref 0.7.1