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
2 changes: 1 addition & 1 deletion .wp-env.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"wp-content/plugins/multiple-required-features.php": "./tests/e2e/src/wordpress-files/test-plugins/multiple-required-features.php",
"wp-content/uploads/content-example.xml": "./tests/e2e/src/wordpress-files/test-docs/content-example.xml",
"wp-content/plugins/echo-shortcode.php": "./tests/e2e/src/wordpress-files/test-plugins/echo-shortcode.php",
"wp-content/plugins/simulate-setting-dependency.php": "./tests/e2e/src/wordpress-files/test-plugins/simulate-setting-dependency.php"
"wp-content/plugins/simulate-setting-dependency.php": "./tests/e2e/src/wordpress-files/test-plugins/simulate-setting-dependency.php"
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions assets/js/autosuggest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -675,9 +675,11 @@ function init() {

if (response && response._shards && response._shards.successful > 0) {
const hits = checkForOrderedPosts(response.hits.hits, searchText);
cachedAutosuggestResults = hits;
const resultsLimit = parseInt(epas.resultsLimit, 10);
const limitedHits = resultsLimit > 0 ? hits.slice(0, resultsLimit) : hits;
cachedAutosuggestResults = limitedHits;

toggleAutosuggest(hits, input);
toggleAutosuggest(limitedHits, input);
} else {
hideAutosuggestBox();
}
Expand Down
36 changes: 33 additions & 3 deletions includes/classes/Feature/Autosuggest/Autosuggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ public function enqueue_scripts() {
'query' => $query['body'],
'placeholder' => $query['placeholder'],
'endpointUrl' => esc_url( untrailingslashit( $endpoint_url ) ),
'resultsLimit' => $query['results_limit'],
'selector' => empty( $settings['autosuggest_selector'] ) ? 'ep-autosuggest' : esc_html( $settings['autosuggest_selector'] ),
/**
* Filter autosuggest default selectors.
Expand Down Expand Up @@ -488,6 +489,34 @@ public function generate_search_query() {
]
);

$results_limit = isset( $args['posts_per_page'] ) ? (int) $args['posts_per_page'] : (int) get_option( 'posts_per_page' );
$search_ordering_feature = $features->get_registered_feature( 'searchordering' );

if ( $results_limit > 0 && $search_ordering_feature && $search_ordering_feature->is_active() ) {
/**
* Filter the number of Autosuggest candidates requested when Custom Search Results is active.
*
* Autosuggest displays the number of results configured by `ep_autosuggest_query_args`, but Custom
* Search Results need a larger candidate set so posts ordered beyond that display limit can still be
* moved into their configured positions.
*
* @since 5.4.0
* @hook ep_autosuggest_custom_results_query_size
* @param {int} $query_size Number of candidates to request.
* @param {int} $results_limit Number of results to display.
* @param {array} $args Autosuggest query args.
* @return {int} New candidate query size.
*/
$query_size = (int) apply_filters(
'ep_autosuggest_custom_results_query_size',
max( $results_limit, 10 ),
$results_limit,
$args
);

$args['posts_per_page'] = max( $results_limit, $query_size );
}

new \WP_Query( $args );

remove_filter( 'posts_pre_query', [ $features->get_registered_feature( $this->slug ), 'return_empty_posts' ], 100 );
Expand All @@ -497,9 +526,10 @@ public function generate_search_query() {
remove_filter( 'ep_weighting_configuration', [ $features->get_registered_feature( $this->slug ), 'apply_autosuggest_weighting' ] );

return [
'body' => $this->autosuggest_query,
'placeholder' => $placeholder,
'query_vars' => $args,
'body' => $this->autosuggest_query,
'placeholder' => $placeholder,
'query_vars' => $args,
'results_limit' => $results_limit,
];
}

Expand Down
23 changes: 23 additions & 0 deletions tests/php/features/TestAutosuggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,29 @@ public function testGenerateSearchQueryFilters() {

$query = $this->get_feature()->generate_search_query();
$this->assertStringContainsString( '1234', $query['body'] );
$this->assertEquals( 1234, $query['results_limit'] );
}

/**
* Test Custom Search Results can request more Autosuggest candidates than are displayed.
*/
public function testGenerateSearchQueryWithSearchOrdering() {
ElasticPress\Features::factory()->activate_feature( 'search' );
ElasticPress\Features::factory()->activate_feature( 'searchordering' );

$test_args_filter = function ( $args ) {
$args['posts_per_page'] = 4;
return $args;
};

add_filter( 'ep_autosuggest_query_args', $test_args_filter );

$query = $this->get_feature()->generate_search_query();
$this->assertStringContainsString( '10', $query['body'] );
$this->assertEquals( 4, $query['results_limit'] );
$this->assertEquals( 10, $query['query_vars']['posts_per_page'] );

remove_filter( 'ep_autosuggest_query_args', $test_args_filter );
}

/**
Expand Down
Loading