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