PluginProbe
Private groups / trunk
Private groups vtrunk
trunk 1.5 1.6 1.7 1.8 1.8.1 1.9.1 1.9.2 2.0 2.0.1 2.2 2.3 2.4 2.5 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 All 116 releases
bbp-private-groups / includes / user-view-post.php

user-view-post.php in Private groups trunk, at includes/user-view-post.php

105 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 global $rpg_topic_permissions ;
25
26 /* Assume the user can view the post at this point. */
27 $can_view = true;
28
29
30 /* Get the groups for the forum */
31 $groups = get_post_meta( $forum_id, '_private_group', false );
32
33 /* If we have groups set for this forum let's get to work. */
34 if ( !empty( $groups ) && is_array( $groups ) ) {
35
36 /**
37 * Since specific groups exist let's assume the user can't view the post at
38 * this point. The rest of this functionality should try to disprove this.
39 */
40 $can_view = false;
41
42 /* If the user's not logged in, assume it's blocked at this point. */
43 if (!is_user_logged_in() ) {
44 //check if topic permissions is set, and if so if non-logged in users can view this forum
45 $tp = '_private_group_nonloggedin' ;
46 $value = get_post_meta( $forum_id, $tp, true);
47 if (!empty ($rpg_topic_permissions['activate']) && (!empty ($value))) {
48 //then anyone can view this forum
49 $can_view = true;
50 }
51 else
52 $can_view = false;
53 }
54
55
56 /*Check if user is keymaster*/
57 if ( bbp_is_user_keymaster($user_id)) $can_view = true;
58 //now we'll check if the user is a moderator.
59 else {
60 $role = bbp_get_user_role( $user_id );
61 $check=get_user_meta( $user_id, 'private_group',true);
62
63 //if they are a mod, and they have no forum groups set, then they can moderate and see across all forums
64 if ($role == 'bbp_moderator' && (empty($check))) {
65 $can_view= true ;
66 //otherwise if they are a moderator with groups set, then they can see (and moderate) only their visible forums
67 //- just as everyone else does
68
69 }
70 /* Else, let's check the user's group against the selected group. */
71 else {
72
73 /* Loop through each group and set $can_view to true if the user has this group. */
74 $check=get_user_meta( $user_id, 'private_group',true);
75
76
77 foreach ( $groups as $group ) {
78 //single group set?
79 if ($check==$group ) $can_view = true;
80 //multiple group set
81 if (strpos($check, '*'.$group.'*') !== FALSE) $can_view = true;
82
83
84 }
85 }
86 }
87
88
89 }
90 //else allow for private and hidden forums that don't have groups set
91 //my assumption is that by creating an allowed list (post__in) I override wordpress/bbpress checking if a forum can be displayed to the user, so it displays anyway - this fixes that
92 else {
93 //check if forum is private or hidden and see if user is allowed to view
94 $status = get_post_status ($forum_id) ;
95 if ($status == 'hidden' && !current_user_can( 'read_hidden_forums' )) $can_view = false ;
96 if ($status == 'private' && !current_user_can( 'read_private_forums' )) $can_view = false ;
97 }
98
99
100 /* Allow developers to overwrite the final return value. */
101 return apply_filters( 'private_groups_can_user_view_post', $can_view, $user_id, $forum_id );
102
103 }
104 ?>
105