[ 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 = 'invite', $r  ) {
  71          // If the user is already a member (because BP at one point allowed two invitations to
  72          // slip through), return early.
  73          if ( groups_is_user_member( $r['user_id'], $r['item_id'] ) ) {
  74              return true;
  75          }
  76  
  77          // Create the new membership
  78          $member = new BP_Groups_Member( $r['user_id'], $r['item_id'] );
  79  
  80          if ( 'request' === $type ) {
  81              $member->accept_request();
  82          } else {
  83              $member->accept_invite();
  84          }
  85  
  86          if ( ! $member->save() ) {
  87              return false;
  88          }
  89  
  90          if ( 'request' === $type ) {
  91              /**
  92               * Fires after a group membership request has been accepted.
  93               *
  94               * @since 1.0.0
  95               *
  96               * @param int  $user_id  ID of the user who accepted membership.
  97               * @param int  $group_id ID of the group that was accepted membership to.
  98               * @param bool $value    If membership was accepted.
  99               */
 100              do_action( 'groups_membership_accepted', $r['user_id'], $r['item_id'], true );
 101          } else {
 102              // Get an inviter_id from the invitation.
 103              $invites = groups_get_invites( $r );
 104              $inviter_id = 0;
 105              if ( $invites ) {
 106                  $inviter_id = current( $invites )->inviter_id;
 107              }
 108  
 109              /**
 110               * Fires after a user has accepted a group invite.
 111               *
 112               * @since 1.0.0
 113               * @since 2.8.0 The $inviter_id arg was added.
 114               *
 115               * @param int $user_id    ID of the user who accepted the group invite.
 116               * @param int $group_id   ID of the group being accepted to.
 117               * @param int $inviter_id ID of the user who invited this user to the group.
 118               */
 119              do_action( 'groups_accept_invite', $r['user_id'], $r['item_id'], $inviter_id );
 120          }
 121  
 122          // Modify group meta.
 123          groups_update_groupmeta( $r['item_id'], 'last_activity', bp_core_current_time() );
 124  
 125          return true;
 126      }
 127  
 128      /**
 129       * With group invitations, we don't need to keep the old record, so we delete rather than
 130       * mark invitations as "accepted."
 131       *
 132       * @since 5.0.0
 133       *
 134       * @see BP_Invitation::mark_accepted_by_data()
 135       *      for a description of arguments.
 136       *
 137       * @param array $args.
 138       */
 139  	public function mark_accepted( $args ) {
 140          // Delete all existing invitations/requests to this group for this user.
 141          $this->delete( array(
 142              'user_id' => $args['user_id'],
 143              'item_id' => $args['item_id'],
 144              'type'    => 'all'
 145          ) );
 146      }
 147  
 148      /**
 149       * Should this invitation be created?
 150       *
 151       * @since 5.0.0
 152       *
 153       * @param array $args.
 154       * @return bool
 155       */
 156  	public function allow_invitation( $args ) {
 157          // Does the inviter have this capability?
 158          if ( ! bp_user_can( $args['inviter_id'], 'groups_send_invitation', array( 'group_id' => $args['item_id'] ) ) ) {
 159              return false;
 160          }
 161  
 162          // Is the invited user eligible to receive an invitation?
 163          if ( ! bp_user_can( $args['user_id'], 'groups_receive_invitation', array( 'group_id' => $args['item_id'] ) ) ) {
 164              return false;
 165          }
 166  
 167          // Prevent duplicated invitations.
 168          if ( groups_check_has_invite_from_user( $args['user_id'], $args['item_id'], $args['inviter_id'], 'all' ) ) {
 169              return false;
 170          }
 171  
 172          return true;
 173      }
 174  
 175      /**
 176       * Should this request be created?
 177       *
 178       * @since 5.0.0
 179       *
 180       * @param array $args.
 181       * @return bool.
 182       */
 183  	public function allow_request( $args ) {
 184          // Does the requester have this capability? (Also checks for duplicates.)
 185          if ( ! bp_user_can( $args['user_id'], 'groups_request_membership', array( 'group_id' => $args['item_id'] ) ) ) {
 186              return false;
 187          }
 188  
 189          return true;
 190      }
 191  }


Generated: Tue Sep 17 01:01:39 2019 Cross-referenced by PHPXref 0.7.1