PluginProbe
Private groups / 1.7
Private groups v1.7
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 / forum-filters.php

forum-filters.php in Private groups 1.7, at includes/forum-filters.php

323 lines 10.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 add_filter('bbp_has_forums', 'private_groups_filter', 10, 2);
3 add_filter('bbp_forum_get_subforums', 'private_groups_get_permitted_subforums', 10, 1);
4 add_filter( 'bbp_before_forum_get_subforums_parse_args', 'bbp_list_private_groups_subforums' );
5 add_filter('bbp_list_forums', 'custom_list_forums' );
6 /**
7 * This function filters the list of forums based on the the users group
8 * Much of this code is based on the work of Aleksandar Adamovic in his Tehnik BBPress Permissions - thanks !
9 */
10 function private_groups_filter($args = '') {
11 global $rpg_settingsf ;
12 $bbp = bbpress();
13 //this code is from includes/forums/template bbp_forum_get_subforums and sets up which forums to look in based on user capabilities
14 $post_stati[] = bbp_get_public_status_id();
15
16 // Check if user can read private forums
17 if (current_user_can('read_private_forums'))
18 $post_stati[] = bbp_get_private_status_id();
19
20 // Check if user can read hidden forums
21 if (current_user_can('read_hidden_forums'))
22 $post_stati[] = bbp_get_hidden_status_id();
23
24 // The default forum query for most circumstances
25 $meta_query = array(
26 'post_type' => bbp_get_forum_post_type(),
27 'post_parent' => bbp_is_forum_archive() ? 0 : bbp_get_forum_id(),
28 'post_status' => implode(',', $post_stati),
29 'posts_per_page' => get_option('_bbp_forums_per_page', 50),
30 'orderby' => 'menu_order',
31 'order' => 'ASC'
32 );
33 //if forums are visible to everyone, then skip filtering
34 if (!$rpg_settingsf['set_forum_visibility']) {
35 //Get an array of forums which the current user has permissions to view posts in
36 global $wpdb;
37 $forum=bbp_get_forum_post_type() ;
38 $post_ids=$wpdb->get_col("select ID from $wpdb->posts where post_type = '$forum'") ;
39 //check this list against those the user is allowed to see, and create a list of valid ones for the wp_query
40 $allowed_posts = check_private_groups_topic_ids($post_ids) ;
41
42
43 // the above generates a list of allowed forums, which is now added to the wp query parameters post__in if set sets which posts are valid to return
44 $meta_query['post__in'] = $allowed_posts;
45 }
46
47 //parse the query to allow other developers to add filters
48 $bbp_f = bbp_parse_args($args, $meta_query, 'has_forums');
49
50 // Now we can run the forum list query
51 $bbp->forum_query = new WP_Query($bbp_f);
52 //and return the valid forum list
53 return apply_filters('pg_filter_forums_by_permissions', $bbp->forum_query->have_posts(), $bbp->forum_query);
54 }
55
56 /**
57 * This function filters the list of sub-forums based on the the users group
58 */
59 function private_groups_get_permitted_subforums($sub_forums = '') {
60 //global $rpg_settingsf ;
61 //if ($rpg_settingsf['set_forum_visibility']) {
62 //return (array) $sub_forums;
63 //}
64 //else {
65 //this code is from includes/forums/template bbp_forum_get_subforums and sets up which forums to look in based on user capabilities
66 // Use passed integer as post_parent
67 if ( is_numeric( $args ) )
68 $args = array( 'post_parent' => $args );
69
70 // Setup possible post__not_in array
71 $post_stati[] = bbp_get_public_status_id();
72
73 // Super admin get whitelisted post statuses
74 if ( bbp_is_user_keymaster() ) {
75 $post_stati = array( bbp_get_public_status_id(), bbp_get_private_status_id(), bbp_get_hidden_status_id() );
76
77 // Not a keymaster, so check caps
78 } else {
79
80 // Check if user can read private forums
81 if ( current_user_can( 'read_private_forums' ) ) {
82 $post_stati[] = bbp_get_private_status_id();
83 }
84
85 // Check if user can read hidden forums
86 if ( current_user_can( 'read_hidden_forums' ) ) {
87 $post_stati[] = bbp_get_hidden_status_id();
88 }
89 }
90
91 // Parse arguments against default values
92 $r = bbp_parse_args( $args, array(
93 'post_parent' => 0,
94 'post_type' => bbp_get_forum_post_type(),
95 'post_status' => implode( ',', $post_stati ),
96 'posts_per_page' => get_option( '_bbp_forums_per_page', 50 ),
97 'orderby' => 'menu_order title',
98 'order' => 'ASC',
99 'ignore_sticky_posts' => true,
100 'no_found_rows' => true
101 ), 'forum_get_subforums' );
102 $r['post_parent'] = bbp_get_forum_id( $r['post_parent'] );
103
104 // Create a new query for the subforums
105 $get_posts = new WP_Query();
106
107 // No forum passed
108 $sub_forums = !empty( $r['post_parent'] ) ? $get_posts->query( $r ) : array();
109
110 global $rpg_settingsf ;
111 //if make forums visible set, then show all public forums
112 if ($rpg_settingsf['set_forum_visibility']) {
113 return (array) apply_filters( 'pg_forum_get_subforums',$sub_forums, $r );
114 }
115
116 //Otherwise now we filter this list to exclude those that the user can't see either because not logged in, or forum does not allowed this group
117
118 $filtered_sub_forums = private_groups_get_permitted_forums($sub_forums);
119
120 return (array) apply_filters( 'pg_forum_get_subforums',$filtered_sub_forums, $r );
121 //}
122 }
123
124
125 /**
126 * Use the given query to determine which forums the user has access to.
127 * Return an array of forums which user has permission to access
128 */
129 function private_groups_get_permitted_forums($forum_list) {
130
131 $filtered_forums = array();
132
133
134 //Get Current User ID
135 $user_id = wp_get_current_user()->ID;
136 foreach ($forum_list as $forum)
137 {
138 $forum_id = $forum->ID;
139
140 //check if user can view this forum (and hence posts in this forum)
141 if(private_groups_can_user_view_post($user_id, $forum_id))
142 {
143 array_push($filtered_forums, $forum);
144 }
145
146 }
147 return (array) $filtered_forums;
148
149 }
150
151 function bbp_list_private_groups_subforums( $args ) {
152 // Use passed integer as post_parent
153 if ( is_numeric( $args ) )
154 $args = array( 'post_parent' => $args );
155 // Setup possible post__not_in array
156 $post_stati[] = bbp_get_public_status_id();
157
158 // Super admin get whitelisted post statuses
159 if ( bbp_is_user_keymaster() ) {
160 $post_stati = array( bbp_get_public_status_id(), bbp_get_private_status_id(), bbp_get_hidden_status_id() );
161
162 // Not a keymaster, so check caps
163 } else {
164
165 // Check if user can read private forums
166 if ( current_user_can( 'read_private_forums' ) ) {
167 $post_stati[] = bbp_get_private_status_id();
168 }
169
170 // Check if user can read hidden forums
171 if ( current_user_can( 'read_hidden_forums' ) ) {
172 $post_stati[] = bbp_get_hidden_status_id();
173 }
174 }
175 $args['post_status'] = implode( ',', $post_stati ) ;
176 return $args ;
177 }
178
179 //This function adds descriptions to the sub forums, and sends non-logged in or users who can't view to a sign-up page
180 function custom_list_forums( $args = '' ) {
181
182 // Define used variables
183 global $rpg_settingsg ;
184 global $rpg_settingsf ;
185 $output = $sub_forums = $topic_count = $reply_count = $counts = '';
186 $i = 0;
187 $count = array();
188
189 // Parse arguments against default values
190 $r = bbp_parse_args( $args, array(
191 'before' => '<ul class="bbp-forums-list">',
192 'after' => '</ul>',
193 'link_before' => '<li class="bbp-forum">',
194 'link_after' => '</li>',
195 'count_before' => ' (',
196 'count_after' => ')',
197 'count_sep' => ', ',
198 'separator' => '<br> ',
199 'forum_id' => '',
200 'show_topic_count' => true,
201 'show_reply_count' => true,
202 ), 'listb_forums' );
203
204
205
206 // Loop through forums and create a list
207 $sub_forums = bbp_forum_get_subforums( $r['forum_id'] );
208 if ( !empty( $sub_forums ) ) {
209
210 // Total count (for separator)
211 $total_subs = count( $sub_forums );
212 foreach ( $sub_forums as $sub_forum ) {
213 $i++; // Separator count
214
215 // Get forum details
216 $count = array();
217 $show_sep = $total_subs > $i ? $r['separator'] : '';
218 $permalink = bbp_get_forum_permalink( $sub_forum->ID );
219 $title = bbp_get_forum_title( $sub_forum->ID );
220 $content = bbp_get_forum_content($sub_forum->ID) ;
221 if($rpg_settingsg['activate_descriptions'] == true) {
222 $content = bbp_get_forum_content($sub_forum->ID) ;
223 }
224 else {
225 $content='';
226 }
227
228
229 // Show topic count
230 if ( !empty( $r['show_topic_count'] ) && !bbp_is_forum_category( $sub_forum->ID ) ) {
231 $count['topic'] = bbp_get_forum_topic_count( $sub_forum->ID );
232 }
233
234 // Show reply count
235 if ( !empty( $r['show_reply_count'] ) && !bbp_is_forum_category( $sub_forum->ID ) ) {
236 $count['reply'] = bbp_get_forum_reply_count( $sub_forum->ID );
237 }
238
239 // Counts to show
240 if ( !empty( $count ) ) {
241 $counts = $r['count_before'] . implode( $r['count_sep'], $count ) . $r['count_after'];
242 }
243
244 if($rpg_settingsg['hide_counts'] == true) {
245 $counts='';
246 }
247 //Build this sub forums link
248 if (bbp_is_forum_private($sub_forum->ID)) {
249 if (!current_user_can( 'read_private_forums' ) ) {
250 if(!$rpg_settingsf['redirect_page']) {
251 $link='/home' ;
252 }
253 else {
254 $link=$rpg_settingsf['redirect_page'] ;
255 }
256 $output .= $r['before'].$r['link_before'] . '<a href="' .$link . '" class="bbp-forum-link">' . $title . $counts . '</a>' . $show_sep . $r['link_after'].'<div class="bbp-forum-content">'.$content.'</div>'.$r['after'];
257 }
258 else {
259 $output .= $r['before'].$r['link_before'] . '<a href="' . esc_url( $permalink ) . '" class="bbp-forum-link">' . $title . $counts . '</a>' . $show_sep . $r['link_after'].'<div class="bbp-forum-content">'.$content.'</div>'.$r['after'];
260 }
261 }
262 else {
263 $output .= $r['before'].$r['link_before'] . '<a href="' . esc_url( $permalink ) . '" class="bbp-forum-link">' . $title . $counts . '</a>' . $show_sep . $r['link_after'].'<div class="bbp-forum-content">'.$content.'</div>'.$r['after'];
264 }
265 }
266 //Output the list
267 return $output ;
268
269 }
270 }
271
272 //restrict the forum display on topic-form to allowed forums
273 add_filter ('bbp_before_get_dropdown_parse_args', 'pg_forum_dropdown') ;
274
275 function pg_forum_dropdown( $args = '' ) {
276 //Get an array of forums which the current user has permissions to view
277 global $wpdb;
278 $forum=bbp_get_forum_post_type() ;
279 if ( bbp_is_user_keymaster()) return $args;
280
281 $user_id = wp_get_current_user()->ID;
282 if (user_can( $user_id, 'moderate' ) ) {
283 $check=get_user_meta( $user_id, 'private_group',true);
284 if ($check=='') return $args;
285 }
286
287 $post_ids=$wpdb->get_col("select ID from $wpdb->posts where post_type = '$forum'") ;
288 //check this list against those the user is allowed to see, and create a list of valid ones for the wp_query
289 $allowed_posts = private_groups_get_dropdown_forums($post_ids) ;
290
291 // the above generates a list of allowed forums, and we compare this against the original list to create and 'exclude' list
292
293 $result=array_diff($post_ids, $allowed_posts) ;
294 $args['exclude'] = $result ;
295 return $args;
296 }
297
298
299 function private_groups_get_dropdown_forums($forum_list) {
300
301 $filtered_forums = array();
302
303
304 //Get Current User ID
305 $user_id = wp_get_current_user()->ID;
306 foreach ($forum_list as $forum)
307 {
308
309 //check if user can view this forum (and hence posts in this forum)
310 if(private_groups_can_user_view_post($user_id, $forum))
311 {
312 array_push($filtered_forums, $forum);
313
314 }
315
316 }
317 return (array) $filtered_forums;
318
319 }
320
321
322
323