diff --git a/includes/classes/Feature/Facets/Facets.php b/includes/classes/Feature/Facets/Facets.php index 84d3f105b1..d9b4868394 100644 --- a/includes/classes/Feature/Facets/Facets.php +++ b/includes/classes/Feature/Facets/Facets.php @@ -46,7 +46,8 @@ public function __construct() { $this->requires_install_reindex = false; $this->default_settings = [ - 'match_type' => 'all', + 'match_type' => 'all', + 'match_type_between' => 'all', ]; $types = [ @@ -125,11 +126,20 @@ public function output_feature_box_settings() { $settings = wp_parse_args( $settings, $this->default_settings ); ?>
-
+

-

+

+
+
+ +
+
+
+
+ +

get_match_type() ) ? 'should' : 'must'; + $es_operator = ( 'any' === $this->get_match_type_between() ) ? 'should' : 'must'; $filters['facets'] = [ 'bool' => [ @@ -615,6 +625,31 @@ public function get_match_type() { return apply_filters( 'ep_facet_match_type', $settings['match_type'] ); } + /** + * Utilitary function to retrieve the match type between different facet selected by the user. + * + * @since 4.4.0 + * @return string + */ + public function get_match_type_between() : string { + $settings = wp_parse_args( + $this->get_settings(), + array( + 'match_type_between' => 'all', + ) + ); + + /** + * Filter the match type between facets. Can be 'all' or 'any'. + * + * @hook ep_facet_match_type_between + * @since 4.4.0 + * @param {string} $match_type Current selection + * @return {string} New selection + */ + return apply_filters( 'ep_facet_match_type_between', $settings['match_type_between'] ); + } + /** * Given an array of filters, remove the facets filter. * diff --git a/includes/classes/Feature/Facets/Types/Taxonomy/FacetType.php b/includes/classes/Feature/Facets/Types/Taxonomy/FacetType.php index 14805eaaaa..eb6cfb2502 100644 --- a/includes/classes/Feature/Facets/Types/Taxonomy/FacetType.php +++ b/includes/classes/Feature/Facets/Types/Taxonomy/FacetType.php @@ -166,7 +166,8 @@ public function facet_query( $query ) { $settings = wp_parse_args( $settings, array( - 'match_type' => 'all', + 'match_type' => 'all', + 'match_type_between' => 'all', ) ); @@ -191,7 +192,7 @@ public function facet_query( $query ) { ]; } - if ( ! empty( $selected_filters['taxonomies'] ) && 'any' === $settings['match_type'] ) { + if ( ! empty( $selected_filters['taxonomies'] ) && 'any' === $settings['match_type_between'] ) { $tax_query['relation'] = 'or'; } diff --git a/tests/cypress/integration/features/facets.cy.js b/tests/cypress/integration/features/facets.cy.js index 8a8c6f2ab7..ebbe50484c 100644 --- a/tests/cypress/integration/features/facets.cy.js +++ b/tests/cypress/integration/features/facets.cy.js @@ -152,6 +152,21 @@ describe('Facets Feature', () => { cy.get('@secondBlock').contains('.term', 'template').click(); cy.url().should('not.include', 'ep_filter_post_tag=template'); cy.url().should('include', 'ep_filter_category=classic'); + + /** + * Set the Match Type Between Facet to "Any" and verify that the results are more than 1. + */ + cy.visitAdminPage('admin.php?page=elasticpress'); + + cy.get('.ep-feature-facets .settings-button').click(); + cy.get("input[name='settings[match_type_between]'").check('any'); + cy.get('.ep-feature-facets .button-primary').click(); + + cy.visit('/'); + cy.get('@firstBlock').contains('.term', 'Uncategorized').click(); + cy.get('@secondBlock').contains('.term', 'read more').click(); + cy.url().should('include', 'ep_filter_category=uncategorized&ep_filter_post_tag=read-more'); + cy.get('.entry-title').its('length').should('be.gte', 1); }); /** diff --git a/tests/php/features/TestFacet.php b/tests/php/features/TestFacet.php index b4af7e607f..b1511e2ef9 100644 --- a/tests/php/features/TestFacet.php +++ b/tests/php/features/TestFacet.php @@ -261,6 +261,7 @@ public function testApplyFacetsFilters() { return 'any'; }; add_filter( 'ep_facet_match_type', $change_match_type ); + add_filter( 'ep_facet_match_type_between', $change_match_type ); $new_filters = $facet_feature->apply_facets_filters( [], [], new \WP_Query( [] ) ); $expected_filter = [