Make WordPress Core


Ignore:
Timestamp:
07/06/2020 08:42:14 PM (5 years ago)
Author:
afercia
Message:

Accessibility: Widgets: Add theme support to make widgets output list of links wrapped within a <nav> element.

Widgets that output list of links can now be wrapped within <nav> elements to improve semantics and accessibility.

The <nav> elements are also native landmark regions, which helps assistive technology users to navigate through them. Themes can opt-in to this new behavior by declaring support for the new html5 feature navigation-widgets.

Props joedolson, simonjanin, audrasjb, afercia.
Fixes #48170.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/widgets/class-wp-widget-archives.php

    r47593 r48349  
    4141     */
    4242    public function widget( $args, $instance ) {
    43         $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Archives' );
     43        $default_title = __( 'Archives' );
     44        $title         = ! empty( $instance['title'] ) ? $instance['title'] : $default_title;
    4445
    4546        /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
     
    121122/* ]]> */
    122123</script>
    123 
    124         <?php } else { ?>
    125         <ul>
    126124            <?php
    127             wp_get_archives(
    128                 /**
    129                  * Filters the arguments for the Archives widget.
    130                  *
    131                  * @since 2.8.0
    132                  * @since 4.9.0 Added the `$instance` parameter.
    133                  *
    134                  * @see wp_get_archives()
    135                  *
    136                  * @param array $args     An array of Archives option arguments.
    137                  * @param array $instance Array of settings for the current widget.
    138                  */
    139                 apply_filters(
    140                     'widget_archives_args',
    141                     array(
    142                         'type'            => 'monthly',
    143                         'show_post_count' => $count,
    144                     ),
    145                     $instance
    146                 )
    147             );
     125        } else {
     126            $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
     127
     128            /**
     129             * Filters the HTML format of widgets with navigation links.
     130             *
     131             * @since 5.5.0
     132             *
     133             * @param string $format The type of markup to use in widgets with navigation links.
     134             *                       Accepts 'html5', 'xhtml'.
     135             */
     136            $format = apply_filters( 'navigation_widgets_format', $format );
     137
     138            if ( 'html5' === $format ) {
     139                // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
     140                $title      = trim( strip_tags( $title ) );
     141                $aria_label = $title ? $title : $default_title;
     142                echo '<nav role="navigation" aria-label="' . esc_attr( $aria_label ) . '">';
     143            }
    148144            ?>
    149         </ul>
     145
     146            <ul>
     147                <?php
     148                wp_get_archives(
     149                    /**
     150                     * Filters the arguments for the Archives widget.
     151                     *
     152                     * @since 2.8.0
     153                     * @since 4.9.0 Added the `$instance` parameter.
     154                     *
     155                     * @see wp_get_archives()
     156                     *
     157                     * @param array $args     An array of Archives option arguments.
     158                     * @param array $instance Array of settings for the current widget.
     159                     */
     160                    apply_filters(
     161                        'widget_archives_args',
     162                        array(
     163                            'type'            => 'monthly',
     164                            'show_post_count' => $count,
     165                        ),
     166                        $instance
     167                    )
     168                );
     169                ?>
     170            </ul>
     171            <?php if ( 'html5' === $format ) : ?>
     172                </nav>
     173            <?php endif; ?>
     174
    150175            <?php
     176            echo $args['after_widget'];
    151177        }
    152 
    153         echo $args['after_widget'];
    154178    }
    155179
Note: See TracChangeset for help on using the changeset viewer.