From e93cd704951cf5213b649e3e2111e9ac68ae6259 Mon Sep 17 00:00:00 2001 From: vinit khollam Date: Thu, 3 Oct 2024 12:19:11 +0530 Subject: [PATCH 1/3] Added : Refactor admin notice handling to eliminate duplication --- src/js/_enqueues/admin/common.js | 17 +++++++++++++++-- src/js/_enqueues/wp/updates.js | 27 ++++----------------------- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/src/js/_enqueues/admin/common.js b/src/js/_enqueues/admin/common.js index 1aa35a37dba29..67d0eae75f7d1 100644 --- a/src/js/_enqueues/admin/common.js +++ b/src/js/_enqueues/admin/common.js @@ -1288,7 +1288,20 @@ $( function() { dismissible = ( data.dismissible && data.dismissible === true ) ? ' is-dismissible' : ''; type = ( data.type ) ? data.type : 'info'; - $adminNotice = '

' + data.message + '

'; + // Support for multiple message formats + var message = data.message; + if (data.successes || data.errors) { + message = 'Success: ' + data.successes + ' operations completed. Errors: ' + data.errors; + if (data.errorMessages) { + message += ''; + } + } + + $adminNotice = '

' + message + '

'; // Check if this admin notice already exists. if ( ! $notice.length ) { @@ -1307,7 +1320,7 @@ $( function() { } } - $document.trigger( 'wp-notice-added' ); + $(document).trigger( 'wp-notice-added' ); }; $( '.bulkactions' ).parents( 'form' ).on( 'submit', function( event ) { diff --git a/src/js/_enqueues/wp/updates.js b/src/js/_enqueues/wp/updates.js index 396fcbb6ffe06..f522671b7bf70 100644 --- a/src/js/_enqueues/wp/updates.js +++ b/src/js/_enqueues/wp/updates.js @@ -251,33 +251,14 @@ * */ wp.updates.addAdminNotice = function( data ) { - var $notice = $( data.selector ), - $headerEnd = $( '.wp-header-end' ), - $adminNotice; + // Simply delegate to the global addAdminNotice function. - delete data.selector; - $adminNotice = wp.updates.adminNotice( data ); + addAdminNotice( data ); - // 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-updates-notice-added' ); + $(document).trigger( 'wp-updates-notice-added' ); }; + /** * Handles Ajax requests to WordPress. * From 1e0e1a95db41c00f12575d8af7a9376406969d84 Mon Sep 17 00:00:00 2001 From: vinit khollam Date: Tue, 8 Oct 2024 00:26:16 +0530 Subject: [PATCH 2/3] Removed : extra formatting of strings in message for admin notice --- src/js/_enqueues/admin/common.js | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/js/_enqueues/admin/common.js b/src/js/_enqueues/admin/common.js index 67d0eae75f7d1..6fe6e489699de 100644 --- a/src/js/_enqueues/admin/common.js +++ b/src/js/_enqueues/admin/common.js @@ -1288,19 +1288,7 @@ $( function() { dismissible = ( data.dismissible && data.dismissible === true ) ? ' is-dismissible' : ''; type = ( data.type ) ? data.type : 'info'; - // Support for multiple message formats var message = data.message; - if (data.successes || data.errors) { - message = 'Success: ' + data.successes + ' operations completed. Errors: ' + data.errors; - if (data.errorMessages) { - message += ''; - } - } - $adminNotice = '

' + message + '

'; // Check if this admin notice already exists. From c399a3ce061ff2401bcf85720a5f581c301a3f5b Mon Sep 17 00:00:00 2001 From: vinit khollam Date: Wed, 9 Oct 2024 13:29:44 +0530 Subject: [PATCH 3/3] Added : Extra arguments similar to PHP admin notice function --- src/js/_enqueues/admin/common.js | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/js/_enqueues/admin/common.js b/src/js/_enqueues/admin/common.js index 6fe6e489699de..1e0c9f918f00a 100644 --- a/src/js/_enqueues/admin/common.js +++ b/src/js/_enqueues/admin/common.js @@ -1281,21 +1281,42 @@ $( function() { $headerEnd = $( '.wp-header-end' ), type, dismissible, + additionalClasses = '', + attributes = '', + paragraphWrap = ( data.paragraph_wrap !== undefined ) ? data.paragraph_wrap : true, $adminNotice; - delete data.selector; - + // Defaults for the arguments. + type = data.type ? data.type : 'info'; dismissible = ( data.dismissible && data.dismissible === true ) ? ' is-dismissible' : ''; - type = ( data.type ) ? data.type : 'info'; + var message = data.message ? data.message : ''; + + // Handle additional classes if any are provided. + if ( data.additional_classes && Array.isArray( data.additional_classes ) ) { + additionalClasses = ' ' + data.additional_classes.join( ' ' ); + } + + // Handle additional attributes if any are provided. + if ( data.attributes && typeof data.attributes === 'object' ) { + Object.keys( data.attributes ).forEach(function( key ) { + attributes += ' ' + key + '="' + data.attributes[key] + '"'; + }); + } + + // Wrap message in

if paragraph_wrap is true. + if ( paragraphWrap ) { + message = '

' + message + '

'; + } - var message = data.message; - $adminNotice = '

' + message + '

'; + // Build the admin notice element. + $adminNotice = '
' + message + '
'; // Check if this admin notice already exists. if ( ! $notice.length ) { $notice = $( '#' + data.id ); } + // Either replace an existing notice or insert a new one. if ( $notice.length ) { $notice.replaceWith( $adminNotice ); } else if ( $headerEnd.length ) { @@ -1308,6 +1329,7 @@ $( function() { } } + // Trigger a global event after adding the notice. $(document).trigger( 'wp-notice-added' ); };