Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions src/js/_enqueues/admin/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -1287,20 +1287,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 <p> if paragraph_wrap is true.
if ( paragraphWrap ) {
message = '<p>' + message + '</p>';
}

$adminNotice = '<div id="' + data.id + '" class="notice notice-' + data.type + dismissible + '"><p>' + data.message + '</p></div>';
// Build the admin notice element.
$adminNotice = '<div id="' + data.id + '" class="notice notice-' + type + dismissible + additionalClasses + '"' + attributes + '>' + message + '</div>';

// 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 ) {
Expand All @@ -1313,7 +1335,8 @@ $( function() {
}
}

$document.trigger( 'wp-notice-added' );
// Trigger a global event after adding the notice.
$(document).trigger( 'wp-notice-added' );
};

$( '.bulkactions' ).parents( 'form' ).on( 'submit', function( event ) {
Expand Down
27 changes: 4 additions & 23 deletions src/js/_enqueues/wp/updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Loading