Skip to content

Commit

Permalink
Search: Add filter filter_ep_enable_do_weighting
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccahum committed Jul 6, 2022
1 parent 1f7faef commit d2498c4
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 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,47 @@ 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 ( ! 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;
}
}

0 comments on commit d2498c4

Please sign in to comment.