Skip to content

Commit

Permalink
Quick/Bulk Edit: Pre-fill category fields with their status.
Browse files Browse the repository at this point in the history
This pre-fills 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.

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.
Fixes #11302.

git-svn-id: https://develop.svn.wordpress.org/trunk@56712 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
costdev committed Sep 26, 2023
1 parent dc31cf0 commit 332304a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
42 changes: 42 additions & 0 deletions src/js/_enqueues/admin/inline-edit-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ 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 = {};
var indeterminatePostCategoryField = $( '<input type="hidden" name="indeterminate_post_category[]">' );
this.revert();

$( '#bulk-edit td' ).attr( 'colspan', $( 'th:visible, td:visible', '.widefat:first thead' ).length );
Expand Down Expand Up @@ -217,6 +220,45 @@ 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() {
// Clear indeterminate states.
$( '<input type="hidden" name="indeterminate_post_category[]">' ).remove();

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 );

// Set indeterminate states for the backend.
indeterminatePostCategoryField.val( $( this ).val() );
$( this ).after( indeterminatePostCategoryField );
}
} );

$( '.inline-edit-categories input[name="post_category[]"]' ).on( 'change', function() {
// Remove the indeterminate flags as there was a specific state change.
$( this ).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.
*
Expand Down
11 changes: 11 additions & 0 deletions src/wp-admin/css/list-tables.css
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,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 {
Expand Down
17 changes: 15 additions & 2 deletions src/wp-admin/includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -641,8 +641,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'] );
}

Expand Down

0 comments on commit 332304a

Please sign in to comment.