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

Add hook for filtering selected filters in Facets.php #3953

Merged
merged 7 commits into from
Aug 23, 2024
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
11 changes: 9 additions & 2 deletions includes/classes/Feature/Facets/Facets.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,15 @@ public function get_selected() {
}
}

return $filters;
/**
* Filter selected filters.
*
* @hook ep_facet_selected_filters
* @since 5.2.0
* @param {array} $filters Current filters
* @return {array} New filters
*/
return apply_filters( 'ep_facet_selected_filters', $filters );
}

/**
Expand Down Expand Up @@ -608,7 +616,6 @@ public function get_facetable_taxonomies() {
_deprecated_function( __METHOD__, '4.3.0', "\ElasticPress\Features::factory()->get_registered_feature( 'facets' )->types['taxonomy']->get_facetable_taxonomies()" );

return $this->types['taxonomy']->get_filter_name();

}

/**
Expand Down
28 changes: 28 additions & 0 deletions tests/php/features/TestFacet.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,34 @@ public function test_get_settings_schema() {
);
}

/**
* Test ep_facet_selected_filters filter.
*
* @since 5.2.0
* @group facets
*/
public function test_ep_facet_selected_filters() {
$facet_feature = Features::factory()->get_registered_feature( 'facets' );

parse_str( 'ep_filter_taxonomy=dolor,sit', $_GET );

$add_prefix_with_terms = function( $filters ) {
$new_terms = [];
foreach ( $filters['taxonomies']['taxonomy']['terms'] as $key => $value ) {
$new_terms[ 'cap-' . $key ] = $value;
}

$filters['taxonomies']['taxonomy']['terms'] = $new_terms;
return $filters;
};
add_filter( 'ep_facet_selected_filters', $add_prefix_with_terms );

$selected = $facet_feature->get_selected();
foreach ( $selected['taxonomies']['taxonomy']['terms'] as $key => $value ) {
$this->assertStringStartsWith( 'cap-', $key );
}
}

/**
* Utilitary function for the testGetSelected test.
*
Expand Down
Loading