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 filter to filter Instant Results taxonomy facet terms #4067

Merged
merged 8 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
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-facet-terms.php": "./tests/cypress/wordpress-files/test-plugins/filter-instant-results-facet-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 facet taxonomy terms.
*
* @filter ep.InstantResults.facet.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.facet.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-facet-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 facet should be one.
*/
cy.get('[id^="ep-search-tax-category-"]').should('have.length', 1);

cy.deactivatePlugin('filter-instant-results-facet-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 facet 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.facet.taxonomy.terms', 'ep-test', filterCategoryTerms);
});
</script>
<?php
}
);
Loading