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 new settings to set Match Type between Facets #3092

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
43 changes: 39 additions & 4 deletions includes/classes/Feature/Facets/Facets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -125,11 +126,20 @@ public function output_feature_box_settings() {
$settings = wp_parse_args( $settings, $this->default_settings );
?>
<div class="field">
<div class="field-name status"><?php esc_html_e( 'Match Type', 'elasticpress' ); ?></div>
<div class="field-name status"><?php esc_html_e( 'Match Type Within Facets', 'elasticpress' ); ?></div>
<div class="input-wrap">
<label><input name="settings[match_type]" type="radio" <?php checked( $settings['match_type'], 'all' ); ?> value="all"><?php echo wp_kses_post( __( 'Show any content tagged to <strong>all</strong> selected terms', 'elasticpress' ) ); ?></label><br>
<label><input name="settings[match_type]" type="radio" <?php checked( $settings['match_type'], 'any' ); ?> value="any"><?php echo wp_kses_post( __( 'Show all content tagged to <strong>any</strong> selected term', 'elasticpress' ) ); ?></label>
<p class="field-description"><?php esc_html_e( '"All" will only show content that matches all facets. "Any" will show content that matches any facet.', 'elasticpress' ); ?></p>
<p class="field-description"><?php esc_html_e( '"All" will only show content that matches within facets. "Any" will show content that matches within facet.', 'elasticpress' ); ?></p>
</div>
</div>

<div class="field">
<div class="field-name status"><?php esc_html_e( 'Match Type Between Facets', 'elasticpress' ); ?></div>
<div class="input-wrap">
<label><input name="settings[match_type_between]" type="radio" <?php checked( $settings['match_type_between'], 'all' ); ?> value="all"><?php echo wp_kses_post( __( 'Show any content tagged to <strong>all</strong> selected terms', 'elasticpress' ) ); ?></label><br>
<label><input name="settings[match_type_between]" type="radio" <?php checked( $settings['match_type_between'], 'any' ); ?> value="any"><?php echo wp_kses_post( __( 'Show all content tagged to <strong>any</strong> selected term', 'elasticpress' ) ); ?></label>
<p class="field-description"><?php esc_html_e( '"All" will only show content that matches between facets. "Any" will show content that matches between facet.', 'elasticpress' ); ?></p>
</div>
</div>
<?php
Expand Down Expand Up @@ -579,7 +589,7 @@ public function apply_facets_filters( $filters, $args, $query ) {
return $filters;
}

$es_operator = ( 'any' === $this->get_match_type() ) ? 'should' : 'must';
$es_operator = ( 'any' === $this->get_match_type_between() ) ? 'should' : 'must';

$filters['facets'] = [
'bool' => [
Expand Down Expand Up @@ -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.
*
Expand Down
5 changes: 3 additions & 2 deletions includes/classes/Feature/Facets/Types/Taxonomy/FacetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
);

Expand 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';
}

Expand Down
15 changes: 15 additions & 0 deletions tests/cypress/integration/features/facets.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

/**
Expand Down
1 change: 1 addition & 0 deletions tests/php/features/TestFacet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down