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

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