PluginProbe
bbPress / 2.6.17
bbPress v2.6.17
2.6.17 trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 2.2 2.2.1 All 72 releases
bbpress / includes / search / template.php

template.php in bbPress 2.6.17, at includes/search/template.php

476 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 /**
4 * bbPress Search Template Tags
5 *
6 * @package bbPress
7 * @subpackage TemplateTags
8 */
9
10 // Exit if accessed directly
11 defined( 'ABSPATH' ) || exit;
12
13 /** Search Loop Functions *****************************************************/
14
15 /**
16 * The main search loop. WordPress does the heavy lifting.
17 *
18 * @since 2.3.0 bbPress (r4579)
19 *
20 * @param array $args All the arguments supported by {@link WP_Query}
21 * @return object Multidimensional array of search information
22 */
23 function bbp_has_search_results( $args = array() ) {
24
25 /** Defaults **************************************************************/
26
27 $default_search_terms = bbp_get_search_terms();
28 $default_post_types = bbp_get_post_types();
29
30 // Default query args
31 $default = array(
32 'post_type' => $default_post_types, // Forums, topics, and replies
33 'posts_per_page' => bbp_get_replies_per_page(), // This many
34 'paged' => bbp_get_paged(), // On this page
35 'orderby' => 'date', // Sorted by date
36 'order' => 'DESC', // Most recent first
37 'ignore_sticky_posts' => true, // Stickies not supported,
38
39 // Conditionally prime the cache for last active posts
40 'update_post_family_cache' => true
41 );
42
43 // Only set 's' if search terms exist
44 // https://bbpress.trac.wordpress.org/ticket/2607
45 if ( false !== $default_search_terms ) {
46 $default['s'] = $default_search_terms;
47 }
48
49 // Default public statuses (topics coincidentally cover all post types)
50 $post_statuses = bbp_get_public_topic_statuses();
51
52 // Add support for private status
53 if ( current_user_can( 'read_private_topics' ) ) {
54 $post_statuses[] = bbp_get_private_status_id();
55 }
56
57 // Add support for hidden status
58 if ( current_user_can( 'read_hidden_topics' ) ) {
59 $post_statuses[] = bbp_get_hidden_status_id();
60 }
61
62 // Join post statuses together
63 $default['post_status'] = $post_statuses;
64
65 /** Setup *****************************************************************/
66
67 // Parse arguments against default values
68 $r = bbp_parse_args( $args, $default, 'has_search_results' );
69
70 // Get bbPress
71 $bbp = bbpress();
72
73 // Only call the search query if 's' is not empty
74 if ( ! empty( $r['s'] ) ) {
75 $bbp->search_query = new WP_Query( $r );
76 }
77
78 // Maybe prime last active posts
79 if ( ! empty( $r['update_post_family_cache'] ) ) {
80 bbp_update_post_family_caches( $bbp->search_query->posts );
81 }
82
83 // Add pagination values to query object
84 $bbp->search_query->posts_per_page = (int) $r['posts_per_page'];
85 $bbp->search_query->paged = (int) $r['paged'];
86
87 // Never home, regardless of what parse_query says
88 $bbp->search_query->is_home = false;
89
90 // Only add pagination is query returned results
91 if ( ! empty( $bbp->search_query->found_posts ) && ! empty( $bbp->search_query->posts_per_page ) ) {
92
93 // Total for pagination boundaries
94 $total_pages = ( $bbp->search_query->posts_per_page === $bbp->search_query->found_posts )
95 ? 1
96 : ceil( $bbp->search_query->found_posts / $bbp->search_query->posts_per_page );
97
98 // Pagination settings with filter
99 $bbp_search_pagination = apply_filters(
100 'bbp_search_results_pagination',
101 array(
102 'base' => bbp_get_search_pagination_base(),
103 'total' => $total_pages,
104 'current' => $bbp->search_query->paged
105 )
106 );
107
108 // Add pagination to query object
109 $bbp->search_query->pagination_links = bbp_paginate_links( $bbp_search_pagination );
110 }
111
112 // Filter & return
113 return apply_filters( 'bbp_has_search_results', $bbp->search_query->have_posts(), $bbp->search_query );
114 }
115
116 /**
117 * Whether there are more search results available in the loop
118 *
119 * @since 2.3.0 bbPress (r4579)
120 *
121 * @return object Search information
122 */
123 function bbp_search_results() {
124
125 // Put into variable to check against next
126 $have_posts = bbpress()->search_query->have_posts();
127
128 // Reset the post data when finished
129 if ( empty( $have_posts ) ) {
130 wp_reset_postdata();
131 }
132
133 return $have_posts;
134 }
135
136 /**
137 * Loads up the current search result in the loop
138 *
139 * @since 2.3.0 bbPress (r4579)
140 *
141 * @return object Search information
142 */
143 function bbp_the_search_result() {
144 $search_result = bbpress()->search_query->the_post();
145
146 // Reset each current (forum|topic|reply) id
147 bbpress()->current_forum_id = bbp_get_forum_id();
148 bbpress()->current_topic_id = bbp_get_topic_id();
149 bbpress()->current_reply_id = bbp_get_reply_id();
150
151 return $search_result;
152 }
153
154 /**
155 * Output the search page title
156 *
157 * @since 2.3.0 bbPress (r4579)
158 */
159 function bbp_search_title() {
160 echo bbp_get_search_title();
161 }
162
163 /**
164 * Get the search page title
165 *
166 * @since 2.3.0 bbPress (r4579)
167 */
168 function bbp_get_search_title() {
169
170 // Get search terms
171 $search_terms = bbp_get_search_terms();
172
173 // No search terms specified
174 if ( empty( $search_terms ) ) {
175 $title = esc_html__( 'Search', 'bbpress' );
176
177 // Include search terms in title
178 } else {
179 /* translators: %s: Search query terms */
180 $title = sprintf( esc_html__( "Search Results for '%s'", 'bbpress' ), esc_attr( $search_terms ) );
181 }
182
183 // Filter & return
184 return apply_filters( 'bbp_get_search_title', $title, $search_terms );
185 }
186
187 /**
188 * Output the search url
189 *
190 * @since 2.3.0 bbPress (r4579)
191 */
192 function bbp_search_url() {
193 echo esc_url( bbp_get_search_url() );
194 }
195 /**
196 * Return the search url
197 *
198 * @since 2.3.0 bbPress (r4579)
199 *
200 * @return string Search url
201 */
202 function bbp_get_search_url() {
203
204 // Pretty permalinks
205 if ( bbp_use_pretty_urls() ) {
206
207 // Run through home_url()
208 $url = bbp_get_root_url() . bbp_get_search_slug();
209 $url = user_trailingslashit( $url );
210 $url = home_url( $url );
211
212 // Unpretty permalinks
213 } else {
214 $url = add_query_arg(
215 array(
216 bbp_get_search_rewrite_id() => ''
217 ),
218 home_url( '/' )
219 );
220 }
221
222 // Filter & return
223 return apply_filters( 'bbp_get_search_url', $url );
224 }
225
226 /**
227 * Output the search results url
228 *
229 * @since 2.4.0 bbPress (r4928)
230 */
231 function bbp_search_results_url() {
232 echo esc_url( bbp_get_search_results_url() );
233 }
234 /**
235 * Return the search url
236 *
237 * @since 2.4.0 bbPress (r4928)
238 *
239 * @return string Search url
240 */
241 function bbp_get_search_results_url() {
242
243 // Get the search terms
244 $search_terms = bbp_get_search_terms();
245
246 // Pretty permalinks
247 if ( bbp_use_pretty_urls() ) {
248
249 // Root search URL
250 $url = bbp_get_root_url() . bbp_get_search_slug();
251
252 // Append search terms
253 if ( ! empty( $search_terms ) ) {
254 $url = trailingslashit( $url ) . urlencode( $search_terms );
255 }
256
257 // Run through home_url()
258 $url = user_trailingslashit( $url );
259 $url = home_url( $url );
260
261 // Unpretty permalinks
262 } else {
263 $url = add_query_arg(
264 array(
265 bbp_get_search_rewrite_id() => urlencode( $search_terms )
266 ),
267 home_url( '/' )
268 );
269 }
270
271 // Filter & return
272 return apply_filters( 'bbp_get_search_results_url', $url );
273 }
274
275 /**
276 * Output the search terms
277 *
278 * @since 2.3.0 bbPress (r4579)
279 *
280 * @param string $search_terms Optional. Search terms
281 */
282 function bbp_search_terms( $search_terms = '' ) {
283 echo esc_attr( bbp_get_search_terms( $search_terms ) );
284 }
285
286 /**
287 * Get the search terms
288 *
289 * @since 2.3.0 bbPress (r4579)
290 *
291 * If search terms are supplied, those are used. Otherwise check the
292 * search rewrite id query var.
293 *
294 * @param string $passed_terms Optional. Search terms
295 * @return bool|string Search terms on success, false on failure
296 */
297 function bbp_get_search_terms( $passed_terms = '' ) {
298
299 // Sanitize terms if they were passed in
300 if ( ! empty( $passed_terms ) ) {
301 $search_terms = sanitize_title( $passed_terms );
302
303 // Use query variable if not
304 } else {
305
306 // Global
307 $search_terms = get_query_var( bbp_get_search_rewrite_id(), null );
308
309 // Searching globally
310 if ( ! is_null( $search_terms ) ) {
311 $search_terms = wp_unslash( $search_terms );
312
313 // Other searches
314 } else {
315
316 // Get known search type IDs
317 $types = bbp_get_search_type_ids();
318
319 // Filterable, so make sure types exist
320 if ( ! empty( $types ) ) {
321
322 // Loop through types
323 foreach ( $types as $type ) {
324
325 // Look for search terms
326 $terms = bbp_sanitize_search_request( $type );
327
328 // Skip if no terms
329 if ( empty( $terms ) ) {
330 continue;
331 }
332
333 // Set terms if not empty
334 $search_terms = $terms;
335 }
336 }
337 }
338 }
339
340 // Trim whitespace & decode if non-empty string, or set to false
341 $search_terms = ! empty( $search_terms ) && is_string( $search_terms )
342 ? urldecode( trim( $search_terms ) )
343 : false;
344
345 // Filter & return
346 return apply_filters( 'bbp_get_search_terms', $search_terms, $passed_terms );
347 }
348
349 /** Pagination ****************************************************************/
350
351 /**
352 * Return the base URL used inside of pagination links
353 *
354 * @since 2.6.0 bbPress (r6679)
355 *
356 * @return string
357 */
358 function bbp_get_search_pagination_base() {
359
360 // If pretty permalinks are enabled, make our pagination pretty
361 if ( bbp_use_pretty_urls() ) {
362
363 // Any single post (for shortcodes)
364 if ( is_singular() ) {
365 $base = get_permalink();
366
367 // Default search location
368 } else {
369 $base = bbp_get_search_results_url();
370 }
371
372 // Add pagination base
373 $base = trailingslashit( $base ) . user_trailingslashit( bbp_get_paged_slug() . '/%#%/' );
374
375 // Unpretty permalinks
376 } else {
377 $base = add_query_arg( 'paged', '%#%' );
378 }
379
380 // Filter & return
381 return apply_filters( 'bbp_get_search_pagination_base', $base );
382 }
383
384 /**
385 * Output the search result pagination count
386 *
387 * @since 2.3.0 bbPress (r4579)
388 */
389 function bbp_search_pagination_count() {
390 echo bbp_get_search_pagination_count();
391 }
392
393 /**
394 * Return the search results pagination count
395 *
396 * @since 2.3.0 bbPress (r4579)
397 *
398 * @return string Search pagination count
399 */
400 function bbp_get_search_pagination_count() {
401 $bbp = bbpress();
402
403 // Define local variable(s)
404 $retstr = '';
405
406 // Set pagination values
407 $total_int = intval( $bbp->search_query->found_posts );
408 $ppp_int = intval( $bbp->search_query->posts_per_page );
409 $start_int = intval( ( $bbp->search_query->paged - 1 ) * $ppp_int ) + 1;
410 $to_int = intval( ( $start_int + ( $ppp_int - 1 ) > $total_int )
411 ? $total_int
412 : $start_int + ( $ppp_int - 1 )
413 );
414
415 // Format numbers for display
416 $total_num = bbp_number_format( $total_int );
417 $from_num = bbp_number_format( $start_int );
418 $to_num = bbp_number_format( $to_int );
419
420 // Single page of results
421 if ( empty( $to_num ) ) {
422 $retstr = sprintf(
423 /* translators: %1$s: Number of results */
424 _n( 'Viewing %1$s result', 'Viewing %1$s results', $total_int, 'bbpress' ),
425 $total_num
426 );
427
428 // Several pages of results
429 } else {
430 $retstr = sprintf(
431 /* translators: 1: Number of results being viewed, 2: First result number, 3: Last result number, 4: Total results */
432 _n(
433 'Viewing %2$s results (of %4$s total)',
434 'Viewing %1$s results - %2$s through %3$s (of %4$s total)',
435 $bbp->search_query->post_count,
436 'bbpress'
437 ),
438 $bbp->search_query->post_count,
439 $from_num,
440 $to_num,
441 $total_num
442 );
443
444 }
445
446 // Filter & return
447 return apply_filters( 'bbp_get_search_pagination_count', esc_html( $retstr ) );
448 }
449
450 /**
451 * Output search pagination links
452 *
453 * @since 2.3.0 bbPress (r4579)
454 */
455 function bbp_search_pagination_links() {
456 echo bbp_get_search_pagination_links();
457 }
458
459 /**
460 * Return search pagination links
461 *
462 * @since 2.3.0 bbPress (r4579)
463 *
464 * @return string Search pagination links
465 */
466 function bbp_get_search_pagination_links() {
467 $bbp = bbpress();
468
469 if ( ! isset( $bbp->search_query->pagination_links ) || empty( $bbp->search_query->pagination_links ) ) {
470 return false;
471 }
472
473 // Filter & return
474 return apply_filters( 'bbp_get_search_pagination_links', $bbp->search_query->pagination_links );
475 }
476