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

Query filters: Add taxonomy to apply button of query filter block #469

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
21 changes: 19 additions & 2 deletions mu-plugins/blocks/query-filter/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,25 @@
);

if ( $selected_count && $has_multiple ) {
/* translators: %s is count of currently selected filters. */
$apply_label = sprintf( __( 'Apply (%s)', 'wporg' ), $selected_count );
$special_singular_mappings = array(
'categories' => 'category',
);

// Possible Input: 'popular tags <span>1</span>'
// Expected Output: 'popular tags'
$label = strtolower( preg_replace( '/\s\d+$/', '', strip_tags( $settings['label'] ) ) );
$label_form = 1 === $selected_count
? ( isset( $special_singular_mappings[ $label ] )
? $special_singular_mappings[ $label ]
: substr( $label, 0, -1 ) )
: $label;

$apply_label = sprintf(
/* translators: 1: the count of currently selected filters. 2: taxonomy being filtered */
__( 'Apply %1$s %2$s', 'wporg' ),
$selected_count,
$label_form
);
} else {
$apply_label = __( 'Apply', 'wporg' );
}
Expand Down
18 changes: 16 additions & 2 deletions mu-plugins/blocks/query-filter/src/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ function closeDropdown( store ) {
document.documentElement.classList.remove( 'is-query-filter-open' );
}

function getTaxonomyForm( count, pluralTaxonomy ) {
const specialSingularMappings = {
categories: 'category',
};
return count === 1
? specialSingularMappings[ pluralTaxonomy ] || pluralTaxonomy.slice( 0, -1 )
: pluralTaxonomy;
}

function updateButtons( store, count ) {
const { context } = store;
if ( ! context.wporg.queryFilter.form ) {
Expand All @@ -33,12 +42,17 @@ function updateButtons( store, count ) {

const applyButton = context.wporg.queryFilter.form.querySelector( 'input[type="submit"]' );
const clearButton = context.wporg.queryFilter.form.querySelector( '.wporg-query-filter__modal-action-clear' );
const taxonomy = context.wporg.queryFilter.toggleButton.textContent.replace( /\s\d+$/, '' ).toLowerCase();

// Only update the apply button if multiple selections are allowed.
if ( context.wporg.queryFilter.hasMultiple ) {
if ( count ) {
/* translators: %s is count of currently selected filters. */
applyButton.value = sprintf( __( 'Apply (%s)', 'wporg' ), count );
applyButton.value = sprintf(
/* translators: 1: the count of currently selected filters. 2: taxonomy being filtered */
__( 'Apply %1$s %2$s', 'wporg' ),
count,
getTaxonomyForm( count, taxonomy )
);
} else {
applyButton.value = __( 'Apply', 'wporg' );
}
Expand Down
Loading