Repository: 10up/ElasticPress (PHP)

Issue:
Turn certain filters into WP_Query arguments
### Is your enhancement related to a problem? Please describe.

In certain parts of the plugin, custom filters are used to apply changes or functionality to `WP_Query` queries. The downside of this approach is that it's hard to target queries specifically as any query that occurs after adding to the filter will receive the changes. 

I ran into this issue developing the facet block, which uses the `ep_is_facetable` filter to enable faceting for the queries used to create the block previews. This filter applies aggregations to the query and populates a global variable with the results of those aggregations. I was experiencing an issue where another query was somehow being fired down the chain, which meant that aggregations were also applied to that query and the global variable was populated with aggregations for the wrong query.

A better approach would be to support an `ep_is_facetable` argument in `WP_Query` which applies the aggregations to just that query. Then, instead of populating a global variable, the aggregation results could be applied to the query object itself. This keeps everything contained to the specific query that needs to be facetable.

With a quick look I've identified these filters that could use this approach:

- `ep_is_facetable`
- `ep_intercept_remote_request`
- `ep_bypass_exclusion_from_search`

There may be others.

### Designs

_No response_

### Describe alternatives you've considered

_No response_

### Code of Conduct

- [X] I agree to follow this project's Code of Conduct



Fix (patch):
diff --git a/includes/classes/Elasticsearch.php b/includes/classes/Elasticsearch.php
index 86be0fa5e3..6eaacb0ed3 100644
--- a/includes/classes/Elasticsearch.php
+++ b/includes/classes/Elasticsearch.php
@@ -1300,7 +1300,7 @@ public function remote_request( $path, $args = [], $query_args = [], $type = ''
 			 * @param {boolean} $intercept True to intercept
 			 * @return {boolean} New value
 			 */
-			if ( true === apply_filters( 'ep_intercept_remote_request', false ) ) {
+			if ( true === apply_filters( 'ep_intercept_remote_request', false ) || ! empty( $query_args['ep_intercept_request'] ) ) {
 				/**
 				 * Filter intercepted request
 				 *
diff --git a/includes/classes/Feature/Autosuggest/Autosuggest.php b/includes/classes/Feature/Autosuggest/Autosuggest.php
index 58622e14ae..83731bf2d2 100644
--- a/includes/classes/Feature/Autosuggest/Autosuggest.php
+++ b/includes/classes/Feature/Autosuggest/Autosuggest.php
@@ -512,7 +512,6 @@ public function generate_search_query() {
 		 */
 		$post_status = apply_filters( 'ep_term_suggest_post_status', array_values( $post_status ) );
 
-		add_filter( 'ep_intercept_remote_request', [ $this, 'intercept_remote_request' ] );
 		add_filter( 'ep_weighting_configuration', [ $features->get_registered_feature( $this->slug ), 'apply_autosuggest_weighting' ] );
 
 		add_filter( 'ep_do_intercept_request', [ $features->get_registered_feature( $this->slug ), 'intercept_search_request' ], 10, 2 );
@@ -543,10 +542,11 @@ public function generate_search_query() {
 			apply_filters(
 				'ep_autosuggest_query_args',
 				[
-					'post_type'    => $post_type,
-					'post_status'  => $post_status,
-					's'            => $placeholder,
-					'ep_integrate' => true,
+					'post_type'            => $post_type,
+					'post_status'          => $post_status,
+					's'                    => $placeholder,
+					'ep_integrate'         => true,
+					'ep_intercept_request' => true,
 				]
 			)
 		);
@@ -557,8 +557,6 @@ public function generate_search_query() {
 
 		remove_filter( 'ep_weighting_configuration', [ $features->get_registered_feature( $this->slug ), 'apply_autosuggest_weighting' ] );
 
-		remove_filter( 'ep_intercept_remote_request', [ $this, 'intercept_remote_request' ] );
-
 		return [
 			'body'        => $this->autosuggest_query,
 			'placeholder' => $placeholder,
@@ -851,6 +849,12 @@ public function epio_autosuggest_set_and_get() {
 	 * @return true
 	 */
 	public function intercept_remote_request() {
+		_doing_it_wrong(
+			__METHOD__,
+			esc_html__( 'Use the WP_Query argument `ep_intercept_request` instead.', 'elasticpress' ),
+			'ElasticPress 5.3.0'
+		);
+
 		return true;
 	}
 
diff --git a/includes/classes/Feature/InstantResults/InstantResults.php b/includes/classes/Feature/InstantResults/InstantResults.php
index d9f00fe5ee..14d25ca54b 100644
--- a/includes/classes/Feature/InstantResults/InstantResults.php
+++ b/includes/classes/Feature/InstantResults/InstantResults.php
@@ -537,21 +537,20 @@ public function get_search_template() {
 
 		wp_set_current_user( $template_user_id );
 
-		add_filter( 'ep_intercept_remote_request', '__return_true' );
 		add_filter( 'ep_do_intercept_request', [ $this, 'intercept_search_request' ], 10, 4 );
 		add_filter( 'ep_is_integrated_request', [ $this, 'is_integrated_request' ], 10, 2 );
 
 		$query = new \WP_Query(
 			array(
-				'ep_integrate'       => true,
-				'ep_search_template' => true,
-				'post_status'        => array_values( $post_statuses ),
-				'post_type'          => $post_types,
-				's'                  => '{{ep_placeholder}}',
+				'ep_integrate'         => true,
+				'ep_search_template'   => true,
+				'post_status'          => array_values( $post_statuses ),
+				'post_type'            => $post_types,
+				's'                    => '{{ep_placeholder}}',
+				'ep_intercept_request' => true,
 			)
 		);
 
-		remove_filter( 'ep_intercept_remote_request', '__return_true' );
 		remove_filter( 'ep_do_intercept_request', [ $this, 'intercept_search_request' ], 10 );
 		remove_filter( 'ep_is_integrated_request', [ $this, 'is_integrated_request' ], 10 );
 
diff --git a/includes/classes/Feature/WooCommerce/OrdersAutosuggest.php b/includes/classes/Feature/WooCommerce/OrdersAutosuggest.php
index 3062fa8eda..4ab1982737 100644
--- a/includes/classes/Feature/WooCommerce/OrdersAutosuggest.php
+++ b/includes/classes/Feature/WooCommerce/OrdersAutosuggest.php
@@ -311,7 +311,6 @@ public function get_search_template() {
 		$order_statuses = wc_get_order_statuses();
 
 		add_filter( 'ep_bypass_exclusion_from_search', '__return_true', 10 );
-		add_filter( 'ep_intercept_remote_request', '__return_true' );
 		add_filter( 'ep_do_intercept_request', [ $this, 'intercept_search_request' ], 10, 3 );
 		add_filter( 'ep_is_integrated_request', [ $this, 'is_integrated_request' ], 10, 2 );
 
@@ -322,11 +321,11 @@ public function get_search_template() {
 				'post_status'              => array_keys( $order_statuses ),
 				'post_type'                => 'shop_order',
 				's'                        => '{{ep_placeholder}}',
+				'ep_intercept_request'     => true,
 			)
 		);
 
 		remove_filter( 'ep_bypass_exclusion_from_search', '__return_true', 10 );
-		remove_filter( 'ep_intercept_remote_request', '__return_true' );
 		remove_filter( 'ep_do_intercept_request', [ $this, 'intercept_search_request' ], 10 );
 		remove_filter( 'ep_is_integrated_request', [ $this, 'is_integrated_request' ], 10 );