<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[57580] trunk: Quick/Bulk Edit: Pre-fill category fields with their status.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/57580">57580</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/57580","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>joedolson</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2024-02-09 19:48:41 +0000 (Fri, 09 Feb 2024)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/changeset/56172">[56172]</a>, 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 <a href="https://core.trac.wordpress.org/ticket/11302">#11302</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcjs_enqueuesadmininlineeditpostjs">trunk/src/js/_enqueues/admin/inline-edit-post.js</a></li>
<li><a href="#trunksrcwpadmincsslisttablescss">trunk/src/wp-admin/css/list-tables.css</a></li>
<li><a href="#trunksrcwpadminincludespostphp">trunk/src/wp-admin/includes/post.php</a></li>
<li><a href="#trunktestsphpunittestsadminincludesPostphp">trunk/tests/phpunit/tests/admin/includesPost.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcjs_enqueuesadmininlineeditpostjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/js/_enqueues/admin/inline-edit-post.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/js/_enqueues/admin/inline-edit-post.js 2024-02-09 18:23:05 UTC (rev 57579)
+++ trunk/src/js/_enqueues/admin/inline-edit-post.js 2024-02-09 19:48:41 UTC (rev 57580)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -178,6 +178,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> setBulk : function(){
</span><span class="cx" style="display: block; padding: 0 10px"> var te = '', type = this.type, c = true;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ var checkedPosts = $( 'tbody th.check-column input[type="checkbox"]:checked' );
+ var categories = {};
</ins><span class="cx" style="display: block; padding: 0 10px"> this.revert();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $( '#bulk-edit td' ).attr( 'colspan', $( 'th:visible, td:visible', '.widefat:first thead' ).length );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -217,6 +219,44 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Populate the list of items to bulk edit.
</span><span class="cx" style="display: block; padding: 0 10px"> $( '#bulk-titles' ).html( '<ul id="bulk-titles-list" role="list">' + te + '</ul>' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // 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 );
+ } );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Binds on click events to handle the list of items to bulk edit.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span></span></pre></div>
<a id="trunksrcwpadmincsslisttablescss"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/css/list-tables.css</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/css/list-tables.css 2024-02-09 18:23:05 UTC (rev 57579)
+++ trunk/src/wp-admin/css/list-tables.css 2024-02-09 19:48:41 UTC (rev 57580)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1149,6 +1149,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> overflow-y: scroll;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+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% );
+}
+
</ins><span class="cx" style="display: block; padding: 0 10px"> #bulk-titles .ntdelbutton,
</span><span class="cx" style="display: block; padding: 0 10px"> #bulk-titles .ntdeltitle,
</span><span class="cx" style="display: block; padding: 0 10px"> .inline-edit-row fieldset ul.cat-checklist label {
</span></span></pre></div>
<a id="trunksrcwpadminincludespostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/post.php 2024-02-09 18:23:05 UTC (rev 57579)
+++ trunk/src/wp-admin/includes/post.php 2024-02-09 19:48:41 UTC (rev 57580)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -649,8 +649,21 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $new_cats ) && in_array( 'category', $tax_names, true ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $cats = (array) wp_get_post_categories( $post_id );
- $post_data['post_category'] = array_unique( array_merge( $cats, $new_cats ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $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 ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> unset( $post_data['tax_input']['category'] );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunktestsphpunittestsadminincludesPostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/admin/includesPost.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/admin/includesPost.php 2024-02-09 18:23:05 UTC (rev 57579)
+++ trunk/tests/phpunit/tests/admin/includesPost.php 2024-02-09 19:48:41 UTC (rev 57580)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -385,6 +385,131 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 11302
+ */
+ public function test_bulk_edit_if_categories_unchanged() {
+ wp_set_current_user( self::$admin_id );
+
+ $post_ids = self::factory()->post->create_many( 3 );
+
+ wp_set_post_categories( $post_ids[0], array( 'test1', 'test2' ) );
+ wp_set_post_categories( $post_ids[1], array( 'test2', 'test3' ) );
+ wp_set_post_categories( $post_ids[2], array( 'test1', 'test3' ) );
+
+ $terms1 = wp_get_post_categories( $post_ids[0] );
+ $terms2 = wp_get_post_categories( $post_ids[1] );
+ $terms3 = wp_get_post_categories( $post_ids[2] );
+
+ $indeterminate_categories = array_merge( $terms1, $terms2, $terms3 );
+
+ $request = array(
+ '_status' => -1,
+ 'post' => $post_ids,
+ 'indeterminate_post_category' => $indeterminate_categories,
+ );
+
+ bulk_edit_posts( $request );
+
+ $updated_terms1 = wp_get_post_categories( $post_ids[0] );
+ $updated_terms2 = wp_get_post_categories( $post_ids[1] );
+ $updated_terms3 = wp_get_post_categories( $post_ids[2] );
+
+ $this->assertSame( $terms1, $updated_terms1, 'Post 1 should have terms 1 and 2.' );
+ $this->assertSame( $terms2, $updated_terms2, 'Post 2 should have terms 2 and 3.' );
+ $this->assertSame( $terms3, $updated_terms3, 'Post 3 should have terms 1 and 3.' );
+ }
+
+ /**
+ * @ticket 11302
+ */
+ public function test_bulk_edit_if_some_categories_added() {
+ wp_set_current_user( self::$admin_id );
+
+ $post_ids = self::factory()->post->create_many( 3 );
+ $term1 = wp_create_category( 'test1' );
+ $term2 = wp_create_category( 'test2' );
+ $term3 = wp_create_category( 'test3' );
+ $term4 = wp_create_category( 'test4' );
+
+ wp_set_post_categories( $post_ids[0], array( $term1, $term2 ) );
+ wp_set_post_categories( $post_ids[1], array( $term2, $term3 ) );
+ wp_set_post_categories( $post_ids[2], array( $term1, $term3 ) );
+
+ $terms1 = wp_get_post_categories( $post_ids[0], array( 'fields' => 'ids' ) );
+ $terms2 = wp_get_post_categories( $post_ids[1], array( 'fields' => 'ids' ) );
+ $terms3 = wp_get_post_categories( $post_ids[2], array( 'fields' => 'ids' ) );
+ // All existing categories are indeterminate.
+ $indeterminate = array_unique( array_merge( $terms1, $terms2, $terms3 ) );
+ // Add new category.
+ $categories[] = $term4;
+
+ $request = array(
+ '_status' => -1,
+ 'post' => $post_ids,
+ 'post_category' => $categories,
+ 'indeterminate_post_category' => $indeterminate,
+ );
+
+ bulk_edit_posts( $request );
+
+ $updated_terms1 = wp_get_post_categories( $post_ids[0], array( 'fields' => 'ids' ) );
+ $updated_terms2 = wp_get_post_categories( $post_ids[1], array( 'fields' => 'ids' ) );
+ $updated_terms3 = wp_get_post_categories( $post_ids[2], array( 'fields' => 'ids' ) );
+
+ // Each post should have the same categories as before and add term 4.
+ $this->assertSame( array( $term1, $term2, $term4 ), $updated_terms1, 'Post should have terms 1, 2, and 4.' );
+ $this->assertSame( array( $term2, $term3, $term4 ), $updated_terms2, 'Post should have terms 2, 3, and 4.' );
+ $this->assertSame( array( $term1, $term3, $term4 ), $updated_terms3, 'Post should have terms 1, 3, and 4.' );
+ }
+
+ /**
+ * @ticket 11302
+ */
+ public function test_bulk_edit_if_some_categories_removed() {
+ wp_set_current_user( self::$admin_id );
+
+ $post_ids = self::factory()->post->create_many( 3 );
+ $term1 = wp_create_category( 'test1' );
+ $term2 = wp_create_category( 'test2' );
+ $term3 = wp_create_category( 'test3' );
+
+ wp_set_post_categories( $post_ids[0], array( $term1, $term2 ) );
+ wp_set_post_categories( $post_ids[1], array( $term2, $term3 ) );
+ wp_set_post_categories( $post_ids[2], array( $term1, $term3 ) );
+
+ $terms1 = wp_get_post_categories( $post_ids[0], array( 'fields' => 'ids' ) );
+ $terms2 = wp_get_post_categories( $post_ids[1], array( 'fields' => 'ids' ) );
+ $terms3 = wp_get_post_categories( $post_ids[2], array( 'fields' => 'ids' ) );
+
+ // Terms 2 and 3 are in indeterminate state.
+ $indeterminate = array( $term2, $term3 );
+ // Remove term 1 from selected categories.
+ $categories = array_unique( array_merge( $terms1, $terms2, $terms3 ) );
+ $remove_key = array_search( $term1, $categories, true );
+ unset( $categories[ $remove_key ] );
+
+ $request = array(
+ '_status' => -1,
+ 'post' => $post_ids,
+ 'post_category' => $categories,
+ 'indeterminate_post_category' => $indeterminate,
+ );
+
+ bulk_edit_posts( $request );
+
+ $updated_terms1 = wp_get_post_categories( $post_ids[0], array( 'fields' => 'ids' ) );
+ $updated_terms2 = wp_get_post_categories( $post_ids[1], array( 'fields' => 'ids' ) );
+ $updated_terms3 = wp_get_post_categories( $post_ids[2], array( 'fields' => 'ids' ) );
+
+ // Post 1 should only have term 2.
+ $this->assertSame( $updated_terms1, array( $term2 ), 'Post 1 should only have term 2.' );
+ // Post 2 should be unchanged.
+ $this->assertSame( $terms2, $updated_terms2, 'Post 2 should be unchanged.' );
+ // Post 3 should only have term 3.
+ $this->assertSame( $updated_terms3, array( $term3 ), 'Post 3 should only have term 3.' );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Tests that `bulk_edit_posts()` fires the 'bulk_edit_posts' action.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 28112
</span></span></pre>
</div>
</div>
</body>
</html>