PluginProbe
bbPress / 2.6.19
bbPress v2.6.19
2.6.19 2.6.18 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 All 74 releases
bbpress / includes / search / template.php

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

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