PluginProbe
Forumax – AI Powered Advanced Community Forum Plugin / trunk
Forumax – AI Powered Advanced Community Forum Plugin vtrunk
2.4.4 2.4.3 2.4.2 2.4.1 2.4.0 trunk 1.0.8 1.1.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 2.0.0 2.1.0 All 29 releases
bbp-core / lib / bbpress / includes / search / template.php

template.php in Forumax – AI Powered Advanced Community Forum Plugin trunk, at lib/bbpress/includes/search/template.php

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