Skip to content

Commit

Permalink
Merge pull request #3038 from 10up/feature/ep_autosuggest_query_args-…
Browse files Browse the repository at this point in the history
…filter

New `ep_autosuggest_query_args` filter + tests
  • Loading branch information
felipeelia authored Oct 3, 2022
2 parents d6985c8 + 138fa52 commit 4220a2d
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 7 deletions.
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

0 comments on commit 4220a2d

Please sign in to comment.