Make WordPress Core


Ignore:
Timestamp:
09/21/2023 06:22:10 PM (9 months ago)
Author:
joedolson
Message:

Login and Registration: Improve HTML for errors and notices.

Improve markup on Login and Registration errors. Use list markup for multiple issues, paragraph when only one to reduce semantic burden in the most common case. Normalize classes and markup for wrapper using wp_admin_notice() and wp_get_admin_notice() functions. Move definition of those functions from wp-admin\includes\misc.php to wp-includes\functions.php. Move tests to functions group.

Props extendwings, sabernhardt, afercia, lukecavanagh, rianrietveld, oglekler, sergeybiryukov, costdev, joedolson.
Fixes #30685.

File:
1 edited

Legend:

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

    r56603 r56654  
    16431643    return $response;
    16441644}
    1645 
    1646 /**
    1647  * Creates and returns the markup for an admin notice.
    1648  *
    1649  * @since 6.4.0
    1650  *
    1651  * @param string $message The message.
    1652  * @param array  $args {
    1653  *     Optional. An array of arguments for the admin notice. Default empty array.
    1654  *
    1655  *     @type string   $type               Optional. The type of admin notice.
    1656  *                                        For example, 'error', 'success', 'warning', 'info'.
    1657  *                                        Default empty string.
    1658  *     @type bool     $dismissible        Optional. Whether the admin notice is dismissible. Default false.
    1659  *     @type string   $id                 Optional. The value of the admin notice's ID attribute. Default empty string.
    1660  *     @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.
    1662  *     @type bool     $paragraph_wrap     Optional. Whether to wrap the message in paragraph tags. Default true.
    1663  * }
    1664  * @return string The markup for an admin notice.
    1665  */
    1666 function wp_get_admin_notice( $message, $args = array() ) {
    1667     $defaults = array(
    1668         'type'               => '',
    1669         'dismissible'        => false,
    1670         'id'                 => '',
    1671         'additional_classes' => array(),
    1672         'attributes'         => array(),
    1673         'paragraph_wrap'     => true,
    1674     );
    1675 
    1676     $args = wp_parse_args( $args, $defaults );
    1677 
    1678     /**
    1679      * Filters the arguments for an admin notice.
    1680      *
    1681      * @since 6.4.0
    1682      *
    1683      * @param array  $args    The arguments for the admin notice.
    1684      * @param string $message The message for the admin notice.
    1685      */
    1686     $args       = apply_filters( 'wp_admin_notice_args', $args, $message );
    1687     $id         = '';
    1688     $classes    = 'notice';
    1689     $attributes = '';
    1690 
    1691     if ( is_string( $args['id'] ) ) {
    1692         $trimmed_id = trim( $args['id'] );
    1693 
    1694         if ( '' !== $trimmed_id ) {
    1695             $id = 'id="' . $trimmed_id . '" ';
    1696         }
    1697     }
    1698 
    1699     if ( is_string( $args['type'] ) ) {
    1700         $type = trim( $args['type'] );
    1701 
    1702         if ( str_contains( $type, ' ' ) ) {
    1703             _doing_it_wrong(
    1704                 __FUNCTION__,
    1705                 sprintf(
    1706                     /* translators: %s: The "type" key. */
    1707                     __( 'The %s key must be a string without spaces.' ),
    1708                     '<code>type</code>'
    1709                 ),
    1710                 '6.4.0'
    1711             );
    1712         }
    1713 
    1714         if ( '' !== $type ) {
    1715             $classes .= ' notice-' . $type;
    1716         }
    1717     }
    1718 
    1719     if ( true === $args['dismissible'] ) {
    1720         $classes .= ' is-dismissible';
    1721     }
    1722 
    1723     if ( is_array( $args['additional_classes'] ) && ! empty( $args['additional_classes'] ) ) {
    1724         $classes .= ' ' . implode( ' ', $args['additional_classes'] );
    1725     }
    1726 
    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 
    1740     if ( false !== $args['paragraph_wrap'] ) {
    1741         $message = "<p>$message</p>";
    1742     }
    1743 
    1744     $markup = sprintf( '<div %1$sclass="%2$s"%3$s>%4$s</div>', $id, $classes, $attributes, $message );
    1745 
    1746     /**
    1747      * Filters the markup for an admin notice.
    1748      *
    1749      * @since 6.4.0
    1750      *
    1751      * @param string $markup  The HTML markup for the admin notice.
    1752      * @param string $message The message for the admin notice.
    1753      * @param array  $args    The arguments for the admin notice.
    1754      */
    1755     return apply_filters( 'wp_admin_notice_markup', $markup, $message, $args );
    1756 }
    1757 
    1758 /**
    1759  * Outputs an admin notice.
    1760  *
    1761  * @since 6.4.0
    1762  *
    1763  * @param string $message The message to output.
    1764  * @param array  $args {
    1765  *     Optional. An array of arguments for the admin notice. Default empty array.
    1766  *
    1767  *     @type string   $type               Optional. The type of admin notice.
    1768  *                                        For example, 'error', 'success', 'warning', 'info'.
    1769  *                                        Default empty string.
    1770  *     @type bool     $dismissible        Optional. Whether the admin notice is dismissible. Default false.
    1771  *     @type string   $id                 Optional. The value of the admin notice's ID attribute. Default empty string.
    1772  *     @type string[] $additional_classes Optional. A string array of class names. Default empty array.
    1773  *     @type bool     $paragraph_wrap     Optional. Whether to wrap the message in paragraph tags. Default true.
    1774  * }
    1775  */
    1776 function wp_admin_notice( $message, $args = array() ) {
    1777     /**
    1778      * Fires before an admin notice is output.
    1779      *
    1780      * @since 6.4.0
    1781      *
    1782      * @param string $message The message for the admin notice.
    1783      * @param array  $args    The arguments for the admin notice.
    1784      */
    1785     do_action( 'wp_admin_notice', $message, $args );
    1786 
    1787     echo wp_kses_post( wp_get_admin_notice( $message, $args ) );
    1788 }
Note: See TracChangeset for help on using the changeset viewer.