Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
12/10/2021 04:14:37 PM (3 years ago)
Author:
dcavins
Message:

Add membership request-related emails and notifications.

Add 'members-membership-request' email and
'membership_request_submitted' notification
which is sent to site admins when a new request
is submitted. Also add
'members-membership-request-rejected' email
which is sent to the requester when denied.

See #8582.

t

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-members/bp-members-membership-requests.php

    r13167 r13168  
    136136add_filter( 'bp_core_signup_send_activation_key_multisite_blog', 'bp_members_membership_requests_cancel_activation_email_multisite', 10, 5 );
    137137
     138/**
     139 * Notifications
     140 *********************************************************************/
     141
     142/**
     143 * Notify site admins about a new membership request.
     144 *
     145 * @since 10.0.0
     146 *
     147 * @param BP_Signup $signup The signup object that has been created.
     148 */
     149function bp_members_membership_requests_notify_site_admins( $signup ) {
     150
     151    if ( ! isset( $signup->signup_id ) ) {
     152        return;
     153    }
     154
     155    // Notify all site admins so the request can be handled.
     156    $admin_ids = get_users(
     157        array(
     158            'fields' => 'ids',
     159            'role'   => 'administrator',
     160        )
     161    );
     162
     163    foreach ( $admin_ids as $admin_id ) {
     164        // Trigger a BuddyPress Notification.
     165        if ( bp_is_active( 'notifications' ) ) {
     166            bp_notifications_add_notification(
     167                array(
     168                    'user_id'          => $admin_id,
     169                    'item_id'          => $signup->signup_id,
     170                    'component_name'   => buddypress()->members->id,
     171                    'component_action' => 'membership_request_submitted',
     172                    'date_notified'    => bp_core_current_time(),
     173                    'is_new'           => 1,
     174                )
     175            );
     176        }
     177
     178        // Bail if member opted out of receiving this email.
     179        if ( 'no' === bp_get_user_meta( $admin_id, 'notification_members_membership_request', true ) ) {
     180            return;
     181        }
     182
     183        $unsubscribe_args = array(
     184            'user_id'           => $admin_id,
     185            'notification_type' => 'members-membership-request',
     186        );
     187
     188        $manage_url = add_query_arg(
     189            array(
     190                'mod_req'   => 1,
     191                'page'      => 'bp-signups',
     192                'signup_id' => $signup->signup_id,
     193                'action'    => 'resend',
     194            ),
     195            bp_get_admin_url( 'users.php' )
     196        );
     197
     198        $args  = array(
     199            'tokens' => array(
     200                'admin.id'                   => $admin_id,
     201                'manage.url'                 => esc_url_raw( $manage_url ),
     202                'requesting-user.user_login' => esc_html( $signup->user_login ),
     203                'unsubscribe'                => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),
     204            ),
     205        );
     206
     207        bp_send_email( 'members-membership-request', (int) $admin_id, $args );
     208    }
     209}
     210add_action( 'bp_members_membership_request_submitted', 'bp_members_membership_requests_notify_site_admins' );
     211
     212/**
     213 * Send a message to the requesting user when his or her
     214 * site membership request has been rejected.
     215 *
     216 * @since 10.0.0
     217 *
     218 * @param array $signup_ids Array of pending IDs to delete.
     219 */
     220function bp_members_membership_requests_send_rejection_mail( $signup_ids ) {
     221    $signups = BP_Signup::get(
     222        array(
     223            'include' => $signup_ids,
     224        )
     225    );
     226
     227    if ( empty( $signups['signups'] ) ) {
     228        return;
     229    }
     230
     231    foreach ( $signups['signups'] as $signup ) {
     232        if ( ! empty( $signup->user_email ) ) {
     233            bp_send_email( 'members-membership-request-rejected', $signup->user_email );
     234        }
     235    }
     236}
     237add_action( 'bp_core_signup_before_delete', 'bp_members_membership_requests_send_rejection_mail' );
     238
     239/**
     240 * When a request is approved, activated or deleted,
     241 * delete the associated notifications.
     242 *
     243 * @since 10.0.0
     244 *
     245 * @param array $signup_ids Array of changing signup IDs.
     246 */
     247function bp_members_membership_requests_delete_notifications_on_change( $signup_ids ) {
     248    foreach ( $signup_ids as $signup_id ) {
     249        BP_Notifications_Notification::delete(
     250            array(
     251                'item_id'          => $signup_id,
     252                'component_action' => 'membership_request_submitted',
     253            )
     254        );
     255    }
     256}
     257add_action( 'bp_core_signup_after_resend',   'bp_members_membership_requests_delete_notifications_on_change' );
     258add_action( 'bp_core_signup_after_activate', 'bp_members_membership_requests_delete_notifications_on_change' );
     259add_action( 'bp_core_signup_after_delete',   'bp_members_membership_requests_delete_notifications_on_change' );
     260
Note: See TracChangeset for help on using the changeset viewer.