Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Did you mean #3425

Merged
merged 15 commits into from
Jun 7, 2023
Prev Previous commit
Next Next commit
Minor changes
  • Loading branch information
burhandodhy committed May 30, 2023
commit b718d4d2d6493829cea149c3324c62d5a7e1c56e
19 changes: 10 additions & 9 deletions includes/classes/Feature/DidYouMean/DidYouMean.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,20 @@ public function get_suggestion( $query = null ) {

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

$html .= $this->get_alternatives( $query );
$html .= $this->get_alternatives_terms( $query );
$terms = $query->suggested_terms['options'] ?? [];

/**
* Filter the Did You Mean HTML output.
* Filter the did you mean suggested term HTML.
*
* @since 4.6.0
* @hook ep_did_you_mean_suggestion_html
* @hook ep_did_you_mean_suggested_term_html
* @param {string} $html The HTML output.
* @param {array} $terms All suggested terms.
* @param {WP_Query} $query The WP_Query object.
* @param {string} $term The suggested term.
* @return {string} New $html value
*/
return apply_filters( 'ep_did_you_mean_suggestion_html', $html, $query, $term );
return apply_filters( 'ep_did_you_mean_suggestion_html', $html, $terms, $query );
}

/**
Expand Down Expand Up @@ -241,9 +242,9 @@ public function output_feature_box_settings() {
* Returns the list of other suggestions
*
* @param WP_Query $query WP_Query object
felipeelia marked this conversation as resolved.
Show resolved Hide resolved
* @return string|boolean
* @return string|false
*/
protected function get_alternatives( $query ) {
protected function get_alternatives_terms( $query ) {
global $wp_query;

if ( ! $query && $wp_query->is_main_query() && $wp_query->is_search() ) {
Expand All @@ -258,9 +259,10 @@ protected function get_alternatives( $query ) {

// If there are posts, we don't need to show the list of suggestions.
if ( 'list' !== $settings['search_behavior'] || $query->found_posts ) {
return '';
return false;
}

$options = $query->suggested_terms['options'] ?? [];
array_shift( $options );

if ( empty( $options ) ) {
Expand All @@ -277,7 +279,6 @@ protected function get_alternatives( $query ) {
$html .= '</div>';

return $html;

}

/**
Expand Down