PluginProbe
Private groups / 1.7
Private groups v1.7
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 / mark-as-read-filter.php

mark-as-read-filter.php in Private groups 1.7, at includes/mark-as-read-filter.php

323 lines 11.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 //This filter works for Pippin Wilkinson's bbpress-mark-as-read plugin to ensure the correct display of unread topics in the subscriptions area
4
5 add_filter('bbp_get_user_unread', 'pg_get_user_unread' );
6
7
8 // This function starts from pippin's plugin function 'bbp_get_user_unread' which it now filters
9 //then uses private groups to get a list of topics the user can see
10 //then create an allowed list based on these two
11 //then passes this through the bbp-has-topics code
12
13
14 //get all unread topic IDs for the specified user
15 function pg_get_user_unread( $user_id = 0 ) {
16
17 // Default to the displayed user
18 $user_id = bbp_get_user_id( $user_id );
19 if ( empty( $user_id ) )
20 return false;
21 // If user has unread topics, load them
22 $read_ids = (string) get_user_meta( $user_id, '_bbp_mar_read_ids', true );
23 $read_ids = (array) explode( ',', $read_ids );
24 $read_ids = array_filter( $read_ids );
25 if ( !empty( $read_ids ) ) {
26 //so we have unreads, so need to create a list of unread that the user can see
27 //so first we create a list of topics the user can see
28 global $wpdb;
29 $post_ids=$wpdb->get_col("select ID from $wpdb->posts where post_type = 'topic'") ;
30 //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
31 $allowed_posts = check_private_groups_topic_ids($post_ids) ;
32 //now we need to take out of that list all read topics for that user
33 foreach ($read_ids as $read_id) {
34 if (($key = array_search($read_id, $allowed_posts)) !== false) {
35 unset($allowed_posts[$key]); }
36 }
37 //so now we have an allowed list that has only topics the user can see, but not topics the user has read
38 //now we use the code from bbp_has_topics to run the list - we can't call it as PG already filters the original function
39
40 global $wp_rewrite;
41
42 /** Defaults **************************************************************/
43
44 // Other defaults
45 $default_topic_search = !empty( $_REQUEST['ts'] ) ? $_REQUEST['ts'] : false;
46 $default_show_stickies = (bool) ( bbp_is_single_forum() || bbp_is_topic_archive() ) && ( false === $default_topic_search );
47 $default_post_parent = bbp_is_single_forum() ? bbp_get_forum_id() : 'any';
48
49 // Default argument array
50 $default = array(
51 'post_type' => bbp_get_topic_post_type(), // Narrow query down to bbPress topics
52 'post_parent' => $default_post_parent, // Forum ID
53 'meta_key' => '_bbp_last_active_time', // Make sure topic has some last activity time
54 'orderby' => 'meta_value', // 'meta_value', 'author', 'date', 'title', 'modified', 'parent', rand',
55 'order' => 'DESC', // 'ASC', 'DESC'
56 'posts_per_page' => bbp_get_topics_per_page(), // Topics per page
57 'paged' => bbp_get_paged(), // Page Number
58 's' => $default_topic_search, // Topic Search
59 'show_stickies' => $default_show_stickies, // Ignore sticky topics?
60 'max_num_pages' => false, // Maximum number of pages to show
61 'post__in' => $allowed_posts // only the allowed posts from above
62 );
63
64 // What are the default allowed statuses (based on user caps)
65 if ( bbp_get_view_all() ) {
66
67 // Default view=all statuses
68 $post_statuses = array(
69 bbp_get_public_status_id(),
70 bbp_get_closed_status_id(),
71 bbp_get_spam_status_id(),
72 bbp_get_trash_status_id()
73 );
74
75 // Add support for private status
76 if ( current_user_can( 'read_private_topics' ) ) {
77 $post_statuses[] = bbp_get_private_status_id();
78 }
79
80 // Join post statuses together
81 $default['post_status'] = implode( ',', $post_statuses );
82
83 // Lean on the 'perm' query var value of 'readable' to provide statuses
84 } else {
85 $default['perm'] = 'readable';
86 }
87
88 // Maybe query for topic tags
89 if ( bbp_is_topic_tag() ) {
90 $default['term'] = bbp_get_topic_tag_slug();
91 $default['taxonomy'] = bbp_get_topic_tag_tax_id();
92 }
93
94 /** Setup *****************************************************************/
95
96 // Parse arguments against default values
97 //stopped to prevent parsing
98 //$r = bbp_parse_args( $args, $default, 'has_topics' );
99
100 // Get bbPress
101 $bbp = bbpress();
102
103 // Call the query
104 //now query the original default
105 $bbp->topic_query = new WP_Query( $default);
106
107 // Set post_parent back to 0 if originally set to 'any'
108 if ( 'any' === $r['post_parent'] )
109 $r['post_parent'] = 0;
110
111 // Limited the number of pages shown
112 if ( !empty( $r['max_num_pages'] ) )
113 $bbp->topic_query->max_num_pages = $r['max_num_pages'];
114
115 /** Stickies **************************************************************/
116
117 // Put sticky posts at the top of the posts array
118 if ( !empty( $r['show_stickies'] ) && $r['paged'] <= 1 ) {
119
120 // Get super stickies and stickies in this forum
121 $stickies = bbp_get_super_stickies();
122
123 // Get stickies for current forum
124 if ( !empty( $r['post_parent'] ) ) {
125 $stickies = array_merge( $stickies, bbp_get_stickies( $r['post_parent'] ) );
126 }
127
128 // Remove any duplicate stickies
129 $stickies = array_unique( $stickies );
130
131 // We have stickies
132 if ( is_array( $stickies ) && !empty( $stickies ) ) {
133
134 // Start the offset at -1 so first sticky is at correct 0 offset
135 $sticky_offset = -1;
136
137 // Loop over topics and relocate stickies to the front.
138 foreach ( $stickies as $sticky_index => $sticky_ID ) {
139
140 // Get the post offset from the posts array
141 $post_offsets = wp_filter_object_list( $bbp->topic_query->posts, array( 'ID' => $sticky_ID ), 'OR', 'ID' );
142
143 // Continue if no post offsets
144 if ( empty( $post_offsets ) ) {
145 continue;
146 }
147
148 // Loop over posts in current query and splice them into position
149 foreach ( array_keys( $post_offsets ) as $post_offset ) {
150 $sticky_offset++;
151
152 $sticky = $bbp->topic_query->posts[$post_offset];
153
154 // Remove sticky from current position
155 array_splice( $bbp->topic_query->posts, $post_offset, 1 );
156
157 // Move to front, after other stickies
158 array_splice( $bbp->topic_query->posts, $sticky_offset, 0, array( $sticky ) );
159
160 // Cleanup
161 unset( $stickies[$sticky_index] );
162 unset( $sticky );
163 }
164
165 // Cleanup
166 unset( $post_offsets );
167 }
168
169 // Cleanup
170 unset( $sticky_offset );
171
172 // If any posts have been excluded specifically, Ignore those that are sticky.
173 if ( !empty( $stickies ) && !empty( $r['post__not_in'] ) ) {
174 $stickies = array_diff( $stickies, $r['post__not_in'] );
175 }
176
177 // Fetch sticky posts that weren't in the query results
178 if ( !empty( $stickies ) ) {
179
180 // Query to use in get_posts to get sticky posts
181 $sticky_query = array(
182 'post_type' => bbp_get_topic_post_type(),
183 'post_parent' => 'any',
184 'meta_key' => '_bbp_last_active_time',
185 'orderby' => 'meta_value',
186 'order' => 'DESC',
187 'include' => $stickies
188 );
189
190 // Cleanup
191 unset( $stickies );
192
193 // Conditionally exclude private/hidden forum ID's
194 $exclude_forum_ids = bbp_exclude_forum_ids( 'array' );
195 if ( ! empty( $exclude_forum_ids ) ) {
196 $sticky_query['post_parent__not_in'] = $exclude_forum_ids;
197 }
198
199 // What are the default allowed statuses (based on user caps)
200 if ( bbp_get_view_all() ) {
201 $sticky_query['post_status'] = $r['post_status'];
202
203 // Lean on the 'perm' query var value of 'readable' to provide statuses
204 } else {
205 $sticky_query['post_status'] = $r['perm'];
206 }
207
208 // Get all stickies
209 $sticky_posts = get_posts( $sticky_query );
210 if ( !empty( $sticky_posts ) ) {
211
212 // Get a count of the visible stickies
213 $sticky_count = count( $sticky_posts );
214
215 // Merge the stickies topics with the query topics .
216 $bbp->topic_query->posts = array_merge( $sticky_posts, $bbp->topic_query->posts );
217
218 // Adjust loop and counts for new sticky positions
219 $bbp->topic_query->found_posts = (int) $bbp->topic_query->found_posts + (int) $sticky_count;
220 $bbp->topic_query->post_count = (int) $bbp->topic_query->post_count + (int) $sticky_count;
221
222 // Cleanup
223 unset( $sticky_posts );
224 }
225 }
226 }
227 }
228
229 // If no limit to posts per page, set it to the current post_count
230 if ( -1 === $r['posts_per_page'] )
231 $r['posts_per_page'] = $bbp->topic_query->post_count;
232
233 // Add pagination values to query object
234 $bbp->topic_query->posts_per_page = $r['posts_per_page'];
235 $bbp->topic_query->paged = $r['paged'];
236
237 // Only add pagination if query returned results
238 if ( ( (int) $bbp->topic_query->post_count || (int) $bbp->topic_query->found_posts ) && (int) $bbp->topic_query->posts_per_page ) {
239
240 // Limit the number of topics shown based on maximum allowed pages
241 if ( ( !empty( $r['max_num_pages'] ) ) && $bbp->topic_query->found_posts > $bbp->topic_query->max_num_pages * $bbp->topic_query->post_count )
242 $bbp->topic_query->found_posts = $bbp->topic_query->max_num_pages * $bbp->topic_query->post_count;
243
244 // If pretty permalinks are enabled, make our pagination pretty
245 if ( $wp_rewrite->using_permalinks() ) {
246
247 // User's topics
248 if ( bbp_is_single_user_topics() ) {
249 $base = bbp_get_user_topics_created_url( bbp_get_displayed_user_id() );
250
251 // User's favorites
252 } elseif ( bbp_is_favorites() ) {
253 $base = bbp_get_favorites_permalink( bbp_get_displayed_user_id() );
254
255 // User's subscriptions
256 } elseif ( bbp_is_subscriptions() ) {
257 $base = bbp_get_subscriptions_permalink( bbp_get_displayed_user_id() );
258
259 // Root profile page
260 } elseif ( bbp_is_single_user() ) {
261 $base = bbp_get_user_profile_url( bbp_get_displayed_user_id() );
262
263 // View
264 } elseif ( bbp_is_single_view() ) {
265 $base = bbp_get_view_url();
266
267 // Topic tag
268 } elseif ( bbp_is_topic_tag() ) {
269 $base = bbp_get_topic_tag_link();
270
271 // Page or single post
272 } elseif ( is_page() || is_single() ) {
273 $base = get_permalink();
274
275 // Forum archive
276 } elseif ( bbp_is_forum_archive() ) {
277 $base = bbp_get_forums_url();
278
279 // Topic archive
280 } elseif ( bbp_is_topic_archive() ) {
281 $base = bbp_get_topics_url();
282
283 // Default
284 } else {
285 $base = get_permalink( (int) $r['post_parent'] );
286 }
287
288 // Use pagination base
289 $base = trailingslashit( $base ) . user_trailingslashit( $wp_rewrite->pagination_base . '/%#%/' );
290
291 // Unpretty pagination
292 } else {
293 $base = add_query_arg( 'paged', '%#%' );
294 }
295
296 // Pagination settings with filter
297 $bbp_topic_pagination = apply_filters( 'bbp_topic_pagination', array (
298 'base' => $base,
299 'format' => '',
300 'total' => $r['posts_per_page'] === $bbp->topic_query->found_posts ? 1 : ceil( (int) $bbp->topic_query->found_posts / (int) $r['posts_per_page'] ),
301 'current' => (int) $bbp->topic_query->paged,
302 'prev_text' => is_rtl() ? '&rarr;' : '&larr;',
303 'next_text' => is_rtl() ? '&larr;' : '&rarr;',
304 'mid_size' => 1
305 ) );
306
307 // Add pagination to query object
308 $bbp->topic_query->pagination_links = paginate_links( $bbp_topic_pagination );
309
310 // Remove first page from pagination
311 $bbp->topic_query->pagination_links = str_replace( $wp_rewrite->pagination_base . "/1/'", "'", $bbp->topic_query->pagination_links );
312 }
313
314 // Return object
315 return apply_filters( 'pg_get_user_unread', $bbp->topic_query->have_posts(), $bbp->topic_query );
316 }
317
318
319
320 //if no unread
321 return bbp_has_topics(); // default query
322
323 }