From 3dbf6c60f2e2f37be9c085e8c283239f3825a85d Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Mon, 27 Jan 2025 13:56:39 +0500 Subject: [PATCH 1/5] Add filter to filter Instant Resutls taxonomy facet terms --- .../components/facets/taxonomy-terms-facet.js | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/assets/js/instant-results/components/facets/taxonomy-terms-facet.js b/assets/js/instant-results/components/facets/taxonomy-terms-facet.js index 7fad81b2f..50c0a7896 100644 --- a/assets/js/instant-results/components/facets/taxonomy-terms-facet.js +++ b/assets/js/instant-results/components/facets/taxonomy-terms-facet.js @@ -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. @@ -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. From 8a710d78301f5417eef84e640f60f8478407abe5 Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Mon, 27 Jan 2025 15:36:38 +0500 Subject: [PATCH 2/5] Add tests --- .../features/instant-results.cy.js | 25 +++++++++++++ .../filter-instant-results-facet-terms.php | 37 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 tests/cypress/wordpress-files/test-plugins/filter-instant-results-facet-terms.php diff --git a/tests/cypress/integration/features/instant-results.cy.js b/tests/cypress/integration/features/instant-results.cy.js index 30963fae0..969ebc31d 100644 --- a/tests/cypress/integration/features/instant-results.cy.js +++ b/tests/cypress/integration/features/instant-results.cy.js @@ -468,5 +468,30 @@ describe('Instant Results Feature', { tags: '@slow' }, () => { */ cy.get('.ep-search-sort :selected').should('contain.text', 'Date, newest to oldest'); }); + + it('Is possible to filter the taxonomy terms', () => { + /** + * Activate test plugin. + */ + cy.maybeEnableFeature('instant-results'); + cy.activatePlugin('filter-instant-results-facet-terms', 'wpCli'); + + /** + * Perform a search. + */ + cy.intercept('*search=block*').as('apiRequest'); + cy.visit('/'); + cy.get('.wp-block-search').last().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'); + }); }); }); diff --git a/tests/cypress/wordpress-files/test-plugins/filter-instant-results-facet-terms.php b/tests/cypress/wordpress-files/test-plugins/filter-instant-results-facet-terms.php new file mode 100644 index 000000000..41fb09f06 --- /dev/null +++ b/tests/cypress/wordpress-files/test-plugins/filter-instant-results-facet-terms.php @@ -0,0 +1,37 @@ + + + Date: Mon, 27 Jan 2025 16:11:20 +0500 Subject: [PATCH 3/5] Fix tests --- .../features/instant-results.cy.js | 61 +++++++++++-------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/tests/cypress/integration/features/instant-results.cy.js b/tests/cypress/integration/features/instant-results.cy.js index 969ebc31d..197c03296 100644 --- a/tests/cypress/integration/features/instant-results.cy.js +++ b/tests/cypress/integration/features/instant-results.cy.js @@ -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', () => { @@ -468,30 +504,5 @@ describe('Instant Results Feature', { tags: '@slow' }, () => { */ cy.get('.ep-search-sort :selected').should('contain.text', 'Date, newest to oldest'); }); - - it('Is possible to filter the taxonomy terms', () => { - /** - * Activate test plugin. - */ - cy.maybeEnableFeature('instant-results'); - cy.activatePlugin('filter-instant-results-facet-terms', 'wpCli'); - - /** - * Perform a search. - */ - cy.intercept('*search=block*').as('apiRequest'); - cy.visit('/'); - cy.get('.wp-block-search').last().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'); - }); }); }); From 382099ce531324307f002d9623dcbbebb8c67a5f Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Mon, 27 Jan 2025 17:00:49 +0500 Subject: [PATCH 4/5] Add missing file --- .wp-env.json | 1 + 1 file changed, 1 insertion(+) diff --git a/.wp-env.json b/.wp-env.json index c102b17dd..8861619ea 100644 --- a/.wp-env.json +++ b/.wp-env.json @@ -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", From bf07372aa57ed10c3ac39382adc1055763672d76 Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Thu, 30 Jan 2025 17:59:52 +0500 Subject: [PATCH 5/5] Rename facet to filter --- .wp-env.json | 2 +- .../components/facets/taxonomy-terms-facet.js | 6 +++--- tests/cypress/integration/features/instant-results.cy.js | 6 +++--- ...-terms.php => filter-instant-results-category-terms.php} | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) rename tests/cypress/wordpress-files/test-plugins/{filter-instant-results-facet-terms.php => filter-instant-results-category-terms.php} (80%) diff --git a/.wp-env.json b/.wp-env.json index 8861619ea..d56970e7d 100644 --- a/.wp-env.json +++ b/.wp-env.json @@ -29,7 +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-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", diff --git a/assets/js/instant-results/components/facets/taxonomy-terms-facet.js b/assets/js/instant-results/components/facets/taxonomy-terms-facet.js index 50c0a7896..c2d7f7df8 100644 --- a/assets/js/instant-results/components/facets/taxonomy-terms-facet.js +++ b/assets/js/instant-results/components/facets/taxonomy-terms-facet.js @@ -84,9 +84,9 @@ export default ({ defaultIsOpen, label, postTypes, name }) => { */ const options = useMemo(() => { /** - * Filter the facet taxonomy terms. + * Filter the taxonomy filter terms. * - * @filter ep.InstantResults.facet.taxonomy.terms + * @filter ep.InstantResults.filter.taxonomy.terms * @since 5.2.0 * * @param {object[]} terms Taxonomy terms. @@ -95,7 +95,7 @@ export default ({ defaultIsOpen, label, postTypes, name }) => { * @returns {object[]} Filtered taxonomy terms. */ return applyFilters( - 'ep.InstantResults.facet.taxonomy.terms', + 'ep.InstantResults.filter.taxonomy.terms', buckets.reduce(reduceOptions, []), name, postTypes, diff --git a/tests/cypress/integration/features/instant-results.cy.js b/tests/cypress/integration/features/instant-results.cy.js index 197c03296..0462ae257 100644 --- a/tests/cypress/integration/features/instant-results.cy.js +++ b/tests/cypress/integration/features/instant-results.cy.js @@ -445,7 +445,7 @@ describe('Instant Results Feature', { tags: '@slow' }, () => { * Activate test plugin. */ cy.maybeEnableFeature('instant-results'); - cy.activatePlugin('filter-instant-results-facet-terms', 'wpCli'); + cy.activatePlugin('filter-instant-results-category-terms', 'wpCli'); cy.visitAdminPage('admin.php?page=elasticpress'); cy.intercept('/wp-json/elasticpress/v1/features*').as('apiRequest'); @@ -469,11 +469,11 @@ describe('Instant Results Feature', { tags: '@slow' }, () => { cy.wait('@apiRequest'); /** - * The number of terms displayed in the facet should be one. + * 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-facet-terms', 'wpCli'); + cy.deactivatePlugin('filter-instant-results-category-terms', 'wpCli'); }); }); diff --git a/tests/cypress/wordpress-files/test-plugins/filter-instant-results-facet-terms.php b/tests/cypress/wordpress-files/test-plugins/filter-instant-results-category-terms.php similarity index 80% rename from tests/cypress/wordpress-files/test-plugins/filter-instant-results-facet-terms.php rename to tests/cypress/wordpress-files/test-plugins/filter-instant-results-category-terms.php index 41fb09f06..9bcc51485 100644 --- a/tests/cypress/wordpress-files/test-plugins/filter-instant-results-facet-terms.php +++ b/tests/cypress/wordpress-files/test-plugins/filter-instant-results-category-terms.php @@ -10,7 +10,7 @@ */ /** - * Limit the Instant Results facet terms to only the "Classic" term. + * Limit the Instant Results terms to only the "Classic" term. */ add_action( 'wp_footer', @@ -29,7 +29,7 @@ function (): void { return filteredTerms; } - wp.hooks.addFilter('ep.InstantResults.facet.taxonomy.terms', 'ep-test', filterCategoryTerms); + wp.hooks.addFilter('ep.InstantResults.filter.taxonomy.terms', 'ep-test', filterCategoryTerms); });