[ 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          $redirect = bp_get_activation_page();
 129  
 130          /**
 131           * Filters the activation signup.
 132           *
 133           * @since 1.1.0
 134           *
 135           * @param bool|int $value Value returned by activation.
 136           *                        Integer on success, boolean on failure.
 137           */
 138          $user = apply_filters( 'bp_core_activate_account', bp_core_activate_signup( $key ) );
 139  
 140          // If there were errors, add a message and redirect.
 141          if ( ! empty( $user->errors ) ) {
 142              /**
 143               * Filter here to redirect the User to a different URL than the activation page.
 144               *
 145               * @since 10.0.0
 146               *
 147               * @param string   $redirect The URL to use to redirect the user.
 148               * @param WP_Error $user     The WP Error object.
 149               */
 150              $redirect = apply_filters( 'bp_members_invitations_activation_errored_redirect', $redirect, $user );
 151  
 152              bp_core_add_message( $user->get_error_message(), 'error' );
 153              bp_core_redirect( $redirect );
 154          }
 155  
 156          /**
 157           * Filter here to redirect the User to a different URL than the activation page.
 158           *
 159           * @since 10.0.0
 160           *
 161           * @param string $redirect The URL to use to redirect the user.
 162           */
 163          $redirect = apply_filters( 'bp_members_invitations_activation_successed_redirect', $redirect );
 164  
 165          bp_core_add_message( __( 'Your account is now active!', 'buddypress' ) );
 166          bp_core_redirect( add_query_arg( 'activated', '1', $redirect ) );
 167      }
 168  }
 169  add_action( 'bp_core_signup_user', 'bp_members_invitations_complete_signup' );
 170  
 171  /**
 172   * Delete site membership invitations when an opt-out request is saved.
 173   *
 174   * @since 8.0.0
 175   *
 176   * @param BP_Optout $optout Characteristics of the opt-out just saved.
 177   */
 178  function bp_members_invitations_delete_optedout_invites( $optout ) {
 179      bp_members_invitations_delete_invites(
 180          array(
 181              'invitee_email' => $optout->email_address,
 182          )
 183      );
 184  }
 185  add_action( 'bp_optout_after_save', 'bp_members_invitations_delete_optedout_invites' );
 186  
 187  /**
 188   * If a user submits a site membership request, but there's a
 189   * sent invitation to her, bypass the manual approval of the request.
 190   *
 191   * @since 10.0.0
 192   *
 193   * @param bool  $send    Whether or not this membership request should be approved
 194   *                       immediately and the activation email sent.
 195   *                       Default is `false` meaning that the request should be
 196   *                       manually approved by a site admin.
 197   * @param array $details The details of the request.
 198   */
 199  function bp_members_invitations_maybe_bypass_request_approval( $send, $details ) {
 200      if ( ! bp_get_members_invitations_allowed() ) {
 201          return $send;
 202      }
 203  
 204      // We'll need the prospective user's email address.
 205      if ( empty( $details['user_email'] ) ) {
 206          return $send;
 207      }
 208  
 209      $invites = bp_members_invitations_get_invites(
 210          array(
 211              'invitee_email' => $details['user_email'],
 212              'invite_sent'   => 'sent'
 213          )
 214      );
 215  
 216      // If pending invitations exist, send the verification mail.
 217      if ( $invites ) {
 218          $send = true;
 219      }
 220  
 221      return $send;
 222  }
 223  add_filter( 'bp_members_membership_requests_bypass_manual_approval', 'bp_members_invitations_maybe_bypass_request_approval', 10, 2 );
 224  add_filter( 'bp_members_membership_requests_bypass_manual_approval_multisite', 'bp_members_invitations_maybe_bypass_request_approval', 10, 2 );


Generated: Sun May 22 01:00:58 2022 Cross-referenced by PHPXref 0.7.1