Skip to content

Commit

Permalink
FEATURE: Actually changing user's bbPress Roles when changing levels …
Browse files Browse the repository at this point in the history
…if you've set a specific role for their level.
  • Loading branch information
ideadude committed Jan 1, 2020
1 parent f794a2e commit 3c289e8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
14 changes: 14 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,18 @@ function pmprobb_getOptions($force = false)
);

return $pmprobb_options;
}

/**
* Get the bbPress role for a given level.
*/
function pmprobb_get_role_for_level( $level_id ) {
$options = pmprobb_getOptions();
if ( ! empty( $options['levels'] )
&& ! empty( $options['levels'][$level_id] )
&& ! empty( $options['levels'][$level_id]['role'] ) ) {
return $options['levels'][$level_id]['role'];
} else {
return get_option( '_bbp_default_role', 'bbp_participant' );
}
}
34 changes: 33 additions & 1 deletion pmpro-bbpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Paid Memberships Pro - bbPress Add On
* Plugin URI: https://www.paidmembershipspro.com/add-ons/pmpro-bbpress/
* Description: Allow individual forums to be locked down for members.
* Version: 1.5.5
* Version: 1.6
* Author: Stranger Studios, Scott Sousa
* Author URI: https://www.paidmembershipspro.com
*/
Expand Down Expand Up @@ -302,6 +302,38 @@ function pmprobb_pmpro_hide_role($args) {
}
add_filter ('bbp_before_get_reply_author_link_parse_args', 'pmprobb_pmpro_hide_role' );

/*
Change user's forum role when they change levels.
TODO: For MMPU compatibility, we need to get all of the user's Levels
and use the highest role found.
*/
function pmprobb_pmpro_after_change_membership_level( $level_id, $user_id, $cancel_level_id ) {
// Make sure bbPress is active.
if ( ! function_exists( 'bbp_set_user_role' ) ) {
return;
}

// Ignore admins.
if ( user_can( $user_id, 'manage_options' ) ) {
return;
}

if ( $level_id > 0 ) {
// Give them the role for their level.
$bbp_new_role = pmprobb_get_role_for_level( $level_id );
bbp_set_user_role( $user_id, $bbp_new_role );
} else {
// Cancelling. Give them the default role back
// if their old level was a non-default role.
$bbp_old_role = pmprobb_get_role_for_level( $cancel_level_id );
$bbp_default_role = get_option( '_bbp_default_role', 'bbp_participant' );
if ( $bbp_old_role != $bbp_default_role ) {
bbp_set_user_role( $user_id, $bbp_default_role );
}
}
}
add_action( 'pmpro_after_change_membership_level', 'pmprobb_pmpro_after_change_membership_level', 10, 3 );

/*
Adds a Section "Membership Level" and displays the user's level
on the bbPress User Profile page.
Expand Down
7 changes: 5 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Contributors: strangerstudios, slocumstudio, jessica o
Tags: paid memberships pro, pmpro, bbpress, forums, membership forum, restrict forum
Requires at least: 4
Tested up to: 5.2.2
Stable tag: 1.5.5
Tested up to: 5.3
Stable tag: 1.6

Integrate bbPress with Paid Memberships Pro to restrict forums by membership level.

Expand Down Expand Up @@ -53,6 +53,9 @@ Shortcode attributes include:
2. Settings -> Forums

== Changelog ==
= 1.6 - 2020-01-01 =
* FEATURE: Actually changing user's bbPress Roles when changing levels if you've set a specific role for their level.

= 1.5.5 =
* BUG FIX: Fixed issue where the pmprobb_auth_reply_view filter was nuking content filters applied to bbpress replies before it.
* BUG FIX/ENHANCEMENT: Now only calling the pmpro_bbp_error_msg filter if we're going to show the error.
Expand Down

0 comments on commit 3c289e8

Please sign in to comment.