/includes/search/template.php

  1. <?php 
  2.  
  3. /** 
  4. * bbPress Search Template Tags 
  5. * 
  6. * @package bbPress 
  7. * @subpackage TemplateTags 
  8. */ 
  9.  
  10. // Exit if accessed directly 
  11. if ( !defined( 'ABSPATH' ) ) exit; 
  12.  
  13. /** Search Loop Functions *****************************************************/ 
  14.  
  15. /** 
  16. * The main search loop. WordPress does the heavy lifting. 
  17. * 
  18. * @since bbPress (r4579) 
  19. * 
  20. * @param mixed $args All the arguments supported by {@link WP_Query} 
  21. * @uses bbp_get_view_all() Are we showing all results? 
  22. * @uses bbp_get_public_status_id() To get the public status id 
  23. * @uses bbp_get_closed_status_id() To get the closed status id 
  24. * @uses bbp_get_spam_status_id() To get the spam status id 
  25. * @uses bbp_get_trash_status_id() To get the trash status id 
  26. * @uses bbp_get_forum_post_type() To get the forum post type 
  27. * @uses bbp_get_topic_post_type() To get the topic post type 
  28. * @uses bbp_get_reply_post_type() To get the reply post type 
  29. * @uses bbp_get_replies_per_page() To get the replies per page option 
  30. * @uses bbp_get_paged() To get the current page value 
  31. * @uses bbp_get_search_terms() To get the search terms 
  32. * @uses WP_Query To make query and get the search results 
  33. * @uses WP_Rewrite::using_permalinks() To check if the blog is using permalinks 
  34. * @uses bbp_get_search_url() To get the forum search url 
  35. * @uses paginate_links() To paginate search results 
  36. * @uses apply_filters() Calls 'bbp_has_search_results' with 
  37. * bbPress::search_query::have_posts() 
  38. * and bbPress::reply_query 
  39. * @return object Multidimensional array of search information 
  40. */ 
  41. function bbp_has_search_results( $args = '' ) { 
  42. global $wp_rewrite; 
  43.  
  44. /** Defaults **************************************************************/ 
  45.  
  46. $default_post_type = array( bbp_get_forum_post_type(), bbp_get_topic_post_type(), bbp_get_reply_post_type() ); 
  47.  
  48. // Default query args 
  49. $default = array( 
  50. 'post_type' => $default_post_type, // Forums, topics, and replies 
  51. 'posts_per_page' => bbp_get_replies_per_page(), // This many 
  52. 'paged' => bbp_get_paged(), // On this page 
  53. 'orderby' => 'date', // Sorted by date 
  54. 'order' => 'DESC', // Most recent first 
  55. 'ignore_sticky_posts' => true, // Stickies not supported 
  56. 's' => bbp_get_search_terms(), // This is a search 
  57. ); 
  58.  
  59. // What are the default allowed statuses (based on user caps) 
  60. if ( bbp_get_view_all() ) { 
  61.  
  62. // Default view=all statuses 
  63. $post_statuses = array( 
  64. bbp_get_public_status_id(),  
  65. bbp_get_closed_status_id(),  
  66. bbp_get_spam_status_id(),  
  67. bbp_get_trash_status_id() 
  68. ); 
  69.  
  70. // Add support for private status 
  71. if ( current_user_can( 'read_private_topics' ) ) { 
  72. $post_statuses[] = bbp_get_private_status_id(); 
  73.  
  74. // Join post statuses together 
  75. $default['post_status'] = implode( ', ', $post_statuses ); 
  76.  
  77. // Lean on the 'perm' query var value of 'readable' to provide statuses 
  78. } else { 
  79. $default['perm'] = 'readable'; 
  80.  
  81. /** Setup *****************************************************************/ 
  82.  
  83. // Parse arguments against default values 
  84. $r = bbp_parse_args( $args, $default, 'has_search_results' ); 
  85.  
  86. // Get bbPress 
  87. $bbp = bbpress(); 
  88.  
  89. // Call the query 
  90. if ( ! empty( $r['s'] ) ) { 
  91. $bbp->search_query = new WP_Query( $r ); 
  92.  
  93. // Add pagination values to query object 
  94. $bbp->search_query->posts_per_page = $r['posts_per_page']; 
  95. $bbp->search_query->paged = $r['paged']; 
  96.  
  97. // Never home, regardless of what parse_query says 
  98. $bbp->search_query->is_home = false; 
  99.  
  100. // Only add pagination is query returned results 
  101. if ( ! empty( $bbp->search_query->found_posts ) && ! empty( $bbp->search_query->posts_per_page ) ) { 
  102.  
  103. // Array of arguments to add after pagination links 
  104. $add_args = array(); 
  105.  
  106. // If pretty permalinks are enabled, make our pagination pretty 
  107. if ( $wp_rewrite->using_permalinks() ) { 
  108.  
  109. // Shortcode territory 
  110. if ( is_page() || is_single() ) { 
  111. $base = trailingslashit( get_permalink() ); 
  112.  
  113. // Default search location 
  114. } else { 
  115. $base = trailingslashit( bbp_get_search_results_url() ); 
  116.  
  117. // Add pagination base 
  118. $base = $base . user_trailingslashit( $wp_rewrite->pagination_base . '/%#%/' ); 
  119.  
  120. // Unpretty permalinks 
  121. } else { 
  122. $base = add_query_arg( 'paged', '%#%' ); 
  123.  
  124. // Add args 
  125. if ( bbp_get_view_all() ) { 
  126. $add_args['view'] = 'all'; 
  127.  
  128. // Add pagination to query object 
  129. $bbp->search_query->pagination_links = paginate_links( 
  130. apply_filters( 'bbp_search_results_pagination', array( 
  131. 'base' => $base,  
  132. 'format' => '',  
  133. 'total' => ceil( (int) $bbp->search_query->found_posts / (int) $r['posts_per_page'] ),  
  134. 'current' => (int) $bbp->search_query->paged,  
  135. 'prev_text' => is_rtl() ? '→' : '←',  
  136. 'next_text' => is_rtl() ? '←' : '→',  
  137. 'mid_size' => 1,  
  138. 'add_args' => $add_args,  
  139. ) ) 
  140. ); 
  141.  
  142. // Remove first page from pagination 
  143. if ( $wp_rewrite->using_permalinks() ) { 
  144. $bbp->search_query->pagination_links = str_replace( $wp_rewrite->pagination_base . '/1/', '', $bbp->search_query->pagination_links ); 
  145. } else { 
  146. $bbp->search_query->pagination_links = str_replace( '&paged=1', '', $bbp->search_query->pagination_links ); 
  147.  
  148. // Return object 
  149. return apply_filters( 'bbp_has_search_results', $bbp->search_query->have_posts(), $bbp->search_query ); 
  150.  
  151. /** 
  152. * Whether there are more search results available in the loop 
  153. * 
  154. * @since bbPress (r4579) 
  155. * 
  156. * @uses WP_Query bbPress::search_query::have_posts() To check if there are more 
  157. * search results available 
  158. * @return object Search information 
  159. */ 
  160. function bbp_search_results() { 
  161.  
  162. // Put into variable to check against next 
  163. $have_posts = bbpress()->search_query->have_posts(); 
  164.  
  165. // Reset the post data when finished 
  166. if ( empty( $have_posts ) ) 
  167. wp_reset_postdata(); 
  168.  
  169. return $have_posts; 
  170.  
  171. /** 
  172. * Loads up the current search result in the loop 
  173. * 
  174. * @since bbPress (r4579) 
  175. * 
  176. * @uses WP_Query bbPress::search_query::the_post() To get the current search result 
  177. * @return object Search information 
  178. */ 
  179. function bbp_the_search_result() { 
  180. $search_result = bbpress()->search_query->the_post(); 
  181.  
  182. // Reset each current (forum|topic|reply) id 
  183. bbpress()->current_forum_id = bbp_get_forum_id(); 
  184. bbpress()->current_topic_id = bbp_get_topic_id(); 
  185. bbpress()->current_reply_id = bbp_get_reply_id(); 
  186.  
  187. return $search_result; 
  188.  
  189. /** 
  190. * Output the search page title 
  191. * 
  192. * @since bbPress (r4579) 
  193. * 
  194. * @uses bbp_get_search_title() 
  195. */ 
  196. function bbp_search_title() { 
  197. echo bbp_get_search_title(); 
  198.  
  199. /** 
  200. * Get the search page title 
  201. * 
  202. * @since bbPress (r4579) 
  203. * 
  204. * @uses bbp_get_search_terms() 
  205. */ 
  206. function bbp_get_search_title() { 
  207.  
  208. // Get search terms 
  209. $search_terms = bbp_get_search_terms(); 
  210.  
  211. // No search terms specified 
  212. if ( empty( $search_terms ) ) { 
  213. $title = esc_html__( 'Search', 'bbpress' ); 
  214.  
  215. // Include search terms in title 
  216. } else { 
  217. $title = sprintf( esc_html__( "Search Results for '%s'", 'bbpress' ), esc_attr( $search_terms ) ); 
  218.  
  219. return apply_filters( 'bbp_get_search_title', $title, $search_terms ); 
  220.  
  221. /** 
  222. * Output the search url 
  223. * 
  224. * @since bbPress (r4579) 
  225. * 
  226. * @uses bbp_get_search_url() To get the search url 
  227. */ 
  228. function bbp_search_url() { 
  229. echo esc_url( bbp_get_search_url() ); 
  230. /** 
  231. * Return the search url 
  232. * 
  233. * @since bbPress (r4579) 
  234. * 
  235. * @uses user_trailingslashit() To fix slashes 
  236. * @uses trailingslashit() To fix slashes 
  237. * @uses bbp_get_forums_url() To get the root forums url 
  238. * @uses bbp_get_search_slug() To get the search slug 
  239. * @uses add_query_arg() To help make unpretty permalinks 
  240. * @return string Search url 
  241. */ 
  242. function bbp_get_search_url() { 
  243. global $wp_rewrite; 
  244.  
  245. // Pretty permalinks 
  246. if ( $wp_rewrite->using_permalinks() ) { 
  247. $url = $wp_rewrite->root . bbp_get_search_slug(); 
  248. $url = home_url( user_trailingslashit( $url ) ); 
  249.  
  250. // Unpretty permalinks 
  251. } else { 
  252. $url = add_query_arg( array( bbp_get_search_rewrite_id() => '' ), home_url( '/' ) ); 
  253.  
  254. return apply_filters( 'bbp_get_search_url', $url ); 
  255.  
  256. /** 
  257. * Output the search results url 
  258. * 
  259. * @since bbPress (r4928) 
  260. * 
  261. * @uses bbp_get_search_url() To get the search url 
  262. */ 
  263. function bbp_search_results_url() { 
  264. echo esc_url( bbp_get_search_results_url() ); 
  265. /** 
  266. * Return the search url 
  267. * 
  268. * @since bbPress (r4928) 
  269. * 
  270. * @uses user_trailingslashit() To fix slashes 
  271. * @uses trailingslashit() To fix slashes 
  272. * @uses bbp_get_forums_url() To get the root forums url 
  273. * @uses bbp_get_search_slug() To get the search slug 
  274. * @uses add_query_arg() To help make unpretty permalinks 
  275. * @return string Search url 
  276. */ 
  277. function bbp_get_search_results_url() { 
  278. global $wp_rewrite; 
  279.  
  280. // Get the search terms 
  281. $search_terms = bbp_get_search_terms(); 
  282.  
  283. // Pretty permalinks 
  284. if ( $wp_rewrite->using_permalinks() ) { 
  285.  
  286. // Root search URL 
  287. $url = $wp_rewrite->root . bbp_get_search_slug(); 
  288.  
  289. // Append search terms 
  290. if ( !empty( $search_terms ) ) { 
  291. $url = trailingslashit( $url ) . user_trailingslashit( urlencode( $search_terms ) ); 
  292.  
  293. // Run through home_url() 
  294. $url = home_url( user_trailingslashit( $url ) ); 
  295.  
  296. // Unpretty permalinks 
  297. } else { 
  298. $url = add_query_arg( array( bbp_get_search_rewrite_id() => urlencode( $search_terms ) ), home_url( '/' ) ); 
  299.  
  300. return apply_filters( 'bbp_get_search_results_url', $url ); 
  301.  
  302. /** 
  303. * Output the search terms 
  304. * 
  305. * @since bbPress (r4579) 
  306. * 
  307. * @param string $search_terms Optional. Search terms 
  308. * @uses bbp_get_search_terms() To get the search terms 
  309. */ 
  310. function bbp_search_terms( $search_terms = '' ) { 
  311. echo bbp_get_search_terms( $search_terms ); 
  312.  
  313. /** 
  314. * Get the search terms 
  315. * 
  316. * @since bbPress (r4579) 
  317. * 
  318. * If search terms are supplied, those are used. Otherwise check the 
  319. * search rewrite id query var. 
  320. * 
  321. * @param string $passed_terms Optional. Search terms 
  322. * @uses sanitize_title() To sanitize the search terms 
  323. * @uses get_query_var() To get the search terms from query variable 
  324. * @return bool|string Search terms on success, false on failure 
  325. */ 
  326. function bbp_get_search_terms( $passed_terms = '' ) { 
  327.  
  328. // Sanitize terms if they were passed in 
  329. if ( !empty( $passed_terms ) ) { 
  330. $search_terms = sanitize_title( $passed_terms ); 
  331.  
  332. // Use query variable if not 
  333. } else { 
  334. $search_terms = get_query_var( bbp_get_search_rewrite_id() ); 
  335.  
  336. // Trim whitespace and decode, or set explicitly to false if empty 
  337. $search_terms = !empty( $search_terms ) ? urldecode( trim( $search_terms ) ) : false; 
  338.  
  339. return apply_filters( 'bbp_get_search_terms', $search_terms, $passed_terms ); 
  340.  
  341. /** 
  342. * Output the search result pagination count 
  343. * 
  344. * @since bbPress (r4579) 
  345. * 
  346. * @uses bbp_get_search_pagination_count() To get the search result pagination count 
  347. */ 
  348. function bbp_search_pagination_count() { 
  349. echo bbp_get_search_pagination_count(); 
  350.  
  351. /** 
  352. * Return the search results pagination count 
  353. * 
  354. * @since bbPress (r4579) 
  355. * 
  356. * @uses bbp_number_format() To format the number value 
  357. * @uses apply_filters() Calls 'bbp_get_search_pagination_count' with the 
  358. * pagination count 
  359. * @return string Search pagination count 
  360. */ 
  361. function bbp_get_search_pagination_count() { 
  362. $bbp = bbpress(); 
  363.  
  364. // Define local variable(s) 
  365. $retstr = ''; 
  366.  
  367. // Set pagination values 
  368. $start_num = intval( ( $bbp->search_query->paged - 1 ) * $bbp->search_query->posts_per_page ) + 1; 
  369. $from_num = bbp_number_format( $start_num ); 
  370. $to_num = bbp_number_format( ( $start_num + ( $bbp->search_query->posts_per_page - 1 ) > $bbp->search_query->found_posts ) ? $bbp->search_query->found_posts : $start_num + ( $bbp->search_query->posts_per_page - 1 ) ); 
  371. $total_int = (int) $bbp->search_query->found_posts; 
  372. $total = bbp_number_format( $total_int ); 
  373.  
  374. // Single page of results 
  375. if ( empty( $to_num ) ) { 
  376. $retstr = sprintf( _n( 'Viewing %1$s result', 'Viewing %1$s results', $total_int, 'bbpress' ), $total ); 
  377.  
  378. // Several pages of results 
  379. } else { 
  380. $retstr = sprintf( _n( 'Viewing %2$s results (of %4$s total)', 'Viewing %1$s results - %2$s through %3$s (of %4$s total)', $bbp->search_query->post_count, 'bbpress' ), $bbp->search_query->post_count, $from_num, $to_num, $total ); 
  381.  
  382.  
  383. // Filter and return 
  384. return apply_filters( 'bbp_get_search_pagination_count', esc_html( $retstr ) ); 
  385.  
  386. /** 
  387. * Output search pagination links 
  388. * 
  389. * @since bbPress (r4579) 
  390. * 
  391. * @uses bbp_get_search_pagination_links() To get the search pagination links 
  392. */ 
  393. function bbp_search_pagination_links() { 
  394. echo bbp_get_search_pagination_links(); 
  395.  
  396. /** 
  397. * Return search pagination links 
  398. * 
  399. * @since bbPress (r4579) 
  400. * 
  401. * @uses apply_filters() Calls 'bbp_get_search_pagination_links' with the 
  402. * pagination links 
  403. * @return string Search pagination links 
  404. */ 
  405. function bbp_get_search_pagination_links() { 
  406. $bbp = bbpress(); 
  407.  
  408. if ( !isset( $bbp->search_query->pagination_links ) || empty( $bbp->search_query->pagination_links ) ) 
  409. return false; 
  410.  
  411. return apply_filters( 'bbp_get_search_pagination_links', $bbp->search_query->pagination_links ); 
.