Make WordPress Core

Ticket #38659: 38659.1.patch

File 38659.1.patch, 3.2 KB (added by sami.keijonen, 8 years ago)
  • src/wp-content/themes/twentyseventeen/inc/icon-functions.php

     
    4949                'icon'        => '',
    5050                'title'       => '',
    5151                'desc'        => '',
    52                 'aria_hidden' => true, // Hide from screen readers.
    5352                'fallback'    => false,
    5453        );
    5554
     
    5756        $args = wp_parse_args( $args, $defaults );
    5857
    5958        // Set aria hidden.
    60         $aria_hidden = '';
     59        $aria_hidden = ' aria-hidden="true"';
    6160
    62         if ( true === $args['aria_hidden'] ) {
    63                 $aria_hidden = ' aria-hidden="true"';
    64         }
    65 
    6661        // Set ARIA.
    6762        $aria_labelledby = '';
    6863
    69         if ( $args['title'] && $args['desc'] ) {
    70                 $aria_labelledby = ' aria-labelledby="title desc"';
     64        /*
     65         * Twentyseventeen doesn't use title or description anywhere in the theme because all the icons are decorative.
     66         *
     67         * But child themes can use title and desc arguments for none decorative icons in accessible way.
     68         *
     69         * Example 1 with title: <?php echo twentyseventeen_get_svg( array( 'icon' => 'arrow-right', 'title' => __( 'This is title', 'textdomain' ) ) ); ?>
     70         *
     71         * Example 2 with title and desc: <?php echo twentyseventeen_get_svg( array( 'icon' => 'arrow-right', 'title' => __( 'This is title', 'textdomain' ), 'desc' => __( 'This is longer desc', 'textdomain' ) ) ); ?>
     72         *
     73         * See https://www.paciellogroup.com/blog/2013/12/using-aria-enhance-svg-accessibility/.
     74         */
     75        if ( $args['title'] ) {
     76                $aria_hidden     = '';
     77                $unique_id       = uniqid();
     78                $aria_labelledby = ' aria-labelledby="title-' . $unique_id . '"';
     79
     80                if ( $args['desc'] ) {
     81                        $aria_labelledby = ' aria-labelledby="title-' . $unique_id . ' desc-' . $unique_id . '"';
     82                }
    7183        }
    7284
    7385        // Begin SVG markup.
    7486        $svg = '<svg class="icon icon-' . esc_attr( $args['icon'] ) . '"' . $aria_hidden . $aria_labelledby . ' role="img">';
    7587
    76         // If there is a title, display it.
     88        // Display the title.
    7789        if ( $args['title'] ) {
    78                 $svg .= '<title>' . esc_html( $args['title'] ) . '</title>';
    79         }
     90                $svg .= '<title id="title-' . $unique_id . '">' . esc_html( $args['title'] ) . '</title>';
    8091
    81         // If there is a description, display it.
    82         if ( $args['desc'] ) {
    83                 $svg .= '<desc>' . esc_html( $args['desc'] ) . '</desc>';
     92                // Display the desc only if the title is already set.
     93                if ( $args['desc'] ) {
     94                        $svg .= '<desc id="desc-' . $unique_id . '">' . esc_html( $args['desc'] ) . '</desc>';
     95                }
    8496        }
    8597
    86         // Use absolute path in the Customizer so that icons show up in there.
    87         if ( is_customize_preview() ) {
    88                 $svg .= '<use xlink:href="' . get_parent_theme_file_uri( '/assets/images/svg-icons.svg#icon-' . esc_html( $args['icon'] ) ) . '"></use>';
    89         } else {
    90                 $svg .= '<use xlink:href="#icon-' . esc_html( $args['icon'] ) . '"></use>';
    91         }
     98        /*
     99         * Display the icon.
     100         *
     101         * The whitespace around `<use>` is intentional. See https://core.trac.wordpress.org/ticket/38387.
     102         */
     103        $svg .= ' <use xlink:href="#icon-' . esc_html( $args['icon'] ) . '"></use> ';
    92104
    93105        // Add some markup to use as a fallback for browsers that do not support SVGs.
    94106        if ( $args['fallback'] ) {