| 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 |
/** |
| 197 |
* Return the search url. |
| 198 |
* |
| 199 |
* @since 2.3.0 bbPress (r4579) |
| 200 |
* |
| 201 |
* @return string Search url. |
| 202 |
*/ |
| 203 |
function bbp_get_search_url() { |
| 204 |
|
| 205 |
// Pretty permalinks |
| 206 |
if ( bbp_use_pretty_urls() ) { |
| 207 |
|
| 208 |
// Run through home_url() |
| 209 |
$url = bbp_get_root_url() . bbp_get_search_slug(); |
| 210 |
$url = user_trailingslashit( $url ); |
| 211 |
$url = home_url( $url ); |
| 212 |
|
| 213 |
// Unpretty permalinks |
| 214 |
} else { |
| 215 |
$url = add_query_arg( |
| 216 |
array( |
| 217 |
bbp_get_search_rewrite_id() => '' |
| 218 |
), |
| 219 |
home_url( '/' ) |
| 220 |
); |
| 221 |
} |
| 222 |
|
| 223 |
// Filter & return |
| 224 |
return apply_filters( 'bbp_get_search_url', $url ); |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Output the search results url. |
| 229 |
* |
| 230 |
* @since 2.4.0 bbPress (r4928) |
| 231 |
*/ |
| 232 |
function bbp_search_results_url() { |
| 233 |
echo esc_url( bbp_get_search_results_url() ); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Return the search url. |
| 238 |
* |
| 239 |
* @since 2.4.0 bbPress (r4928) |
| 240 |
* |
| 241 |
* @return string Search url. |
| 242 |
*/ |
| 243 |
function bbp_get_search_results_url() { |
| 244 |
|
| 245 |
// Get the search terms |
| 246 |
$search_terms = bbp_get_search_terms(); |
| 247 |
|
| 248 |
// Pretty permalinks |
| 249 |
if ( bbp_use_pretty_urls() ) { |
| 250 |
|
| 251 |
// Root search URL |
| 252 |
$url = bbp_get_root_url() . bbp_get_search_slug(); |
| 253 |
|
| 254 |
// Append search terms |
| 255 |
if ( ! empty( $search_terms ) ) { |
| 256 |
$url = trailingslashit( $url ) . urlencode( $search_terms ); |
| 257 |
} |
| 258 |
|
| 259 |
// Run through home_url() |
| 260 |
$url = user_trailingslashit( $url ); |
| 261 |
$url = home_url( $url ); |
| 262 |
|
| 263 |
// Unpretty permalinks |
| 264 |
} else { |
| 265 |
$url = add_query_arg( |
| 266 |
array( |
| 267 |
bbp_get_search_rewrite_id() => urlencode( $search_terms ) |
| 268 |
), |
| 269 |
home_url( '/' ) |
| 270 |
); |
| 271 |
} |
| 272 |
|
| 273 |
// Filter & return |
| 274 |
return apply_filters( 'bbp_get_search_results_url', $url ); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Output the search terms. |
| 279 |
* |
| 280 |
* @since 2.3.0 bbPress (r4579) |
| 281 |
* |
| 282 |
* @param string $search_terms Optional. Search terms. |
| 283 |
*/ |
| 284 |
function bbp_search_terms( $search_terms = '' ) { |
| 285 |
echo esc_attr( bbp_get_search_terms( $search_terms ) ); |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* Get the search terms. |
| 290 |
* |
| 291 |
* @since 2.3.0 bbPress (r4579) |
| 292 |
* |
| 293 |
* If search terms are supplied, those are used. Otherwise check the |
| 294 |
* search rewrite id query var. |
| 295 |
* |
| 296 |
* @param string $passed_terms Optional. Search terms. |
| 297 |
* @return bool|string Search terms on success, false on failure. |
| 298 |
*/ |
| 299 |
function bbp_get_search_terms( $passed_terms = '' ) { |
| 300 |
|
| 301 |
// Sanitize terms if they were passed in |
| 302 |
if ( ! empty( $passed_terms ) ) { |
| 303 |
$search_terms = sanitize_title( $passed_terms ); |
| 304 |
|
| 305 |
// Use query variable if not |
| 306 |
} else { |
| 307 |
|
| 308 |
// Global |
| 309 |
$search_terms = get_query_var( bbp_get_search_rewrite_id(), null ); |
| 310 |
|
| 311 |
// Searching globally |
| 312 |
if ( ! is_null( $search_terms ) ) { |
| 313 |
$search_terms = wp_unslash( $search_terms ); |
| 314 |
|
| 315 |
// Other searches |
| 316 |
} else { |
| 317 |
|
| 318 |
// Get known search type IDs |
| 319 |
$types = bbp_get_search_type_ids(); |
| 320 |
|
| 321 |
// Filterable, so make sure types exist |
| 322 |
if ( ! empty( $types ) ) { |
| 323 |
|
| 324 |
// Loop through types |
| 325 |
foreach ( $types as $type ) { |
| 326 |
|
| 327 |
// Look for search terms |
| 328 |
$terms = bbp_sanitize_search_request( $type ); |
| 329 |
|
| 330 |
// Skip if no terms |
| 331 |
if ( empty( $terms ) ) { |
| 332 |
continue; |
| 333 |
} |
| 334 |
|
| 335 |
// Set terms if not empty |
| 336 |
$search_terms = $terms; |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
// Trim whitespace & decode if non-empty string, or set to false |
| 343 |
$search_terms = ! empty( $search_terms ) && is_string( $search_terms ) |
| 344 |
? urldecode( trim( $search_terms ) ) |
| 345 |
: false; |
| 346 |
|
| 347 |
// Filter & return |
| 348 |
return apply_filters( 'bbp_get_search_terms', $search_terms, $passed_terms ); |
| 349 |
} |
| 350 |
|
| 351 |
/** Pagination ****************************************************************/ |
| 352 |
|
| 353 |
/** |
| 354 |
* Return the base URL used inside of pagination links. |
| 355 |
* |
| 356 |
* @since 2.6.0 bbPress (r6679) |
| 357 |
* |
| 358 |
* @return string |
| 359 |
*/ |
| 360 |
function bbp_get_search_pagination_base() { |
| 361 |
|
| 362 |
// If pretty permalinks are enabled, make our pagination pretty |
| 363 |
if ( bbp_use_pretty_urls() ) { |
| 364 |
|
| 365 |
// Any single post (for shortcodes) |
| 366 |
if ( is_singular() ) { |
| 367 |
$base = get_permalink(); |
| 368 |
|
| 369 |
// Default search location |
| 370 |
} else { |
| 371 |
$base = bbp_get_search_results_url(); |
| 372 |
} |
| 373 |
|
| 374 |
// Add pagination base |
| 375 |
$base = trailingslashit( $base ) . user_trailingslashit( bbp_get_paged_slug() . '/%#%/' ); |
| 376 |
|
| 377 |
// Unpretty permalinks |
| 378 |
} else { |
| 379 |
$base = add_query_arg( 'paged', '%#%' ); |
| 380 |
} |
| 381 |
|
| 382 |
// Filter & return |
| 383 |
return apply_filters( 'bbp_get_search_pagination_base', $base ); |
| 384 |
} |
| 385 |
|
| 386 |
/** |
| 387 |
* Output the search result pagination count. |
| 388 |
* |
| 389 |
* @since 2.3.0 bbPress (r4579) |
| 390 |
*/ |
| 391 |
function bbp_search_pagination_count() { |
| 392 |
echo bbp_get_search_pagination_count(); |
| 393 |
} |
| 394 |
|
| 395 |
/** |
| 396 |
* Return the search results pagination count. |
| 397 |
* |
| 398 |
* @since 2.3.0 bbPress (r4579) |
| 399 |
* |
| 400 |
* @return string Search pagination count. |
| 401 |
*/ |
| 402 |
function bbp_get_search_pagination_count() { |
| 403 |
$bbp = bbpress(); |
| 404 |
|
| 405 |
// Define local variable(s) |
| 406 |
$retstr = ''; |
| 407 |
|
| 408 |
// Set pagination values |
| 409 |
$total_int = intval( $bbp->search_query->found_posts ); |
| 410 |
$ppp_int = intval( $bbp->search_query->posts_per_page ); |
| 411 |
$start_int = intval( ( $bbp->search_query->paged - 1 ) * $ppp_int ) + 1; |
| 412 |
$to_int = intval( ( $start_int + ( $ppp_int - 1 ) > $total_int ) |
| 413 |
? $total_int |
| 414 |
: $start_int + ( $ppp_int - 1 ) |
| 415 |
); |
| 416 |
|
| 417 |
// Format numbers for display |
| 418 |
$total_num = bbp_number_format( $total_int ); |
| 419 |
$from_num = bbp_number_format( $start_int ); |
| 420 |
$to_num = bbp_number_format( $to_int ); |
| 421 |
|
| 422 |
// Single page of results |
| 423 |
if ( empty( $to_num ) ) { |
| 424 |
$retstr = sprintf( |
| 425 |
/* translators: %1$s: Number of results */ |
| 426 |
_n( 'Viewing %1$s result', 'Viewing %1$s results', $total_int, 'bbpress' ), |
| 427 |
$total_num |
| 428 |
); |
| 429 |
|
| 430 |
// Several pages of results |
| 431 |
} else { |
| 432 |
$retstr = sprintf( |
| 433 |
/* translators: 1: Number of results being viewed, 2: First result number, 3: Last result number, 4: Total results */ |
| 434 |
_n( |
| 435 |
'Viewing %1$s result - %2$s through %3$s (of %4$s total)', |
| 436 |
'Viewing %1$s results - %2$s through %3$s (of %4$s total)', |
| 437 |
$bbp->search_query->post_count, |
| 438 |
'bbpress' |
| 439 |
), |
| 440 |
$bbp->search_query->post_count, |
| 441 |
$from_num, |
| 442 |
$to_num, |
| 443 |
$total_num |
| 444 |
); |
| 445 |
|
| 446 |
// Filter & return |
| 447 |
return apply_filters( 'bbp_get_search_pagination_count', esc_html( $retstr ) ); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
/** |
| 452 |
* Output search pagination links. |
| 453 |
* |
| 454 |
* @since 2.3.0 bbPress (r4579) |
| 455 |
*/ |
| 456 |
function bbp_search_pagination_links() { |
| 457 |
echo bbp_get_search_pagination_links(); |
| 458 |
} |
| 459 |
|
| 460 |
/** |
| 461 |
* Return search pagination links. |
| 462 |
* |
| 463 |
* @since 2.3.0 bbPress (r4579) |
| 464 |
* |
| 465 |
* @return string Search pagination links. |
| 466 |
*/ |
| 467 |
function bbp_get_search_pagination_links() { |
| 468 |
$bbp = bbpress(); |
| 469 |
|
| 470 |
if ( ! isset( $bbp->search_query->pagination_links ) || empty( $bbp->search_query->pagination_links ) ) { |
| 471 |
return false; |
| 472 |
} |
| 473 |
|
| 474 |
// Filter & return |
| 475 |
return apply_filters( 'bbp_get_search_pagination_links', $bbp->search_query->pagination_links ); |
| 476 |
} |
| 477 |
|