Skip to content

Commit 37ae0ee

Browse files
committed
Fix boolean mode validation and improve search term sanitization
1 parent 93f864d commit 37ae0ee

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

includes/class-better-search-core-query.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -532,17 +532,30 @@ public function set_class_variables( $search_query = '' ) {
532532
if ( $use_fulltext ) {
533533
$use_fulltext_proxy = false;
534534
foreach ( $search_words as $search_word ) {
535-
if ( strlen( $search_word ) >= (int) $min_char ) {
535+
$clean_word = preg_replace( '/[^\w\s]/u', '', $search_word );
536+
if ( strlen( $clean_word ) >= (int) $min_char ) {
536537
$use_fulltext_proxy = true;
537538
}
538539
}
539540
$use_fulltext = $use_fulltext_proxy;
540541
}
541542

542-
$this->search_query = $search_query;
543-
$this->search_terms = $search_words;
544-
$this->use_fulltext = $use_fulltext;
545-
$this->is_boolean_mode = $this->input_query_args['boolean_mode'] ?? bsearch_get_option( 'boolean_mode' );
543+
$this->search_query = $search_query;
544+
$this->search_terms = $search_words;
545+
$this->use_fulltext = $use_fulltext;
546+
$this->is_boolean_mode = $this->input_query_args['boolean_mode'] ?? bsearch_get_option( 'boolean_mode' );
547+
548+
// If boolean mode is enabled, check if there are any terms long enough.
549+
if ( $this->is_boolean_mode ) {
550+
$boolean_proxy = false;
551+
foreach ( $search_words as $search_word ) {
552+
$clean_word = preg_replace( '/[^\w\s]/u', '', $search_word );
553+
if ( strlen( $clean_word ) >= (int) $min_char ) {
554+
$boolean_proxy = true;
555+
}
556+
}
557+
$this->is_boolean_mode = $boolean_proxy;
558+
}
546559
$this->is_seamless_mode = $this->input_query_args['seamless'] ?? bsearch_get_option( 'seamless' );
547560
$this->should_use_custom_table();
548561
}
@@ -835,7 +848,7 @@ public function posts_search( $where, $query ) {
835848
}
836849

837850
foreach ( (array) $search_terms as $term ) {
838-
$term = str_replace( array( "'", '"', '"', '\+', '\-' ), '', $term );
851+
$term = preg_replace( '/[+\-*"~<>()@\']/', '', $term );
839852

840853
// If there is an $exclusion_prefix, terms prefixed with it should be excluded.
841854
$exclude = $exclusion_prefix && ( substr( $term, 0, 1 ) === $exclusion_prefix );
@@ -873,7 +886,7 @@ public function posts_search( $where, $query ) {
873886
// Let's do a LIKE search for all other fields.
874887
$searchand = '';
875888
foreach ( (array) $search_terms as $term ) {
876-
$term = str_replace( array( "'", '"', '&quot;', '\+', '\-' ), '', $term );
889+
$term = preg_replace( '/[+\-*"~<>()@\']/', '', $term );
877890
$clause = array();
878891

879892
// If there is an $exclusion_prefix, terms prefixed with it should be excluded.

readme.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ You can report security bugs through the Patchstack Vulnerability Disclosure Pro
121121

122122
== Changelog ==
123123

124+
= 4.2.4 =
125+
126+
* Features:
127+
* Better Search form: The "any" post type option label can now be customised when the post type dropdown is enabled.
128+
129+
* Fixed:
130+
* Fixed an issue where selecting "any" post type would search through all post types instead of respecting the configured post types from settings.
131+
* [Pro] Custom table searches now include post slug matching when “Search post slug” is enabled.
132+
* [Pro] Fixed SQL syntax error in multisite search queries when custom tables are disabled, caused by malformed GROUP BY clause stripping.
133+
* Fixed improper stripping of boolean mode operators in LIKE clauses, ensuring consistent behavior between FULLTEXT and LIKE searches.
134+
124135
= 4.2.3 =
125136

126137
* Modifications:
@@ -193,5 +204,8 @@ For previous changelog entries, please refer to the separate changelog.txt file
193204

194205
== Upgrade Notice ==
195206

207+
= 4.2.4 =
208+
Fixes post type selection to respect configured settings when "any" is selected.
209+
196210
= 4.2.3 =
197211
Adds WooCommerce product indexing (Pro only), modernizes taxonomy search with Tom Select, and enhances seamless mode logic with improved tracker response handling.

0 commit comments

Comments
 (0)