Skip to content

Commit

Permalink
Merge pull request #3953 from 10up/introduce-new-filter-in-get_selected
Browse files Browse the repository at this point in the history
Add hook for filtering selected filters in Facets.php
  • Loading branch information
felipeelia committed Aug 23, 2024
2 parents dcfc7fd + 4d2408a commit e64faf3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
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

0 comments on commit e64faf3

Please sign in to comment.