PluginProbe
Private groups / 1.6
Private groups v1.6
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.6, at includes/forum-filters.php

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