From 51b34f498e239acb67b1d6d69a1654c25a5eb307 Mon Sep 17 00:00:00 2001 From: Rebecca Hum <16962021+rebeccahum@users.noreply.github.com> Date: Thu, 30 Jun 2022 12:31:17 -0600 Subject: [PATCH] Search: Add filter filter_ep_enable_do_weighting --- search/includes/classes/class-search.php | 50 +++++++++++++++++++ .../includes/classes/test-class-search.php | 33 ++++++++++++ 2 files changed, 83 insertions(+) diff --git a/search/includes/classes/class-search.php b/search/includes/classes/class-search.php index c8649418bf..3d19720d33 100644 --- a/search/includes/classes/class-search.php +++ b/search/includes/classes/class-search.php @@ -640,6 +640,8 @@ protected function setup_hooks() { add_filter( 'ep_elasticsearch_version', [ $this, 'fallback_elasticsearch_version' ], PHP_INT_MAX, 1 ); add_filter( 'ep_es_info_cache_expiration', [ $this, 'filter__es_info_cache_expiration' ], PHP_INT_MAX, 1 ); + + add_filter( 'ep_enable_do_weighting', [ $this, 'filter_ep_enable_do_weighting' ], PHP_INT_MAX, 4 ); } protected function load_commands() { @@ -2330,4 +2332,52 @@ public function fallback_elasticsearch_version( $version ) { public function filter__es_info_cache_expiration( $time ) { return wp_rand( 24 * HOUR_IN_SECONDS, 36 * HOUR_IN_SECONDS ); } + + /** + * A filter that only enables weighting if it is needed. E.g. When weightings are set by UI or filtered. + * + * @param bool Whether to enable weight config, defaults to true for search requests that are public or REST + * @param array $weight_config Current weight config + * @param array $args WP Query arguments + * @param array $formatted_args Formatted ES arguments + * @return bool $should_do_weighting New value on whether to enable weight config + */ + public function filter_ep_enable_do_weighting( $should_do_weighting, $weight_config, $args, $formatted_args ) { + if ( defined( 'VIP_GO_APP_ENVIRONMENT' ) && 'production' === constant( VIP_GO_APP_ENVIRONMENT ) ) { + // Rollout to non-prod only for now, otherwise skip if production. + return $should_do_weighting; + } + + if ( ! empty( $weight_config ) ) { + return true; + } + + global $wp_filter; + + $ep_filters = [ + 'ep_weighting_configuration_for_search', + 'ep_query_weighting_fields', + 'ep_weighting_configuration', + 'ep_weighting_fields_for_post_type', + ]; + + foreach ( $ep_filters as $ep_filter ) { + if ( isset( $wp_filter[ $ep_filter ] ) ) { + foreach ( $wp_filter[ $ep_filter ]->callbacks as $callback ) { + foreach ( $callback as $el ) { + if ( $el['function'] instanceof \Closure ) { + return true; + } else { + $class = get_class( $el['function'][0] ); + if ( false === strpos( $class, 'ElasticPress\Feature' ) ) { + return true; + } + } + } + } + } + } + + return false; + } } diff --git a/tests/search/includes/classes/test-class-search.php b/tests/search/includes/classes/test-class-search.php index 9b1368647f..c1cc087531 100644 --- a/tests/search/includes/classes/test-class-search.php +++ b/tests/search/includes/classes/test-class-search.php @@ -3107,6 +3107,39 @@ public function test__are_es_constants_defined__no_endpoints() { $this->assertFalse( $result ); } + public function test__filter_ep_enable_do_weighting__default_no_weighting() { + $this->search_instance->init(); + + $this->assertFalse( apply_filters( 'ep_enable_do_weighting', true, [], [], [] ) ); + } + + public function test__filter_ep_enable_do_weighting__anonymous_function() { + $this->search_instance->init(); + + add_filter( + 'ep_weighting_configuration_for_search', + function( $weight_config ) { + return $weight_config; + } + ); + + $this->assertTrue( apply_filters( 'ep_enable_do_weighting', true, [], [], [] ) ); + } + + public function test__filter_ep_enable_do_weighting__class_function() { + $this->search_instance->init(); + + add_filter( 'ep_weighting_configuration_for_search', [ $this, 'some_function' ] ); + + $this->assertTrue( apply_filters( 'ep_enable_do_weighting', true, [], [], [] ) ); + } + + public function test__filter_ep_enable_do_weighting__weight_config() { + $this->search_instance->init(); + + $this->assertTrue( apply_filters( 'ep_enable_do_weighting', true, [ 'foo' => 'bar' ], [], [] ) ); + } + /** * Helper function for accessing protected methods. */