Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions includes/classes/Feature/DidYouMean/DidYouMean.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,22 @@ public function get_suggestion( $query = null ) {
return false;
}

// No need to suggest alternatives if the current query already returned results.
if ( $query->found_posts > 0 ) {
Comment thread
faisalahammad marked this conversation as resolved.
Outdated
return false;
}

$term = $this->get_suggested_term( $query );
if ( empty( $term ) ) {
return false;
}

// No need to suggest the same term the user already typed.
$search_term = $query->query_vars['s'] ?? '';
if ( strtolower( $term ) === strtolower( $search_term ) ) {
return false;
}

$html = sprintf( '<span class="ep-spell-suggestion">%s: <a href="%s">%s</a>?</span>', esc_html__( 'Did you mean', 'elasticpress' ), get_search_link( $term ), $term );

$html .= $this->get_alternatives_terms( $query );
Expand Down Expand Up @@ -296,6 +307,12 @@ public function automatically_redirect_user() {
return;
}

// Do not redirect to the same term the user already searched for.
$search_term = $wp_query->query_vars['s'] ?? '';
if ( strtolower( $term ) === strtolower( $search_term ) ) {
return;
}

$url = get_search_link( $term );
$url = add_query_arg(
[
Expand Down
82 changes: 82 additions & 0 deletions tests/php/features/TestDidYouMean.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ public function testGetSearchSuggestionMethod() {

$this->assertTrue( $query->elasticsearch_success );

// Force no results so the suggestion is displayed.
$query->found_posts = 0;

$expected = sprintf( '<span class="ep-spell-suggestion">Did you mean: <a href="%s">test</a>?</span>', get_search_link( 'test' ) );
$this->assertEquals( $expected, ElasticPress\Features::factory()->get_registered_feature( 'did-you-mean' )->get_suggestion( $query ) );
}
Expand Down Expand Up @@ -143,6 +146,9 @@ public function testGetSearchSuggestionMethodReturnsSuggestionForMainQuery() {

$query = $query->query( $args );

// Force no results so the suggestion is displayed.
$wp_query->found_posts = 0;

$expected = sprintf( '<span class="ep-spell-suggestion">Did you mean: <a href="%s">test</a>?</span>', get_search_link( 'test' ) );
$this->assertEquals( $expected, ElasticPress\Features::factory()->get_registered_feature( 'did-you-mean' )->get_suggestion() );
}
Expand Down Expand Up @@ -180,6 +186,10 @@ function ( $html, $terms, $query ) use ( $expected_result ) {
's' => 'teet',
]
);

// Force no results so the suggestion is displayed.
$query->found_posts = 0;

$this->assertEquals( $expected_result, ElasticPress\Features::factory()->get_registered_feature( 'did-you-mean' )->get_suggestion( $query ) );
}

Expand Down Expand Up @@ -338,6 +348,9 @@ public function testEPSuggestionsAction() {
$wp_the_query = $query;
$wp_query = $query;

// Force no results so the suggestion is displayed.
$query->found_posts = 0;

ob_start();
do_action( 'ep_suggestions' );
$output = ob_get_clean();
Expand Down Expand Up @@ -371,6 +384,9 @@ public function testEPSuggestionsActionOtherThanMainQuery() {

parse_str( 'ep_suggestion_original_term=Original Term', $_GET );

// Force no results so the suggestion is displayed.
$query->found_posts = 0;

ob_start();
do_action( 'ep_suggestions', $query );
$output = ob_get_clean();
Expand Down Expand Up @@ -457,4 +473,70 @@ public function test_get_settings_schema() {
$settings_keys
);
}

/**
* Tests that get_suggestion returns false when the current search term already has results.
*/
public function testGetSuggestionNotShownWhenResultsExist() {
$this->ep_factory->post->create( [ 'post_content' => 'Shirt product' ] );
$this->ep_factory->post->create( [ 'post_content' => 'Shift product' ] );

ElasticPress\Elasticsearch::factory()->refresh_indices();

$query = new \WP_Query(
[
's' => 'shirt',
]
);

$this->assertTrue( $query->elasticsearch_success );
$this->assertGreaterThan( 0, $query->found_posts );
$this->assertFalse( ElasticPress\Features::factory()->get_registered_feature( 'did-you-mean' )->get_suggestion( $query ) );
}

/**
* Tests that get_suggestion returns false when the top suggestion matches the original term.
*/
public function testGetSuggestionNotShownWhenTopSuggestionMatchesOriginalTerm() {
$query = new \WP_Query(
[
's' => 'shirt',
]
);
$query->found_posts = 0;
$query->suggested_terms = [
'options' => [
[
'text' => 'shirt',
],
],
];
$query->query_vars['s'] = 'shirt';
$query->elasticsearch_success = true;

$this->assertFalse( ElasticPress\Features::factory()->get_registered_feature( 'did-you-mean' )->get_suggestion( $query ) );
}

/**
* Tests that get_suggestion returns false when the top suggestion matches the original term with different casing.
*/
public function testGetSuggestionNotShownWhenTopSuggestionMatchesOriginalTermCaseInsensitive() {
$query = new \WP_Query(
[
's' => 'shirt',
]
);
$query->found_posts = 0;
$query->suggested_terms = [
'options' => [
[
'text' => 'Shirt',
],
],
];
$query->query_vars['s'] = 'shirt';
$query->elasticsearch_success = true;

$this->assertFalse( ElasticPress\Features::factory()->get_registered_feature( 'did-you-mean' )->get_suggestion( $query ) );
}
}