Skip to content

Commit

Permalink
Merge pull request #3386 from 10up/fix/post-type-labels
Browse files Browse the repository at this point in the history
Exclude non-searchable post types from facet post types.
  • Loading branch information
felipeelia committed Mar 20, 2023
2 parents 440c486 + e89aa26 commit 0f32025
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
6 changes: 5 additions & 1 deletion includes/classes/Feature/InstantResults/InstantResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -835,9 +835,13 @@ public function get_facets() {
$slug
);

$post_types = Features::factory()->get_registered_feature( 'search' )->get_searchable_post_types();
$post_types = array_intersect( $post_types, $taxonomy->object_type );
$post_types = array_values( $post_types );

$facets[ $name ] = array(
'type' => 'taxonomy',
'post_types' => $taxonomy->object_type,
'post_types' => $post_types,
'labels' => array(
'admin' => $admin_label,
'frontend' => $labels->singular_name,
Expand Down
30 changes: 29 additions & 1 deletion tests/cypress/integration/features/instant-results.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('Instant Results Feature', { tags: '@slow' }, () => {

beforeEach(() => {
cy.deactivatePlugin(
'custom-instant-results-template open-instant-results-with-buttons filter-instant-results-per-page',
'custom-instant-results-template open-instant-results-with-buttons filter-instant-results-per-page cpt-and-custom-tax',
'wpCli',
);
cy.login();
Expand Down Expand Up @@ -112,6 +112,7 @@ describe('Instant Results Feature', { tags: '@slow' }, () => {
* Can change the URL when search term is changed
*/
it('Can see instant results elements, URL changes, reload, and update after changing search term', () => {
cy.activatePlugin('cpt-and-custom-tax', 'wpCli');
cy.maybeEnableFeature('instant-results');

cy.intercept({
Expand All @@ -121,6 +122,25 @@ describe('Instant Results Feature', { tags: '@slow' }, () => {
},
}).as('apiRequest');

/**
* Add product category facet to test the labelling of facets
* with the same name.
*/
cy.intercept('**/wp-admin/admin-ajax.php*').as('ajaxRequest');
cy.visitAdminPage('admin.php?page=elasticpress');
cy.get('.ep-feature-instant-results .settings-button').click();
cy.get('.ep-feature-instant-results .components-form-token-field__input').type(
'cat{downArrow}{enter}',
);
cy.get('.ep-feature-instant-results .components-form-token-field__input').type(
'prod{downArrow}{enter}{esc}',
);
cy.get('.ep-feature-instant-results .button-primary').click();
cy.wait('@ajaxRequest');

/**
* Perform a search.
*/
cy.visit('/');

cy.get('.wp-block-search').last().as('searchBlock');
Expand All @@ -136,6 +156,14 @@ describe('Instant Results Feature', { tags: '@slow' }, () => {
// Show the number of results
cy.get('@searchModal').find('.ep-search-results__title').contains(/\d+/);

/**
* The Category facet should specify its searchable post types.
*/
cy.get('.ep-search-panel__button').contains('Category').as('categoryFacet');
cy.get('@categoryFacet').should('contain', 'Posts');
cy.get('@categoryFacet').should('contain', 'Movies');
cy.get('@categoryFacet').should('not.contain', 'Albums');

cy.get('.ep-search-sidebar #ep-search-post-type-post')
.click()
.then(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace ElasticPress\Tests\E2e;

/**
* Create a CPT called "Movies".
* Create a CPT called "Movies" and a non-searchable CPT called "Group".
*/
function create_post_type() {
register_post_type(
Expand All @@ -24,6 +24,21 @@ function create_post_type() {
],
'public' => true,
'has_archive' => true,
'taxonomies' => [ 'category' ],
]
);

register_post_type(
'group',
[
'labels' => [
'name' => __( 'Albums' ),
'singular_name' => __( 'Album' ),
],
'exclude_from_search' => true,
'has_archive' => true,
'public' => true,
'taxonomies' => [ 'category' ],
]
);
}
Expand Down

0 comments on commit 0f32025

Please sign in to comment.