Make WordPress Core

Opened 8 months ago

Last modified 3 months ago

#62139 assigned enhancement

Broaden use of `addAdminNotice()`

Reported by: joedolson's profile joedolson Owned by:
Milestone: 6.9 Priority: normal
Severity: normal Version:
Component: General Keywords: has-patch
Focuses: javascript Cc:

Description

#58479 added addAdminNotice() to common.js in order to display an on-demand notice with bulk editing. This function is largely duplicating wp.updates.addAdminNotice(), and updates.js has common.js as a dependency.

We should abstract the new function and update wp.updates.addAdminNotice() to pass arguments into the new function. We'll then be able to use addAdminNotice() as a general mechanism in core to insert JS driven notices.

See comments at https://core.trac.wordpress.org/ticket/58479#comment:42, see #57791 for PHP equivalent.

Change History (13)

This ticket was mentioned in PR #7485 on WordPress/wordpress-develop by @vineet2003.


7 months ago
#1

  • Keywords has-patch added; needs-patch removed

This PR refactors the handling of admin notices in WordPress to eliminate code duplication and enhance standardization across the platform. The following key changes have been implemented:

Centralization of Admin Notice Functionality: The addAdminNotice() function in common.js has been refactored to support all arguments previously handled by wp.updates.addAdminNotice(). This allows for a unified approach to displaying admin notices with enhanced flexibility for various use cases (e.g., bulk editing, updates).

Backward Compatibility: The wp.updates.addAdminNotice() function in updates.js has been modified to simply delegate calls to the newly refactored addAdminNotice() function. This change ensures that existing usages of wp.updates.addAdminNotice() continue to function without issues, maintaining backward compatibility.

Improved Message Formatting: The new implementation supports multiple message formats, including success and error messages, thereby providing users with clearer feedback when actions are performed in the admin area.


Trac Ticket

Trac ticket: #62139

#2 @vineet2003
7 months ago

@joedolson please have a look at this PR i've raised and let me know if you want any changes in the wrapper functions .

#3 @joedolson
7 months ago

@vineet2003 It's a great start, but to make it broadly usable across core, it's going to need to have expanded support for some additional conditions. I made a comment in the PR with more details. Thanks!

@vineet2003 commented on PR #7485:


7 months ago
#4

Do we need to rewrite this function in a way that it needs to merge the passed data into a default configuration, similar to how the PHP equivalent works ? Handle custom classes and additional attributes flexibly.

What i think we can do is

The addAdminNotice() function should accept parameters like:

  1. type: For notice types (error, success, warning, info).
  2. dismissible: To determine if the notice can be dismissed.
  3. id: A unique identifier for the notice.
  4. additional_classes: An array of extra CSS classes.
  5. attributes: Extra HTML attributes for the notice element.
  6. paragraph_wrap: To determine if the message should be wrapped in a p tag.

I will do this changes and will update the PR @joedolson but just let me know if this is what we actually want or its in the wrong direction .

@vineet2003 commented on PR #7485:


7 months ago
#5

	var addAdminNotice = function( data ) {
		var $notice = $( data.selector ),
			$headerEnd = $( '.wp-header-end' ),
			type,
			dismissible,
			additionalClasses = '',
			attributes = '',
			paragraphWrap = ( data.paragraph_wrap !== undefined ) ? data.paragraph_wrap : true,
			$adminNotice;
	
		// Defaults for the arguments.
		type = data.type ? data.type : 'info';
		dismissible = ( data.dismissible && data.dismissible === true ) ? ' is-dismissible' : '';
		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>';
		}
	
		// 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 ) {
			$headerEnd.after( $adminNotice );
		} else {
			if ( 'customize' === pagenow ) {
				$( '.customize-themes-notifications' ).append( $adminNotice );
			} else {
				$( '.wrap' ).find( '> h1' ).after( $adminNotice );
			}
		}
	
		// Trigger a global event after adding the notice.
		$(document).trigger( 'wp-notice-added' );
	};

Have a look at this @joedolson we can do this to make our existing JS function handle additional requirements similar to PHP admin notice arguments.

@joedolson commented on PR #7485:


7 months ago
#6

Yes, that's what I had in mind. We need to anticipate possible usages, so that if extenders start to use this, it's flexible enough for usage.

@vineet2003 commented on PR #7485:


7 months ago
#7

Yes, that's what I had in mind. We need to anticipate possible usages, so that if extenders start to use this, it's flexible enough for usage.

Anything else to be done or is this patch ready to be merged .... it will be my first contribution 😄 !!!

@vineet2003 commented on PR #7485:


7 months ago
#8

@joedolson - please check this function i have added in latest commit

@vineet2003 commented on PR #7485:


7 months ago
#9

@joedolson anything left from my side to be done for this PR?

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


3 months ago

#11 @audrasjb
3 months ago

  • Focuses javascript added

As per today's bug scrub:
@joedolson is this still in your radar for 6.8? Should we move it to Future Release of 6.9?

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


3 months ago

#13 @audrasjb
3 months ago

  • Milestone changed from 6.8 to 6.9

As per today's bug scrub: It appears this ticket still needs some work. As 6.8 beta 1 is very close, I'm moving it to 6.9. Feel free to move it back to 6.8 if it can be committed by Monday.

Note: See TracTickets for help on using tickets.