| 1 |
<?php |
| 2 |
/* |
| 3 |
* Conditional tag to check if a user can view a specific post, by checking if they can see the forum that it belongs to. |
| 4 |
* Non-logged in site visitors cannot view posts if groups were selected for the forum they belong to. |
| 5 |
* A user cannot view a post if their group role has not been selected in the meta box on the edit forum screen in the dashboard. |
| 6 |
* If no groups were selected, all users and site visitors can view the content. |
| 7 |
|
| 8 |
* Keymasters can always view, and moderators with no group set can view |
| 9 |
* |
| 10 |
* |
| 11 |
*Some of this code is Justin Tadlocks' from the members plugin - thanks Justin for awesome work ! |
| 12 |
*/ |
| 13 |
|
| 14 |
function private_groups_can_user_view_post_id($forum_id) { |
| 15 |
//get user ID |
| 16 |
$user_id = wp_get_current_user()->ID; |
| 17 |
//then check if they can view the forum the post belongs to |
| 18 |
return private_groups_can_user_view_post($user_id, $forum_id); |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
function private_groups_can_user_view_post( $user_id, $forum_id = '' ) { |
| 23 |
//the $forum_id that needs to be passed to this function is the forum_id that the post belongs to |
| 24 |
|
| 25 |
/* Assume the user can view the post at this point. */ |
| 26 |
$can_view = true; |
| 27 |
|
| 28 |
|
| 29 |
/* Get the groups for the forum */ |
| 30 |
$groups = get_post_meta( $forum_id, '_private_group', false ); |
| 31 |
|
| 32 |
/* If we have groups set for this forum let's get to work. */ |
| 33 |
if ( !empty( $groups ) && is_array( $groups ) ) { |
| 34 |
|
| 35 |
/** |
| 36 |
* Since specific groups exist let's assume the user can't view the post at |
| 37 |
* this point. The rest of this functionality should try to disprove this. |
| 38 |
*/ |
| 39 |
$can_view = false; |
| 40 |
|
| 41 |
/* If the user's not logged in, assume it's blocked at this point. */ |
| 42 |
if (!is_user_logged_in() ) { |
| 43 |
$can_view = false; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
/*Check if user is keymaster*/ |
| 48 |
if ( bbp_is_user_keymaster()) $can_view = true; |
| 49 |
//now we'll check if the user is a moderator. |
| 50 |
else { |
| 51 |
$role = bbp_get_user_role( $user_id ); |
| 52 |
$check=get_user_meta( $user_id, 'private_group',true); |
| 53 |
//if they are a mod, and they have no forum groups set, then they can moderate and see across all forums |
| 54 |
if ($role == 'bbp_moderator' && (empty($check))) $can_view= true ; |
| 55 |
//otherwise if they are a moderator with groups set, then they can see (and moderate) only their visible forums |
| 56 |
//- just as everyone else does |
| 57 |
|
| 58 |
/* Else, let's check the user's group against the selected group. */ |
| 59 |
else { |
| 60 |
|
| 61 |
/* Loop through each group and set $can_view to true if the user has this group. */ |
| 62 |
$check=get_user_meta( $user_id, 'private_group',true); |
| 63 |
|
| 64 |
foreach ( $groups as $group ) { |
| 65 |
//single group set? |
| 66 |
if ($check==$group ) $can_view = true; |
| 67 |
//multiple group set |
| 68 |
if (strpos($check, '*'.$group.'*') !== FALSE) $can_view = true; |
| 69 |
|
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
/* Allow developers to overwrite the final return value. */ |
| 77 |
return apply_filters( 'private_groups_can_user_view_post', $can_view, $user_id, $forum_id ); |
| 78 |
|
| 79 |
} |
| 80 |
?> |
| 81 |
|