# bbp-private-groups/1.6/includes/user-view-post.php

Private groups, version 1.6. 77 lines.

- Page: https://pluginprobe.com/plugins/bbp-private-groups/1.6/code/includes/user-view-post.php
- Raw: https://pluginprobe.com/plugins/bbp-private-groups/1.6/raw/includes/user-view-post.php
- Modified: 2014-05-16T07:02:18+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/bbp-private-groups/1.6/code/includes/user-view-post.php#L10-L20`.

```php
<?php
/*
 * Conditional tag to check if a user can view a specific post, by checking if they can see the forum that it belongs to.  
 * Non-logged in site visitors cannot view posts if groups were selected for the forum they belong to. 
* 	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.  
 *  If no groups were selected, all users and site visitors 
 * 	can view the content.
 *
 * There are exceptions to this rule though.  The post author, and users that have the ability to edit the post can always 
 * view the post, even if their role was not granted permission to view it.
 *
 *Much of this code is Justin Tadlocks' from the members plugin - thanks Justin for awesome work ! 
 */
 
 function private_groups_can_user_view_post_id($forum_id) {
	//get user ID
	$user_id = wp_get_current_user()->ID;
	//then check if they can view the forum the post belongs to
    return private_groups_can_user_view_post($user_id, $forum_id);
}

 
function private_groups_can_user_view_post( $user_id, $forum_id = '' ) {
//the $forum_id that needs to be passed to this function is the forum_id that the post belongs to

	/* Assume the user can view the post at this point. */
	$can_view = true;
	
	
	/* Get the groups for the forum */
		$groups = get_post_meta( $forum_id, '_private_group', false );

		/* If we have groups set for this forum  let's get to work. */
		if ( !empty( $groups ) && is_array( $groups ) ) {

			/**
			 * Since specific groups exist let's assume the user can't view the post at 
			 * this point.  The rest of this functionality should try to disprove this.
			 */
			$can_view = false;
			
			/* If the user's not logged in, assume it's blocked at this point. */
			if (!is_user_logged_in() ) {
				$can_view = false;
			}
			
			

			/*Check if user is moderator or keymaster
			*This code is only here for the widgets, other bbp code already overrides $can_view
			*to allow moderators and keymasters to see forum lists, topics and replies*/
			
			if ( bbp_is_user_keymaster() || user_can( $user_id, 'moderate' ) ) {
				$can_view = true;
			}
			
		
			/* Else, let's check the user's group against the selected group. */
			else {

				/* Loop through each group and set $can_view to true if the user has this group. */
				$check=get_user_meta( $user_id, 'private_group',true);
				foreach ( $groups as $group ) {
				if ($check==$group ) 
		
						$can_view = true;
				}
			}
		}
	

	/* Allow developers to overwrite the final return value. */
	return apply_filters( 'private_groups_can_user_view_post', $can_view, $user_id, $forum_id );
 
}
?>

```
