Skip to content

Commit

Permalink
Utility function for checking if a request should be integrated. Addr…
Browse files Browse the repository at this point in the history
…esses #2148.
  • Loading branch information
JakePT committed Jul 14, 2021
1 parent 9dfe833 commit 1726bcd
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 77 deletions.
2 changes: 1 addition & 1 deletion includes/classes/Elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public function query( $index, $type, $query, $query_args, $query_object = null
(
Utils\is_epio() &&
! empty( $query_args['s'] ) &&
! is_admin() &&
Utils\is_integrated_request() &&
! isset( $_GET['post_type'] ) // phpcs:ignore WordPress.Security.NonceVerification
),
$query_args
Expand Down
5 changes: 2 additions & 3 deletions includes/classes/Feature/Autosuggest/Autosuggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public function mapping( $mapping ) {
* @return array
*/
public function set_fuzziness( $fuzziness, $search_fields, $args ) {
if ( ! is_admin() && ! empty( $args['s'] ) ) {
if ( Utils\is_integrated_request() && ! empty( $args['s'] ) ) {
return 'auto';
}
return $fuzziness;
Expand All @@ -237,7 +237,7 @@ public function set_fuzziness( $fuzziness, $search_fields, $args ) {
* @return array $query adjusted ES Query arguments
*/
public function adjust_fuzzy_fields( $query, $post_type, $args ) {
if ( ! is_admin() && ! empty( $args['s'] ) ) {
if ( Utils\is_integrated_request() && ! empty( $args['s'] ) ) {
/**
* Filter autosuggest ngram fields
*
Expand Down Expand Up @@ -899,4 +899,3 @@ public function epio_autosuggest_health_check_info( $debug_info ) {
return $debug_info;
}
}

4 changes: 1 addition & 3 deletions includes/classes/Feature/Documents/Documents.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function attachments_mapping( $mapping ) {
* @since 2.3
*/
public function setup_document_search( $query ) {
if ( is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
if ( ! Utils\is_integrated_request( false ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
return;
}

Expand Down Expand Up @@ -517,5 +517,3 @@ public function filter_attachment_post_type_weights( $weights, $post_type ) {
return $weights;
}
}


11 changes: 5 additions & 6 deletions includes/classes/Feature/ProtectedContent/ProtectedContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,8 @@ public function __construct() {
public function setup() {
add_filter( 'ep_indexable_post_status', [ $this, 'get_statuses' ] );
add_filter( 'ep_indexable_post_types', [ $this, 'post_types' ], 10, 1 );

if ( is_admin() ) {
add_filter( 'ep_admin_wp_query_integration', '__return_true' );
add_action( 'pre_get_posts', [ $this, 'integrate' ] );
}
add_filter( 'ep_admin_wp_query_integration', '__return_true' );
add_action( 'pre_get_posts', [ $this, 'integrate' ] );
}

/**
Expand Down Expand Up @@ -106,6 +103,9 @@ public function post_types( $post_types ) {
* @since 2.1
*/
public function integrate( $query ) {
if ( ! Utils\is_integrated_request( false ) ) {
return;
}

// Lets make sure this doesn't interfere with the CLI
if ( defined( 'WP_CLI' ) && WP_CLI ) {
Expand Down Expand Up @@ -228,4 +228,3 @@ public function requirements_status() {
return $status;
}
}

44 changes: 6 additions & 38 deletions includes/classes/Feature/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use ElasticPress\Feature as Feature;
use ElasticPress\Indexables as Indexables;
use ElasticPress\Utils as Utils;

/**
* Search feature class
Expand Down Expand Up @@ -88,40 +89,6 @@ public function setup() {
* @since 3.0
*/
public function search_setup() {
/**
* By default EP will not integrate on admin or ajax requests. Since admin-ajax.php is
* technically an admin request, there is some weird logic here. If we are doing ajax
* and ep_ajax_wp_query_integration is filtered true, then we skip the next admin check.
*/

/**
* Filter to integrate with admin queries
*
* @hook ep_admin_wp_query_integration
* @param {bool} $integrate True to integrate
* @return {bool} New value
*/
$admin_integration = apply_filters( 'ep_admin_wp_query_integration', false );

if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
/**
* Filter to integrate with admin ajax queries
*
* @hook ep_ajax_wp_query_integration
* @param {bool} $integrate True to integrate
* @return {bool} New value
*/
if ( ! apply_filters( 'ep_ajax_wp_query_integration', false ) ) {
return;
} else {
$admin_integration = true;
}
}

if ( is_admin() && ! $admin_integration ) {
return;
}

add_filter( 'ep_elasticpress_enabled', [ $this, 'integrate_search_queries' ], 10, 2 );
add_filter( 'ep_formatted_args', [ $this, 'weight_recent' ], 11, 2 );
add_filter( 'ep_query_post_type', [ $this, 'filter_query_post_type_for_search' ], 10, 2 );
Expand Down Expand Up @@ -194,9 +161,6 @@ public function add_search_highlight_tags( $formatted_args, $args ) {
return $formatted_args;
}

/** This filter is documented in search_setup() method. */
$should_send_in_ajax = ( defined( 'DOING_AJAX' ) && DOING_AJAX ) && apply_filters( 'ep_ajax_wp_query_integration', false );

/**
* Filter whether to add the `highlight` clause in the query or not.
*
Expand All @@ -210,7 +174,7 @@ public function add_search_highlight_tags( $formatted_args, $args ) {
$add_highlight_clause = apply_filters(
'ep_highlight_should_add_clause',
(
( ! is_admin() || $should_send_in_ajax ) &&
( Utils\is_integrated_request( false ) ) &&
( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST )
),
$formatted_args,
Expand Down Expand Up @@ -607,6 +571,10 @@ public function output_feature_box_long() {
* @return bool
*/
public function integrate_search_queries( $enabled, $query ) {
if ( ! Utils\is_integrated_request() ) {
return;
}

if ( ! is_a( $query, 'WP_Query' ) ) {
return $enabled;
}
Expand Down
3 changes: 2 additions & 1 deletion includes/classes/Feature/Search/Weighting.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use ElasticPress\Features;
use ElasticPress\Indexable\Post\Post;
use ElasticPress\Utils as Utils;

/**
* Controls search weighting and search fields dashboard
Expand Down Expand Up @@ -564,7 +565,7 @@ public function do_weighting( $formatted_args, $args ) {
*/
$weight_config = apply_filters( 'ep_weighting_configuration_for_search', $weight_config, $args );

if ( ! is_admin() && ! empty( $args['s'] ) ) {
if ( Utils\is_integrated_request() && ! empty( $args['s'] ) ) {
/*
* This section splits up the single query clause for all post types into separate nested clauses (one for each post type)
* which then get combined into one result set. By having separate clauses for each post type, we can then
Expand Down
27 changes: 2 additions & 25 deletions includes/classes/Feature/WooCommerce/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use ElasticPress\Feature as Feature;
use ElasticPress\FeatureRequirementsStatus as FeatureRequirementsStatus;
use ElasticPress\Indexables as Indexables;
use ElasticPress\Utils as Utils;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
Expand Down Expand Up @@ -950,31 +951,7 @@ protected function should_integrate_with_query( $query ) {
return false;
}

/**
* Filter to integrate with admin queries
*
* @hook ep_admin_wp_query_integration
* @param {bool} $integrate True to integrate
* @return {bool} New value
*/
$admin_integration = apply_filters( 'ep_admin_wp_query_integration', false );

if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
/**
* Filter to integrate with admin ajax queries
*
* @hook ep_ajax_wp_query_integration
* @param {bool} $integrate True to integrate
* @return {bool} New value
*/
if ( ! apply_filters( 'ep_ajax_wp_query_integration', false ) ) {
return false;
} else {
$admin_integration = true;
}
}

if ( is_admin() && ! $admin_integration ) {
if ( ! Utils\is_integrated_request() ) {
return false;
}

Expand Down
66 changes: 66 additions & 0 deletions includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,69 @@ function get_indexing_status() {
return $index_status;

}

/**
* Check if queries for the current request should not be integrated with
* ElasticPress.
*
* Requests should not be integrated in the admin unless admin integration
* is enabled via the `ep_admin_wp_query_integration` filter, or they are
* admin-ajax.php requests and integration with AJAX requests has been enabled
* with the `ep_ajax_wp_query_integration` filter.
*
* @param bool $supports_admin Whether the check supports optional admin
* integration.
* @return bool Whether the request supports ElasticPress integration.
*
* @since 3.6.0
*/
function is_integrated_request( $supports_admin = true ) {
$is_admin_request = is_admin();
$is_ajax_request = defined( 'DOING_AJAX' ) && DOING_AJAX;
$is_public_request = ! is_admin();
$is_integrated_admin_request = false;
$is_integrated_ajax_request = false;

if ( $is_admin_request && ! $is_ajax_request && $supports_admin ) {

/**
* Filter whether to integrate with admin queries.
*
* @hook ep_admin_wp_query_integration
* @param bool $integrate True to integrate.
* @return bool New value.
*/
$is_integrated_admin_request = apply_filters( 'ep_admin_wp_query_integration', false );
}

if ( $is_ajax_request ) {

/**
* Filter to integrate with admin ajax queries.
*
* @hook ep_ajax_wp_query_integration
* @param bool $integrate True to integrate.
* @return bool New value.
*/
$is_integrated_ajax_request = apply_filters( 'ep_ajax_wp_query_integration', false );
}

/**
* Is the current request any of the supported requests.
*/
$is_integrated_request = $is_public_request || $is_integrated_admin_request || $is_integrated_ajax_request;

/**
* Filter whether the queries for the current request should integrate.
*
* @hook ep_is_integrated_request
* @param bool $is_integrated_request True for admin requests.
* @param bool $support_admin_integration Whether the check supported admin integration.
* @param bool $admin Whether the check supports admin integration.
* @param bool $ajax Whether the check supports AJAX integration.
* @return bool New value
*
* @since 3.6.0
*/
return apply_filters( 'ep_is_integrated_request', $is_integrated_request, $supports_admin );
}

0 comments on commit 1726bcd

Please sign in to comment.