Skip to content

Commit

Permalink
Search: Add a hook on returning filters. (jetpack:10706)
Browse files Browse the repository at this point in the history
Summary:
 <!--- Provide a general summary of your changes in the Title above -->

 Fixes N/A(?)

 #### Changes proposed in this Pull Request:
 <!--- Explain what functional changes your PR includes -->

 I recently had a use case where I wanted to add custom filters (i.e. not
 a taxonomy, post type or date histogram) and found I could set the
 filter no problem but `get_filters()` would ignore it as it wasn't
 supported.

 I'd like to be able to easily support custom filters through a hook into
 `get_filters()`.

 #### Testing instructions:
 <!-- Please include detailed testing steps, explaining how to test your change. -->
 <!-- Bear in mind that context you working on is not obvious for everyone.  -->
 <!-- Adding "simple" configuration steps will help reviewers to get to your PR as quickly as possible. -->
 <!-- "Before / After" screenshots can also be very helpful when the change is visual. -->

 * Add custom filters using `set_filters()` e.g.

 ```php
 Jetpack_Search::instance()->set_filters(
 	[
 		'Price Range' => [
 			'type' => 'range',
 			'label' => 'price_range',
 			'field' => 'price',
 			'ranges' => $ranges,
 			'count' => count( $ranges ),
 		],
 	]
 );
 ```

 Call `get_filters()` and observe that the added (unsupported) filter is not available.

 #### Proposed changelog entry for your changes:
 <!-- Please do not leave this empty. If no changelog entry needed, state as such. -->

 * Added hook to `get_filters()` to allow the use of custom filters.

--

Automatically created by Jetpack Fusion from a Pull Request:
#10706

Test Plan: - [ ] Dummy test plan.

Reviewers: benoitperson, hypertextranch, gibrown, github-phab-bot

Tags: #touches_jetpack_files

Differential Revision: https://[private link]

Merges r185368-wpcom.
  • Loading branch information
jeherve authored and gibrown committed Nov 8, 2019
1 parent f72eac1 commit 4489043
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions modules/search/class.jetpack-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ public function set_filters_from_widgets() {
* @param WP_Query $query A WP_Query instance.
*/
public function maybe_add_post_type_as_var( WP_Query $query ) {
if ( $this->should_handle_query( $query ) && ! empty( $_GET['post_type'] ) ) {
if ( $query->is_main_query() && $query->is_search && ! empty( $_GET['post_type'] ) ) {
$post_types = ( is_string( $_GET['post_type'] ) && false !== strpos( $_GET['post_type'], ',' ) )
? $post_type = explode( ',', $_GET['post_type'] )
: (array) $_GET['post_type'];
Expand Down Expand Up @@ -460,7 +460,17 @@ public function search( array $es_args ) {
* @return array Array of matching posts.
*/
public function filter__posts_pre_query( $posts, $query ) {
if ( ! $this->should_handle_query( $query ) ) {
/**
* Determine whether a given WP_Query should be handled by ElasticSearch.
*
* @module search
*
* @since 5.6.0
*
* @param bool $should_handle Should be handled by Jetpack Search.
* @param WP_Query $query The WP_Query object.
*/
if ( ! apply_filters( 'jetpack_search_should_handle_query', ( $query->is_main_query() && $query->is_search() ), $query ) ) {
return $posts;
}

Expand Down Expand Up @@ -515,7 +525,7 @@ public function filter__posts_pre_query( $posts, $query ) {
* @param WP_Query $query The original WP_Query to use for the parameters of our search.
*/
public function do_search( WP_Query $query ) {
if ( ! $this->should_handle_query( $query ) ) {
if ( ! $query->is_main_query() || ! $query->is_search() ) {
return;
}

Expand Down Expand Up @@ -1830,27 +1840,6 @@ public function move_search_widgets_to_inactive( $module ) {
}
}

/**
* Determine whether a given WP_Query should be handled by ElasticSearch.
*
* @param WP_Query $query The WP_Query object.
*
* @return bool
*/
public function should_handle_query( $query ) {
/**
* Determine whether a given WP_Query should be handled by ElasticSearch.
*
* @module search
*
* @since 5.6.0
*
* @param bool $should_handle Should be handled by Jetpack Search.
* @param WP_Query $query The WP_Query object.
*/
return apply_filters( 'jetpack_search_should_handle_query', $query->is_main_query() && $query->is_search(), $query );
}

/**
* Transforms an array with fields name as keys and boosts as value into
* shorthand "caret" format.
Expand Down

0 comments on commit 4489043

Please sign in to comment.