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 / functions.php

functions.php in Private groups trunk, at includes/functions.php

1,063 lines 36.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 global $rpg_settingsf ;
4
5 add_action('bbp_template_redirect', 'private_group_enforce_permissions', 1);
6 add_filter('protected_title_format', 'pg_remove_protected_title');
7 add_filter('private_title_format', 'pg_remove_private_title');
8 //handle favorites
9 add_filter( 'bbp_get_user_favorites', 'rpg_get_user_favorites' );
10
11 //links and topic counts
12 add_filter('bbp_get_forum_freshness_link', 'pg_get_forum_freshness_link', 10, 2 );
13 add_filter ('bbp_get_single_forum_description' , 'rpg_get_single_forum_description' , 10 , 2) ;
14 add_filter ('bbp_get_forum_topic_count' , 'rpg_get_forum_topic_count', 10 , 2 ) ;
15 add_filter ('bbp_get_forum_reply_count' , 'rpg_get_forum_reply_count', 10 , 2 ) ;
16 add_filter ('bbp_get_forum_post_count' , 'rpg_get_forum_post_count', 10 , 2 ) ;
17
18 add_filter ('bbp_get_forum_last_active_id' , 'rpg_get_forum_last_active_id', 10 , 2 ) ;
19 add_filter( 'bbp_user_can_view_forum', 'rpg_user_can_view_forum', 10, 3 );
20
21 //filter back end access to topics and replies to ensure that moderators can only see their forums they have access to
22 add_filter( 'bbp_request', 'rpg_filter_bbp_request', 10, 1 );
23
24 add_action( 'admin_notices', 'rpg_warning' );
25
26
27 global $rpg_settingsf ;
28
29 //hide forum menu items if visibilty switched off
30 if (empty($rpg_settingsf['set_forum_visibility'])) add_filter( 'wp_get_nav_menu_items', 'rpg_exclude_menu_items', null, 3 );
31
32
33 //This code hides the author link if needed - we only potentially need to get this link if forum visibility is active, otherwise it is not needed.
34 //and user can't see the post - but then function will test that !
35
36 if (!empty ($rpg_settingsf['set_forum_visibility']) ) {
37 remove_filter( 'bbp_get_author_link', 'bbp_suppress_private_author_link', 10, 2 );
38 remove_filter( 'bbp_get_topic_author_link', 'bbp_suppress_private_author_link', 10, 2 );
39 remove_filter( 'bbp_get_reply_author_link', 'bbp_suppress_private_author_link', 10, 2 );
40 add_filter( 'bbp_get_author_link', 'pg_check_profile', 10, 2 ) ;
41 add_filter( 'bbp_get_topic_author_link', 'pg_check_profile', 10, 2 ) ;
42 add_filter( 'bbp_get_reply_author_link', 'pg_check_profile', 10, 2 ) ;
43 }
44
45 //add assign role to register,
46 add_action ('bbp_user_register', 'pg_role_group') ;
47
48 //and to wp-login
49 global $rpg_roles ;
50 if (!empty ($rpg_roles['login'])) {
51 add_action('wp_login', 'pg_assign_role_on_login', 10, 2);
52 }
53 //and on every visit
54 if (!empty ($rpg_roles['change_on_visit'] )) {
55 add_action('init', 'pg_assign_role_on_init');
56 }
57
58 // ADD FORUM ID column to admin
59 add_action("manage_edit-forum_columns", 'rpg_ID_column_add');
60 add_filter("manage_forum_posts_custom_column", 'rpg_ID_column_value', 10, 3);
61
62 //add groups to forum lists
63 add_action("manage_edit-forum_columns", 'rpg_groups_column_add') ;
64 add_filter("manage_forum_posts_custom_column", 'rpg_groups_column_value', 10, 3);
65
66
67 // add filters for bbp-style-pack if stylepack is running
68 add_filter('bsp_get_freshness_display_title', 'pg_get_forum_freshness_title' );
69 add_filter('bsp_display_topic_index_query', 'pg_display_topic_index_query_filter');
70 add_filter('bsp_display_forum_query', 'pg_display_forum_query_filter');
71 add_filter('bsp_activity_widget', 'pg_latest_activity_forum_query_filter') ;
72
73 // add filters for bbp-shortcodes plugin shortcodes if plugin is running
74 add_filter('asc_display_topic_index_query', 'pg_display_topic_index_query_filter');
75 add_filter('asc_display_forum_query', 'pg_display_forum_query_filter');
76
77
78
79
80 /*
81 * Check if the current user has rights to view the given Post ID
82 * Some of this core code is from the work of Aleksandar Adamovic in his Tehnik BBPress Permissions - thanks !
83 *
84 */
85
86 function private_groups_check_can_user_view_post() {
87 //uses $post_id and $post_type to get the forum ($forum_id) that the post belongs to
88 global $wp_query;
89
90 // Get Forum Id for the current post
91 $post_id = $wp_query->post->ID;
92 $post_type = $wp_query->get('post_type');
93
94 if (bbp_is_topic_super_sticky($post_id)) return true;
95
96
97 $forum_id = private_groups_get_forum_id_from_post_id($post_id, $post_type);
98 //then call the function that checks if the user can view this forum, and hence this post
99 if (private_groups_can_user_view_post_id($forum_id) && topic_permissions_check ($post_id)) {
100 return true ;
101 }
102 }
103
104
105
106 /**
107 * Use the given query to determine which forums the user has access to.
108 *
109 * returns: an array of post IDs which user has access to.
110 */
111 function private_groups_get_permitted_post_ids($post_query) {
112
113 //Init the Array which will hold our list of allowed posts
114 $allowed_posts = array();
115
116
117 //Loop through all the posts
118 while ($post_query->have_posts()) :
119 $post_query->the_post();
120 //Get the Post ID and post type
121 $post_id = $post_query->post->ID;
122 $post_type = $post_query->post->post_type;
123 //Get the Forum ID based on Post Type (Reply, Topic, Forum)
124 $forum_id = private_groups_get_forum_id_from_post_id($post_id, $post_type);
125 //Check if User has permissions to view this Post ID
126 //by calling the function that checks if the user can view this forum, and hence this post
127 if (private_groups_can_user_view_post_id($forum_id) && topic_permissions_check ($post_id)) {
128
129 //User can view this post - add it to the allowed array
130 array_push($allowed_posts, $post_id);
131 }
132
133 endwhile;
134
135 //Return the list
136 return $allowed_posts;
137 }
138
139
140 /*
141 * Returns the bbPress Forum ID from given Post ID and Post Type
142 *
143 * returns: bbPRess Forum ID
144 */
145 function private_groups_get_forum_id_from_post_id($post_id, $post_type = 0) {
146 if (empty ($post_type )) $post_type = get_post_type ($post_id) ;
147 $forum_id = 0;
148
149 // Check post type
150 switch ($post_type) {
151 // Forum
152 case bbp_get_forum_post_type() :
153 $forum_id = bbp_get_forum_id($post_id);
154 break;
155
156 // Topic
157 case bbp_get_topic_post_type() :
158 $forum_id = bbp_get_topic_forum_id($post_id);
159 break;
160
161 // Reply
162 case bbp_get_reply_post_type() :
163 $forum_id = bbp_get_reply_forum_id($post_id);
164 break;
165 }
166
167 return $forum_id;
168 }
169
170 //enforce permission to ensure users only see permitted posts
171 function private_group_enforce_permissions() {
172 global $rpg_settingsf ;
173 // Bail if not viewing a bbPress item
174 if (!is_bbpress())
175 return;
176
177 // Bail if not viewing a single item or if user has caps
178 if (!is_singular() || bbp_is_user_keymaster() )
179
180 return;
181
182 if (!private_groups_check_can_user_view_post() ) {
183 if (!is_user_logged_in()) {
184 if(!empty($rpg_settingsf['redirect_page2'])) {
185 $link= apply_filters('rpg_forum_visibility_2' , $rpg_settingsf['redirect_page2']) ;
186 wp_redirect($link);
187 exit;
188 }
189 else {
190 auth_redirect();
191 }
192 }
193 else {
194 if(!empty($rpg_settingsf['redirect_page1'])) {
195 $link=$rpg_settingsf['redirect_page1'] ;
196 wp_redirect($link);
197 exit;
198 }
199 else {
200 bbp_set_404();
201 }
202
203 }
204 }
205 }
206
207
208
209
210 function pg_remove_private_title($title) {
211 global $rpg_settingsg ;
212 if (isset( $rpg_settingsg['activate_remove_private_prefix']) ) {
213 return '%s';
214 }
215 else {
216 Return $title ;
217 }
218 }
219
220 function pg_remove_protected_title($title) {
221 global $rpg_settingsg ;
222 if (isset ($rpg_settingsg['activate_remove_private_prefix']) ) {
223 return '%s';
224 }
225 else {
226 Return $title ;
227 }
228 }
229
230 function pg_get_forum_freshness_link ($anchor, $forum_id) {
231 $anchor = pg_get_forum_freshness_link_anchor($forum_id) ;
232 if ($anchor ['title'] == 'no_topics' ) {
233 $anchor = esc_html__( 'No Topics', 'bbpress' );
234 }
235 else {
236 $link_url = $anchor ['link_url'] ;
237 $title = $anchor ['title'] ;
238 $display = $anchor ['display'] ;
239 $anchor = '<a href="' .esc_url( $link_url) . '" title="' . esc_attr( $title ) . '">' .esc_html( $display ) .'</a>';
240 }
241 return $anchor ;
242 }
243
244 function pg_get_forum_freshness_title () {
245 $anchor = pg_get_forum_freshness_link_anchor() ;
246 if ($anchor ['title'] == 'no_topics' ) {
247 $anchor = esc_html__( 'No Topics', 'bbpress' );
248 }
249 else {
250 $link_url = $anchor ['link_url'] ;
251 $title = $anchor ['title'] ;
252 $display = $anchor ['display'] ;
253 $anchor = $anchor = '<a href="' .esc_url( $link_url) . '" title="' . esc_attr( $title ) . '">' .esc_html( $title ) .'</a><p/>';
254 }
255 return $anchor ;
256 }
257
258 function rpg_get_forum_last_active_id ($active_id, $forum_id) {
259 //amended to allow for case where a forum has topics in the forum, and sub forums as well - previosuly only allowed for subforums
260 //so first check if the last active is in this forum itself (rather than a sub forum) if so then we can just retirn $active)id
261 $parent = get_post_meta( $active_id, '_bbp_forum_id', true );
262 if ($parent == $forum_id ) return $active_id ;
263 //else we need to check which sub forums this user can see.
264 $sub_forums = private_groups_get_permitted_subforums($forum_id) ;
265 if ( !empty( $sub_forums ) ) {
266 $active_id = 0;
267 $show = array();
268 //find the latest permissible
269 foreach ( $sub_forums as $sub_forum ) {
270 $sub_forum_id = $sub_forum->ID ;
271 $active_id = get_post_meta( $sub_forum_id , '_bbp_last_active_id', true );
272 $last_active = get_post_meta( $sub_forum_id, '_bbp_last_active_time', true );
273 if ( empty( $active_id ) ) { // not replies, maybe topics ?
274 $active_id = bbp_get_forum_last_topic_id( $sub_forum_id );
275 if ( !empty( $active_id ) ) {
276 $last_active = bbp_get_topic_last_active_time( $active_id );
277 }
278 }
279 if ( !empty( $active_id ) ) {
280 $curdate = strtotime($last_active);
281 $show[$curdate] = $active_id ;
282 }
283 }
284 $mostRecent= 0;
285 foreach($show as $date=>$value){
286 if ($date > $mostRecent) {
287 $mostRecent = $date;
288 }
289 }
290 if ($mostRecent != 0) {
291 $active_id = $show[$mostRecent] ;
292 } else {
293 $active_id = 0;
294 }
295 }
296
297
298
299 return apply_filters( 'pg_get_forum_last_active_id', $active_id, $forum_id );
300 }
301
302
303
304 function pg_get_forum_freshness_link_anchor( $forum_id = 0 ) {
305 global $rpg_settingsf ;
306 $forum_id = bbp_get_forum_id( $forum_id );
307
308 //this returns the wrong answer - I suspect because there is another filter acting
309 //$active_id = bbp_get_forum_last_active_id( $forum_id );
310 $active_id = (int) get_post_meta( $forum_id, '_bbp_last_active_id', true );
311
312
313 if ( empty( $active_id ) )
314 $active_id = bbp_get_forum_last_reply_id( $forum_id );
315
316 if ( empty( $active_id ) )
317 $active_id = bbp_get_forum_last_topic_id( $forum_id );
318
319 //then reset forum_id to the forum of the active topic in case it is a sub forum
320 $forum_id = private_groups_get_forum_id_from_post_id($active_id) ;
321
322 $link_url = $title = '';
323
324 if ( bbp_is_topic( $active_id ) ) {
325 $link_url = bbp_get_forum_last_topic_permalink( $forum_id );
326 $title = bbp_get_forum_last_topic_title( $forum_id );
327
328
329 } elseif ( bbp_is_reply( $active_id ) ) {
330 $link_url = bbp_get_forum_last_reply_url( $forum_id );
331 $title = bbp_get_forum_last_reply_title( $forum_id );
332
333
334 }
335
336 $time_since = bbp_get_forum_last_active_time( $forum_id );
337
338
339 if ( !empty( $time_since ) && !empty( $link_url ) ) {
340 //test if user can see this post, and post link if they can
341 $user_id = wp_get_current_user()->ID;
342 //now we can check if the user can view this, and if it's not private
343 if (private_groups_can_user_view_post($user_id,$forum_id) && !bbp_is_forum_private($forum_id) && topic_permissions_check ($active_id)) {
344 $anchor ['link_url'] = $link_url ;
345 $anchor ['title'] = $title;
346 $anchor ['display'] = $time_since;
347 //$anchor ['display'] = $forum_id ;
348
349 }
350 //if it is private, then check user can view
351 elseif (private_groups_can_user_view_post($user_id,$forum_id) && bbp_is_forum_private($forum_id) && current_user_can( 'read_private_forums' ) && topic_permissions_check ($active_id) ) {
352 $anchor ['link_url'] = $link_url ;
353 $anchor ['title'] = $title;
354 $anchor ['display'] = $time_since;
355 }
356 //else we need to work out what to show so
357
358 elseif (private_groups_can_user_view_post($user_id,$forum_id) && !topic_permissions_check ($active_id)) {
359 //now we check if it was only that user wasn't allowed to view the topic, and then find the last active topic for this user ('own topics' active)
360 $last_active_topic = rpg_own_topic_freshness ($forum_id);
361 //if user has no topics then we return with 0
362 if (empty($last_active_topic)) $anchor ['title'] = 'no_topics';
363 else {
364 $last_active_id = get_post_meta ($last_active_topic, '_bbp_last_active_id', true) ;
365 $anchor ['link_url'] = bbp_get_topic_permalink( $last_active_id);
366 $anchor ['title'] = bbp_get_topic_title($last_active_id );
367 $anchor ['display'] = bbp_get_topic_last_active_time($last_active_topic ) ;
368 }
369 }
370 //so no access unless... if visibility is set...
371
372 elseif (!empty ($rpg_settingsf['set_forum_visibility'])) {
373
374 //so we need to set up a $link that will determine where they go, and change the date to a freshness message if set
375 //so set up for not logged in and logged in
376 if (!is_user_logged_in()) {
377 if($rpg_settingsf['redirect_page2']) {
378 $link_url= apply_filters('rpg_forum_freshness_visibility_2' , $rpg_settingsf['redirect_page2']) ;
379 }
380 else {
381 $link_url="/wp-login";
382 }
383 }
384 //so if logged in,
385 else {
386 if($rpg_settingsf['redirect_page1']) {
387 $link_url=$rpg_settingsf['redirect_page1'] ;
388 }
389 else {
390 $link_url='/404';
391 }
392 }
393 //now see if there is a freshness message
394 if (!empty ($rpg_settingsf['set_freshness_message']) ) {
395 $title=$rpg_settingsf['freshness_message'] ;
396 }
397 else {
398 $title = esc_html( $time_since ) ;
399 }
400
401
402 //so we have a link and a title, so create the anchor
403 $anchor ['link_url'] = $link_url ;
404 $anchor ['title'] = $title;
405 $anchor ['display'] = $title;
406
407
408 }
409
410 else {
411 //visibility is not set, and we have no topics to show the user so
412 $anchor ['title'] = 'no_topics';
413 }
414
415
416 }
417 else {
418 // we have no topics, or something else has gone wrong !
419 $anchor ['title'] = 'no_topics';
420 }
421
422 return $anchor;
423 }
424
425 //not currently used - put in whilst testing time since - can be removed if this line still shows !
426 function rpg_get_forum_last_active_time( $forum_id = 0 ) {
427
428 // Verify forum and get last active meta
429 $forum_id = bbp_get_forum_id( $forum_id );
430 $last_active = get_post_meta( $forum_id, '_bbp_last_active_time', true );
431
432 if ( empty( $last_active ) ) {
433 $reply_id = bbp_get_forum_last_reply_id( $forum_id );
434 if ( ! empty( $reply_id ) ) {
435 $last_active = get_post_field( 'post_date', $reply_id );
436 } else {
437 $topic_id = bbp_get_forum_last_topic_id( $forum_id );
438 if ( ! empty( $topic_id ) ) {
439 $last_active = bbp_get_topic_last_active_time( $topic_id );
440 }
441 }
442 }
443
444 $active_time = ! empty( $last_active ) ? bbp_get_time_since( bbp_convert_date( $last_active ) ) : '';
445
446 // Filter & return
447 return apply_filters( 'rpg_get_forum_last_active', $active_time, $forum_id );
448 }
449
450 //I can't see where I use this function !!
451 //this function is only used if we have a user with 'access own topics' only to forum
452 //basically it just changes the post_id called to the correct one
453 function rpg_get_last_active_author ($args) {
454 $user_id = wp_get_current_user()->ID;
455 $forum_id = 0 ;
456 $active_id = $args['post_id'] ;
457 if (private_groups_can_user_view_post($user_id,$forum_id) && !topic_permissions_check ($active_id)){
458 $last_active_topic = rpg_own_topic_freshness ($forum_id);
459 //if user has no topics then we return with 0
460 if (!empty($last_active_topic)) {
461 $post_id = get_post_meta ($last_active_topic, '_bbp_last_active_id', true) ;
462 $args['post_id'] = $post_id ;
463 }
464 }
465 return $args ;
466
467 }
468
469
470 //we only need to get this link if forum visibility is active, otherwise it is not needed.
471 //and if user can't see the freshness link
472 function pg_check_profile($author_link, $args ) {
473 //so we start with the code from the original suppress function
474 // Assume the author link is the return value
475 $retval = $author_link;
476
477 // Show the normal author link
478 if ( !empty( $args['post_id'] ) && !current_user_can( 'read_private_forums' ) ) {
479
480 // What post type are we looking at?
481 $post_type = get_post_field( 'post_type', $args['post_id'] );
482
483 switch ( $post_type ) {
484
485 // Topic
486 case bbp_get_topic_post_type() :
487 if ( bbp_is_forum_private( bbp_get_topic_forum_id( $args['post_id'] ) ) )
488 $retval = '';
489
490 break;
491
492 // Reply
493 case bbp_get_reply_post_type() :
494 if ( bbp_is_forum_private( bbp_get_reply_forum_id( $args['post_id'] ) ) )
495 $retval = '';
496
497 break;
498
499 // Post
500 default :
501 if ( bbp_is_forum_private( $args['post_id'] ) )
502 $retval = '';
503
504 break;
505 }
506 }
507 //so now to code for private groups
508 //so if retval isn't already blank, lets see if we should blank the author as the user isn't allowed to see this post
509 //we also check whether $args['post_id'] is blank as for a new forum with no topics AND any super sticky topics in other forums, then post_id is blank
510 if (!empty( $args['post_id'] ) && $retval !='') {
511 $forum_id_check = private_groups_get_forum_id_from_post_id($args['post_id']);
512 $current_user = wp_get_current_user()->ID;
513 //now we can check if the user can view this
514 if (!private_groups_can_user_view_post($current_user,$forum_id_check))
515 $retval = '' ;
516 }
517
518 return apply_filters( 'pg_no_profile', $retval );
519
520 }
521
522
523
524
525 //This function is added to bbp_user_register which in turn hooks to wordpress user_register. It checks if the user role has a group set against it, and if so assigns that to the user
526
527 function pg_role_group ($user_id) {
528 if ($user_id == 0) return ; // bail if no user ID
529 global $rpg_roles ;
530 if (empty ($rpg_roles)) return ; //bail if not set in $rpg_roles
531 pg_set_user_group ($user_id) ;
532 }
533
534 //this function assigns user to roles on login
535 function pg_assign_role_on_login($user_login, $user) {
536 $user_id = $user->ID ;
537 //bail if user groups are set for this user
538 $blank_test = get_user_meta ($user_id , 'private_group', true) ;
539 if (!empty ($blank_test)) return ; //has an entry in the database
540 //bail if login option not set in $rpg_roles
541 global $rpg_roles ;
542 if (empty ($rpg_roles)) return ; //bail if not set in $rpg_roles
543 pg_set_user_group ($user_id) ;
544 }
545
546 //this function assigns user to roles on every visit (and every page refresh!)
547 function pg_assign_role_on_init() {
548 global $rpg_roles ;
549 $user = wp_get_current_user();
550 $user_id = $user->ID ;
551 if (empty ($rpg_roles)) return ; //bail if not set in $rpg_roles
552 pg_set_user_group ($user_id) ;
553 }
554
555 //this function has been superseded by the one below to allow for multiple wordpress roles
556 function pg_set_user_group_old ($user_id) {
557 global $rpg_roles ;
558 $user_info = get_userdata($user_id);
559 if (empty ($user_info)) return ;
560 $user_roles = $user_info->roles ;
561 foreach ((array)$user_roles as $list=>$role) {
562 if (!empty ($rpg_roles[$role] ) ) {
563 $group = $rpg_roles[$role] ;
564 if ($group != 'no-group' && (!empty($group)) ) {
565 update_user_meta( $user_id, 'private_group', $group);
566 }
567 }
568 }
569
570 }
571
572 function pg_set_user_group ($user_id) {
573 global $rpg_roles ;
574 $string='' ;
575 $user_info = get_userdata($user_id);
576 if (empty ($user_info)) return ;
577 $user_roles = $user_info->roles ;
578 foreach ((array)$user_roles as $list=>$role) {
579 if (!empty ($rpg_roles[$role] ) ) {
580 $group = $rpg_roles[$role] ;
581 if ($group != 'no-group' && (!empty($group)) ) {
582 $string.= $group.'*' ;
583 }
584 }
585 }
586 if (!empty ($string)) update_user_meta( $user_id, 'private_group', '*'.$string);
587 }
588
589
590
591 //function for style pack topic index query
592 function pg_display_topic_index_query_filter ($args) {
593 // if we have no forums to select against then do nothing as this function has nothing to do. has_topics will do the filter
594 if (!empty ($args['post_parent__in'])) {
595 //get forums this user is allowed to see
596 $allowed_posts = pg_get_user_forums () ;
597 if (empty ($allowed_posts)) $allowed_posts[] = -1 ;
598 // now we have $allowed forums, so then we need to only have forums that are common to both (intersect)
599 if (!empty($args['post_parent__in'])) {
600 $allowed_posts = array_intersect( $allowed_posts, $args['post_parent__in']);
601 }
602
603 //if there are no allowed forums set post_type to rubbish to ensure a nil return (otherwise it shows all allowed as post__in is blank)
604 if (empty ($allowed_posts)) $args['post_type'] = 'qvyzzvxx' ;
605
606 //then we can create the post__in data
607 $args['post_parent__in'] = $allowed_posts;
608 }
609
610
611 return apply_filters( 'pg_display_topic_index_query_filter', $args );
612 }
613
614
615 //function for style pack topic index query
616 function pg_display_forum_query_filter ($args) {
617 global $rpg_settingsf ;
618 //if forums are visible to everyone, then skip filtering, so if set_forum_visibility is empty then we need to filter, otherwise don't
619 if (empty($rpg_settingsf['set_forum_visibility'])) {
620 //get forums this user is allowed to see
621 $allowed_posts = pg_get_user_forums () ;
622 if (empty ($allowed_posts)) $allowed_posts[] = -1 ;
623 // now we have $allowed forums, so then we need to only have forums that are common to both (intersect)
624 if (!empty($args['post__in'])) {
625 $allowed_posts = array_intersect( $allowed_posts, $args['post__in']);
626 }
627 //if there are no allowed forums set post_type to rubbish to ensure a nil return (otherwise it shows all allowed as post__in is blank)
628 if (empty ($allowed_posts)) $args['post_type'] = 'qvyzzvxx' ;
629 //then we can create the post__in data
630 $args['post__in'] = $allowed_posts;
631 }
632
633 return apply_filters( 'pg_display_topic_index_query_filter', $args );
634 }
635
636
637 //function to create array used by above two functions
638 function pg_get_user_forums () {
639 //create an array of current forums this user can view
640 $query_data = array(
641 'post_type' => bbp_get_forum_post_type(),
642 'post_status' => bbp_get_public_status_id(),
643 'posts_per_page' => get_option('_bbp_forums_per_page', 50),
644 'orderby' => 'menu_order',
645 'order' => 'ASC'
646 );
647 //PRIVATE GROUPS Get an array of forums which the current user has permissions to view
648 $allowed_posts = private_groups_get_permitted_post_ids(new WP_Query($query_data));
649 return $allowed_posts ;
650 }
651
652 //function for style pack latest activity widget
653 function pg_latest_activity_forum_query_filter ($topics_query) {
654 $topics_query['posts_per_page'] = 200;
655 $allowed_posts = private_groups_get_permitted_post_ids(new WP_Query($topics_query));
656 $topics_query['post__in'] = $allowed_posts ;
657 return apply_filters( 'pg_latest_activity_forum_query_filter', $topics_query ) ;
658 }
659
660
661
662
663
664 function rpg_get_user_favorites( $user_id = 0 ) {
665 $user_id = bbp_get_user_id( $user_id );
666 if ( empty( $user_id ) )
667 return false;
668
669 // If user has favorites, load them
670 $favorites = bbp_get_user_favorites_topic_ids( $user_id );
671 //create a filtered list
672 if ( !empty( $favorites ) ) {
673 $filtered_favorites = array();
674 foreach ($favorites as $list=>$fav) {
675 $post_type = get_post_type ($fav) ;
676 $forum_id = private_groups_get_forum_id_from_post_id($fav, $post_type);
677 //then call the function that checks if the user can view this forum, and hence this post
678 if ( private_groups_can_user_view_post_id($forum_id, $post_type) && topic_permissions_check ($fav) ) array_push($filtered_favorites, $fav);
679 }
680 }
681 //then show if any..
682 if ( !empty( $filtered_favorites ) ) {
683 $query = bbp_has_topics( array( 'post__in' => $filtered_favorites ) );
684 } else {
685 $query = false;
686 }
687 return apply_filters( 'rpg_get_user_favorites', $query, $user_id, $favorites );
688 }
689
690 //amended single forum description
691 function rpg_get_single_forum_description( $retstr, $r ) {
692 $forum_id = bbp_get_forum_id( $r['forum_id'] );
693 // check if this forum has sub-forums and if it hasn't then just return
694 $children = bbp_forum_query_subforum_ids( $forum_id );
695 if ( empty( $children ) ) {
696 return apply_filters( 'rpg_get_single_forum_description1', $retstr, $r );
697 }
698
699 // Unhook the 'view all' query var adder
700 remove_filter( 'bbp_get_forum_permalink', 'bbp_add_view_all' );
701
702 // Get some forum data
703 $tc_int = bbp_get_forum_topic_count( $forum_id, false );
704 $rc_int = bbp_get_forum_reply_count( $forum_id, false );
705 $topic_count = bbp_get_forum_topic_count( $forum_id );
706 $reply_count = bbp_get_forum_reply_count( $forum_id );
707 // this taken out as it seems to get a faulty answer, possibly due to something alreday filtering the bbp_get_forum_last_active_id function ! so now go direct !
708 //$last_active = bbp_get_forum_last_active_id( $forum_id );
709 $last_active = (int) get_post_meta( $forum_id, '_bbp_last_active_id', true );
710
711 // Has replies
712 if ( !empty( $reply_count ) ) {
713 $reply_text = sprintf( _n( '%s reply', '%s replies', $rc_int, 'bbpress' ), $reply_count );
714 }
715
716 $topic_text = sprintf( _n( '%s topic', '%s topics', $tc_int, 'bbpress' ), $topic_count );
717 $check=false ;
718
719 // Forum has active data
720 if ( !empty( $last_active ) ) {
721 $last_updated_by = bbp_get_author_link( array( 'post_id' => $last_active, 'size' => $r['size'] ) );
722 //$last_updated_by = $forum_id.' '.$last_active ;
723 //now get the forum the LAST ACTIVE topic/reply comes from
724 $last_active_forum_id = private_groups_get_forum_id_from_post_id($last_active) ;
725 //and see if our user is allowed to see this post, if so display the lastest
726 if (private_groups_can_user_view_post_id($last_active_forum_id )) {
727 $check = true ; //set check as we're outputing in this section
728 $time_since = bbp_get_forum_last_active_time( $forum_id );
729 //$time_since = bbp_get_forum_freshness_link( $forum_id );
730
731
732 // Has replies
733 if ( ! empty( $reply_count ) ) {
734 $retstr = bbp_is_forum_category( $forum_id )
735 ? sprintf( esc_html__( 'This category has %1$s, %2$s, and was last updated %3$s by %4$s.', 'bbpress' ), $topic_text, $reply_text, $time_since, $last_updated_by )
736 : sprintf( esc_html__( 'This forum has %1$s, %2$s, and was last updated %3$s by %4$s.', 'bbpress' ), $topic_text, $reply_text, $time_since, $last_updated_by );
737
738 // Only has topics
739 } else {
740 $retstr = bbp_is_forum_category( $forum_id )
741 ? sprintf( esc_html__( 'This category has %1$s, and was last updated %2$s by %3$s.', 'bbpress' ), $topic_text, $time_since, $last_updated_by )
742 : sprintf( esc_html__( 'This forum has %1$s, and was last updated %2$s by %3$s.', 'bbpress' ), $topic_text, $time_since, $last_updated_by );
743 }
744
745
746 } //end of if user can view
747
748 // Forum has no last active data
749 //OR user is not allowed to see this data
750 }
751 if ($check == false) {
752
753 if ( !empty( $reply_count ) ) {
754
755 if ( bbp_is_forum_category( $forum_id ) ) {
756 $retstr = sprintf( esc_html__( 'This category contains %1$s and %2$s.', 'bbpress' ), $topic_text, $reply_text );
757 } else {
758 $retstr = sprintf( esc_html__( 'This forum contains %1$s and %2$s.', 'bbpress' ), $topic_text, $reply_text );
759 }
760
761 } else {
762
763 if ( !empty( $topic_count ) ) {
764
765 if ( bbp_is_forum_category( $forum_id ) ) {
766 $retstr = sprintf( esc_html__( 'This category contains %1$s.', 'bbpress' ), $topic_text );
767 } else {
768 $retstr = sprintf( esc_html__( 'This forum contains %1$s.', 'bbpress' ), $topic_text );
769 }
770
771 } else {
772 $retstr = esc_html__( 'This forum is empty.', 'bbpress' );
773 }
774 }
775 }
776
777 // Add the 'view all' filter back
778 add_filter( 'bbp_get_forum_permalink', 'bbp_add_view_all' );
779
780 // Combine the elements together
781 $retstr = $r['before'] . $retstr . $r['after'];
782
783 // Return filtered result
784 return apply_filters( 'rpg_get_single_forum_description', $retstr, $r );
785
786 }
787
788
789 //function to deal with cases where a forum/category has sub forums, but the user might not be able to see all these sub forums, and gather data
790 function rpg_check_subforums ($forum_id) {
791 $forum_id = bbp_get_forum_id( $forum_id );
792 $sub_forums = private_groups_get_permitted_subforums($forum_id) ;
793 if ( !empty( $sub_forums ) ) {
794 //set up an array $show with dates for each forum set to last active topic ID
795 foreach ( $sub_forums as $sub_forum ) {
796 $sub_forum_id = $sub_forum->ID ;
797 $active_id = bbp_get_forum_last_active_id( $sub_forum_id );
798 $last_active = get_post_meta( $active_id, '_bbp_last_active_time', true );
799 $curdate = strtotime($last_active);
800 $show[$curdate] = $active_id ;
801 }
802 //then loop thorugh this array to find the most recent date
803 $mostRecent= 0;
804 foreach($show as $date=>$value){
805 if ($date > $mostRecent) {
806 $mostRecent = $date;
807 }
808 }
809 //then assemble the anchor for latest topic
810 $latest_topic = $show[$mostRecent] ;
811 $link_url = bbp_get_topic_permalink( $latest_topic );
812 $title = bbp_get_topic_title( $latest_topic );
813 $time_since = bbp_get_topic_last_active_time( $latest_topic );
814 $anchor ['latest_topic'] = $latest_topic ;
815 $anchor ['link_url'] = $link_url ;
816 $anchor ['title'] = $title;
817 $anchor ['display'] = $time_since;
818 }
819 else {
820 //pass back a title which we can use to say none
821 $anchor ['title'] = 'no_topics';
822 }
823 return $anchor ;
824 }
825
826
827 function rpg_get_forum_topic_count ($topics, $forum_id) {
828 $forum_id = bbp_get_forum_id( $forum_id );
829 //do a check to ensure that user can see all items in this forum and if not calculate topics by author - for 'access own' forums
830 if (!rpg_access_own_topics_check ($forum_id)) {
831 $topics = rpg_calaculate_own_topics ($forum_id) ;
832 return apply_filters('rpg_get_forum_topic_count3' , $topics, $forum_id );
833 }
834 $sub_forums = private_groups_get_permitted_subforums($forum_id) ;
835
836 if ( !empty( $sub_forums ) ) {
837 $topics = 0 ;
838 foreach ( $sub_forums as $sub_forum ) {
839 $sub_forum_id = $sub_forum->ID ;
840 //and increment topic totals
841 $topics = $topics + get_post_meta( $sub_forum_id, '_bbp_total_topic_count', true );
842 }
843 //if main forum is also a forum not a category, then add this in
844 if (bbp_get_forum_type($forum_id) == 'forum') {
845 $topics = $topics + get_post_meta( $forum_id, '_bbp_topic_count', true );
846 }
847 return apply_filters('rpg_get_forum_topic_count1' , $topics, $forum_id );
848 }
849 else {
850 return apply_filters('rpg_get_forum_topic_count2', $topics, $forum_id );
851 }
852 }
853
854 function rpg_calaculate_own_topics ($forum_id) {
855 $count=0 ;
856 $user_id = wp_get_current_user()->ID;
857 //Get an array of topics
858 global $wpdb;
859 $post=bbp_get_topic_post_type() ;
860 $post_ids=$wpdb->get_col("select ID from $wpdb->posts where post_type = '$post' and post_parent = '$forum_id' and post_status <> 'trash'") ;
861 foreach ($post_ids as $post_id) {
862 $author_id = bbp_get_topic_author_id( $post_id);
863 if ($user_id == $author_id)
864 $count++ ;
865 }
866 return $count;
867 }
868
869 function rpg_calaculate_own_replies ($forum_id) {
870 $count=0 ;
871 $topics = array();
872 $user_id = wp_get_current_user()->ID;
873 //Get an array of topics first
874 global $wpdb;
875 $post=bbp_get_topic_post_type() ;
876 $post_ids=$wpdb->get_col("select ID from $wpdb->posts where post_type = '$post' and post_parent = '$forum_id' and post_status <> 'trash'") ;
877 foreach ($post_ids as $post_id) {
878 $author_id = bbp_get_topic_author_id( $post_id);
879 if ($user_id == $author_id) {
880 //get the reply count
881 $count= $count + get_post_meta ($post_id, '_bbp_reply_count', true) ;
882 }
883 }
884 return $count;
885 }
886
887
888
889 function rpg_get_forum_reply_count ($replies, $forum_id) {
890 $forum_id = bbp_get_forum_id( $forum_id );
891 if (!rpg_access_own_topics_check ($forum_id)) {
892 $replies = rpg_calaculate_own_replies ($forum_id) ;
893 return apply_filters('rpg_get_forum_reply_count3' , $replies, $forum_id );
894 }
895 $sub_forums = private_groups_get_permitted_subforums($forum_id) ;
896 if ( !empty( $sub_forums ) ) {
897 $replies = 0 ;
898 foreach ( $sub_forums as $sub_forum ) {
899 $sub_forum_id = $sub_forum->ID ;
900 //and increment reply totals
901 $replies = $replies + get_post_meta( $sub_forum_id, '_bbp_total_reply_count', true );
902 }
903 //if main forum is also a forum not a category, then add this in
904 if (bbp_get_forum_type($forum_id) == 'forum') {
905 $replies = $replies + get_post_meta( $forum_id, '_bbp_reply_count', true );
906 }
907 return apply_filters('rpg_get_forum_reply_count1' , $replies, $forum_id );
908 }
909 else {
910 return apply_filters('rpg_get_forum_reply_count2', $replies, $forum_id );
911 }
912 }
913
914 function rpg_get_forum_post_count ($retval, $forum_id) {
915 $forum_id = bbp_get_forum_id( $forum_id );
916 if (!rpg_access_own_topics_check ($forum_id)) {
917 $retval = rpg_calaculate_own_replies ($forum_id) ;
918 return apply_filters('rpg_get_forum_reply_count3' , $retval, $forum_id );
919 }
920 $sub_forums = private_groups_get_permitted_subforums($forum_id) ;
921 if ( !empty( $sub_forums ) ) {
922 $replies = $topics = $retval = 0 ;
923 foreach ( $sub_forums as $sub_forum ) {
924 $sub_forum_id = $sub_forum->ID ;
925 //and increment reply totals
926 $replies = $replies + get_post_meta( $sub_forum_id, '_bbp_total_reply_count', true );
927 $topics = $topics + get_post_meta( $sub_forum_id, '_bbp_total_topic_count', true );
928 }
929 //if main forum is also a forum not a category, then add this in
930 /*if (bbp_get_forum_type($forum_id) == 'forum') {
931 $replies = $replies + get_post_meta( $sub_forum_id, '_bbp_reply_count', true );
932 $topics = $topics + get_post_meta( $sub_forum_id, '_bbp_topic_count', true );
933 }*/
934 $retval = $replies + $topics ;
935 return apply_filters('rpg_get_forum_post_count1' , $retval, $forum_id );
936 }
937 else {
938 return apply_filters('rpg_get_forum_post_count2', $retval, $forum_id );
939 }
940 }
941
942
943
944
945 //add private groups to forum lists
946
947 ////////////////////////////// ADD FORUM ID column to admin if not added by style pack
948
949 function rpg_ID_column_add($columns) {
950 $new = array();
951 foreach($columns as $key => $title) {
952 if ($key=='bbp_reply_topic') // Put the forum ID column before the Topics column
953 $new['rpg_id'] = 'Forum ID';
954 $new[$key] = $title;
955 }
956 return $new;
957 }
958
959 function rpg_ID_column_value($column_name, $id) {
960 if ($column_name == 'rpg_id') echo $id;
961 }
962
963
964 function rpg_groups_column_add($columns) {
965 $new = array();
966 foreach($columns as $key => $title) {
967 if ($key=='bbp_forum_topic_count') // Put the forum ID column before the Topics column
968 $new['rpg_pg'] = 'Private Groups';
969 $new[$key] = $title;
970 }
971 return $new;
972 }
973
974 function rpg_groups_column_value($column_name, $id) {
975 if ($column_name == 'rpg_pg') {
976 global $rpg_groups ;
977 $meta = get_post_meta( $id, '_private_group', false );
978 echo '<ul>' ;
979 foreach ($meta as $group) {
980 $groupname=__('Group','bbp-private-groups').substr($group,5,strlen($group)) ;
981 $details = $rpg_groups[$group] ;
982 echo '<li>'.$groupname.' '.$details.'</li>' ;
983 }
984 echo '</ul>' ;
985 }
986
987 }
988
989 function rpg_user_can_view_forum ($retval, $forum_id, $user_id ){
990 //if it's already false then just return it, otherwise...
991 if ($retval == true) {
992 $retval = private_groups_can_user_view_post( $user_id, $forum_id ) ;
993 }
994 return $retval ;
995 }
996
997
998 //filter back end topics and replies to ensure that moderators can only see their own forums
999 function rpg_filter_bbp_request( $array ) {
1000 //only apply if we are in backend admin (otherwise all forum displays go to 404 !)
1001 $link = $_SERVER['REQUEST_URI'];
1002 $topic_admin = '/wp-admin/edit.php?post_type='.bbp_get_topic_post_type() ;
1003 $reply_admin = '/wp-admin/edit.php?post_type='.bbp_get_reply_post_type() ;
1004 $topic_date_filter = '/wp-admin/edit.php?s&post_status=all&post_type='.bbp_get_topic_post_type() ;
1005 $reply_date_filter = '/wp-admin/edit.php?s&post_status=all&post_type='.bbp_get_reply_post_type() ;
1006 if ( strpos($link, $topic_admin) !== false || strpos($link, $reply_admin) !== false || strpos($link, $topic_date_filter) !== false || strpos($link, $reply_date_filter) !== false ) {
1007 global $wpdb;
1008 $forum=bbp_get_forum_post_type() ;
1009 $forum_ids=$wpdb->get_col("select ID from $wpdb->posts where post_type = '$forum'") ;
1010 //check this list against those the user is allowed to see, and create a list of valid ones for the wp_query
1011 $allowed_forums = private_groups_check_permitted_forums($forum_ids) ;
1012 $array['meta_key'] = '_bbp_forum_id';
1013 $array['meta_value'] = $allowed_forums;
1014 $array['compare']= 'IN' ;
1015 }
1016 return $array ;
1017 }
1018
1019 function rpg_warning(){
1020
1021 // bbpress version
1022 if (function_exists('bbPress')) {
1023 $bbp = bbpress();
1024 } else {
1025 global $bbp;
1026 }
1027 if (isset($bbp->version)) {
1028 $bbpversion = $bbp->version;
1029 $bbpversion = (substr($bbpversion, 0, 3)) ;
1030 } else {
1031 $bbpversion = '???';
1032 }
1033
1034 // this is a success message
1035 if($bbpversion =='2.6' ):
1036 $n = get_option ('rpg_warning', 0) ;
1037 $n++ ;
1038 if ($n<11) {
1039 ?>
1040 <div class='notice notice-success is-dismissible'>
1041 <?php
1042 _e('WARNING : You have bbp-Private-Groups plugin working with bbPress 2.6. If you have forums that are set to \'hidden\' or \'private\' PLEASE CHECK that they display correctly - 2.6 has changed the way these forums are excluded. If in doubt, set these forums to a group that is not being used to ensure they do not display by mistake. ', 'bbp-private-groups'); ?>
1043 </div>
1044 <?php
1045 update_option ('rpg_warning' , $n) ;
1046 }
1047
1048 endif;
1049 }
1050
1051 function rpg_exclude_menu_items( $items, $menu, $args ) {
1052 // Iterate over the items to search and remove
1053 foreach ( $items as $key => $item ) {
1054 if ( $item->object == 'forum' ) {
1055 if (!private_groups_can_user_view_post_id($item->object_id)) unset( $items[$key] );
1056 }
1057 }
1058 return $items;
1059 }
1060
1061
1062
1063