Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 28 additions & 0 deletions includes/classes/Feature/DidYouMean/DidYouMean.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,33 @@ public function get_suggestion( $query = null ) {
return false;
}

/**
* Filter whether to suppress the suggestion when the current query already returned results.
*
* @since 5.4.0
* @hook ep_suggestion_suppress_when_results_exist
* @param {bool} $suppress Whether to suppress the suggestion. Default true.
* @param {WP_Query} $query The WP_Query object.
* @return {bool} New value
*/
$suppress_when_results_exist = apply_filters( 'ep_suggestion_suppress_when_results_exist', true, $query );

// No need to suggest alternatives if the current query already returned results.
if ( $suppress_when_results_exist && $query->found_posts > 0 ) {
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 +318,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
111 changes: 111 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,99 @@ 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 `ep_suggestion_suppress_when_results_exist` filter can restore the suggestion when results exist.
*/
public function testGetSuggestionSuppressWhenResultsExistFilter() {
$query = new \WP_Query(
[
's' => 'shirt',
]
);
$query->found_posts = 1;
$query->suggested_terms = [
'options' => [
[
'text' => 'shift',
],
],
];
$query->query_vars['s'] = 'shirt';
$query->elasticsearch_success = true;

// Default behavior: suppressed because the query already has results.
$this->assertFalse( ElasticPress\Features::factory()->get_registered_feature( 'did-you-mean' )->get_suggestion( $query ) );

add_filter( 'ep_suggestion_suppress_when_results_exist', '__return_false' );

$expected = sprintf( '<span class="ep-spell-suggestion">Did you mean: <a href="%s">shift</a>?</span>', get_search_link( 'shift' ) );
$this->assertEquals( $expected, 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 ) );
}
}