Skip to:
Content

BuddyPress.org

Changeset 13168


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

Location:
trunk/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-functions.php

    r13159 r13168  
    38133813 *
    38143814 * @since 2.5.1
     3815 * @since 10.0.0 Added members-membership-request and
     3816 *               members-membership-request-rejected email types.
    38153817 *
    38163818 * @return array
     
    39723974            /* translators: do not remove {} brackets or translate its contents. */
    39733975            'post_excerpt' => __( "{{inviter.name}} has invited you to join the site \"{{site.name}}\".\n\n{{usermessage}}\n\nTo accept your invitation, visit: {{{invite.accept_url}}}\n\nTo learn more about the site, visit: {{{site.url}}}.\nTo view {{inviter.name}}'s profile, visit: {{{inviter.url}}}", 'buddypress' ),
     3976        ),
     3977        'members-membership-request' => array(
     3978            /* translators: do not remove {} brackets or translate its contents. */
     3979            'post_title'   => __( '{{requesting-user.user_login}} would like to join {{site.name}}', 'buddypress' ),
     3980            /* translators: do not remove {} brackets or translate its contents. */
     3981            'post_content' => __( "{{requesting-user.user_login}} would like to join the site: &quot;{{site.name}}&quot;.\n\n<a href=\"{{{manage.url}}}\">Manage the request</a>.", 'buddypress' ),
     3982            /* translators: do not remove {} brackets or translate its contents. */
     3983            'post_excerpt' => __( "{{requesting-user.user_login}} would like to join the site \"{{site.name}}\".\n\nTo manage the request, visit: {{{manage.url}}}.", 'buddypress' ),
     3984        ),
     3985        'members-membership-request-rejected' => array(
     3986            /* translators: do not remove {} brackets or translate its contents. */
     3987            'post_title'   => __( 'Your request to join {{site.name}} has been declined', 'buddypress' ),
     3988            /* translators: do not remove {} brackets or translate its contents. */
     3989            'post_content' => __( "Sorry, your request to join the site &quot;{{site.name}}&quot; has been declined.", 'buddypress' ),
     3990            /* translators: do not remove {} brackets or translate its contents. */
     3991            'post_excerpt' => __( "Sorry, your request to join the site \"{{site.name}}\" has been declined.", 'buddypress' ),
    39743992        ),
    39753993    ) );
     
    41454163    );
    41464164
     4165    $members_membership_request = array(
     4166        'description'      => __( 'Someone has requested membership on this site.', 'buddypress' ),
     4167        'named_salutation' => true,
     4168        'unsubscribe'      => array(
     4169            'meta_key' => 'notification_members_membership_request',
     4170            'message'  => __( 'You will no longer receive emails when people submit requests to join this site.', 'buddypress' ),
     4171        ),
     4172    );
     4173
     4174    $members_membership_request_rejected = array(
     4175        'description'      => __( 'A site membership request has been rejected.', 'buddypress' ),
     4176        'named_salutation' => false,
     4177        'unsubscribe'      => false,
     4178    );
     4179
    41474180    $types = array(
    4148         'activity-comment'                   => $activity_comment,
    4149         'activity-comment-author'            => $activity_comment_author,
    4150         'activity-at-message'                => $activity_at_message,
    4151         'groups-at-message'                  => $groups_at_message,
    4152         'core-user-registration'             => $core_user_registration,
    4153         'core-user-registration-with-blog'   => $core_user_registration_with_blog,
    4154         'friends-request'                    => $friends_request,
    4155         'friends-request-accepted'           => $friends_request_accepted,
    4156         'groups-details-updated'             => $groups_details_updated,
    4157         'groups-invitation'                  => $groups_invitation,
    4158         'groups-member-promoted'             => $groups_member_promoted,
    4159         'groups-membership-request'          => $groups_membership_request,
    4160         'messages-unread'                    => $messages_unread,
    4161         'settings-verify-email-change'       => $settings_verify_email_change,
    4162         'groups-membership-request-accepted' => $groups_membership_request_accepted,
    4163         'groups-membership-request-rejected' => $groups_membership_request_rejected,
    4164         'core-user-activation'               => $core_user_activation,
    4165         'bp-members-invitation'              => $members_invitation,
     4181        'activity-comment'                    => $activity_comment,
     4182        'activity-comment-author'             => $activity_comment_author,
     4183        'activity-at-message'                 => $activity_at_message,
     4184        'groups-at-message'                   => $groups_at_message,
     4185        'core-user-registration'              => $core_user_registration,
     4186        'core-user-registration-with-blog'    => $core_user_registration_with_blog,
     4187        'friends-request'                     => $friends_request,
     4188        'friends-request-accepted'            => $friends_request_accepted,
     4189        'groups-details-updated'              => $groups_details_updated,
     4190        'groups-invitation'                   => $groups_invitation,
     4191        'groups-member-promoted'              => $groups_member_promoted,
     4192        'groups-membership-request'           => $groups_membership_request,
     4193        'messages-unread'                     => $messages_unread,
     4194        'settings-verify-email-change'        => $settings_verify_email_change,
     4195        'groups-membership-request-accepted'  => $groups_membership_request_accepted,
     4196        'groups-membership-request-rejected'  => $groups_membership_request_rejected,
     4197        'core-user-activation'                => $core_user_activation,
     4198        'bp-members-invitation'               => $members_invitation,
     4199        'members-membership-request'          => $members_membership_request,
     4200        'members-membership-request-rejected' => $members_membership_request_rejected,
    41664201    );
    41674202
  • trunk/src/bp-core/bp-core-update.php

    r12929 r13168  
    279279            bp_update_to_8_0();
    280280        }
     281
     282        // Version 10.0.0.
     283        if ( $raw_db_version < 13165 ) {
     284            bp_update_to_10_0();
     285        }
     286
    281287    }
    282288
     
    700706    if ( isset( $emails['bp-members-invitation'] ) ) {
    701707        $new_emails['bp-members-invitation'] = $emails['bp-members-invitation'];
     708    }
     709
     710    return $new_emails;
     711}
     712
     713/**
     714 * 10.0.0 update routine.
     715 *
     716 * - Install new BP Emails for membership requests.
     717 *
     718 * @since 10.0.0
     719 */
     720function bp_update_to_10_0() {
     721
     722    // Install membership request emails.
     723    add_filter( 'bp_email_get_schema', 'bp_core_get_10_0_upgrade_email_schema' );
     724
     725    bp_core_install_emails();
     726
     727    remove_filter( 'bp_email_get_schema', 'bp_core_get_10_0_upgrade_email_schema' );
     728}
     729
     730/**
     731 * Select only the emails that need to be installed with version 10.0.
     732 *
     733 * @since 10.0.0
     734 *
     735 * @param array $emails The array of emails schema.
     736 */
     737function bp_core_get_10_0_upgrade_email_schema( $emails ) {
     738    $new_emails = array();
     739
     740    if ( isset( $emails['members-membership-request'] ) ) {
     741        $new_emails['members-membership-request'] = $emails['members-membership-request'];
     742    }
     743
     744    if ( isset( $emails['members-membership-request-rejected'] ) ) {
     745        $new_emails['members-membership-request-rejected'] = $emails['members-membership-request-rejected'];
    702746    }
    703747
  • 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
  • trunk/src/bp-members/bp-members-notifications.php

    r13105 r13168  
    6060                    $text = sprintf( __( '%s is now a member of the site', 'buddypress' ),  bp_core_get_user_displayname( $item_id ) );
    6161                }
     62            }
     63            break;
     64
     65        case 'membership_request_submitted':
     66            // $item_id is the id of the signup, not the user ID.
     67            $signup = new BP_Signup( $item_id );
     68
     69            // Set up the string and the filter.
     70            if ( (int) $total_items > 1 ) {
     71                $link   = bp_get_notifications_permalink();
     72                $amount = 'multiple';
     73
     74                $text = sprintf( __( '%d people have requested site membership.', 'buddypress' ), (int) $total_items );
     75            } else {
     76                $link   = add_query_arg( array(
     77                    'mod_req'   => 1,
     78                    'page'      => 'bp-signups',
     79                    'signup_id' => $item_id,
     80                    'action'    => 'resend',
     81                ), bp_get_admin_url( 'users.php' ) );
     82                $amount = 'single';
     83
     84                /* translators: %s: new user name */
     85                $text = sprintf( __( '%s has requested site membership.', 'buddypress' ),  esc_html( $signup->user_login ) );
    6286            }
    6387            break;
     
    168192        ),
    169193        array(
    170             'user_id' => bp_loggedin_user_id(),
    171             'item_id' => bp_displayed_user_id(),
     194            'user_id'          => bp_loggedin_user_id(),
     195            'item_id'          => bp_displayed_user_id(),
     196            'component_action' => 'accepted_invitation',
    172197        )
    173198    );
     
    176201
    177202/**
     203 * Mark new membership request notifications as read when user visits Manage BP Signups screen.
     204 *
     205 * @since 10.0.0
     206 */
     207function bp_members_mark_read_submitted_membership_request_notification() {
     208
     209    $signup_screens = array( 'users_page_bp-signups', 'users_page_bp-signups-network' );
     210    if ( ! wp_doing_ajax() && in_array( get_current_screen()->base, $signup_screens, true ) && ! empty( $_GET['mod_req'] ) && ! empty( $_GET['signup_id'] ) ) {
     211        // Mark all notifications about this request as read.
     212        BP_Notifications_Notification::update(
     213            array(
     214                'is_new' => false,
     215            ),
     216            array(
     217                'item_id'          => $_GET['signup_id'],
     218                'component_action' => 'membership_request_submitted',
     219            )
     220        );
     221    }
     222}
     223add_action( 'admin_footer', 'bp_members_mark_read_submitted_membership_request_notification' );
     224
     225/**
    178226 * Add Members-related settings to the Settings > Notifications page.
    179227 *
     
    182230function members_screen_notification_settings() {
    183231
    184     // Bail early if invitations are not allowed--they are the only members notification so far.
    185     if ( ! bp_get_members_invitations_allowed () ) {
     232    // Bail early if invitations and requests are not allowed--they are the only members notification so far.
     233    if ( ! bp_get_members_invitations_allowed() && ( ! bp_get_membership_requests_required() || ! user_can( bp_displayed_user_id(), 'bp_moderate' ) ) ) {
    186234        return;
    187     }
    188 
    189     if ( ! $allow_acceptance_emails = bp_get_user_meta( bp_displayed_user_id(), 'notification_members_invitation_accepted', true ) ) {
    190         $allow_acceptance_emails = 'yes';
    191235    }
    192236    ?>
     
    203247
    204248        <tbody>
    205             <tr id="members-notification-settings-invitation_accepted">
    206                 <td></td>
    207                 <td><?php _ex( 'Someone accepts your membership invitation', 'Member settings on notification settings page', 'buddypress' ) ?></td>
    208                 <td class="yes"><input type="radio" name="notifications[notification_members_invitation_accepted]" id="notification-members-invitation-accepted-yes" value="yes" <?php checked( $allow_acceptance_emails, 'yes', true ) ?>/><label for="notification-members-invitation-accepted-yes" class="bp-screen-reader-text"><?php
    209                     /* translators: accessibility text */
    210                     _e( 'Yes, send email', 'buddypress' );
    211                 ?></label></td>
    212                 <td class="no"><input type="radio" name="notifications[notification_members_invitation_accepted]" id="notification-members-invitation-accepted-no" value="no" <?php checked( $allow_acceptance_emails, 'no', true ) ?>/><label for="notification-members-invitation-accepted-no" class="bp-screen-reader-text"><?php
    213                     /* translators: accessibility text */
    214                     _e( 'No, do not send email', 'buddypress' );
    215                 ?></label></td>
    216             </tr>
    217249
    218250            <?php
     251            if ( bp_get_members_invitations_allowed() ) :
     252                if ( ! $allow_acceptance_emails = bp_get_user_meta( bp_displayed_user_id(), 'notification_members_invitation_accepted', true ) ) {
     253                    $allow_acceptance_emails = 'yes';
     254                }
     255                ?>
     256                <tr id="members-notification-settings-invitation_accepted">
     257                    <td></td>
     258                    <td><?php echo esc_html_x( 'Someone accepts your membership invitation', 'Member settings on notification settings page', 'buddypress' ); ?></td>
     259                    <td class="yes"><input type="radio" name="notifications[notification_members_invitation_accepted]" id="notification-members-invitation-accepted-yes" value="yes" <?php checked( $allow_acceptance_emails, 'yes', true ) ?>/><label for="notification-members-invitation-accepted-yes" class="bp-screen-reader-text">
     260                        <?php
     261                        /* translators: accessibility text */
     262                        esc_html_e( 'Yes, send email', 'buddypress' );
     263                        ?>
     264                    </label></td>
     265                    <td class="no"><input type="radio" name="notifications[notification_members_invitation_accepted]" id="notification-members-invitation-accepted-no" value="no" <?php checked( $allow_acceptance_emails, 'no', true ) ?>/><label for="notification-members-invitation-accepted-no" class="bp-screen-reader-text">
     266                        <?php
     267                        /* translators: accessibility text */
     268                        esc_html_e( 'No, do not send email', 'buddypress' );
     269                        ?>
     270                    </label></td>
     271                </tr>
     272                <?php
     273            endif;
     274
     275            if ( bp_get_membership_requests_required() && user_can( bp_displayed_user_id(), 'bp_moderate' ) ) :
     276                if ( ! $allow_request_emails = bp_get_user_meta( bp_displayed_user_id(), 'notification_members_membership_request', true ) ) {
     277                    $allow_request_emails = 'yes';
     278                }
     279                ?>
     280                <tr id="members-notification-settings-submitted_membership_request">
     281                    <td></td>
     282                    <td><?php echo esc_html_x( 'Someone has requested site membership', 'Member settings on notification settings page', 'buddypress' ) ?></td>
     283                    <td class="yes"><input type="radio" name="notifications[notification_members_membership_request]" id="notification-members-submitted_membership_request-yes" value="yes" <?php checked( $allow_request_emails, 'yes', true ) ?>/><label for="notification-members-submitted_membership_request-yes" class="bp-screen-reader-text">
     284                        <?php
     285                        /* translators: accessibility text */
     286                        esc_html_e( 'Yes, send email', 'buddypress' );
     287                        ?>
     288                    </label></td>
     289                    <td class="no"><input type="radio" name="notifications[notification_members_membership_request]" id="notification-members-submitted_membership_request-no" value="no" <?php checked( $allow_request_emails, 'no', true ) ?>/><label for="notification-members-submitted_membership_request-no" class="bp-screen-reader-text">
     290                        <?php
     291                        /* translators: accessibility text */
     292                        esc_html_e( 'No, do not send email', 'buddypress' );
     293                        ?>
     294                    </label></td>
     295                </tr>
     296                <?php
     297            endif;
    219298
    220299            /**
Note: See TracChangeset for help on using the changeset viewer.