PluginProbe
Private groups / trunk
Private groups vtrunk
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 / search.php

search.php in Private groups trunk, at includes/search.php

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