Skip to content

Commit 332304a

Browse files
committed
Quick/Bulk Edit: Pre-fill category fields with their status.
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
1 parent dc31cf0 commit 332304a

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

src/js/_enqueues/admin/inline-edit-post.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ window.wp = window.wp || {};
178178
*/
179179
setBulk : function(){
180180
var te = '', type = this.type, c = true;
181+
var checkedPosts = $( 'tbody th.check-column input[type="checkbox"]:checked' );
182+
var categories = {};
183+
var indeterminatePostCategoryField = $( '<input type="hidden" name="indeterminate_post_category[]">' );
181184
this.revert();
182185

183186
$( '#bulk-edit td' ).attr( 'colspan', $( 'th:visible, td:visible', '.widefat:first thead' ).length );
@@ -217,6 +220,45 @@ window.wp = window.wp || {};
217220
// Populate the list of items to bulk edit.
218221
$( '#bulk-titles' ).html( '<ul id="bulk-titles-list" role="list">' + te + '</ul>' );
219222

223+
// Gather up some statistics on which of these checked posts are in which categories.
224+
checkedPosts.each( function() {
225+
var id = $( this ).val();
226+
var checked = $( '#category_' + id ).text().split( ',' );
227+
228+
checked.map( function( cid ) {
229+
categories[ cid ] || ( categories[ cid ] = 0 );
230+
// Just record that this category is checked.
231+
categories[ cid ]++;
232+
} );
233+
} );
234+
235+
// Compute initial states.
236+
$( '.inline-edit-categories input[name="post_category[]"]' ).each( function() {
237+
// Clear indeterminate states.
238+
$( '<input type="hidden" name="indeterminate_post_category[]">' ).remove();
239+
240+
if ( categories[ $( this ).val() ] == checkedPosts.length ) {
241+
// If the number of checked categories matches the number of selected posts, then all posts are in this category.
242+
$( this ).prop( 'checked', true );
243+
} else if ( categories[ $( this ).val() ] > 0 ) {
244+
// If the number is less than the number of selected posts, then it's indeterminate.
245+
$( this ).prop( 'indeterminate', true );
246+
247+
// Set indeterminate states for the backend.
248+
indeterminatePostCategoryField.val( $( this ).val() );
249+
$( this ).after( indeterminatePostCategoryField );
250+
}
251+
} );
252+
253+
$( '.inline-edit-categories input[name="post_category[]"]' ).on( 'change', function() {
254+
// Remove the indeterminate flags as there was a specific state change.
255+
$( this ).parent().find( 'input[name="indeterminate_post_category[]"]' ).remove();
256+
} );
257+
258+
$( '.inline-edit-save button' ).on( 'click', function() {
259+
$( '.inline-edit-categories input[name="post_category[]"]' ).prop( 'indeterminate', false );
260+
} );
261+
220262
/**
221263
* Binds on click events to handle the list of items to bulk edit.
222264
*

src/wp-admin/css/list-tables.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,17 @@ ul.cat-checklist {
11471147
overflow-y: scroll;
11481148
}
11491149

1150+
ul.cat-checklist input[name="post_category[]"]:indeterminate::before {
1151+
content: '';
1152+
border-top: 2px solid grey;
1153+
width: 65%;
1154+
height: 2px;
1155+
position: absolute;
1156+
top: calc( 50% + 1px );
1157+
left: 50%;
1158+
transform: translate( -50%, -50% );
1159+
}
1160+
11501161
#bulk-titles .ntdelbutton,
11511162
#bulk-titles .ntdeltitle,
11521163
.inline-edit-row fieldset ul.cat-checklist label {

src/wp-admin/includes/post.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,21 @@ function bulk_edit_posts( $post_data = null ) {
641641
}
642642

643643
if ( isset( $new_cats ) && in_array( 'category', $tax_names, true ) ) {
644-
$cats = (array) wp_get_post_categories( $post_id );
645-
$post_data['post_category'] = array_unique( array_merge( $cats, $new_cats ) );
644+
$cats = (array) wp_get_post_categories( $post_id );
645+
646+
if (
647+
isset( $post_data['indeterminate_post_category'] )
648+
&& is_array( $post_data['indeterminate_post_category'] )
649+
) {
650+
$indeterminate_post_category = $post_data['indeterminate_post_category'];
651+
} else {
652+
$indeterminate_post_category = array();
653+
}
654+
655+
$indeterminate_cats = array_intersect( $cats, $indeterminate_post_category );
656+
$determinate_cats = array_diff( $new_cats, $indeterminate_post_category );
657+
$post_data['post_category'] = array_unique( array_merge( $indeterminate_cats, $determinate_cats ) );
658+
646659
unset( $post_data['tax_input']['category'] );
647660
}
648661

0 commit comments

Comments
 (0)