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

New ep_facet_should_check_if_allowed filter #3430

Merged
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
27 changes: 25 additions & 2 deletions includes/classes/Feature/Facets/Types/Meta/FacetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,35 @@ public function add_query_filters( $filters ) {
$feature = Features::factory()->get_registered_feature( 'facets' );

$selected_filters = $feature->get_selected();

if ( empty( $selected_filters ) || empty( $selected_filters[ $this->get_filter_type() ] ) ) {
return $filters;
}

$meta_fields = $selected_filters[ $this->get_filter_type() ];
$match_type = $feature->get_match_type();
/**
* Filter if EP should only filter by fields selected in facets. Defaults to true.
*
* @since 4.5.1
* @hook ep_facet_should_check_if_allowed
* @param {bool} $should_check Whether it should or not check fields
* @return {string} New value
*/
$should_check_if_allowed = apply_filters( 'ep_facet_should_check_if_allowed', true );
if ( $should_check_if_allowed ) {
$allowed_meta_fields = $this->get_facets_meta_fields();

$meta_fields = array_filter(
$selected_filters[ $this->get_filter_type() ],
function ( $meta_field ) use ( $allowed_meta_fields ) {
return in_array( $meta_field, $allowed_meta_fields, true );
},
ARRAY_FILTER_USE_KEY
);
} else {
$meta_fields = $selected_filters[ $this->get_filter_type() ];
}

$match_type = $feature->get_match_type();

foreach ( $meta_fields as $meta_field => $values ) {
if ( 'any' === $match_type ) {
Expand Down
23 changes: 22 additions & 1 deletion includes/classes/Feature/Facets/Types/MetaRange/FacetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,28 @@ public function add_query_filters( $filters, $query_args = [] ) {
return $filters;
}

$selected_range_filters = $all_selected_filters[ $this->get_filter_type() ];
/**
* Filter if EP should only filter by fields selected in facets. Defaults to true.
*
* @since 4.5.1
* @hook ep_facet_should_check_if_allowed
* @param {bool} $should_check Whether it should or not check fields
* @return {string} New value
*/
$should_check_if_allowed = apply_filters( 'ep_facet_should_check_if_allowed', true );
if ( $should_check_if_allowed ) {
$allowed_meta_fields = $this->get_facets_meta_fields();

$selected_range_filters = array_filter(
$all_selected_filters[ $this->get_filter_type() ],
function ( $meta_field ) use ( $allowed_meta_fields ) {
return in_array( $meta_field, $allowed_meta_fields, true );
},
ARRAY_FILTER_USE_KEY
);
} else {
$selected_range_filters = $all_selected_filters[ $this->get_filter_type() ];
}

$range_filters = [];
foreach ( $selected_range_filters as $field_name => $values ) {
Expand Down
62 changes: 62 additions & 0 deletions tests/php/features/TestFacetTypeMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ public function testAddQueryFilters() {
$facet_feature = Features::factory()->get_registered_feature( 'facets' );
$facet_type = $facet_feature->types['meta'];

$allow_field = function ( $fields ) {
$fields[] = 'my_custom_field';
return $fields;
};
add_filter( 'ep_facet_meta_fields', $allow_field );

parse_str( 'ep_meta_filter_my_custom_field=dolor,amet', $_GET );

$new_filters = $facet_type->add_query_filters( [] );
Expand Down Expand Up @@ -248,6 +254,62 @@ public function testAddQueryFilters() {
$this->assertSame( $expected, $new_filters );
}

/**
* Test add_query_filters with not allowed parameters
*
* @since 4.5.1
* @group facets
*/
public function testAddQueryFiltersWithNotAllowedParameters() {
$facet_feature = Features::factory()->get_registered_feature( 'facets' );
$facet_type = $facet_feature->types['meta'];

$allow_field = function ( $fields ) {
$fields[] = 'my_custom_field';
return $fields;
};
add_filter( 'ep_facet_meta_fields', $allow_field );

parse_str( 'ep_meta_filter_my_custom_field=dolor,amet&ep_meta_filter_not_allowed=1', $_GET );

$new_filters = $facet_type->add_query_filters( [] );
$expected = [
[
'term' => [
'meta.my_custom_field.raw' => 'dolor',
],
],
[
'term' => [
'meta.my_custom_field.raw' => 'amet',
],
],
];
$this->assertSame( $expected, $new_filters );

add_filter( 'ep_facet_should_check_if_allowed', '__return_false' );

$new_filters = $facet_type->add_query_filters( [] );
$expected = [
[
'term' => [
'meta.my_custom_field.raw' => 'dolor',
],
],
[
'term' => [
'meta.my_custom_field.raw' => 'amet',
],
],
[
'term' => [
'meta.not_allowed.raw' => 1,
],
],
];
$this->assertSame( $expected, $new_filters );
}

/**
* Test get_facets_meta_fields
*
Expand Down
60 changes: 60 additions & 0 deletions tests/php/features/TestFacetTypeMetaRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public function testGetFilterType() {
* @group facets
*/
public function testAddQueryFilters() {
$allow_field = function ( $fields ) {
$fields[] = 'my_custom_field';
return $fields;
};
add_filter( 'ep_facet_meta_range_fields', $allow_field );

parse_str( 'ep_meta_range_filter_my_custom_field_min=5&ep_meta_range_filter_my_custom_field_max=25', $_GET );

$new_filters = $this->facet_type->add_query_filters( [] );
Expand All @@ -114,6 +120,60 @@ public function testAddQueryFilters() {
$this->assertSame( [], $new_filters );
}

/**
* Test add_query_filters with not allowed parameters
*
* @since 4.5.1
* @group facets
*/
public function testAddQueryFiltersWithNotAllowedParameters() {
$allow_field = function ( $fields ) {
$fields[] = 'my_custom_field';
return $fields;
};
add_filter( 'ep_facet_meta_range_fields', $allow_field );

parse_str( 'ep_meta_range_filter_my_custom_field_min=5&ep_meta_range_filter_my_custom_field_max=25&ep_meta_range_filter_not_allowed_min=5&ep_meta_range_filter_not_allowed_max=25', $_GET );

// Should not have `not_allowed` yet
$new_filters = $this->facet_type->add_query_filters( [] );
$expected = [
[
'range' => [
'meta.my_custom_field.double' => [
'gte' => floatval( 5 ),
'lte' => floatval( 25 ),
],
],
],
];
$this->assertSame( $expected, $new_filters );

add_filter( 'ep_facet_should_check_if_allowed', '__return_false' );

// As we are not checking, it should have `not_allowed` now
$new_filters = $this->facet_type->add_query_filters( [] );
$expected = [
[
'range' => [
'meta.my_custom_field.double' => [
'gte' => floatval( 5 ),
'lte' => floatval( 25 ),
],
],
],
[
'range' => [
'meta.not_allowed.double' => [
'gte' => floatval( 5 ),
'lte' => floatval( 25 ),
],
],
],
];
$this->assertSame( $expected, $new_filters );
}

/**
* Test set_wp_query_aggs
*
Expand Down