Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Readd the ep_facet_adding_agg_filters flag + unit test #3368

Merged
merged 1 commit into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions includes/classes/Feature/Facets/Facets.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ public function set_agg_filters( $args, $query_args, $query ) {
add_filter( 'ep_post_filters', [ $this, 'remove_facets_filter' ], 11 );
}

/**
* This flag is used to differentiate filters being applied to the query and to its aggregations.
*/
$query_args['ep_facet_adding_agg_filters'] = true;

/**
* Filter WP query arguments that will be used to build the aggregations filter.
*
Expand Down
30 changes: 29 additions & 1 deletion tests/php/features/TestFacet.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function testSetAggFilter() {

/**
* Without any function hooked to `ep_facet_agg_filters` we expect
* aggregation filters to matche exactly the filter applied to the main
* aggregation filters to match exactly the filter applied to the main
* query.
*/
remove_all_filters( 'ep_facet_agg_filters' );
Expand All @@ -219,6 +219,34 @@ public function testSetAggFilter() {
$this->assertSame( $formatted_args['post_filter'], $formatted_args_with_args['aggs']['terms']['filter'] );
}

/**
* Test if the `ep_facet_adding_agg_filters` flag is set in `set_agg_filters`
*
* @since 4.5.0
* @group facets
*/
public function testSetAggFilterAddingAggFiltersFlag() {
$facet_feature = Features::factory()->get_registered_feature( 'facets' );

$query_args = [
'ep_facet' => 1,
'post_type' => 'post',
'post_status' => 'publish',
];

$check_flag = function ( $query_args ) {
$this->assertTrue( $query_args['ep_facet_adding_agg_filters'] );
return $query_args;
};
add_filter( 'ep_facet_agg_filters', $check_flag );

$previous_filter_count = did_filter( 'ep_facet_agg_filters' );
$facet_feature->set_agg_filters( [], $query_args, new \WP_Query() );
$current_filter_count = did_filter( 'ep_facet_agg_filters' );

$this->assertGreaterThan( $previous_filter_count, $current_filter_count );
}

/**
* Test apply_facets_filters
*
Expand Down