<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][12918] trunk/src/bp-members/bp-members-functions.php: Member Invites: Add creation, deletion functions.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://buddypress.trac.wordpress.org/changeset/12918">12918</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/12918","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dcavins</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-04-28 23:50:21 +0000 (Wed, 28 Apr 2021)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Member Invites: Add creation, deletion functions.

Introduce functions that apply the logic in
`BP_Members_Invitation_Manager`, including:
{U+2022} `bp_members_invitations_get_invites`
{U+2022} `bp_members_invitations_user_has_sent_invites`
{U+2022} `bp_members_invitations_invite_user`
{U+2022} `bp_members_invitation_resend_by_id`
{U+2022} `bp_members_invitations_delete_by_id`
{U+2022} `bp_members_invitations_delete_invites`
{U+2022} `bp_members_invitations_get_hash`
{U+2022} `bp_get_members_invitation_from_request`

See <a href="http://buddypress.trac.wordpress.org/ticket/8139">#8139</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpmembersbpmembersfunctionsphp">trunk/src/bp-members/bp-members-functions.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpmembersbpmembersfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-members/bp-members-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-members/bp-members-functions.php     2021-04-28 23:50:09 UTC (rev 12917)
+++ trunk/src/bp-members/bp-members-functions.php       2021-04-28 23:50:21 UTC (rev 12918)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3342,3 +3342,300 @@
</span><span class="cx" style="display: block; padding: 0 10px">        bp_send_email( 'core-user-activation', $user_id, array( 'tokens' => $welcome_tokens ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'bp_core_activated_user', 'bp_send_welcome_email', 10, 1 );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Get invitations to the BP community filtered by arguments.
+ *
+ * @since 8.0.0
+ *
+ * @param array $args     Invitation arguments.
+ *                        See BP_Invitation::get() for list.
+ *
+ * @return array $invites     Matching BP_Invitation objects.
+ */
+function bp_members_invitations_get_invites( $args = array() ) {
+       $invites_class = new BP_Members_Invitation_Manager();
+       return $invites_class->get_invitations( $args );
+}
+
+/**
+ * Check whether a user has sent any community invitations.
+ *
+ * @since 8.0.0
+ *
+ * @param int $user_id ID of user to check for invitations sent by.
+ *                     Defaults to the current user's ID.
+ *
+ * @return bool $invites True if user has sent invites.
+ */
+function bp_members_invitations_user_has_sent_invites( $user_id = 0 ) {
+       if ( 0 === $user_id ) {
+               $user_id = bp_loggedin_user_id();
+               if ( ! $user_id ) {
+                       return false;
+               }
+       }
+       $invites_class = new BP_Members_Invitation_Manager();
+       $args = array(
+               'inviter_id' => $user_id,
+       );
+       return (bool) $invites_class->invitation_exists( $args );
+}
+
+/**
+ * Invite a user to a BP community.
+ *
+ * @since 8.0.0
+ *
+ * @param array|string $args {
+ *     Array of arguments.
+ *     @type int    $invitee_email Email address of the user being invited.
+ *     @type int    $network_id    ID of the network to which the user is being invited.
+ *     @type int    $inviter_id    Optional. ID of the inviting user. Default:
+ *                                 ID of the logged-in user.
+ *     @type string $date_modified Optional. Modified date for the invitation.
+ *                                 Default: current date/time.
+ *     @type string $content       Optional. Message to invitee.
+ *     @type bool   $send_invite   Optional. Whether the invitation should be
+ *                                 sent now. Default: false.
+ * }
+ * @return bool True on success, false on failure.
+ */
+function bp_members_invitations_invite_user( $args ) {
+       $r = bp_parse_args( $args, array(
+               'invitee_email' => '',
+               'network_id'    => get_current_network_id(),
+               'inviter_id'    => bp_loggedin_user_id(),
+               'date_modified' => bp_core_current_time(),
+               'content'       => '',
+               'send_invite'   => 0
+       ), 'community_invite_user' );
+
+       $inv_args = array(
+               'invitee_email' => $r['invitee_email'],
+               'item_id'       => $r['network_id'],
+               'inviter_id'    => $r['inviter_id'],
+               'date_modified' => $r['date_modified'],
+               'content'       => $r['content'],
+               'send_invite'   => $r['send_invite']
+       );
+
+       // Create the invitataion.
+       $invites_class = new BP_Members_Invitation_Manager();
+       $created       = $invites_class->add_invitation( $inv_args );
+
+       /**
+        * Fires after the creation of a new network invite.
+        *
+        * @since 8.0.0
+        *
+        * @param array    $r       Array of parsed arguments for the network invite.
+        * @param int|bool $created The ID of the invitation or false if it couldn't be created.
+        */
+       do_action( 'bp_members_invitations_invite_user', $r, $created );
+
+       return $created;
+}
+
+/**
+ * Resend a membership invitation email by id.
+ *
+ * @since 8.0.0
+ *
+ * @param int $id ID of the invitation to resend.
+ * @return bool True on success, false on failure.
+ */
+function bp_members_invitation_resend_by_id( $id = 0 ) {
+
+       // Find the invitation before deleting it.
+       $existing_invite = new BP_Invitation( $id );
+       $invites_class   = new BP_Members_Invitation_Manager();
+       $success         = $invites_class->send_invitation_by_id( $id );
+
+       if ( ! $success ) {
+               return $success;
+       }
+
+       /**
+        * Fires after the re-sending of a network invite.
+        *
+        * @since 8.0.0
+        *
+        * @param BP_Invitation $existing_invite The invitation that was resent.
+        */
+       do_action( 'bp_members_invitations_resend_invitation', $existing_invite );
+
+       return $success;
+}
+
+/**
+ * Delete a membership invitation by id.
+ *
+ * @since 8.0.0
+ *
+ * @param int $id ID of the invitation to delete.
+ * @return int|bool Number of rows deleted on success, false on failure.
+ */
+function bp_members_invitations_delete_by_id( $id = 0 ) {
+
+       // Find the invitation before deleting it.
+       $existing_invite = new BP_Invitation( $id );
+       $invites_class   = new BP_Members_Invitation_Manager();
+       $success         = $invites_class->delete_by_id( $id );
+
+       if ( ! $success ) {
+               return $success;
+       }
+
+       // Run a different action depending on the status of the invite.
+       if ( ! $existing_invite->invite_sent ) {
+               /**
+                * Fires after the deletion of an unsent community invite.
+                *
+                * @since 8.0.0
+                *
+                * @param BP_Invitation $existing_invite The invitation to be deleted.
+                */
+               do_action( 'bp_members_invitations_canceled_invitation', $existing_invite );
+       } else if ( ! $existing_invite->accepted ) {
+               /**
+                * Fires after the deletion of a sent, but not yet accepted, community invite.
+                *
+                * @since 8.0.0
+                *
+                * @param BP_Invitation $existing_invite The invitation to be deleted.
+                */
+               do_action( 'bp_members_invitations_revoked_invitation', $existing_invite );
+       } else {
+               /**
+                * Fires after the deletion of a sent and accepted community invite.
+                *
+                * @since 8.0.0
+                *
+                * @param BP_Invitation $existing_invite The invitation to be deleted.
+                */
+               do_action( 'bp_members_invitations_deleted_invitation', $existing_invite );
+       }
+
+       return $success;
+}
+
+/**
+ * Delete a membership invitation.
+ *
+ * @since 8.0.0
+ *
+ * @param intring $args {
+ *     Array of arguments.
+ *     @type int|array $id            Id(s) of the invitation(s) to remove.
+ *     @type int       $invitee_email Email address of the user being invited.
+ *     @type int       $network_id    ID of the network to which the user is being invited.
+ *     @type int       $inviter_id    ID of the inviting user.
+ *     @type int       $accepted      Whether the invitation has been accepted yet.
+ *     @type int       $invite_sent   Whether the invitation has been sent yet.
+ * }
+ * @return bool True if all were deleted.
+ */
+function bp_members_invitations_delete_invites( $args ) {
+       $r = bp_parse_args( $args, array(
+               'id'            => false,
+               'invitee_email' => '',
+               'network_id'    => get_current_network_id(),
+               'inviter_id'    => null,
+               'accepted'      => null,
+               'invite_sent'   => null
+       ), 'members_invitations_delete_invites' );
+
+       $inv_args = array(
+               'id'            => $r['id'],
+               'invitee_email' => $r['invitee_email'],
+               'item_id'       => $r['network_id'],
+               'inviter_id'    => $r['inviter_id'],
+               'accepted'      => $r['accepted'],
+               'invite_sent'   => $r['invite_sent']
+       );
+
+       // Find the invitation(s).
+       $invites       = bp_members_invitations_get_invites( $inv_args );
+       $total_count   = count( $invites );
+
+       // Loop through, deleting each invitation.
+       $deleted = 0;
+       foreach ( $invites as $invite ) {
+               $success = bp_members_invitations_delete_by_id( $invite->id );
+               if ( $success ) {
+                       $deleted++;
+               }
+       }
+
+       return $deleted === $total_count;
+}
+
+/**
+ * Get hash based on details of a membership invitation and the inviter.
+ *
+ * @since 8.0.0
+ *
+ * @param BP_Invitation object $invitation Invitation to create hash from.
+ *
+ * @return string $hash Calculated sha1 hash.
+ */
+function bp_members_invitations_get_hash( BP_Invitation $invitation ) {
+       $hash = false;
+
+       if ( ! empty( $invitation->id ) ) {
+               $inviter_ud = get_userdata( $invitation->inviter_id );
+               if ( $inviter_ud ) {
+                       /*
+                        * Use some inviter details as part of the hash so that invitations from
+                        * users who are subsequently marked as spam will be invalidated.
+                        */
+                       $hash = wp_hash( "{$invitation->inviter_id}:{$invitation->invitee_email}:{$inviter_ud->user_status}:{$inviter_ud->user_registered}" );
+               }
+       }
+
+       // If there's a problem, return a string that will change and thus fail.
+       if ( ! $hash ) {
+               $hash = wp_generate_password( 32, false );
+       }
+
+       /**
+        * Filters the hash calculated by the invitation details.
+        *
+        * @since 8.0.0
+        *
+        * @param string $hash Calculated sha1 hash.
+        * @param BP_Invitation object $invitation Invitation hash was created from.
+        */
+       return apply_filters( 'bp_members_invitations_get_hash', $hash, $invitation );
+}
+
+/**
+ * Get the current invitation specified by the $_GET parameters.
+ *
+ * @since 8.0.0
+ *
+ * @return BP_Invitation $invite Invitation specified by the $_GET parameters.
+ */
+function bp_get_members_invitation_from_request() {
+       $invites_class = new BP_Members_Invitation_Manager();
+       $invite        = $invites_class->get_by_id( 0 );
+
+       if ( bp_get_members_invitations_allowed() && ! empty( $_GET['inv'] ) ) {
+               // Check to make sure the passed hash matches a calculated hash.
+               $maybe_invite = $invites_class->get_by_id( absint( $_GET['inv'] ) );
+               $hash = bp_members_invitations_get_hash( $maybe_invite );
+               if ( $_GET['ih'] === $hash ) {
+                       $invite = $maybe_invite;
+               }
+       }
+
+       /**
+        * Filters the invitation specified by the $_GET parameters.
+        *
+        * @since 8.0.0
+        *
+        * @param BP_Invitation $invite Invitation specified by the $_GET parameters.
+        */
+       return apply_filters( 'bp_get_members_invitation_from_request', $invite );
+}
</ins></span></pre>
</div>
</div>

</body>
</html>