Make WordPress Core

Changeset 59134


Ignore:
Timestamp:
09/30/2024 06:22:36 PM (23 months ago)
Author:
joedolson
Message:

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.

Location:
trunk/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/js/_enqueues/admin/common.js

    r58449 r59134  
    11151115        }
    11161116
    1117         $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 );
     1117        $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 );
    11181118
    11191119        // Init screen meta.
     
    12761276        // Marry the secondary "Change role to" controls to the primary controls:
    12771277        marryControls( $('#new_role'), $('#changeit'), $('#new_role2'), $('#changeit2') );
     1278
     1279        var addAdminNotice = function( data ) {
     1280                var $notice = $( data.selector ),
     1281                        $headerEnd = $( '.wp-header-end' ),
     1282                        type,
     1283                        dismissible,
     1284                        $adminNotice;
     1285
     1286                delete data.selector;
     1287
     1288                dismissible = ( data.dismissible && data.dismissible === true ) ? ' is-dismissible' : '';
     1289                type        = ( data.type ) ? data.type : 'info';
     1290
     1291                $adminNotice = '<div id="' + data.id + '" class="notice notice-' + data.type + dismissible + '"><p>' + data.message + '</p></div>';
     1292
     1293                // Check if this admin notice already exists.
     1294                if ( ! $notice.length ) {
     1295                        $notice = $( '#' + data.id );
     1296                }
     1297
     1298                if ( $notice.length ) {
     1299                        $notice.replaceWith( $adminNotice );
     1300                } else if ( $headerEnd.length ) {
     1301                        $headerEnd.after( $adminNotice );
     1302                } else {
     1303                        if ( 'customize' === pagenow ) {
     1304                                $( '.customize-themes-notifications' ).append( $adminNotice );
     1305                        } else {
     1306                                $( '.wrap' ).find( '> h1' ).after( $adminNotice );
     1307                        }
     1308                }
     1309
     1310                $document.trigger( 'wp-notice-added' );
     1311        };
     1312
     1313        $( '.bulkactions' ).parents( 'form' ).on( 'submit', function( event ) {
     1314                var form = this,
     1315                        submitterName = event.originalEvent && event.originalEvent.submitter ? event.originalEvent.submitter.name : false;
     1316
     1317                // Observe submissions from posts lists for 'bulk_action' or users lists for 'new_role'.
     1318                var bulkFieldRelations = {
     1319                        'bulk_action' : 'action',
     1320                        'changeit' : 'new_role'
     1321                };
     1322                if ( ! Object.keys( bulkFieldRelations ).includes( submitterName ) ) {
     1323                        return;
     1324                }
     1325
     1326                var values = new FormData(form);
     1327                var value = values.get( bulkFieldRelations[ submitterName ] ) || '-1';
     1328
     1329                // Check that the action is not the default one.
     1330                if ( value !== '-1' ) {
     1331                        // Check that at least one item is selected.
     1332                        var itemsSelected = form.querySelectorAll( '.wp-list-table tbody .check-column input[type="checkbox"]:checked' );
     1333
     1334                        if ( itemsSelected.length > 0 ) {
     1335                                return;
     1336                        }
     1337                }
     1338                event.preventDefault();
     1339                event.stopPropagation();
     1340                $( 'html, body' ).animate( { scrollTop: 0 } );
     1341
     1342                var errorMessage = __( 'Please select at least one item to perform this action on.' );
     1343                addAdminNotice( {
     1344                        id: 'no-items-selected',
     1345                        type: 'error',
     1346                        message: errorMessage,
     1347                        dismissible: true,
     1348                } );
     1349
     1350                wp.a11y.speak( errorMessage );
     1351        });
    12781352
    12791353        /**
  • trunk/src/js/_enqueues/admin/inline-edit-post.js

    r59016 r59134  
    149149                 */
    150150                $('#doaction').on( 'click', function(e){
    151                         var n;
     151                        var n,
     152                                $itemsSelected = $( '#posts-filter .check-column input[type="checkbox"]:checked' );
     153
     154                        if ( $itemsSelected.length < 1 ) {
     155                                return;
     156                        }
    152157
    153158                        t.whichBulkButtonId = $( this ).attr( 'id' );
  • trunk/src/js/_enqueues/wp/updates.js

    r58957 r59134  
    28332833                        // Bail if there were no items selected.
    28342834                        if ( ! itemsSelected.length ) {
    2835                                 event.preventDefault();
    2836                                 $( 'html, body' ).animate( { scrollTop: 0 } );
    2837 
    2838                                 return wp.updates.addAdminNotice( {
    2839                                         id:        'no-items-selected',
    2840                                         className: 'notice-error is-dismissible',
    2841                                         message:   __( 'Please select at least one item to perform this action on.' )
    2842                                 } );
     2835                                bulkAction = false;
    28432836                        }
    28442837
  • trunk/src/wp-admin/includes/class-wp-list-table.php

    r58745 r59134  
    626626                echo "</select>\n";
    627627
    628                 submit_button( __( 'Apply' ), 'action', '', false, array( 'id' => "doaction$two" ) );
     628                submit_button( __( 'Apply' ), 'action', 'bulk_action', false, array( 'id' => "doaction$two" ) );
    629629                echo "\n";
    630630        }
  • trunk/src/wp-includes/script-loader.php

    r59129 r59134  
    755755        );
    756756
    757         $scripts->add( 'common', "/wp-admin/js/common$suffix.js", array( 'jquery', 'hoverIntent', 'utils' ), false, 1 );
     757        $scripts->add( 'common', "/wp-admin/js/common$suffix.js", array( 'jquery', 'hoverIntent', 'utils', 'wp-a11y' ), false, 1 );
    758758        $scripts->set_translations( 'common' );
    759759
Note: See TracChangeset for help on using the changeset viewer.