[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-groups/classes/ -> class-bp-groups-invitation-manager.php (source)

   1  <?php
   2  /**
   3   * Group invitations class.
   4   *
   5   * @package BuddyPress
   6   * @subpackage Core
   7   * @since 5.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Group invitations class.
  15   *
  16   * An extension of the core Invitations class that adapts the
  17   * core logic to accommodate group invitation behavior.
  18   *
  19   * @since 5.0.0
  20   */
  21  class BP_Groups_Invitation_Manager extends BP_Invitation_Manager {
  22      /**
  23       * Construct parameters.
  24       *
  25       * @since 5.0.0
  26       *
  27       * @param array|string $args.
  28       */
  29  	public function __construct( $args = '' ) {
  30          parent::__construct();
  31      }
  32  
  33      /**
  34       * This is where custom actions are added to run when notifications of an
  35       * invitation or request need to be generated & sent.
  36       *
  37       * @since 5.0.0
  38       *
  39       * @param int $id The ID of the invitation to mark as sent.
  40       * @return bool True on success, false on failure.
  41       */
  42  	public function run_send_action( BP_Invitation $invitation ) {
  43          // Notify group admins of the pending request
  44          if ( 'request' === $invitation->type ) {
  45              $admins = groups_get_group_admins( $invitation->item_id );
  46  
  47              foreach ( $admins as $admin ) {
  48                  groups_notification_new_membership_request( $invitation->user_id, $admin->user_id, $invitation->item_id, $invitation->id );
  49              }
  50              return true;
  51  
  52          // Notify the invitee of the invitation.
  53          } else {
  54              $group = groups_get_group( $invitation->item_id );
  55              groups_notification_group_invites( $group, $invitation->user_id, $invitation->inviter_id );
  56              return true;
  57          }
  58      }
  59  
  60      /**
  61       * This is where custom actions are added to run when an invitation
  62       * or request is accepted.
  63       *
  64       * @since 5.0.0
  65       *
  66       * @param string $type Are we accepting an invitation or request?
  67       * @param array  $r    Parameters that describe the invitation being accepted.
  68       * @return bool True on success, false on failure.
  69       */
  70  	public function run_acceptance_action( $type, $r ) {
  71          if ( ! $type || ! in_array( $type, array( 'request', 'invite' ), true ) ) {
  72              return false;
  73          }
  74  
  75          // If the user is already a member (because BP at one point allowed two invitations to
  76          // slip through), return early.
  77          if ( groups_is_user_member( $r['user_id'], $r['item_id'] ) ) {
  78              return true;
  79          }
  80  
  81          // Create the new membership
  82          $member = new BP_Groups_Member( $r['user_id'], $r['item_id'] );
  83  
  84          if ( 'request' === $type ) {
  85              $member->accept_request();
  86          } else {
  87              $member->accept_invite();
  88          }
  89  
  90          if ( ! $member->save() ) {
  91              return false;
  92          }
  93  
  94          if ( 'request' === $type ) {
  95              /**
  96               * Fires after a group membership request has been accepted.
  97               *
  98               * @since 1.0.0
  99               *
 100               * @param int  $user_id  ID of the user who accepted membership.
 101               * @param int  $group_id ID of the group that was accepted membership to.
 102               * @param bool $value    If membership was accepted.
 103               */
 104              do_action( 'groups_membership_accepted', $r['user_id'], $r['item_id'], true );
 105          } else {
 106              // Get an inviter_id from the invitation.
 107              $invites = groups_get_invites( $r );
 108              $inviter_id = 0;
 109              if ( $invites ) {
 110                  $inviter_id = current( $invites )->inviter_id;
 111              }
 112  
 113              /**
 114               * Fires after a user has accepted a group invite.
 115               *
 116               * @since 1.0.0
 117               * @since 2.8.0 The $inviter_id arg was added.
 118               *
 119               * @param int $user_id    ID of the user who accepted the group invite.
 120               * @param int $group_id   ID of the group being accepted to.
 121               * @param int $inviter_id ID of the user who invited this user to the group.
 122               */
 123              do_action( 'groups_accept_invite', $r['user_id'], $r['item_id'], $inviter_id );
 124          }
 125  
 126          // Modify group meta.
 127          groups_update_groupmeta( $r['item_id'], 'last_activity', bp_core_current_time() );
 128  
 129          return true;
 130      }
 131  
 132      /**
 133       * With group invitations, we don't need to keep the old record, so we delete rather than
 134       * mark invitations as "accepted."
 135       *
 136       * @since 5.0.0
 137       *
 138       * @see BP_Invitation::mark_accepted_by_data()
 139       *      for a description of arguments.
 140       *
 141       * @param array $args.
 142       */
 143  	public function mark_accepted( $args ) {
 144          // Delete all existing invitations/requests to this group for this user.
 145          $this->delete( array(
 146              'user_id' => $args['user_id'],
 147              'item_id' => $args['item_id'],
 148              'type'    => 'all'
 149          ) );
 150      }
 151  
 152      /**
 153       * Should this invitation be created?
 154       *
 155       * @since 5.0.0
 156       *
 157       * @param array $args.
 158       * @return bool
 159       */
 160  	public function allow_invitation( $args ) {
 161          // Does the inviter have this capability?
 162          if ( ! bp_user_can( $args['inviter_id'], 'groups_send_invitation', array( 'group_id' => $args['item_id'] ) ) ) {
 163              return false;
 164          }
 165  
 166          // Is the invited user eligible to receive an invitation?
 167          if ( ! bp_user_can( $args['user_id'], 'groups_receive_invitation', array( 'group_id' => $args['item_id'] ) ) ) {
 168              return false;
 169          }
 170  
 171          // Prevent duplicated invitations.
 172          if ( groups_check_has_invite_from_user( $args['user_id'], $args['item_id'], $args['inviter_id'], 'all' ) ) {
 173              return false;
 174          }
 175  
 176          return true;
 177      }
 178  
 179      /**
 180       * Should this request be created?
 181       *
 182       * @since 5.0.0
 183       *
 184       * @param array $args.
 185       * @return bool.
 186       */
 187  	public function allow_request( $args ) {
 188          // Does the requester have this capability? (Also checks for duplicates.)
 189          if ( ! bp_user_can( $args['user_id'], 'groups_request_membership', array( 'group_id' => $args['item_id'] ) ) ) {
 190              return false;
 191          }
 192  
 193          return true;
 194      }
 195  }


Generated: Thu Nov 21 01:00:57 2024 Cross-referenced by PHPXref 0.7.1