Skip to content

Commit

Permalink
Search: Add filter filter_ep_enable_do_weighting (#3340)
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccahum authored Jul 6, 2022
1 parent 0bba658 commit 608c2f0
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
50 changes: 50 additions & 0 deletions search/includes/classes/class-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
}
}
33 changes: 33 additions & 0 deletions tests/search/includes/classes/test-class-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit 608c2f0

Please sign in to comment.