summaryrefslogtreecommitdiffstatshomepage
path: root/src/js/_enqueues/admin/common.js
diff options
context:
space:
mode:
authorJoe Dolson <joedolson@git.wordpress.org>2024-09-30 18:22:36 +0000
committerJoe Dolson <joedolson@git.wordpress.org>2024-09-30 18:22:36 +0000
commit967479355d7184ceeb4605044b30a955c6211b08 (patch)
tree8edd5977cd92ec35c95a58b8dd26dd6fbf1bc3ea /src/js/_enqueues/admin/common.js
parent1f40d422c23a2e3a35140c4b3856721b7eac70db (diff)
downloadwordpress-967479355d7184ceeb4605044b30a955c6211b08.tar.gz
wordpress-967479355d7184ceeb4605044b30a955c6211b08.zip
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
Diffstat (limited to 'src/js/_enqueues/admin/common.js')
-rw-r--r--src/js/_enqueues/admin/common.js76
1 files changed, 75 insertions, 1 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 = '<div id="' + data.id + '" class="notice notice-' + data.type + dismissible + '"><p>' + data.message + '</p></div>';
+
+ // 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.
*