Make WordPress Core

Changeset 56603


Ignore:
Timestamp:
09/18/2023 12:28:24 AM (3 years ago)
Author:
joedolson
Message:

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.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/misc.php

    r56549 r56603  
    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 * }
     
    16691670                'id'                 => '',
    16701671                'additional_classes' => array(),
     1672                'attributes'         => array(),
    16711673                'paragraph_wrap'     => true,
    16721674        );
     
    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'] ) ) {
     
    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        /**
  • trunk/src/wp-includes/kses.php

    r56559 r56603  
    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,
  • trunk/tests/phpunit/tests/admin/wpAdminNotice.php

    r56408 r56603  
    212212                                ),
    213213                                'expected' => '<div class="notice"><p>A notice with additional classes that are not an array.</p></div>',
     214                        ),
     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>',
    214279                        ),
    215280                        'paragraph wrapping as a falsy value rather than (bool) false' => array(
  • trunk/tests/phpunit/tests/admin/wpGetAdminNotice.php

    r56408 r56603  
    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.',
Note: See TracChangeset for help on using the changeset viewer.