Skip to content
Open
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
91 changes: 91 additions & 0 deletions src/js/_enqueues/admin/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,97 @@ window.columns = {

$( function() { columns.init(); } );

/**
* Manages taxonomy filter dropdown visibility preferences in Screen Options.
*
* @since 7.0.0
*
* Binds change events to the taxonomy filter checkboxes to show or hide
* the corresponding filter dropdowns instantly, and persists the preference
* via AJAX. Hidden dropdowns have their <select> element disabled so they are
* not submitted as part of the filter form.
*/
var taxonomyFilters = {

/**
* Initialises the taxonomy filter visibility toggles.
*
* @since 7.0.0
*
* @return {void}
*/
init : function() {
$( '.taxonomy-filter-tog', '#adv-settings' ).on( 'change', function() {
var $checkbox = $( this ),
taxonomy = $checkbox.val(),
$container = $( '.taxonomy-filter-container[data-taxonomy="' + taxonomy + '"]' ),
$select = $container.find( 'select, [data-name]' );

if ( $checkbox.prop( 'checked' ) ) {
$container.removeClass( 'hidden' );
// Restore the `name` attribute so the control is submitted with the form.
if ( $select.is( '[data-name]' ) ) {
$select.attr( 'name', $select.attr( 'data-name' ) ).removeAttr( 'data-name' );
}
$select.prop( 'disabled', false );
// Ensure the dedicated taxonomy filters row is visible.
$( '.taxonomy-filters-row' ).removeClass( 'hidden' );
} else {
$container.addClass( 'hidden' );
// Move `name` to `data-name` so the control is never submitted with the form.
if ( $select.is( '[name]' ) ) {
$select.attr( 'data-name', $select.attr( 'name' ) ).removeAttr( 'name' );
}
$select.prop( 'disabled', true );
// Hide the row when no taxonomy filters remain enabled.
if ( $( '.taxonomy-filter-tog:checked', '#adv-settings' ).length === 0 ) {
$( '.taxonomy-filters-row' ).addClass( 'hidden' );
}
}

taxonomyFilters.saveState();
} );
},

/**
* Saves the current taxonomy filter visibility state via AJAX.
*
* @since 7.0.0
*
* @return {void}
*/
saveState : function() {
var visible = this.visible();
$.post(
ajaxurl,
{
action: 'taxonomy-filter-visibility',
visible: visible,
screenoptionnonce: $( '#screenoptionnonce' ).val(),
page: pagenow
},
function() {
wp.a11y.speak( __( 'Screen Options updated.' ) );
}
);
},

/**
* Gets the slugs of all currently visible taxonomy filter dropdowns.
*
* @since 7.0.0
*
* @return {string} Comma-separated taxonomy slugs that are currently visible.
*/
visible : function() {
return $( '.taxonomy-filter-tog:checked', '#adv-settings' ).map( function() {
return $( this ).val();
} ).get().join( ',' );
}
};

$( function() { taxonomyFilters.init(); } );

/**
* Validates that the required form fields are not empty.
*
Expand Down
1 change: 1 addition & 0 deletions src/wp-admin/admin-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
'add-user',
'closed-postboxes',
'hidden-columns',
'taxonomy-filter-visibility',
'update-welcome-panel',
'menu-get-metabox',
'wp-link-ajax',
Expand Down
21 changes: 21 additions & 0 deletions src/wp-admin/css/list-tables.css
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,27 @@ th.sorted a span {
padding: 0 8px 0 0;
}

.tablenav .taxonomy-filters-row {
clear: left;
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 6px 0;
}

.tablenav .taxonomy-filters-row.hidden {
display: none;
}

.tablenav .taxonomy-filters-row .taxonomy-filter-container,
.tablenav .taxonomy-filters-row .taxonomy-filter-container.hidden {
float: none;
}

.tablenav .actions + .taxonomy-filters-row {
margin: 6px 0;
}

.wp-filter .actions {
display: inline-block;
vertical-align: middle;
Expand Down
27 changes: 27 additions & 0 deletions src/wp-admin/includes/ajax-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1856,6 +1856,33 @@ function wp_ajax_hidden_columns() {
wp_die( 1 );
}

/**
* Handles saving taxonomy filter visibility preferences via AJAX.
*
* @since 7.0.0
*/
function wp_ajax_taxonomy_filter_visibility() {
check_ajax_referer( 'screen-options-nonce', 'screenoptionnonce' );

$page = $_POST['page'] ?? '';

if ( sanitize_key( $page ) !== $page ) {
wp_die( 0 );
}

$user = wp_get_current_user();
if ( ! $user ) {
wp_die( -1 );
}

$visible = ! empty( $_POST['visible'] ) ? explode( ',', $_POST['visible'] ) : array();
$visible = array_map( 'sanitize_key', $visible );

update_user_meta( $user->ID, "manage{$page}taxonomyfilters", $visible );

wp_die( 1 );
}

/**
* Handles updating whether to display the welcome panel via AJAX.
*
Expand Down
Loading
Loading