From 967479355d7184ceeb4605044b30a955c6211b08 Mon Sep 17 00:00:00 2001 From: Joe Dolson Date: Mon, 30 Sep 2024 18:22:36 +0000 Subject: [PATCH] Quick/Bulk Edit: Add notice if no items selected. Add an error notice if a user attempts to apply bulk edits with no items selected. Applies to post lists, comments, taxonomies, and plugins screens. Props garrett-eclipse, nrqsnchz, sumitsingh, nihar007, royho, sabernhardt, oglekler, quadthemes, ankit-k-gupta, fnpen, ukdrahul, joedolson. Fixes #45006, #58479. git-svn-id: https://develop.svn.wordpress.org/trunk@59134 602fd350-edb4-49c9-b593-d223f7449a82 --- src/js/_enqueues/admin/common.js | 76 ++++++++++++++++++- src/js/_enqueues/admin/inline-edit-post.js | 7 +- src/js/_enqueues/wp/updates.js | 9 +-- src/wp-admin/includes/class-wp-list-table.php | 2 +- src/wp-includes/script-loader.php | 2 +- 5 files changed, 84 insertions(+), 12 deletions(-) diff --git a/src/js/_enqueues/admin/common.js b/src/js/_enqueues/admin/common.js index b2d8b56c63..1aa35a37db 100644 --- a/src/js/_enqueues/admin/common.js +++ b/src/js/_enqueues/admin/common.js @@ -1114,7 +1114,7 @@ $( function() { }); } - $document.on( 'wp-updates-notice-added wp-plugin-install-error wp-plugin-update-error wp-plugin-delete-error wp-theme-install-error wp-theme-delete-error', makeNoticesDismissible ); + $document.on( 'wp-updates-notice-added wp-plugin-install-error wp-plugin-update-error wp-plugin-delete-error wp-theme-install-error wp-theme-delete-error wp-notice-added', makeNoticesDismissible ); // Init screen meta. screenMeta.init(); @@ -1276,6 +1276,80 @@ $( function() { // Marry the secondary "Change role to" controls to the primary controls: marryControls( $('#new_role'), $('#changeit'), $('#new_role2'), $('#changeit2') ); + var addAdminNotice = function( data ) { + var $notice = $( data.selector ), + $headerEnd = $( '.wp-header-end' ), + type, + dismissible, + $adminNotice; + + delete data.selector; + + dismissible = ( data.dismissible && data.dismissible === true ) ? ' is-dismissible' : ''; + type = ( data.type ) ? data.type : 'info'; + + $adminNotice = '

' + data.message + '

'; + + // Check if this admin notice already exists. + if ( ! $notice.length ) { + $notice = $( '#' + data.id ); + } + + if ( $notice.length ) { + $notice.replaceWith( $adminNotice ); + } else if ( $headerEnd.length ) { + $headerEnd.after( $adminNotice ); + } else { + if ( 'customize' === pagenow ) { + $( '.customize-themes-notifications' ).append( $adminNotice ); + } else { + $( '.wrap' ).find( '> h1' ).after( $adminNotice ); + } + } + + $document.trigger( 'wp-notice-added' ); + }; + + $( '.bulkactions' ).parents( 'form' ).on( 'submit', function( event ) { + var form = this, + submitterName = event.originalEvent && event.originalEvent.submitter ? event.originalEvent.submitter.name : false; + + // Observe submissions from posts lists for 'bulk_action' or users lists for 'new_role'. + var bulkFieldRelations = { + 'bulk_action' : 'action', + 'changeit' : 'new_role' + }; + if ( ! Object.keys( bulkFieldRelations ).includes( submitterName ) ) { + return; + } + + var values = new FormData(form); + var value = values.get( bulkFieldRelations[ submitterName ] ) || '-1'; + + // Check that the action is not the default one. + if ( value !== '-1' ) { + // Check that at least one item is selected. + var itemsSelected = form.querySelectorAll( '.wp-list-table tbody .check-column input[type="checkbox"]:checked' ); + + if ( itemsSelected.length > 0 ) { + return; + } + } + event.preventDefault(); + event.stopPropagation(); + $( 'html, body' ).animate( { scrollTop: 0 } ); + + var errorMessage = __( 'Please select at least one item to perform this action on.' ); + addAdminNotice( { + id: 'no-items-selected', + type: 'error', + message: errorMessage, + dismissible: true, + } ); + + wp.a11y.speak( errorMessage ); + }); + /** * Shows row actions on focus of its parent container element or any other elements contained within. * diff --git a/src/js/_enqueues/admin/inline-edit-post.js b/src/js/_enqueues/admin/inline-edit-post.js index 5963a2211e..6f313fc055 100644 --- a/src/js/_enqueues/admin/inline-edit-post.js +++ b/src/js/_enqueues/admin/inline-edit-post.js @@ -148,7 +148,12 @@ window.wp = window.wp || {}; * Adds onclick events to the apply buttons. */ $('#doaction').on( 'click', function(e){ - var n; + var n, + $itemsSelected = $( '#posts-filter .check-column input[type="checkbox"]:checked' ); + + if ( $itemsSelected.length < 1 ) { + return; + } t.whichBulkButtonId = $( this ).attr( 'id' ); n = t.whichBulkButtonId.substr( 2 ); diff --git a/src/js/_enqueues/wp/updates.js b/src/js/_enqueues/wp/updates.js index 558a40f6de..396fcbb6ff 100644 --- a/src/js/_enqueues/wp/updates.js +++ b/src/js/_enqueues/wp/updates.js @@ -2832,14 +2832,7 @@ // Bail if there were no items selected. if ( ! itemsSelected.length ) { - event.preventDefault(); - $( 'html, body' ).animate( { scrollTop: 0 } ); - - return wp.updates.addAdminNotice( { - id: 'no-items-selected', - className: 'notice-error is-dismissible', - message: __( 'Please select at least one item to perform this action on.' ) - } ); + bulkAction = false; } // Determine the type of request we're dealing with. diff --git a/src/wp-admin/includes/class-wp-list-table.php b/src/wp-admin/includes/class-wp-list-table.php index 253bcca800..a0d4c33bcb 100644 --- a/src/wp-admin/includes/class-wp-list-table.php +++ b/src/wp-admin/includes/class-wp-list-table.php @@ -625,7 +625,7 @@ class WP_List_Table { echo "\n"; - submit_button( __( 'Apply' ), 'action', '', false, array( 'id' => "doaction$two" ) ); + submit_button( __( 'Apply' ), 'action', 'bulk_action', false, array( 'id' => "doaction$two" ) ); echo "\n"; } diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 4f9fedd512..406bf39d08 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -754,7 +754,7 @@ function wp_default_scripts( $scripts ) { ) ); - $scripts->add( 'common', "/wp-admin/js/common$suffix.js", array( 'jquery', 'hoverIntent', 'utils' ), false, 1 ); + $scripts->add( 'common', "/wp-admin/js/common$suffix.js", array( 'jquery', 'hoverIntent', 'utils', 'wp-a11y' ), false, 1 ); $scripts->set_translations( 'common' ); $scripts->add( 'wp-sanitize', "/wp-includes/js/wp-sanitize$suffix.js", array(), false, 1 );