From ae0605b32343ae059d3584850f796aacd35d7c32 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 21 Mar 2019 10:36:22 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@44957 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-walker-category.php | 40 ++++++++++++++++++++--- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-walker-category.php b/src/wp-includes/class-walker-category.php index 7b13264b95..c881e68045 100644 --- a/src/wp-includes/class-walker-category.php +++ b/src/wp-includes/class-walker-category.php @@ -108,7 +108,9 @@ class Walker_Category extends Walker { return; } - $link = 'description ) ) { /** * Filters the category description for display. @@ -118,11 +120,40 @@ class Walker_Category extends Walker { * @param string $description Category description. * @param object $category Category object. */ - $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"'; + $atts['title'] = strip_tags( apply_filters( 'category_description', $category->description, $category ) ); } - $link .= '>'; - $link .= $cat_name . ''; + /** + * Filters the HTML attributes applied to a category list item's anchor element. + * + * @since 5.2.0 + * + * @param array $atts { + * The HTML attributes applied to the list item's `` element, empty strings are ignored. + * + * @type string $href The href attribute. + * @type string $title The title attribute. + * } + * @param WP_Term $category Term data object. + * @param int $depth Depth of category, used for padding. + * @param array $args An array of arguments. + * @param int $id ID of the current category. + */ + $atts = apply_filters( 'category_list_link_attributes', $atts, $category, $depth, $args, $id ); + + $attributes = ''; + foreach ( $atts as $attr => $value ) { + if ( ! empty( $value ) ) { + $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); + $attributes .= ' ' . $attr . '="' . $value . '"'; + } + } + + $link = sprintf( + '%s', + $attributes, + $cat_name + ); if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) { $link .= ' '; @@ -134,6 +165,7 @@ class Walker_Category extends Walker { $link .= '