[ 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   * @param string $activation_key Activation key to be sent.
  76   * @param array  $usermeta       Miscellaneous metadata about the user (blog-specific
  77   *                               signup data, xprofile data, etc).
  78   * @return bool Whether or not to send the activation key.
  79   */
  80  function bp_members_invitations_cancel_activation_email( $send, $user_id = 0, $user_email = '', $activation_key = '', $usermeta = array() ) {
  81      $invite = bp_members_invitations_get_invites(
  82          array(
  83              'invitee_email' => $user_email,
  84              'invite_sent'   => 'sent'
  85          )
  86      );
  87  
  88      if ( $invite ) {
  89          $send = false;
  90      }
  91  
  92      return $send;
  93  }
  94  add_filter( 'bp_core_signup_send_activation_key', 'bp_members_invitations_cancel_activation_email', 10, 5 );
  95  
  96  /**
  97   * When a user joins the network via an invitation:
  98   * - mark all invitations and requests as accepted
  99   * - activate the user upon signup
 100   *
 101   * @since 8.0.0
 102   *
 103   * @param bool|WP_Error   $user_id       True on success, WP_Error on failure.
 104   * @param string          $user_login    Login name requested by the user.
 105   * @param string          $user_password Password requested by the user.
 106   * @param string          $user_email    Email address requested by the user.
 107   */
 108  function bp_members_invitations_complete_signup( $user_id, $user_login = '', $user_password = '', $user_email = '' ) {
 109      if ( ! $user_id ) {
 110          return;
 111      }
 112  
 113      // Check to see if this signup is the result of a valid invitation.
 114      $invite = bp_get_members_invitation_from_request();
 115      if ( ! $invite->id ) {
 116          return;
 117      }
 118  
 119      // Accept the invitation.
 120      $invites_class = new BP_Members_Invitation_Manager();
 121      $args          = array(
 122          'id' => $invite->id,
 123      );
 124      $invites_class->accept_invitation( $args );
 125  
 126      // User has already verified their email by responding to the invitation, so we can activate.
 127      $key = bp_get_user_meta( $user_id, 'activation_key', true );
 128      if ( $key ) {
 129          /**
 130           * Filters the activation signup.
 131           *
 132           * @since 1.1.0
 133           *
 134           * @param bool|int $value Value returned by activation.
 135           *                        Integer on success, boolean on failure.
 136           */
 137          $user = apply_filters( 'bp_core_activate_account', bp_core_activate_signup( $key ) );
 138  
 139          // If there were errors, add a message and redirect.
 140          if ( ! empty( $user->errors ) ) {
 141              bp_core_add_message( $user->get_error_message(), 'error' );
 142              bp_core_redirect( trailingslashit( bp_get_root_domain() . '/' . $bp->pages->activate->slug ) );
 143          }
 144  
 145          bp_core_add_message( __( 'Your account is now active!', 'buddypress' ) );
 146          bp_core_redirect( add_query_arg( 'activated', '1', bp_get_activation_page() ) );
 147      }
 148  }
 149  add_action( 'bp_core_signup_user', 'bp_members_invitations_complete_signup', 10, 4 );
 150  
 151  /**
 152   * Delete site membership invitations when an opt-out request is saved.
 153   *
 154   * @since 8.0.0
 155   *
 156   * @param BP_Optout $optout Characteristics of the opt-out just saved.
 157   */
 158  function bp_members_invitations_delete_optedout_invites( $optout ) {
 159  
 160      $args = array(
 161          'invitee_email' => $optout->email_address,
 162      );
 163      bp_members_invitations_delete_invites( $args );
 164  }
 165  add_action( 'bp_optout_after_save', 'bp_members_invitations_delete_optedout_invites' );


Generated: Fri Jun 18 01:01:39 2021 Cross-referenced by PHPXref 0.7.1