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

New ep_autosuggest_query_args filter + tests #3038

Merged
merged 1 commit into from
Oct 3, 2022
Merged
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
37 changes: 30 additions & 7 deletions includes/classes/Feature/Autosuggest/Autosuggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -552,13 +552,36 @@ public function generate_search_query() {

add_filter( 'posts_pre_query', [ $features->get_registered_feature( $this->slug ), 'return_empty_posts' ], 100, 1 ); // after ES Query to ensure we are not falling back to DB in any case

$search = new \WP_Query(
[
'post_type' => $post_type,
'post_status' => $post_status,
's' => $placeholder,
'ep_integrate' => true,
]
new \WP_Query(
/**
* Filter WP Query args of the autosuggest query template.
*
* If you want to display 20 posts in autosuggest:
*
* ```
* add_filter(
* 'ep_autosuggest_query_args',
* function( $args ) {
* $args['posts_per_page'] = 20;
* return $args;
* }
* );
* ```
*
* @since 4.4.0
* @hook ep_autosuggest_query_args
* @param {array} $args Query args
* @return {array} New query args
*/
apply_filters(
'ep_autosuggest_query_args',
[
'post_type' => $post_type,
'post_status' => $post_status,
's' => $placeholder,
'ep_integrate' => true,
]
)
);

remove_filter( 'posts_pre_query', [ $features->get_registered_feature( $this->slug ), 'return_empty_posts' ], 100 );
Expand Down
59 changes: 59 additions & 0 deletions tests/php/features/TestAutosuggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,65 @@ public function testGenerateSearchQuery() {
$this->assertContains( 'ep_autosuggest_placeholder', $query );
}

public function testGenerateSearchQueryFilters() {
/**
* Test the `ep_autosuggest_query_placeholder` filter.
*/
$test_placeholder_filter = function() {
return 'lorem-ipsum';
};

add_filter( 'ep_autosuggest_query_placeholder', $test_placeholder_filter );

$query = $this->get_feature()->generate_search_query();
$this->assertContains( 'lorem-ipsum', $query['body'] );

remove_filter( 'ep_autosuggest_query_placeholder', $test_placeholder_filter );

/**
* Test the `ep_autosuggest_query_placeholder` filter.
*/
$test_post_type_filter = function() {
return [ 'my-custom-post-type' ];
};

add_filter( 'ep_term_suggest_post_type', $test_post_type_filter );

$query = $this->get_feature()->generate_search_query();
$this->assertContains( 'my-custom-post-type', $query['body'] );

remove_filter( 'ep_term_suggest_post_type', $test_post_type_filter );

/**
* Test the `ep_term_suggest_post_status` filter.
*/
$test_post_status_filter = function() {
return [ 'trash' ];
};

add_filter( 'ep_term_suggest_post_status', $test_post_status_filter );

$query = $this->get_feature()->generate_search_query();
$this->assertContains( 'trash', $query['body'] );

remove_filter( 'ep_term_suggest_post_status', $test_post_status_filter );

/**
* Test the `ep_term_suggest_post_status` filter.
*/
$test_args_filter = function( $args ) {
$args['posts_per_page'] = 1234;
return $args;
};

add_filter( 'ep_autosuggest_query_args', $test_args_filter );

$query = $this->get_feature()->generate_search_query();
$this->assertContains( '1234', $query['body'] );

remove_filter( 'ep_autosuggest_query_args', $test_args_filter );
}

public function testReturnEmptyPosts() {
$this->assertEmpty( $this->get_feature()->return_empty_posts() );
}
Expand Down