PluginProbe
bbPress / trunk
bbPress vtrunk
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 2.2 2.2.1 2.2.2 All 71 releases
bbpress / includes / search / functions.php

functions.php in bbPress trunk, at includes/search/functions.php

143 lines 3.0 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 Functions.
5 *
6 * @package bbPress
7 * @subpackage Functions
8 */
9
10 // Exit if accessed directly
11 defined( 'ABSPATH' ) || exit;
12
13 /** Query *********************************************************************/
14
15 /**
16 * Run the search query.
17 *
18 * @since 2.3.0 bbPress (r4579)
19 *
20 * @param mixed $new_args New arguments.
21 * @return bool False if no results, otherwise if search results are there.
22 */
23 function bbp_search_query( $new_args = array() ) {
24
25 // Existing arguments
26 $query_args = bbp_get_search_query_args();
27
28 // Merge arguments
29 if ( ! empty( $new_args ) ) {
30 $new_args = bbp_parse_args( $new_args, array(), 'search_query' );
31 $query_args = array_merge( $query_args, $new_args );
32 }
33
34 return bbp_has_search_results( $query_args );
35 }
36
37 /**
38 * Return the search query arguments.
39 *
40 * @since 2.3.0 bbPress (r4579)
41 *
42 * @return array Query arguments.
43 */
44 function bbp_get_search_query_args() {
45
46 // Get search terms
47 $search_terms = bbp_get_search_terms();
48 $retval = ! empty( $search_terms )
49 ? array( 's' => $search_terms )
50 : array();
51
52 // Filter & return
53 return apply_filters( 'bbp_get_search_query_args', $retval );
54 }
55
56 /**
57 * Redirect to search results page if needed.
58 *
59 * @since 2.4.0 bbPress (r4928)
60 *
61 * @return If a redirect is not needed.
62 */
63 function bbp_search_results_redirect() {
64
65 // Bail if not a search request action
66 if ( empty( $_GET['action'] ) || ( 'bbp-search-request' !== $_GET['action'] ) ) {
67 return;
68 }
69
70 // Bail if not using pretty permalinks
71 if ( ! bbp_use_pretty_urls() ) {
72 return;
73 }
74
75 // Get the redirect URL
76 $redirect_to = bbp_get_search_results_url();
77 if ( empty( $redirect_to ) ) {
78 return;
79 }
80
81 // Redirect and bail
82 bbp_redirect( $redirect_to );
83 }
84
85 /**
86 * Return an array of search types.
87 *
88 * @since 2.6.0 bbPress (r6903)
89 *
90 * @return array
91 */
92 function bbp_get_search_type_ids() {
93 return apply_filters( 'bbp_get_search_types', array( 's', 'fs', 'ts', 'rs' ) );
94 }
95
96 /**
97 * Sanitize a query argument used to pass some search terms.
98 *
99 * Accepts a single parameter to be used for forums, topics, or replies.
100 *
101 * @since 2.6.0 bbPress (r6903)
102 *
103 * @param string $query_arg s|fs|ts|rs.
104 *
105 * @return mixed
106 */
107 function bbp_sanitize_search_request( $query_arg = 's' ) {
108
109 // Define allowed keys
110 $allowed = bbp_get_search_type_ids();
111
112 // Bail if not an allowed query string key
113 if ( ! in_array( $query_arg, $allowed, true ) ) {
114 return false;
115 }
116
117 // Get search terms if requested
118 $terms = ! empty( $_REQUEST[ $query_arg ] )
119 ? $_REQUEST[ $query_arg ]
120 : false;
121
122 // Bail if query argument does not exist
123 if ( empty( $terms ) ) {
124 return false;
125 }
126
127 // Special case for array of terms
128 if ( is_array( $terms ) ) {
129
130 // Filter out any non-scalar array values, to avoid PHP warnings
131 $terms = array_filter( $terms, 'is_scalar' );
132
133 // Implode terms with spaces, to sanitize later
134 $terms = implode( ' ', $terms );
135 }
136
137 // Sanitize
138 $retval = sanitize_text_field( $terms );
139
140 // Filter & return
141 return apply_filters( 'bbp_sanitize_search_request', $retval, $query_arg );
142 }
143