Administration: Add support for attributes in wp_admin_notice().

Allow admin notices to be created with additional attributes. Test attributes include `hidden`, `data-*`, and `role="*"` values, which are all in use in various admin notices across core. 

This commit adds `aria-live` and `hidden` to the KSES global attributes array to support core usages.

Follow up to [56408], [56409], [56410], [56518], [56570], [56571], [56572], [56573], [56576], [56589], [56590], [56597], [56599], [56600], [56601], [56602].

Props costdev, joedolson.
See #57791.

git-svn-id: https://develop.svn.wordpress.org/trunk@56603 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Joe Dolson
2023-09-18 00:28:24 +00:00
parent 4e0e46018d
commit 4bcf32a698
4 changed files with 146 additions and 5 deletions

View File

@@ -1658,6 +1658,7 @@ function wp_check_php_version() {
* @type bool $dismissible Optional. Whether the admin notice is dismissible. Default false.
* @type string $id Optional. The value of the admin notice's ID attribute. Default empty string.
* @type string[] $additional_classes Optional. A string array of class names. Default empty array.
* @type string[] $attributes Optional. Additional attributes for the notice div. Default empty array.
* @type bool $paragraph_wrap Optional. Whether to wrap the message in paragraph tags. Default true.
* }
* @return string The markup for an admin notice.
@@ -1668,6 +1669,7 @@ function wp_get_admin_notice( $message, $args = array() ) {
'dismissible' => false,
'id' => '',
'additional_classes' => array(),
'attributes' => array(),
'paragraph_wrap' => true,
);
@@ -1681,9 +1683,10 @@ function wp_get_admin_notice( $message, $args = array() ) {
* @param array $args The arguments for the admin notice.
* @param string $message The message for the admin notice.
*/
$args = apply_filters( 'wp_admin_notice_args', $args, $message );
$id = '';
$classes = 'notice';
$args = apply_filters( 'wp_admin_notice_args', $args, $message );
$id = '';
$classes = 'notice';
$attributes = '';
if ( is_string( $args['id'] ) ) {
$trimmed_id = trim( $args['id'] );
@@ -1721,11 +1724,24 @@ function wp_get_admin_notice( $message, $args = array() ) {
$classes .= ' ' . implode( ' ', $args['additional_classes'] );
}
if ( is_array( $args['attributes'] ) && ! empty( $args['attributes'] ) ) {
$attributes = '';
foreach ( $args['attributes'] as $attr => $val ) {
if ( is_bool( $val ) ) {
$attributes .= $val ? ' ' . $attr : '';
} elseif ( is_int( $attr ) ) {
$attributes .= ' ' . esc_attr( trim( $val ) );
} elseif ( $val ) {
$attributes .= ' ' . $attr . '="' . esc_attr( trim( $val ) ) . '"';
}
}
}
if ( false !== $args['paragraph_wrap'] ) {
$message = "<p>$message</p>";
}
$markup = sprintf( '<div %1$sclass="%2$s">%3$s</div>', $id, $classes, $message );
$markup = sprintf( '<div %1$sclass="%2$s"%3$s>%4$s</div>', $id, $classes, $attributes, $message );
/**
* Filters the markup for an admin notice.

View File

@@ -2645,12 +2645,14 @@ function _wp_add_global_attributes( $value ) {
'aria-describedby' => true,
'aria-details' => true,
'aria-expanded' => true,
'aria-hidden' => true,
'aria-label' => true,
'aria-labelledby' => true,
'aria-hidden' => true,
'aria-live' => true,
'class' => true,
'data-*' => true,
'dir' => true,
'hidden' => true,
'id' => true,
'lang' => true,
'style' => true,

View File

@@ -212,6 +212,71 @@ class Tests_Admin_WpAdminNotice extends WP_UnitTestCase {
),
'expected' => '<div class="notice"><p>A notice with additional classes that are not an array.</p></div>',
),
'additional attribute with a value' => array(
'message' => 'A notice with an additional attribute with a value.',
'args' => array(
'attributes' => array( 'aria-live' => 'assertive' ),
),
'expected' => '<div class="notice" aria-live="assertive"><p>A notice with an additional attribute with a value.</p></div>',
),
'additional hidden attribute' => array(
'message' => 'A notice with the hidden attribute.',
'args' => array(
'attributes' => array( 'hidden' => true ),
),
'expected' => '<div class="notice" hidden><p>A notice with the hidden attribute.</p></div>',
),
'additional attribute no associative keys' => array(
'message' => 'A notice with a boolean attribute without an associative key.',
'args' => array(
'attributes' => array( 'hidden' ),
),
'expected' => '<div class="notice" hidden><p>A notice with a boolean attribute without an associative key.</p></div>',
),
'additional attribute with role' => array(
'message' => 'A notice with an additional attribute role.',
'args' => array(
'attributes' => array( 'role' => 'alert' ),
),
'expected' => '<div class="notice" role="alert"><p>A notice with an additional attribute role.</p></div>',
),
'multiple additional attributes' => array(
'message' => 'A notice with multiple additional attributes.',
'args' => array(
'attributes' => array(
'role' => 'alert',
'data-test' => -1,
),
),
'expected' => '<div class="notice" role="alert" data-test="-1"><p>A notice with multiple additional attributes.</p></div>',
),
'data attribute with unsafe value' => array(
'message' => 'A notice with an additional attribute with an unsafe value.',
'args' => array(
'attributes' => array( 'data-unsafe' => '<script>alert( "Howdy, admin!" );</script>' ),
),
'expected' => '<div class="notice" data-unsafe="&lt;script&gt;alert( &quot;Howdy, admin!&quot; );&lt;/script&gt;"><p>A notice with an additional attribute with an unsafe value.</p></div>',
),
'additional invalid attribute' => array(
'message' => 'A notice with an additional attribute that is invalid.',
'args' => array(
'attributes' => array( 'not-valid' => 'not-valid' ),
),
'expected' => '<div class="notice"><p>A notice with an additional attribute that is invalid.</p></div>',
),
'multiple attributes with "role", invalid, data-*, numeric, and boolean' => array(
'message' => 'A notice with multiple attributes with "role", invalid, "data-*", numeric, and boolean.',
'args' => array(
'attributes' => array(
'role' => 'alert',
'disabled' => 'disabled',
'data-name' => 'my-name',
'data-id' => 1,
'hidden',
),
),
'expected' => '<div class="notice" role="alert" data-name="my-name" data-id="1" hidden><p>A notice with multiple attributes with "role", invalid, "data-*", numeric, and boolean.</p></div>',
),
'paragraph wrapping as a falsy value rather than (bool) false' => array(
'message' => 'A notice with paragraph wrapping as a falsy value rather than (bool) false.',
'args' => array(

View File

@@ -208,6 +208,64 @@ class Tests_Admin_WpGetAdminNotice extends WP_UnitTestCase {
),
'expected' => '<div class="notice"><p>A notice with additional classes that are not an array.</p></div>',
),
'additional attribute with a value' => array(
'message' => 'A notice with an additional attribute with a value.',
'args' => array(
'attributes' => array( 'aria-live' => 'assertive' ),
),
'expected' => '<div class="notice" aria-live="assertive"><p>A notice with an additional attribute with a value.</p></div>',
),
'additional hidden attribute' => array(
'message' => 'A notice with the hidden attribute.',
'args' => array(
'attributes' => array( 'hidden' => true ),
),
'expected' => '<div class="notice" hidden><p>A notice with the hidden attribute.</p></div>',
),
'additional attribute no associative keys' => array(
'message' => 'A notice with a boolean attribute without an associative key.',
'args' => array(
'attributes' => array( 'hidden' ),
),
'expected' => '<div class="notice" hidden><p>A notice with a boolean attribute without an associative key.</p></div>',
),
'additional attribute with role' => array(
'message' => 'A notice with an additional attribute role.',
'args' => array(
'attributes' => array( 'role' => 'alert' ),
),
'expected' => '<div class="notice" role="alert"><p>A notice with an additional attribute role.</p></div>',
),
'multiple additional attributes' => array(
'message' => 'A notice with multiple additional attributes.',
'args' => array(
'attributes' => array(
'role' => 'alert',
'data-test' => -1,
),
),
'expected' => '<div class="notice" role="alert" data-test="-1"><p>A notice with multiple additional attributes.</p></div>',
),
'data attribute with unsafe value' => array(
'message' => 'A notice with an additional attribute with an unsafe value.',
'args' => array(
'attributes' => array( 'data-unsafe' => '<script>alert( "Howdy, admin!" );</script>' ),
),
'expected' => '<div class="notice" data-unsafe="&lt;script&gt;alert( &quot;Howdy, admin!&quot; );&lt;/script&gt;"><p>A notice with an additional attribute with an unsafe value.</p></div>',
),
'multiple attributes with "role", invalid, data-*, numeric, and boolean' => array(
'message' => 'A notice with multiple attributes with "role", invalid, "data-*", numeric, and boolean.',
'args' => array(
'attributes' => array(
'role' => 'alert',
'disabled' => 'disabled',
'data-name' => 'my-name',
'data-id' => 1,
'hidden',
),
),
'expected' => '<div class="notice" role="alert" disabled="disabled" data-name="my-name" data-id="1" hidden><p>A notice with multiple attributes with "role", invalid, "data-*", numeric, and boolean.</p></div>',
),
'paragraph wrapping as a falsy value rather than (bool) false' => array(
'message' => 'A notice with paragraph wrapping as a falsy value rather than (bool) false.',
'args' => array(