PageRenderTime 67ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/bbpress/includes/search/template.php

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