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

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

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