Skip to content

Commit

Permalink
More highlight fixes
Browse files Browse the repository at this point in the history
* Fix for seamless mode off
* Only highlight words > 2 characters
* Quotes are properly highlighted
  • Loading branch information
ajaydsouza committed Dec 7, 2024
1 parent 517115d commit 17d149c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
5 changes: 2 additions & 3 deletions includes/frontend/class-display.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __construct() {
* @return string Post Content
*/
public function content( $content ) {
if ( is_admin() || ! in_the_loop() || ! is_main_query() ) {
if ( is_admin() || ! in_the_loop() ) {
return $content;
}
$referer = wp_get_referer() ? urldecode( wp_get_referer() ) : '';
Expand Down Expand Up @@ -89,8 +89,7 @@ public function content( $content ) {
}

if ( ! empty( $search_query ) ) {
$search_query = str_replace( array( '"', '"' ), '', $search_query );
$search_query = preg_replace( '/[^a-zA-Z0-9\s]/', '', $search_query );
$search_query = str_replace( array( "'", '"', '"', '\+', '\-' ), '', $search_query );
$keys = preg_split( '/[\s,\+\.]+/', $search_query );
$content = Helpers::highlight( $content, $keys );
}
Expand Down
3 changes: 1 addition & 2 deletions includes/general-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ function get_bsearch_excerpt( $post = null, $excerpt_length = 0, $use_excerpt =

if ( $relevant ) {
$search_query = get_bsearch_query();
$search_query = str_replace( array( '"', '"' ), '', $search_query );
$search_query = preg_replace( '/[^a-zA-Z0-9\s]/', '', $search_query );
$search_query = str_replace( array( "'", '"', '"', '\+', '\-' ), '', $search_query );
$words = preg_split( '/[\s,\+\.]+/', $search_query );

$output = Helpers::extract_relevant_excerpt( $words, $output, $excerpt_more );
Expand Down
5 changes: 4 additions & 1 deletion includes/util/class-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,10 @@ public static function extract_locations( $words, $fulltext ) {
) {
foreach ( $words as $word ) {
$wordlen = strlen( $word );
$loc = stripos( $fulltext, $word );
if ( $wordlen < 2 ) {
continue;
}
$loc = stripos( $fulltext, $word );
while ( false !== $loc ) {
$locations[] = $loc;
$loc = stripos( $fulltext, $word, $loc + $wordlen );
Expand Down
3 changes: 3 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ Release post: [https://webberzone.com/announcements/better-search-v4-enhance-wor

* Bug fixes:
* Fixed a bug where post titles in search results were not highlighted properly.
* Fix for highlighting when Seamless Mode is off.
* Only highlight words > 2 characters.
* Words with double or single quotes are properly highlighted.

= 4.0.2 =

Expand Down

0 comments on commit 17d149c

Please sign in to comment.