Make WordPress Core

Changeset 44957


Ignore:
Timestamp:
03/21/2019 10:36:22 AM (6 years ago)
Author:
SergeyBiryukov
Message:

Accessibility: Introduce category_list_link_attributes filter in Walker_Category::start_el() for the HTML attributes applied to a category list item's anchor element.

This complements the page_menu_link_attributes filter in Walker_Page::start_el() and the nav_menu_link_attributes filter in Walker_Nav_Menu::start_el().

Props pbiron, audrasjb, afercia.
Fixes #40666. See #40359.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-walker-category.php

    r44414 r44957  
    109109        }
    110110
    111         $link = '<a href="' . esc_url( get_term_link( $category ) ) . '" ';
     111        $atts         = array();
     112        $atts['href'] = get_term_link( $category );
     113
    112114        if ( $args['use_desc_for_title'] && ! empty( $category->description ) ) {
    113115            /**
     
    119121             * @param object $category    Category object.
    120122             */
    121             $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
    122         }
    123 
    124         $link .= '>';
    125         $link .= $cat_name . '</a>';
     123            $atts['title'] = strip_tags( apply_filters( 'category_description', $category->description, $category ) );
     124        }
     125
     126        /**
     127         * Filters the HTML attributes applied to a category list item's anchor element.
     128         *
     129         * @since 5.2.0
     130         *
     131         * @param array   $atts {
     132         *     The HTML attributes applied to the list item's `<a>` element, empty strings are ignored.
     133         *
     134         *     @type string $href  The href attribute.
     135         *     @type string $title The title attribute.
     136         * }
     137         * @param WP_Term $category Term data object.
     138         * @param int     $depth    Depth of category, used for padding.
     139         * @param array   $args     An array of arguments.
     140         * @param int     $id       ID of the current category.
     141         */
     142        $atts = apply_filters( 'category_list_link_attributes', $atts, $category, $depth, $args, $id );
     143
     144        $attributes = '';
     145        foreach ( $atts as $attr => $value ) {
     146            if ( ! empty( $value ) ) {
     147                $value       = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
     148                $attributes .= ' ' . $attr . '="' . $value . '"';
     149            }
     150        }
     151
     152        $link = sprintf(
     153            '<a%s>%s</a>',
     154            $attributes,
     155            $cat_name
     156        );
    126157
    127158        if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
     
    135166
    136167            if ( empty( $args['feed'] ) ) {
     168                /* translators: %s: category name */
    137169                $alt = ' alt="' . sprintf( __( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
    138170            } else {
Note: See TracChangeset for help on using the changeset viewer.