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

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

360 lines 18.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 // filter the topics so only those viewable by user are shown, and topic and reply forms only shown where needed
5 //only add filters if topic permissions set
6 global $rpg_topic_permissions ;
7 if (!empty ($rpg_topic_permissions['activate']) ) {
8 //filter display
9 add_filter('bbp_before_has_topics_parse_args', 'private_groups_topics', 10, 2);
10 //filter forms being shown
11 add_filter ( 'bbp_current_user_can_access_create_topic_form', 'pg_current_user_can_access_create_topic_form') ;
12 add_filter ( 'bbp_current_user_can_access_create_reply_form', 'pg_current_user_can_access_create_reply_form') ;
13 }
14
15
16 function private_groups_topics ($args='') {
17 //we only need to filter topics if this forum has topic permissions set to 5 - Create/Edit/view OWN Topics, create/edit Replies to those topics and view any Replies to those topics
18 //so check if this is the case
19 $valuecheck = pg_check_topic_permissions(bbp_get_forum_id()) ;
20 //then we'll quit unless $valuecheck['check5']='1', as we don't need to filter
21 if (empty($valuecheck['check5'])) return $args;
22 //but then we need to check if this user has permission as part of other groups, if so quit
23 if (!empty($valuecheck['check1']) || !empty($valuecheck['check2']) || !empty($valuecheck['check3']) || !empty($valuecheck['check4']) ) return $args ;
24 //so now as we only have check5 - only show topics by the author
25 $args['author'] = wp_get_current_user()->ID;
26 return $args ;
27 }
28
29 function topic_permissions_check ($post_id = 0) {
30 //determines if topic permissions are active, and if so if user can see a post
31 global $rpg_topic_permissions ;
32 if (empty ($rpg_topic_permissions['activate']) )
33 return true;
34 //now we test if this is a forum - if so then topic permissions don't apply so we return true - revised in 3.6.2 for 'access own' type forums
35 $post_type = get_post_type ($post_id) ;
36 if ($post_type == bbp_get_forum_post_type())
37 return true;
38 //first check if this is a sticky topic and if so show it
39 if (bbp_is_topic_super_sticky($post_id) || bbp_is_topic_sticky($post_id))
40 return true;
41 //now we do a topic permissions check
42 $forum_id = private_groups_get_forum_id_from_post_id($post_id);
43 //and check if user can see this topic by checking if he only has $valuecheck['check5']='1' for the forum
44 if (rpg_access_own_topics_check ($forum_id, $post_id))
45 return true;
46
47 }
48
49
50 function rpg_access_own_topics_check ($forum_id, $post_id=0) {
51 $valuecheck = pg_check_topic_permissions($forum_id) ;
52 //then we'll quit unless $valuecheck['check5']='1', as we don't need to filter
53 if (empty($valuecheck['check5']))
54 return true;
55 //but then we need to check if this user has permission as part of other groups, if so quit
56 if (!empty($valuecheck['check1']) || !empty($valuecheck['check2']) || !empty($valuecheck['check3']) || !empty($valuecheck['check4']) )
57 return true;
58 //so now as we only have check5 - only show topics, or replies to topics, by the author
59 if (!empty ($post_id)) {
60 if (bbp_is_reply ($post_id) ) $topic_id = bbp_get_reply_topic_id($post_id) ;
61 else $topic_id = bbp_get_topic_id ($post_id) ;
62 $author_id = bbp_get_topic_author_id( $topic_id);
63 $user_id = wp_get_current_user()->ID;
64 if ($user_id == $author_id)
65 return true ;
66 }
67 }
68
69
70 function rpg_own_topic_freshness ($forum_id) {
71 $user_id = wp_get_current_user()->ID;
72 $latest = array() ;
73 //Get an array of topics
74 global $wpdb;
75 $post=bbp_get_topic_post_type() ;
76 $post_ids=$wpdb->get_col("select ID from $wpdb->posts where post_type = '$post' and post_parent = '$forum_id' and post_status <> 'trash'") ;
77 foreach ($post_ids as $post_id) {
78 $author_id = bbp_get_topic_author_id( $post_id);
79 if ($user_id == $author_id) {
80 $time = get_post_meta ($post_id, '_bbp_last_active_time', true) ;
81 //then add to the array the last active time
82 $latest[$post_id] = $time ;
83 }
84 }
85 if (empty($latest)) $retval = 0 ;
86 else {
87 $maxs[0] = array_keys($latest, max($latest)) ;
88 $retval = implode ('',$maxs[0]) ;
89 }
90 return $retval ;
91 }
92
93
94
95 function pg_current_user_can_access_create_reply_form() {
96 // Users need to earn access
97 $retval = false;
98
99 $user_id = wp_get_current_user()->ID;
100 // Always allow keymasters
101 if ( bbp_is_user_keymaster() ) {
102 $retval = true;
103 }
104 //disallow editing of closed topics unless keymaster
105 elseif (!bbp_is_user_keymaster() && bbp_is_topic_closed()) {
106 $retval = false;
107 }
108
109 //for replies
110
111 //we are in a single topic so check if user is allowed replies in the forum the topic belongs to
112 //or we are editing an existing reply - so check even though user should not have been able to create !
113
114
115
116 // Looking at a single topic, topic is open, and forum is open
117 elseif ( ( bbp_is_single_topic() || is_page() || is_single() ) && bbp_is_topic_open() && bbp_is_forum_open() ) {
118 //so now find the topic, and then check if user has topic pernissions that need checking, by finding the forum
119 $topic = bbp_get_topic_id() ;
120 //we should only ever have a topic ID, and in theory the next lines aren't needed, but here just in case
121 if (!empty ($topic)) $forum_id = get_post_field( 'post_parent', $topic );
122 //if no topic is set, then we are in theory creating a new topic so should not be in this part, but just in case - we don't have topic=forum, so we need to find if we're in a single forum, and check that
123 elseif (bbp_is_single_forum() ) $forum_id = bbp_get_forum_id() ;
124 else {
125 //else no idea why we are in this function so return false to be safe
126 $retval = false;
127 return (bool) apply_filters( 'pg_current_user_can_access_create_reply_form_a', (bool) $retval );
128 }
129 //else we have a forum, so check permissions
130
131 $valuecheck = pg_check_topic_permissions ($forum_id) ;
132
133
134 //so we exit with potentials of on the $valuecheck array, and now need to check these in order
135 //users can have multiple groups, so we start with highest permission and work down
136 if (empty ($valuecheck) || !empty ($valuecheck['check4']) ) {
137 //if the array doesn't exist, then either the forum has no topic permissions, or the user doesn't have any matching so we can return
138 //if we have a $value('check4'] then we can return, as access has already been decided and we can return
139 //either with retval true as the user is admin or if the user can edit
140 $retval = bbp_current_user_can_publish_replies();
141 return (bool) apply_filters( 'pg_current_user_can_access_create_reply_form_b', (bool) $retval );
142 }
143 //if $value['check3'] exists, then user can edit replies, so set user capabilities and return
144 if (!empty ($valuecheck['check3']) ) {
145 $retval = bbp_current_user_can_publish_replies();
146 return (bool) apply_filters( 'pg_current_user_can_access_create_reply_form_c', (bool) $retval );
147 }
148
149 //if $value['check5'] (helpdesk type forum) exists, then user can create and edit his own topics - so he can create create replies to his own topics
150 if (!empty ($valuecheck['check5']) ) {
151 //so we see if he is author of this topic, set to true to allow reply
152 $user_id = wp_get_current_user()->ID;
153 $author_id = bbp_get_topic_author_id( $topic);
154 if ($user_id == $author_id)
155 $retval = bbp_current_user_can_publish_replies();
156 else $retval = false ;
157 return (bool) apply_filters( 'pg_current_user_can_access_create_topic_form_d', (bool) $retval );
158 }
159
160
161 //if $value['check2'] or $value ['check1'] exists, then user can only either edit topics or view, so set to false and return
162 if (!empty ($valuecheck['check2']) || !empty ($valuecheck['check1']) ) {
163 $retval = false;
164 return (bool) apply_filters( 'pg_current_user_can_access_create_topic_form_e', (bool) $retval );
165 }
166
167 // or.. User can edit this reply
168 } elseif ( bbp_is_reply_edit() ) {
169 $topic = bbp_get_topic_id() ;
170 if (!empty ($topic)) $forum_id = get_post_field( 'post_parent', $topic );
171 $valuecheck = pg_check_topic_permissions ($forum_id) ;
172 //so we exit with potentials of on the $valuecheck array, and now need to check these in order
173 //users can have multiple groups, so we start with highest permission and work down
174 //var_dump ($valuecheck) ;
175 if (empty ($valuecheck) || !empty ($valuecheck['check4']) ) {
176 //if the array doesn't exist, then either the forum has no topic permissions, or the user doesn't have any matching so we can return
177 //if we have a $value('check4'] then we can return, as access has already been decided and we can return
178 //either with retval true as the user is admin or if the user can edit
179 $retval = current_user_can( 'edit_reply', bbp_get_topic_id() );
180 return (bool) apply_filters( 'pg_current_user_can_access_create_topic_form_f', (bool) $retval );
181 }
182 //if $value['check3'] exists, then user can edit reply, so set to true then test user capabilities and return
183 if (!empty ($valuecheck['check3']) ) {
184 //echo '<br> value check is 2 create replies' ;
185 $retval = current_user_can( 'edit_reply', bbp_get_topic_id() );
186 return (bool) apply_filters( 'pg_current_user_can_access_create_topic_form_g', (bool) $retval );
187 }
188 //if $value['check5'] (helpdesk type forum) exists, then user can create and edit his own topics - so he can edit his own topic
189 //he shouldn't be able to see or edit other topics, but we'll do a check just in case , and set to false if needbe
190 //- for instance if he is a moderator, then we might not want him moderating this forum
191 if (!empty ($valuecheck['check5']) ) {
192 //so we see if he is author of this topic, set to true
193 $user_id = wp_get_current_user()->ID;
194 $author_id = bbp_get_topic_author_id( $topic);
195 if (user_id == $author_id)
196 $retval = current_user_can( 'edit_reply', $topic );
197 else $retval = false ;
198 return (bool) apply_filters( 'pg_current_user_can_access_create_topic_form_h', (bool) $retval );
199 }
200 //if $value['check2'] or $value ['check1'] exists, then user can only either edit toopics or view , so set to false and return
201 if (!empty ($valuecheck['check2']) || !empty ($valuecheck['check1']) ) {
202 //echo '<br> value check is 2 or 1 create replies or only view - so currently set to false as only done for replies' ;
203 $retval = false;
204 return (bool) apply_filters( 'pg_current_user_can_access_create_topic_form_j', (bool) $retval );
205 }
206
207
208 } //end of reply edit
209 return (bool) apply_filters( 'pg_current_user_can_access_create_reply_form', (bool) $retval );
210 }
211
212 function pg_current_user_can_access_create_topic_form() {
213 // Users need to earn access
214 $retval = false;
215
216 $user_id = wp_get_current_user()->ID;
217 // Always allow keymasters
218 if ( bbp_is_user_keymaster() ) {
219 $retval = true;
220 }
221 //disallow editing of closed topics unless keymaster
222 elseif (!bbp_is_user_keymaster() && bbp_is_topic_closed()) {
223 $retval = false;
224 }
225
226 //for topics
227 //either we are in multiple forums - in which case this function is not called - 'private_groups_get_dropdown_forums' in forum functions takes care of restricting access
228 //or we are in a single forum so check if user is allowed topics in that forum
229 //or we are editing an existing topic - so check even though user should not have been able to create !
230
231
232
233 // Looking at a single forum & forum is open
234 elseif ( ( bbp_is_single_forum() || is_page() || is_single() ) && bbp_is_forum_open() ) {
235 $forum_id = bbp_get_forum_id() ;
236 $valuecheck = pg_check_topic_permissions ($forum_id) ;
237 //so we exit with potentials of on the $valuecheck array, and now need to check these in order
238 //users can have multiple groups, so we start with highest permission and work down
239 if (empty ($valuecheck) || !empty ($valuecheck['check4']) ) {
240 //if the array doesn't exist, then either the forum has no topic permissions, or the user doesn't have any matching so we can return
241 //if we have a $value('check4'] then we can return, as access has already been decided and we can return
242 //either with retval true as the user is admin or if the user can edit
243 $retval = bbp_current_user_can_publish_topics();
244 return (bool) apply_filters( 'pg_current_user_can_access_create_topic_form_a', (bool) $retval );
245 }
246 //if $value['check2'] exists, then user can edit topic, so set to true then test user capabilities and return
247 if (!empty ($valuecheck['check2']) ) {
248 $retval = bbp_current_user_can_publish_topics();
249 return (bool) apply_filters( 'pg_current_user_can_access_create_topic_form_b', (bool) $retval );
250 }
251
252 //if $value['check5'] (helpdesk type forum) exists, then user can create and edit his own topics - so he can create topics
253 if (!empty ($valuecheck['check5']) ) {
254 $retval = bbp_current_user_can_publish_topics();
255 return (bool) apply_filters( 'pg_current_user_can_access_create_topic_form_c', (bool) $retval );
256 }
257 //if $value['check3'] or $value ['check1'] exists, then user can only either edit replies or view, so set to false and return
258 if (!empty ($valuecheck['check3']) || !empty ($valuecheck['check1']) ) {
259 //echo '<br> value check is 3 or 1 create topics or only view - so currently set to false as only done for replies' ;
260 $retval = false;
261 return (bool) apply_filters( 'pg_current_user_can_access_create_topic_form_d', (bool) $retval );
262 }
263
264 // or.. User can edit this topic
265 } elseif ( bbp_is_topic_edit() ) {
266 $topic = bbp_get_topic_id() ;
267 if (!empty ($topic)) $forum_id = get_post_field( 'post_parent', $topic );
268 $valuecheck = pg_check_topic_permissions ($forum_id) ;
269 //so we exit with potentials of on the $valuecheck array, and now need to check these in order
270 //users can have multiple groups, so we start with highest permission and work down
271 //var_dump ($valuecheck) ;
272 if (empty ($valuecheck) || !empty ($valuecheck['check4']) ) {
273 //if the array doesn't exist, then either the forum has no topic permissions, or the user doesn't have any matching so we can return
274 //if we have a $value('check4'] then we can return, as access has already been decided and we can return
275 //either with retval true as the user is admin or if the user can edit
276 $retval = current_user_can( 'edit_topic', bbp_get_topic_id() );
277 return (bool) apply_filters( 'pg_current_user_can_access_create_topic_form_e', (bool) $retval );
278 }
279 //if $value['check2'] exists, then user can edit topic, so set to true then test user capabilities and return
280 if (!empty ($valuecheck['check2']) ) {
281 //echo '<br> value check is 2 create topics' ;
282 $retval = true;
283 //unless their base permission changes it back to false
284 $retval = current_user_can( 'edit_topic', bbp_get_topic_id() );
285 return (bool) apply_filters( 'pg_current_user_can_access_create_topic_form_f', (bool) $retval );
286 }
287 //if $value['check5'] (helpdesk type forum) exists, then user can create and edit his own topics - so he can edit his own topic
288 //he shouldn't be able to see or edit other topics, but we'll do a check just in case , and set to false if needbe
289 //- for instance if he is a moderator, then we might not want him moderating this forum
290 if (!empty ($valuecheck['check5']) ) {
291 //so we see if he is author of this topic, set to true
292 $user_id = wp_get_current_user()->ID;
293 $topic_id = bbp_get_topic_id() ;
294 $author_id = bbp_get_topic_author_id( $topic_id);
295 if (user_id == $author_id)
296 $retval = current_user_can( 'edit_topic', bbp_get_topic_id() );
297 else $retval = false ;
298 return (bool) apply_filters( 'pg_current_user_can_access_create_topic_form_g', (bool) $retval );
299 }
300 //if $value['check3'] or $value ['check1'] exists, then user can only either edit replies or view , so set to false and return
301 if (!empty ($valuecheck['check3']) || !empty ($valuecheck['check1']) ) {
302 //echo '<br> value check is 3 or 1 create replies or only view - so currently set to false as only done for replies' ;
303 $retval = false;
304 return (bool) apply_filters( 'pg_current_user_can_access_create_topic_form_h', (bool) $retval );
305 }
306
307
308 } //end of topic edit
309 return (bool) apply_filters( 'pg_current_user_can_access_create_topic_form', (bool) $retval );
310 }
311
312
313 function pg_check_topic_permissions ($forum_id = 0) {
314 if ($forum_id == 0 ) {
315 return ;
316 }
317 $valuecheck = array();
318 if (!is_user_logged_in() ) {
319 //if user is not logged-in, then we only allow access if topic permissions are activated AND anonymous posting is allowed, AND anon user is allowed to create topics
320 global $rpg_topic_permissions ;
321 if (!empty ($rpg_topic_permissions['activate']) && bbp_allow_anonymous() ) {
322 $value = get_post_meta( $forum_id, '_private_group_nonloggedin', true );
323 if ($value == '1') $valuecheck['check1']='1' ;
324 if ($value == '2' ) $valuecheck['check2']='1' ;
325 if ($value == '3') $valuecheck['check3']='1' ;
326 if ($value == '4') $valuecheck['check4']='1' ;
327 }
328 //if not then it will return an empty array, so no access
329 }
330 else{
331 //else user is logged in
332 $user_id = wp_get_current_user()->ID;
333 $groups = get_post_meta( $forum_id, '_private_group', false );
334 //so now we know which forum it is in - now we need to know if user has access to any groups of this forum - so we get the groups this user has
335 $check=get_user_meta( $user_id, 'private_group',true);
336 foreach ( $groups as $group ) {
337 $has_group = false ;
338 //single group set?
339 if ($check==$group ) $has_group = true;
340 //multiple group set
341 if (strpos($check, '*'.$group.'*') !== FALSE) $has_group = true;
342 //so if user has this group, so check if forum has this set in topic permissions.
343 if (!empty($has_group)) {
344 //user might have multiple groups with different topic permissions, so we need to create an array of these.
345 $tp = '_private_group_'.$group ;
346 $value = get_post_meta( $forum_id, $tp, true);
347 //$value = (!empty (get_post_meta( $forum_id, $tp, true)) ? get_post_meta( $forum_id, $tp, true ) : '' );
348 if ($value == '1') $valuecheck['check1']='1' ;
349 if ($value == '2' ) $valuecheck['check2']='1' ;
350 if ($value == '3') $valuecheck['check3']='1' ;
351 if ($value == '4') $valuecheck['check4']='1' ;
352 if ($value == '5') $valuecheck['check5']='1' ;
353 }
354 } //end of foreach
355 } // end of else
356 return ($valuecheck) ;
357 }
358
359
360