Quick/Bulk Edit: Pre-fill category fields with their status.

Pre-fill category fields in the Quick/Bulk Edit form with their current status.

When bulk editing, if only some of the selected items are in a given category, the category's checkbox will display a line to indicate an indeterminate status.

Originally committed in [56172], but reverted due to a bug that removed all categories. Updated commit fixes the bug, adds unit tests, and improves the accessibility of the indeterminate state checkboxes.

Props pavelevap, scribu, chasedsiedu, helen, joshcanhelp, ubernaut, Cyberchicken, laumindproductscomau, SergeyBiryukov, Marcoevich, tomybyte, thinkluke, virtality-marketing-solutions, Michalooki, dmsnell, itecrs, pannelars, WHSajid, samba45, Mte90, johnbillion, tomluckies, soulseekah, francina, oglekler, ajmcfadyen, mukesh27, costdev, hellofromTonya, peterwilsoncc, joedolson, pbiron, oglekler, webcommsat, jorbin, ajmcfadyen, huzaifaalmesbah.
Fixes #11302.
Built from https://develop.svn.wordpress.org/trunk@57580


git-svn-id: http://core.svn.wordpress.org/trunk@57081 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
joedolson 2024-02-09 19:50:14 +00:00
parent ae0b8ee134
commit 5940784180
8 changed files with 81 additions and 6 deletions

View File

@ -1150,6 +1150,17 @@ ul.cat-checklist {
overflow-y: scroll;
}
ul.cat-checklist input[name="post_category[]"]:indeterminate::before {
content: '';
border-top: 2px solid grey;
width: 65%;
height: 2px;
position: absolute;
top: calc( 50% + 1px );
right: 50%;
transform: translate( 50%, -50% );
}
#bulk-titles .ntdelbutton,
#bulk-titles .ntdeltitle,
.inline-edit-row fieldset ul.cat-checklist label {

File diff suppressed because one or more lines are too long

View File

@ -1149,6 +1149,17 @@ ul.cat-checklist {
overflow-y: scroll;
}
ul.cat-checklist input[name="post_category[]"]:indeterminate::before {
content: '';
border-top: 2px solid grey;
width: 65%;
height: 2px;
position: absolute;
top: calc( 50% + 1px );
left: 50%;
transform: translate( -50%, -50% );
}
#bulk-titles .ntdelbutton,
#bulk-titles .ntdeltitle,
.inline-edit-row fieldset ul.cat-checklist label {

File diff suppressed because one or more lines are too long

View File

@ -649,8 +649,21 @@ function bulk_edit_posts( $post_data = null ) {
}
if ( isset( $new_cats ) && in_array( 'category', $tax_names, true ) ) {
$cats = (array) wp_get_post_categories( $post_id );
$post_data['post_category'] = array_unique( array_merge( $cats, $new_cats ) );
$cats = (array) wp_get_post_categories( $post_id );
if (
isset( $post_data['indeterminate_post_category'] )
&& is_array( $post_data['indeterminate_post_category'] )
) {
$indeterminate_post_category = $post_data['indeterminate_post_category'];
} else {
$indeterminate_post_category = array();
}
$indeterminate_cats = array_intersect( $cats, $indeterminate_post_category );
$determinate_cats = array_diff( $new_cats, $indeterminate_post_category );
$post_data['post_category'] = array_unique( array_merge( $indeterminate_cats, $determinate_cats ) );
unset( $post_data['tax_input']['category'] );
}

View File

@ -178,6 +178,8 @@ window.wp = window.wp || {};
*/
setBulk : function(){
var te = '', type = this.type, c = true;
var checkedPosts = $( 'tbody th.check-column input[type="checkbox"]:checked' );
var categories = {};
this.revert();
$( '#bulk-edit td' ).attr( 'colspan', $( 'th:visible, td:visible', '.widefat:first thead' ).length );
@ -217,6 +219,44 @@ window.wp = window.wp || {};
// Populate the list of items to bulk edit.
$( '#bulk-titles' ).html( '<ul id="bulk-titles-list" role="list">' + te + '</ul>' );
// Gather up some statistics on which of these checked posts are in which categories.
checkedPosts.each( function() {
var id = $( this ).val();
var checked = $( '#category_' + id ).text().split( ',' );
checked.map( function( cid ) {
categories[ cid ] || ( categories[ cid ] = 0 );
// Just record that this category is checked.
categories[ cid ]++;
} );
} );
// Compute initial states.
$( '.inline-edit-categories input[name="post_category[]"]' ).each( function() {
if ( categories[ $( this ).val() ] == checkedPosts.length ) {
// If the number of checked categories matches the number of selected posts, then all posts are in this category.
$( this ).prop( 'checked', true );
} else if ( categories[ $( this ).val() ] > 0 ) {
// If the number is less than the number of selected posts, then it's indeterminate.
$( this ).prop( 'indeterminate', true );
if ( ! $( this ).parent().find( 'input[name="indeterminate_post_category[]"]' ).length ) {
// Get the term label text.
var label = $( this ).parent().text();
// Set indeterminate states for the backend. Add accessible text for indeterminate inputs.
$( this ).after( '<input type="hidden" name="indeterminate_post_category[]" value="' + $( this ).val() + '">' ).attr( 'aria-label', label.trim() + ': ' + wp.i18n.__( 'Some selected posts have this category' ) );
}
}
} );
$( '.inline-edit-categories input[name="post_category[]"]:indeterminate' ).on( 'change', function() {
// Remove accessible label text. Remove the indeterminate flags as there was a specific state change.
$( this ).removeAttr( 'aria-label' ).parent().find( 'input[name="indeterminate_post_category[]"]' ).remove();
} );
$( '.inline-edit-save button' ).on( 'click', function() {
$( '.inline-edit-categories input[name="post_category[]"]' ).prop( 'indeterminate', false );
} );
/**
* Binds on click events to handle the list of items to bulk edit.
*

File diff suppressed because one or more lines are too long

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.5-alpha-57579';
$wp_version = '6.5-alpha-57580';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.