Skip to content

Loading…

Limit invitations by user #80

Closed
boonebgorges opened this Issue · 3 comments

2 participants

@boonebgorges

Allow admins to cap how many invitations each member can send.

@Jeradin

If anyone else is looking to limit invites per user here is how you can add the function.

Boone, Thank you for the tip on invite_anyone_get_invitations_by_inviter_id() it worked well, hopefully I didn't add some extra php in getting the post count below.

I tried to write this code as similar to yours as possible so you might not need to rewrite it all yourself if you decide to implement this. I added an option to the admin section with a checkbox and invite limit amount field. Obviously there is more work for you then just adding the code since language files would all need to be updated too. I was also going to use the code to specify the user level that would be excluded from the limit but for my site this isn't really needed, others might want that option though.

I only had to edit 3 files to get it working slick like. functions.php, admin-panel.php and by-email.php

functions.php
line 40 - (add default values for limiting invites to Wordpress options)
```,'email_limit_invites_toggle' => 'no',
'limit_invites_per_user' => 0

admin-panel.php - (Added max invites per user to admin panel)
line 78 - (add setting options variables)
```,'email_limit_invites_toggle',
'limit_invites_per_user

line 166 - (add call settings function to admin section)
```add_settings_field('invite_anyone_settings_limit_invites', __('Limit the number of invitations', 'bp-invite-anyone'), 'invite_anyone_settings_limit_invites', 'invite_anyone', 'invite_anyone_access_settings');

line 332 - (add function for setting options checkbox to activate limiting by user and a input field for the amount)
```function invite_anyone_settings_limit_invites() {
    $options = invite_anyone_options();
?>
    <ul>
        <li>        
            <input type="checkbox" name="invite_anyone[email_limit_invites_toggle]" value="yes" <?php checked( $options['email_limit_invites_toggle'], 'yes' ) ?> /> <?php _e( 'Limit number of invites per user :', 'bp-invite-anyone' ) ?>
            <input name='invite_anyone['limit_invites_per_user']' size='10' type='text' value='<?php echo $options['limit_invites_per_user'] ?>' />     </li>
    </ul>
<?php
}

by-email.php (add limit invite count code to front end)
line 454 - (Hide invite anyone form on page if user used all their invites exclude editor and above)
``` if ( isset( $iaoptions['email_limit_invites_toggle'] ) && $iaoptions['email_limit_invites_toggle'] == 'yes' && !current_user_can( 'delete_others_pages' ) )
$sent_invite_count = invite_anyone_get_invitations_by_inviter_id( bp_loggedin_user_id());
$limit_invite_count = $sent_invite_count->post_count;
$limit_invite_count = $iaoptions['limit_invites_per_user']-$limit_invite_count;
if ( $limit_invite_count == 0 ){?>

<?php _e( 'Invite New Members', 'bp-invite-anyone' ); ?>


<?php _e( 'You have invited the maximum allowed people, you will be notified when there are more invites available.
*You may also cancel a previously invited person to get more invite credits.', 'bp-invite-anyone' ); ?>


<?php return false;
}

line 520 - (add message of how many more users you are allow to invite. excluding editor and above)
 ```<?php
    if ( isset( $iaoptions['email_limit_invites_toggle'] ) && $iaoptions['email_limit_invites_toggle'] == 'yes' && !current_user_can( 'switch_themes' ) ) {
        $sent_invite_count = invite_anyone_get_invitations_by_inviter_id( bp_loggedin_user_id());
        $limit_invite_count = $sent_invite_count->post_count;
                $limit_invite_count =  $iaoptions['limit_invites_per_user']-$limit_invite_count;
        $limit_invite_message = sprintf( __( 'You can invite %s more people.', 'bp-invite-anyone' ), $limit_invite_count );
        echo $limit_invite_message;
    }
    ?>

line 1034 - (Add error message if user tries to send more emails than the limit number excludes editors and above)
```$sent_invite_count = invite_anyone_get_invitations_by_inviter_id( bp_loggedin_user_id());
$limit_invite_count = $sent_invite_count->post_count;
$limit_invite_count = $options['limit_invites_per_user']-$limit_invite_count;
if ( count( $emails ) > 2 && !current_user_can( 'delete_others_pages' ) ) {
$returned_data['error_message'] = sprintf( __( 'You are only allowed to invite up to %s people. Please remove some addresses and try again', 'bp-invite-anyone' ), $limit_invite_count );
$returned_data['error_emails'] = $emails;
// Stash error info in cookies so we can use it after a redirect
setcookie( 'invite-anyone-error-data', maybe_serialize( $returned_data ), time()+606024, COOKIEPATH, COOKIE_DOMAIN );
bp_core_redirect( $bp->loggedin_user->domain . $bp->invite_anyone->slug . '/invite-new-members' );
}

@boonebgorges

Thanks, @Jeradin - and sorry it took so long to respond. I made a few modifications, but overall this worked well. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.