Skip to content

Commit 4bcf32a

Browse files
committed
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
1 parent 4e0e460 commit 4bcf32a

4 files changed

Lines changed: 146 additions & 5 deletions

File tree

src/wp-admin/includes/misc.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,7 @@ function wp_check_php_version() {
16581658
* @type bool $dismissible Optional. Whether the admin notice is dismissible. Default false.
16591659
* @type string $id Optional. The value of the admin notice's ID attribute. Default empty string.
16601660
* @type string[] $additional_classes Optional. A string array of class names. Default empty array.
1661+
* @type string[] $attributes Optional. Additional attributes for the notice div. Default empty array.
16611662
* @type bool $paragraph_wrap Optional. Whether to wrap the message in paragraph tags. Default true.
16621663
* }
16631664
* @return string The markup for an admin notice.
@@ -1668,6 +1669,7 @@ function wp_get_admin_notice( $message, $args = array() ) {
16681669
'dismissible' => false,
16691670
'id' => '',
16701671
'additional_classes' => array(),
1672+
'attributes' => array(),
16711673
'paragraph_wrap' => true,
16721674
);
16731675

@@ -1681,9 +1683,10 @@ function wp_get_admin_notice( $message, $args = array() ) {
16811683
* @param array $args The arguments for the admin notice.
16821684
* @param string $message The message for the admin notice.
16831685
*/
1684-
$args = apply_filters( 'wp_admin_notice_args', $args, $message );
1685-
$id = '';
1686-
$classes = 'notice';
1686+
$args = apply_filters( 'wp_admin_notice_args', $args, $message );
1687+
$id = '';
1688+
$classes = 'notice';
1689+
$attributes = '';
16871690

16881691
if ( is_string( $args['id'] ) ) {
16891692
$trimmed_id = trim( $args['id'] );
@@ -1721,11 +1724,24 @@ function wp_get_admin_notice( $message, $args = array() ) {
17211724
$classes .= ' ' . implode( ' ', $args['additional_classes'] );
17221725
}
17231726

1727+
if ( is_array( $args['attributes'] ) && ! empty( $args['attributes'] ) ) {
1728+
$attributes = '';
1729+
foreach ( $args['attributes'] as $attr => $val ) {
1730+
if ( is_bool( $val ) ) {
1731+
$attributes .= $val ? ' ' . $attr : '';
1732+
} elseif ( is_int( $attr ) ) {
1733+
$attributes .= ' ' . esc_attr( trim( $val ) );
1734+
} elseif ( $val ) {
1735+
$attributes .= ' ' . $attr . '="' . esc_attr( trim( $val ) ) . '"';
1736+
}
1737+
}
1738+
}
1739+
17241740
if ( false !== $args['paragraph_wrap'] ) {
17251741
$message = "<p>$message</p>";
17261742
}
17271743

1728-
$markup = sprintf( '<div %1$sclass="%2$s">%3$s</div>', $id, $classes, $message );
1744+
$markup = sprintf( '<div %1$sclass="%2$s"%3$s>%4$s</div>', $id, $classes, $attributes, $message );
17291745

17301746
/**
17311747
* Filters the markup for an admin notice.

src/wp-includes/kses.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2645,12 +2645,14 @@ function _wp_add_global_attributes( $value ) {
26452645
'aria-describedby' => true,
26462646
'aria-details' => true,
26472647
'aria-expanded' => true,
2648+
'aria-hidden' => true,
26482649
'aria-label' => true,
26492650
'aria-labelledby' => true,
2650-
'aria-hidden' => true,
2651+
'aria-live' => true,
26512652
'class' => true,
26522653
'data-*' => true,
26532654
'dir' => true,
2655+
'hidden' => true,
26542656
'id' => true,
26552657
'lang' => true,
26562658
'style' => true,

tests/phpunit/tests/admin/wpAdminNotice.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,71 @@ public function data_should_output_admin_notice() {
212212
),
213213
'expected' => '<div class="notice"><p>A notice with additional classes that are not an array.</p></div>',
214214
),
215+
'additional attribute with a value' => array(
216+
'message' => 'A notice with an additional attribute with a value.',
217+
'args' => array(
218+
'attributes' => array( 'aria-live' => 'assertive' ),
219+
),
220+
'expected' => '<div class="notice" aria-live="assertive"><p>A notice with an additional attribute with a value.</p></div>',
221+
),
222+
'additional hidden attribute' => array(
223+
'message' => 'A notice with the hidden attribute.',
224+
'args' => array(
225+
'attributes' => array( 'hidden' => true ),
226+
),
227+
'expected' => '<div class="notice" hidden><p>A notice with the hidden attribute.</p></div>',
228+
),
229+
'additional attribute no associative keys' => array(
230+
'message' => 'A notice with a boolean attribute without an associative key.',
231+
'args' => array(
232+
'attributes' => array( 'hidden' ),
233+
),
234+
'expected' => '<div class="notice" hidden><p>A notice with a boolean attribute without an associative key.</p></div>',
235+
),
236+
'additional attribute with role' => array(
237+
'message' => 'A notice with an additional attribute role.',
238+
'args' => array(
239+
'attributes' => array( 'role' => 'alert' ),
240+
),
241+
'expected' => '<div class="notice" role="alert"><p>A notice with an additional attribute role.</p></div>',
242+
),
243+
'multiple additional attributes' => array(
244+
'message' => 'A notice with multiple additional attributes.',
245+
'args' => array(
246+
'attributes' => array(
247+
'role' => 'alert',
248+
'data-test' => -1,
249+
),
250+
),
251+
'expected' => '<div class="notice" role="alert" data-test="-1"><p>A notice with multiple additional attributes.</p></div>',
252+
),
253+
'data attribute with unsafe value' => array(
254+
'message' => 'A notice with an additional attribute with an unsafe value.',
255+
'args' => array(
256+
'attributes' => array( 'data-unsafe' => '<script>alert( "Howdy, admin!" );</script>' ),
257+
),
258+
'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>',
259+
),
260+
'additional invalid attribute' => array(
261+
'message' => 'A notice with an additional attribute that is invalid.',
262+
'args' => array(
263+
'attributes' => array( 'not-valid' => 'not-valid' ),
264+
),
265+
'expected' => '<div class="notice"><p>A notice with an additional attribute that is invalid.</p></div>',
266+
),
267+
'multiple attributes with "role", invalid, data-*, numeric, and boolean' => array(
268+
'message' => 'A notice with multiple attributes with "role", invalid, "data-*", numeric, and boolean.',
269+
'args' => array(
270+
'attributes' => array(
271+
'role' => 'alert',
272+
'disabled' => 'disabled',
273+
'data-name' => 'my-name',
274+
'data-id' => 1,
275+
'hidden',
276+
),
277+
),
278+
'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>',
279+
),
215280
'paragraph wrapping as a falsy value rather than (bool) false' => array(
216281
'message' => 'A notice with paragraph wrapping as a falsy value rather than (bool) false.',
217282
'args' => array(

tests/phpunit/tests/admin/wpGetAdminNotice.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,64 @@ public function data_should_return_admin_notice() {
208208
),
209209
'expected' => '<div class="notice"><p>A notice with additional classes that are not an array.</p></div>',
210210
),
211+
'additional attribute with a value' => array(
212+
'message' => 'A notice with an additional attribute with a value.',
213+
'args' => array(
214+
'attributes' => array( 'aria-live' => 'assertive' ),
215+
),
216+
'expected' => '<div class="notice" aria-live="assertive"><p>A notice with an additional attribute with a value.</p></div>',
217+
),
218+
'additional hidden attribute' => array(
219+
'message' => 'A notice with the hidden attribute.',
220+
'args' => array(
221+
'attributes' => array( 'hidden' => true ),
222+
),
223+
'expected' => '<div class="notice" hidden><p>A notice with the hidden attribute.</p></div>',
224+
),
225+
'additional attribute no associative keys' => array(
226+
'message' => 'A notice with a boolean attribute without an associative key.',
227+
'args' => array(
228+
'attributes' => array( 'hidden' ),
229+
),
230+
'expected' => '<div class="notice" hidden><p>A notice with a boolean attribute without an associative key.</p></div>',
231+
),
232+
'additional attribute with role' => array(
233+
'message' => 'A notice with an additional attribute role.',
234+
'args' => array(
235+
'attributes' => array( 'role' => 'alert' ),
236+
),
237+
'expected' => '<div class="notice" role="alert"><p>A notice with an additional attribute role.</p></div>',
238+
),
239+
'multiple additional attributes' => array(
240+
'message' => 'A notice with multiple additional attributes.',
241+
'args' => array(
242+
'attributes' => array(
243+
'role' => 'alert',
244+
'data-test' => -1,
245+
),
246+
),
247+
'expected' => '<div class="notice" role="alert" data-test="-1"><p>A notice with multiple additional attributes.</p></div>',
248+
),
249+
'data attribute with unsafe value' => array(
250+
'message' => 'A notice with an additional attribute with an unsafe value.',
251+
'args' => array(
252+
'attributes' => array( 'data-unsafe' => '<script>alert( "Howdy, admin!" );</script>' ),
253+
),
254+
'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>',
255+
),
256+
'multiple attributes with "role", invalid, data-*, numeric, and boolean' => array(
257+
'message' => 'A notice with multiple attributes with "role", invalid, "data-*", numeric, and boolean.',
258+
'args' => array(
259+
'attributes' => array(
260+
'role' => 'alert',
261+
'disabled' => 'disabled',
262+
'data-name' => 'my-name',
263+
'data-id' => 1,
264+
'hidden',
265+
),
266+
),
267+
'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>',
268+
),
211269
'paragraph wrapping as a falsy value rather than (bool) false' => array(
212270
'message' => 'A notice with paragraph wrapping as a falsy value rather than (bool) false.',
213271
'args' => array(

0 commit comments

Comments
 (0)