| 1 |
<?php |
| 2 |
|
| 3 |
//version 1.9.2 fixed private groups forums for subscriptions |
| 4 |
//version 3.9.7 fixed private groups topics for subscriptions |
| 5 |
|
| 6 |
|
| 7 |
add_filter ('bbp_before_has_topics_parse_args', 'pg_has_topics') ; |
| 8 |
|
| 9 |
|
| 10 |
function pg_has_topics( $args = '' ) { |
| 11 |
|
| 12 |
//for forums, check if being called by subscriptions and if so skip filtering (as you can only subscribe to forums you can already see) |
| 13 |
|
| 14 |
if(isset($args['post__in']) ){ |
| 15 |
|
| 16 |
return $args ; |
| 17 |
|
| 18 |
} |
| 19 |
|
| 20 |
/*then fix for topic subscriptions - logic here is that if a user has subscribed to a topic, then he is allowed to view that topic, so we |
| 21 |
don't need to filter. So in \bbpress\includes\users\engagements.php we have the function bbp_get_user_topic_subscriptions whch is used in |
| 22 |
\bbpress\bbpress\templates\default\bbpress\user-subscriptions.php to get the topic subscriptions. This sets up the $args for bbp_has_topics |
| 23 |
which includes the meta args below, so we test for this, and then we know we are in a subscription call. |
| 24 |
*/ |
| 25 |
|
| 26 |
if(isset($args['meta_query'][0]['key'] )){ |
| 27 |
|
| 28 |
if ($args['meta_query'][0]['key'] == '_bbp_subscription') |
| 29 |
return $args ; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
$user_id2 = false; |
| 34 |
|
| 35 |
if (isset($args['author'])) { |
| 36 |
|
| 37 |
$user_id2 = bbp_get_user_id($args['author']); |
| 38 |
|
| 39 |
} elseif (isset($args['meta_query'][0]['value'])) { |
| 40 |
|
| 41 |
$user_id2 = bbp_get_user_id($args['meta_query'][0]['value']); |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
$default_post_parent = bbp_is_single_forum() ? bbp_get_forum_id() : 'any'; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
if ($default_post_parent == 'any') { |
| 52 |
|
| 53 |
if ( bbp_is_user_keymaster()) return $args; |
| 54 |
|
| 55 |
$user_id = wp_get_current_user()->ID; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
if (user_can( $user_id, 'moderate' ) ) { |
| 60 |
|
| 61 |
$check=get_user_meta( $user_id, 'private_group',true); |
| 62 |
|
| 63 |
if ($check=='') return $args; |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
global $wpdb; |
| 72 |
|
| 73 |
$topic=bbp_get_topic_post_type() ; |
| 74 |
|
| 75 |
if (empty($user_id2)) { |
| 76 |
|
| 77 |
$post_ids=$wpdb->get_col("select ID from $wpdb->posts where post_type = '$topic'") ; |
| 78 |
|
| 79 |
} else { |
| 80 |
|
| 81 |
$post_ids=$wpdb->get_col("select ID from $wpdb->posts where post_type = '$topic' and post_author = '$user_id2'") ; |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
//check this list against those the user is allowed to see, and create a list of valid ones for the wp_query in bbp_has_topics |
| 86 |
|
| 87 |
$allowed_posts = check_private_groups_topic_ids($post_ids) ; |
| 88 |
|
| 89 |
if (empty ($allowed_posts)) $allowed_posts[] = -1 ; |
| 90 |
|
| 91 |
$args['post__in'] = $allowed_posts; |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
return $args; |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
//the function to check the above ! |
| 104 |
|
| 105 |
function check_private_groups_topic_ids($post_ids) { |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
//Init the Array which will hold our list of allowed posts |
| 110 |
|
| 111 |
$allowed_posts = array(); |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
//Loop through all the posts |
| 118 |
|
| 119 |
foreach ( $post_ids as $post_id ) { |
| 120 |
|
| 121 |
//Get the Forum ID based on Post Type Topic |
| 122 |
|
| 123 |
$topic=bbp_get_topic_post_type() ; |
| 124 |
|
| 125 |
$forum_id = private_groups_get_forum_id_from_post_id($post_id, $topic); |
| 126 |
|
| 127 |
//Check if User has permissions to view this Post ID |
| 128 |
|
| 129 |
//by calling the function that checks if the user can view this forum, and hence this post |
| 130 |
|
| 131 |
if (private_groups_can_user_view_post_id($forum_id) && topic_permissions_check ($post_id)) { |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
//User can view this post - add it to the allowed array |
| 136 |
|
| 137 |
array_push($allowed_posts, $post_id); |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
//Return the list |
| 146 |
|
| 147 |
return $allowed_posts; |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|