| 1 |
<?php |
| 2 |
|
| 3 |
//this function filters to the bbp search function to allow only returns from allowed forums |
| 4 |
|
| 5 |
function pg_has_search_results( $args = '' ) { |
| 6 |
global $wp_rewrite; |
| 7 |
//start with code as per bbp search ! |
| 8 |
/** Defaults **************************************************************/ |
| 9 |
|
| 10 |
$default_post_type = array( bbp_get_forum_post_type(), bbp_get_topic_post_type(), bbp_get_reply_post_type() ); |
| 11 |
|
| 12 |
// Default query args |
| 13 |
$default = array( |
| 14 |
'post_type' => $default_post_type, // Forums, topics, and replies |
| 15 |
'posts_per_page' => bbp_get_replies_per_page(), // This many |
| 16 |
'paged' => bbp_get_paged(), // On this page |
| 17 |
'orderby' => 'date', // Sorted by date |
| 18 |
'order' => 'DESC', // Most recent first |
| 19 |
'ignore_sticky_posts' => true, // Stickies not supported |
| 20 |
's' => bbp_get_search_terms(), // This is a search |
| 21 |
); |
| 22 |
|
| 23 |
// What are the default allowed statuses (based on user caps) |
| 24 |
if ( bbp_get_view_all() ) { |
| 25 |
|
| 26 |
// Default view=all statuses |
| 27 |
$post_statuses = array( |
| 28 |
bbp_get_public_status_id(), |
| 29 |
bbp_get_closed_status_id(), |
| 30 |
bbp_get_spam_status_id(), |
| 31 |
bbp_get_trash_status_id() |
| 32 |
); |
| 33 |
|
| 34 |
// Add support for private status |
| 35 |
if ( current_user_can( 'read_private_topics' ) ) { |
| 36 |
$post_statuses[] = bbp_get_private_status_id(); |
| 37 |
} |
| 38 |
|
| 39 |
// Join post statuses together |
| 40 |
$default['post_status'] = implode( ',', $post_statuses ); |
| 41 |
|
| 42 |
// Lean on the 'perm' query var value of 'readable' to provide statuses |
| 43 |
} else { |
| 44 |
$default['perm'] = 'readable'; |
| 45 |
} |
| 46 |
|
| 47 |
//PG then loop to find allowable forums |
| 48 |
//Get an array of IDs which the current user has permissions to view |
| 49 |
$allowed_posts = private_groups_get_permitted_post_ids(new WP_Query($default)); |
| 50 |
|
| 51 |
// The default forum query with allowed forum ids array added |
| 52 |
$default['post__in'] = $allowed_posts; |
| 53 |
if (empty ($allowed_posts )) $default['post__in'] = array(0) ; |
| 54 |
|
| 55 |
//then return to bbp search code |
| 56 |
/** Setup *****************************************************************/ |
| 57 |
|
| 58 |
// Parse arguments against default values |
| 59 |
$r = bbp_parse_args( $args, $default, 'has_search_results' ); |
| 60 |
|
| 61 |
// Get bbPress |
| 62 |
$bbp = bbpress(); |
| 63 |
|
| 64 |
// Call the query |
| 65 |
if ( ! empty( $r['s'] ) ) { |
| 66 |
$bbp->search_query = new WP_Query( $r ); |
| 67 |
} |
| 68 |
|
| 69 |
// Add pagination values to query object |
| 70 |
$bbp->search_query->posts_per_page = $r['posts_per_page']; |
| 71 |
$bbp->search_query->paged = $r['paged']; |
| 72 |
|
| 73 |
// Never home, regardless of what parse_query says |
| 74 |
$bbp->search_query->is_home = false; |
| 75 |
|
| 76 |
// Only add pagination is query returned results |
| 77 |
if ( ! empty( $bbp->search_query->found_posts ) && ! empty( $bbp->search_query->posts_per_page ) ) { |
| 78 |
|
| 79 |
// Array of arguments to add after pagination links |
| 80 |
$add_args = array(); |
| 81 |
|
| 82 |
// If pretty permalinks are enabled, make our pagination pretty |
| 83 |
if ( $wp_rewrite->using_permalinks() ) { |
| 84 |
|
| 85 |
// Shortcode territory |
| 86 |
if ( is_page() || is_single() ) { |
| 87 |
$base = trailingslashit( get_permalink() ); |
| 88 |
|
| 89 |
// Default search location |
| 90 |
} else { |
| 91 |
$base = trailingslashit( bbp_get_search_results_url() ); |
| 92 |
} |
| 93 |
|
| 94 |
// Add pagination base |
| 95 |
$base = $base . user_trailingslashit( $wp_rewrite->pagination_base . '/%#%/' ); |
| 96 |
|
| 97 |
// Unpretty permalinks |
| 98 |
} else { |
| 99 |
$base = add_query_arg( 'paged', '%#%' ); |
| 100 |
} |
| 101 |
|
| 102 |
// Add args |
| 103 |
if ( bbp_get_view_all() ) { |
| 104 |
$add_args['view'] = 'all'; |
| 105 |
} |
| 106 |
|
| 107 |
// Add pagination to query object |
| 108 |
$bbp->search_query->pagination_links = paginate_links( |
| 109 |
apply_filters( 'bbp_search_results_pagination', array( |
| 110 |
'base' => $base, |
| 111 |
'format' => '', |
| 112 |
'total' => ceil( (int) $bbp->search_query->found_posts / (int) $r['posts_per_page'] ), |
| 113 |
'current' => (int) $bbp->search_query->paged, |
| 114 |
'prev_text' => is_rtl() ? '→' : '←', |
| 115 |
'next_text' => is_rtl() ? '←' : '→', |
| 116 |
'mid_size' => 1, |
| 117 |
'add_args' => $add_args, |
| 118 |
) ) |
| 119 |
); |
| 120 |
|
| 121 |
// Remove first page from pagination |
| 122 |
if ( $wp_rewrite->using_permalinks() ) { |
| 123 |
$bbp->search_query->pagination_links = str_replace( $wp_rewrite->pagination_base . '/1/', '', $bbp->search_query->pagination_links ); |
| 124 |
} else { |
| 125 |
$bbp->search_query->pagination_links = str_replace( '&paged=1', '', $bbp->search_query->pagination_links ); |
| 126 |
} |
| 127 |
} |
| 128 |
//finally filter to return |
| 129 |
// Return object |
| 130 |
return apply_filters( 'pg_has_search_results', $bbp->search_query->have_posts(), $bbp->search_query ); |
| 131 |
} |
| 132 |
|
| 133 |
add_filter ('bbp_has_search_results', 'pg_has_search_results') ; |