PluginProbe
Private groups / 2.3
Private groups v2.3
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 2.3, at includes/functions.php

307 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 add_action('bbp_template_redirect', 'private_group_enforce_permissions', 1);
4 add_filter('protected_title_format', 'pg_remove_protected_title');
5 add_filter('private_title_format', 'pg_remove_private_title');
6 add_filter('bbp_get_forum_freshness_link', 'custom_freshness_link' );
7 add_filter('bbp_get_author_link', 'pg_get_author_link' ) ;
8 add_action ('bbp_user_register', 'pg_role_group') ;
9
10
11
12 /*
13 * Check if the current user has rights to view the given Post ID
14 * Much of this code is from the work of Aleksandar Adamovic in his Tehnik BBPress Permissions - thanks !
15 *
16 */
17
18
19 function private_groups_check_can_user_view_post() {
20 //uses $post_id and $post_type to get the forum ($forum_id) that the post belongs to
21 global $wp_query;
22
23 // Get Forum Id for the current post
24 $post_id = $wp_query->post->ID;
25 $post_type = $wp_query->get('post_type');
26
27 if (bbp_is_topic_super_sticky($post_id)) return true;
28
29
30 $forum_id = private_groups_get_forum_id_from_post_id($post_id, $post_type);
31 //then call the function that checks if the user can view this forum, and hence this post
32 if (private_groups_can_user_view_post_id($forum_id))
33 return true;
34 }
35
36
37
38 /**
39 * Use the given query to determine which forums the user has access to.
40 *
41 * returns: an array of post IDs which user has access to.
42 */
43 function private_groups_get_permitted_post_ids($post_query) {
44
45 //Init the Array which will hold our list of allowed posts
46 $allowed_posts = array();
47
48
49 //Loop through all the posts
50 while ($post_query->have_posts()) :
51 $post_query->the_post();
52 //Get the Post ID and post type
53 $post_id = $post_query->post->ID;
54 $post_type = $post_query->post->post_type;
55 //Get the Forum ID based on Post Type (Reply, Topic, Forum)
56 $forum_id = private_groups_get_forum_id_from_post_id($post_id, $post_type);
57 //Check if User has permissions to view this Post ID
58 //by calling the function that checks if the user can view this forum, and hence this post
59 if (private_groups_can_user_view_post_id($forum_id)) {
60
61 //User can view this post - add it to the allowed array
62 array_push($allowed_posts, $post_id);
63 }
64
65 endwhile;
66
67 //Return the list
68 return $allowed_posts;
69 }
70
71
72
73
74 /*
75 * Returns the bbPress Forum ID from given Post ID and Post Type
76 *
77 * returns: bbPRess Forum ID
78 */
79 function private_groups_get_forum_id_from_post_id($post_id, $post_type) {
80 $forum_id = 0;
81
82 // Check post type
83 switch ($post_type) {
84 // Forum
85 case bbp_get_forum_post_type() :
86 $forum_id = bbp_get_forum_id($post_id);
87 break;
88
89 // Topic
90 case bbp_get_topic_post_type() :
91 $forum_id = bbp_get_topic_forum_id($post_id);
92 break;
93
94 // Reply
95 case bbp_get_reply_post_type() :
96 $forum_id = bbp_get_reply_forum_id($post_id);
97 break;
98 }
99
100 return $forum_id;
101 }
102
103 //enforce permission to ensure users only see permitted posts
104 function private_group_enforce_permissions() {
105 global $rpg_settingsf ;
106 // Bail if not viewing a bbPress item
107 if (!is_bbpress())
108 return;
109
110 // Bail if not viewing a single item or if user has caps
111 if (!is_singular() || bbp_is_user_keymaster() || current_user_can('read_hidden_forums'))
112 return;
113
114 if (!private_groups_check_can_user_view_post()) {
115 if (!is_user_logged_in()) {
116 if($rpg_settingsf['redirect_page2']) {
117 $link=$rpg_settingsf['redirect_page2'] ;
118 header( "Location: $link" );
119 }
120 else {
121 auth_redirect();
122 }
123 }
124 else {
125 if($rpg_settingsf['redirect_page1']) {
126 $link=$rpg_settingsf['redirect_page1'] ;
127 header( "Location: $link" );
128 }
129 else {
130 bbp_set_404();
131 }
132
133 }
134 }
135 }
136
137 //removes 'private' and protected prefix for forums
138 function pg_remove_private_title($title) {
139 global $rpg_settingsg ;
140 if( $rpg_settingsg['activate_remove_private_prefix']) {
141 return '%s';
142 }
143 else {
144 Return $title ;
145 }
146 }
147
148 function pg_remove_protected_title($title) {
149 global $rpg_settingsg ;
150 if( $rpg_settingsg['activate_remove_private_prefix']) {
151 return '%s';
152 }
153 else {
154 Return $title ;
155 }
156 }
157
158 function custom_freshness_link( $forum_id = 0 ) {
159 global $rpg_settingsf ;
160 $forum_id = bbp_get_forum_id( $forum_id );
161 $active_id = bbp_get_forum_last_active_id( $forum_id );
162 $link_url = $title = '';
163 $forum_title= bbp_get_forum_title ($forum_id) ;
164
165 if ( empty( $active_id ) )
166 $active_id = bbp_get_forum_last_reply_id( $forum_id );
167
168 if ( empty( $active_id ) )
169 $active_id = bbp_get_forum_last_topic_id( $forum_id );
170
171 if ( bbp_is_topic( $active_id ) ) {
172 $link_url = bbp_get_forum_last_topic_permalink( $forum_id );
173 //$link_id added to get post_id and type to allow for later check
174 $link_id= bbp_get_forum_last_topic_id ($forum_id) ;
175 $check="topic" ;
176 $title = bbp_get_forum_last_topic_title( $forum_id );
177 $forum_id_last_active = bbp_get_topic_forum_id($active_id);
178 } elseif ( bbp_is_reply( $active_id ) ) {
179 $link_url = bbp_get_forum_last_reply_url( $forum_id );
180 //$link-id added to get post-id and type to allow for later check
181 $link_id = bbp_get_forum_last_reply_id ( $forum_id );
182 $check="reply" ;
183 $title = bbp_get_forum_last_reply_title( $forum_id );
184 $forum_id_last_active = bbp_get_reply_forum_id($active_id);
185 }
186
187 $time_since = bbp_get_forum_last_active_time( $forum_id );
188
189
190 if ( !empty( $time_since ) && !empty( $link_url ) ) {
191 //ADDITIONAL CODE to original bbp_get_forum_freshness_link function
192 //test if user can see this post, and post link if they can
193 $user_id = wp_get_current_user()->ID;
194 //get the forum id for the post - that's the forum ID against which we check to see if it is in a group - no idea what forum group the stuff above produces, suspect post id of when last changed.
195 $forum_id_check = private_groups_get_forum_id_from_post_id($link_id, $check);
196 //now we can check if the user can view this, and if it's not private
197 if (private_groups_can_user_view_post($user_id,$forum_id_check) && !bbp_is_forum_private($forum_id_last_active)) {
198 $anchor = '<a href="' .esc_url( $link_url) . '" title="' . esc_attr( $title ) . '">' .esc_html( $time_since ) .'</a>';
199 }
200 //if it is private, then check user can view
201 elseif (private_groups_can_user_view_post($user_id,$forum_id_check) && bbp_is_forum_private($forum_id_last_active) && current_user_can( 'read_private_forums' ) ) {
202 $anchor = '<a href="' .esc_url( $link_url) . '" title="' . esc_attr( $title ) . '">' .esc_html( $time_since ) .'</a>';
203 }
204 //else user cannot see post so...
205 else {
206 //set up which link to send them to
207 if (!is_user_logged_in()) {
208 if($rpg_settingsf['redirect_page2']) {
209 $link=$rpg_settingsf['redirect_page2'] ;
210 }
211 else {
212 $link="/wp-login";
213 }
214 }
215 else {
216 if($rpg_settingsf['redirect_page1']) {
217 $link=$rpg_settingsf['redirect_page1'] ;
218 }
219 else {
220 $link='/404';
221 }
222
223 }
224 //now see if there is a freshness message
225 if ($rpg_settingsf['set_freshness_message']) {
226 $title=$rpg_settingsf['freshness_message'] ;
227 //and set up anchor
228 $anchor = '<a href="' . esc_url($link) . '">' .$title. '</a>';
229 }
230 else{
231 $anchor = '<a href="' . esc_url($link) . '">' .esc_html( $time_since ) .'</a>';
232 }
233
234 }
235 }
236
237 else
238 $anchor = esc_html__( 'No Topics', 'bbpress' );
239
240 return $anchor;
241 }
242
243
244
245 function pg_get_author_link( ) {
246 $user_id2 = wp_get_current_user()->ID;
247
248 // Parse arguments against default values
249 $r = bbp_parse_args( $args, array(
250 'post_id' => $post_id,
251 'link_title' => '',
252 'type' => 'both',
253 'size' => 14
254 ), 'pg_get_author_link' );
255
256
257 //confirmed topic
258 if( bbp_is_topic( $post_id) ) {
259 $topic=bbp_get_topic_post_type() ;
260 $forum_id_check = private_groups_get_forum_id_from_post_id($post_id, $topic);
261 //now we can check if the user can view this
262 if (!private_groups_can_user_view_post($user_id2,$forum_id_check))
263 return;
264 return bbp_get_topic_author_link( $r );
265
266
267 // Confirmed reply
268 } elseif ( bbp_is_reply( $post_id ) ) {
269 $reply=bbp_get_reply_post_type() ;
270 $forum_id_check = private_groups_get_forum_id_from_post_id($post_id, $reply);
271 //now we can check if the user can view this
272 if (!private_groups_can_user_view_post($user_id2,$forum_id_check))
273 return ;
274 return bbp_get_reply_author_link( $r );
275 }
276 // Neither a reply nor a topic, so could be a revision
277 //if it isn't a topic or reply, not sure so better to just not display the author in this case
278 //could be revised to look up the post_parent of this post and then churn that round the code above if required return ;
279 return ;
280
281 }
282
283
284 //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
285
286 function pg_role_group ($user_id) {
287 $test=get_option ('rpg_roles') ;
288 //if no roles set, then exit
289 if (empty ($test)) return ;
290 //we have roles set, so now cycle through the roles for this user
291 //$user_id = get_current_user_id();
292 //if ($user_id == 0) return ; // bail if no user ID
293 $roles = get_usermeta( $user_id, 'wp_capabilities',false ) ;
294 foreach ($roles as $role=>$value) {
295 foreach ($test as $check=>$group){
296 if ($role == $check ) {
297 if ($group != 'no-group') update_user_meta( $user_id, 'private_group', $group);
298 }
299 }
300 }
301 }
302
303
304
305
306
307 ?>