[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-members/ -> bp-members-invitations.php (source)

   1  <?php
   2  /**
   3   * BuddyPress Membersip Invitations
   4   *
   5   * @package BuddyPress
   6   * @subpackage MembersInvitations
   7   * @since 8.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Set up the displayed user's Members Invitations nav.
  15   *
  16   * @since 8.0.0
  17   */
  18  function bp_members_invitations_setup_nav() {
  19      if ( ! bp_get_members_invitations_allowed() ) {
  20          return;
  21      }
  22  
  23      $user_has_access     = bp_user_has_access();
  24      $default_subnav_slug = ( bp_is_my_profile() && bp_user_can( bp_displayed_user_id(), 'bp_members_invitations_view_send_screen' ) ) ? 'send-invites' : 'list-invites';
  25  
  26      /* Add 'Invitations' to the main user profile navigation */
  27      bp_core_new_nav_item(
  28          array(
  29              'name'                    => __( 'Invitations', 'buddypress' ),
  30              'slug'                    => bp_get_members_invitations_slug(),
  31              'position'                => 80,
  32              'screen_function'         => 'members_screen_send_invites',
  33              'default_subnav_slug'     => $default_subnav_slug,
  34              'show_for_displayed_user' => $user_has_access && bp_user_can( bp_displayed_user_id(), 'bp_members_invitations_view_screens' )
  35          )
  36      );
  37  
  38      $parent_link = trailingslashit( bp_displayed_user_domain() . bp_get_members_invitations_slug() );
  39  
  40      /* Create two subnav items for community invitations */
  41      bp_core_new_subnav_item(
  42          array(
  43              'name'            => __( 'Send Invites', 'buddypress' ),
  44              'slug'            => 'send-invites',
  45              'parent_slug'     => bp_get_members_invitations_slug(),
  46              'parent_url'      => $parent_link,
  47              'screen_function' => 'members_screen_send_invites',
  48              'position'        => 10,
  49              'user_has_access' => $user_has_access && bp_is_my_profile() && bp_user_can( bp_displayed_user_id(), 'bp_members_invitations_view_send_screen' )
  50          )
  51      );
  52  
  53      bp_core_new_subnav_item(
  54          array(
  55              'name'            => __( 'Pending Invites', 'buddypress' ),
  56              'slug'            => 'list-invites',
  57              'parent_slug'     => bp_get_members_invitations_slug(),
  58              'parent_url'      => $parent_link,
  59              'screen_function' => 'members_screen_list_sent_invites',
  60              'position'        => 20,
  61              'user_has_access' => $user_has_access && bp_user_can( bp_displayed_user_id(), 'bp_members_invitations_view_screens' )
  62          )
  63      );
  64  }
  65  add_action( 'bp_setup_nav', 'bp_members_invitations_setup_nav' );
  66  
  67  /**
  68   * When a user joins the network via an invitation, skip sending the activation email.
  69   *
  70   * @since 8.0.0
  71   *
  72   * @param bool   $send       Whether or not to send the activation key.
  73   * @param int    $user_id    User ID to send activation key to.
  74   * @param string $user_email User email to send activation key to.
  75   *
  76   * @return bool Whether or not to send the activation key.
  77   */
  78  function bp_members_invitations_cancel_activation_email( $send, $user_id = 0, $user_email = '' ) {
  79      $invite = bp_members_invitations_get_invites(
  80          array(
  81              'invitee_email' => $user_email,
  82              'invite_sent'   => 'sent',
  83          )
  84      );
  85  
  86      if ( $invite ) {
  87          $send = false;
  88      }
  89  
  90      return $send;
  91  }
  92  add_filter( 'bp_core_signup_send_activation_key', 'bp_members_invitations_cancel_activation_email', 10, 3 );
  93  
  94  /**
  95   * When a user joins the network via an invitation:
  96   * - mark all invitations and requests as accepted
  97   * - activate the user upon signup
  98   *
  99   * @since 8.0.0
 100   *
 101   * @global BuddyPress $bp The one true BuddyPress instance.
 102   *
 103   * @param bool|WP_Error $user_id True on success, WP_Error on failure.
 104   */
 105  function bp_members_invitations_complete_signup( $user_id ) {
 106      $bp = buddypress();
 107  
 108      if ( ! $user_id ) {
 109          return;
 110      }
 111  
 112      // Check to see if this signup is the result of a valid invitation.
 113      $invite = bp_get_members_invitation_from_request();
 114      if ( ! $invite->id ) {
 115          return;
 116      }
 117  
 118      // Accept the invitation.
 119      $invites_class = new BP_Members_Invitation_Manager();
 120      $args          = array(
 121          'id' => $invite->id,
 122      );
 123      $invites_class->accept_invitation( $args );
 124  
 125      // User has already verified their email by responding to the invitation, so we can activate.
 126      $key = bp_get_user_meta( $user_id, 'activation_key', true );
 127      if ( $key ) {
 128          /**
 129           * Filters the activation signup.
 130           *
 131           * @since 1.1.0
 132           *
 133           * @param bool|int $value Value returned by activation.
 134           *                        Integer on success, boolean on failure.
 135           */
 136          $user = apply_filters( 'bp_core_activate_account', bp_core_activate_signup( $key ) );
 137  
 138          // If there were errors, add a message and redirect.
 139          if ( ! empty( $user->errors ) ) {
 140              bp_core_add_message( $user->get_error_message(), 'error' );
 141              bp_core_redirect( trailingslashit( bp_get_root_domain() . '/' . $bp->pages->activate->slug ) );
 142          }
 143  
 144          bp_core_add_message( __( 'Your account is now active!', 'buddypress' ) );
 145          bp_core_redirect( add_query_arg( 'activated', '1', bp_get_activation_page() ) );
 146      }
 147  }
 148  add_action( 'bp_core_signup_user', 'bp_members_invitations_complete_signup' );
 149  
 150  /**
 151   * Delete site membership invitations when an opt-out request is saved.
 152   *
 153   * @since 8.0.0
 154   *
 155   * @param BP_Optout $optout Characteristics of the opt-out just saved.
 156   */
 157  function bp_members_invitations_delete_optedout_invites( $optout ) {
 158      bp_members_invitations_delete_invites(
 159          array(
 160              'invitee_email' => $optout->email_address,
 161          )
 162      );
 163  }
 164  add_action( 'bp_optout_after_save', 'bp_members_invitations_delete_optedout_invites' );


Generated: Fri Sep 24 01:01:36 2021 Cross-referenced by PHPXref 0.7.1