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 / mark-as-read-filter.php

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

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