Skip to content

Commit

Permalink
Merge pull request #4067 from 10up/feature-3813/add-new-filter
Browse files Browse the repository at this point in the history
Add filter to filter Instant Results taxonomy facet terms
  • Loading branch information
felipeelia authored Feb 4, 2025
2 parents 95726c4 + d3df3dc commit c70c903
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
1 change: 1 addition & 0 deletions .wp-env.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"wp-content/plugins/enable-debug-bar.php": "./tests/cypress/wordpress-files/test-plugins/enable-debug-bar.php",
"wp-content/plugins/fake-log-messages.php": "./tests/cypress/wordpress-files/test-plugins/fake-log-messages.php",
"wp-content/plugins/fake-new-activation.php": "./tests/cypress/wordpress-files/test-plugins/fake-new-activation.php",
"wp-content/plugins/filter-instant-results-category-terms.php": "./tests/cypress/wordpress-files/test-plugins/filter-instant-results-category-terms.php",
"wp-content/plugins/filter-instant-results-per-page.php": "./tests/cypress/wordpress-files/test-plugins/filter-instant-results-per-page.php",
"wp-content/plugins/filter-instant-results-args-schema.php": "./tests/cypress/wordpress-files/test-plugins/filter-instant-results-args-schema.php",
"wp-content/plugins/filter-autosuggest-navigate-callback.php": "./tests/cypress/wordpress-files/test-plugins/filter-autosuggest-navigate-callback.php",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { useCallback, useMemo, WPElement } from '@wordpress/element';
import { decodeEntities } from '@wordpress/html-entities';
import { __, sprintf } from '@wordpress/i18n';
import { applyFilters } from '@wordpress/hooks';

/**
* Internal dependencies.
Expand Down Expand Up @@ -81,7 +82,25 @@ export default ({ defaultIsOpen, label, postTypes, name }) => {
/**
* Reduce buckets to options.
*/
const options = useMemo(() => buckets.reduce(reduceOptions, []), [buckets, reduceOptions]);
const options = useMemo(() => {
/**
* Filter the taxonomy filter terms.
*
* @filter ep.InstantResults.filter.taxonomy.terms
* @since 5.2.0
*
* @param {object[]} terms Taxonomy terms.
* @param {string} name Taxonomy name.
* @param {Array} postTypes Post types label.
* @returns {object[]} Filtered taxonomy terms.
*/
return applyFilters(
'ep.InstantResults.filter.taxonomy.terms',
buckets.reduce(reduceOptions, []),
name,
postTypes,
);
}, [buckets, reduceOptions, name, postTypes]);

/**
* Reduce options to labels.
Expand Down
36 changes: 36 additions & 0 deletions tests/cypress/integration/features/instant-results.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,42 @@ describe('Instant Results Feature', { tags: '@slow' }, () => {
cy.wait('@apiRequest');
cy.url().should('include', 'post_type=product');
});

it('Is possible to filter the taxonomy terms', () => {
/**
* Activate test plugin.
*/
cy.maybeEnableFeature('instant-results');
cy.activatePlugin('filter-instant-results-category-terms', 'wpCli');

cy.visitAdminPage('admin.php?page=elasticpress');
cy.intercept('/wp-json/elasticpress/v1/features*').as('apiRequest');

cy.contains('button', 'Instant Results').click();
cy.get('.components-form-token-field__input').type(
'{backspace}{backspace}{backspace}(category){downArrow}{enter}{esc}',
);
cy.contains('button', 'Save changes').click();

cy.wait('@apiRequest');

/**
* Perform a search.
*/
cy.intercept('*search=block*').as('apiRequest');
cy.visit('/');
cy.get('.wp-block-search').first().as('searchBlock');
cy.get('@searchBlock').find('input[type="search"]').type('block');
cy.get('@searchBlock').find('button').click();
cy.wait('@apiRequest');

/**
* The number of terms displayed in the filter should be one.
*/
cy.get('[id^="ep-search-tax-category-"]').should('have.length', 1);

cy.deactivatePlugin('filter-instant-results-category-terms', 'wpCli');
});
});

it('Is possible to filter the arguments schema', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Plugin Name: Filter Instant Results Taxonomy Terms
* Description: Filters the Instant Results taxonomy terms for test purposes.
* Version: 1.0.0
* Author: 10up Inc.
* License: GPLv2 or later
*
* @package ElasticPress_Tests_E2e
*/

/**
* Limit the Instant Results terms to only the "Classic" term.
*/
add_action(
'wp_footer',
function (): void {
?>
<script>
document.addEventListener('DOMContentLoaded', function() {
const filterCategoryTerms = (terms, taxonomyName) => {

if (taxonomyName !== 'tax-category') {
return terms;
}

// keep only Term Classic
const filteredTerms = terms.filter(term => term.label === 'Classic');
return filteredTerms;
}

wp.hooks.addFilter('ep.InstantResults.filter.taxonomy.terms', 'ep-test', filterCategoryTerms);
});
</script>
<?php
}
);

0 comments on commit c70c903

Please sign in to comment.