From ae13d2fa9e458ba4d272d7f55840c7427dfaa1d2 Mon Sep 17 00:00:00 2001 From: "Stephen A. Bernhardt" Date: Sun, 22 Sep 2024 22:19:54 -0500 Subject: [PATCH 1/3] Check if title attribute matches filtered visual text --- src/wp-includes/class-walker-nav-menu.php | 40 ++++++++++++++--------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/wp-includes/class-walker-nav-menu.php b/src/wp-includes/class-walker-nav-menu.php index 4683970c61d85..9e205fa7a41fe 100644 --- a/src/wp-includes/class-walker-nav-menu.php +++ b/src/wp-includes/class-walker-nav-menu.php @@ -126,6 +126,7 @@ public function end_lvl( &$output, $depth = 0, $args = null ) { * @since 4.4.0 The {@see 'nav_menu_item_args'} filter was added. * @since 5.9.0 Renamed `$item` to `$data_object` and `$id` to `$current_object_id` * to match parent class for PHP 8 named parameter support. + * @since 6.7.0 Removed redundant title attributes. * * @see Walker::start_el() * @@ -212,8 +213,22 @@ public function start_el( &$output, $data_object, $depth = 0, $args = null, $cur $output .= $indent . ''; + /** This filter is documented in wp-includes/post-template.php */ + $title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID ); + + /** + * Filters a menu item's title. + * + * @since 4.4.0 + * + * @param string $title The menu item's title. + * @param WP_Post $menu_item The current menu item object. + * @param stdClass $args An object of wp_nav_menu() arguments. + * @param int $depth Depth of menu item. Used for padding. + */ + $title = apply_filters( 'nav_menu_item_title', $title, $menu_item, $args, $depth ); + $atts = array(); - $atts['title'] = ! empty( $menu_item->attr_title ) ? $menu_item->attr_title : ''; $atts['target'] = ! empty( $menu_item->target ) ? $menu_item->target : ''; if ( '_blank' === $menu_item->target && empty( $menu_item->xfn ) ) { $atts['rel'] = 'noopener'; @@ -233,6 +248,14 @@ public function start_el( &$output, $data_object, $depth = 0, $args = null, $cur $atts['aria-current'] = $menu_item->current ? 'page' : ''; + if ( ! empty( $menu_item->attr_title ) + && trim( strtolower( $menu_item->attr_title ) ) !== trim( strtolower( $title ) ) + ) { + $atts['title'] = $menu_item->attr_title; + } else { + $atts['title'] = ''; + } + /** * Filters the HTML attributes applied to a menu item's anchor element. * @@ -255,21 +278,6 @@ public function start_el( &$output, $data_object, $depth = 0, $args = null, $cur $atts = apply_filters( 'nav_menu_link_attributes', $atts, $menu_item, $args, $depth ); $attributes = $this->build_atts( $atts ); - /** This filter is documented in wp-includes/post-template.php */ - $title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID ); - - /** - * Filters a menu item's title. - * - * @since 4.4.0 - * - * @param string $title The menu item's title. - * @param WP_Post $menu_item The current menu item object. - * @param stdClass $args An object of wp_nav_menu() arguments. - * @param int $depth Depth of menu item. Used for padding. - */ - $title = apply_filters( 'nav_menu_item_title', $title, $menu_item, $args, $depth ); - $item_output = $args->before; $item_output .= ''; $item_output .= $args->link_before . $title . $args->link_after; From 75196b18cc13bf0258778072d6f73ae902756bb4 Mon Sep 17 00:00:00 2001 From: "Stephen A. Bernhardt" Date: Thu, 3 Oct 2024 17:38:12 -0500 Subject: [PATCH 2/3] Compare `attr_title` against `title` and its filtered values --- src/wp-includes/class-walker-nav-menu.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/wp-includes/class-walker-nav-menu.php b/src/wp-includes/class-walker-nav-menu.php index 9e205fa7a41fe..780adddb8b261 100644 --- a/src/wp-includes/class-walker-nav-menu.php +++ b/src/wp-includes/class-walker-nav-menu.php @@ -216,6 +216,9 @@ public function start_el( &$output, $data_object, $depth = 0, $args = null, $cur /** This filter is documented in wp-includes/post-template.php */ $title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID ); + // Save filtered value before filtering again. + $title_filtered = $title; + /** * Filters a menu item's title. * @@ -248,7 +251,10 @@ public function start_el( &$output, $data_object, $depth = 0, $args = null, $cur $atts['aria-current'] = $menu_item->current ? 'page' : ''; + // Add title attribute only if it does not match the link text (before or after filtering). if ( ! empty( $menu_item->attr_title ) + && trim( strtolower( $menu_item->attr_title ) ) !== trim( strtolower( $menu_item->title ) ) + && trim( strtolower( $menu_item->attr_title ) ) !== trim( strtolower( $title_filtered ) ) && trim( strtolower( $menu_item->attr_title ) ) !== trim( strtolower( $title ) ) ) { $atts['title'] = $menu_item->attr_title; From e17915261256598f5b00993bf9854bfe53ce37ad Mon Sep 17 00:00:00 2001 From: "Stephen A. Bernhardt" Date: Thu, 3 Oct 2024 17:49:53 -0500 Subject: [PATCH 3/3] `$the_title_filtered` variable name --- src/wp-includes/class-walker-nav-menu.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-walker-nav-menu.php b/src/wp-includes/class-walker-nav-menu.php index 780adddb8b261..6cf9f149c1272 100644 --- a/src/wp-includes/class-walker-nav-menu.php +++ b/src/wp-includes/class-walker-nav-menu.php @@ -217,7 +217,7 @@ public function start_el( &$output, $data_object, $depth = 0, $args = null, $cur $title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID ); // Save filtered value before filtering again. - $title_filtered = $title; + $the_title_filtered = $title; /** * Filters a menu item's title. @@ -254,7 +254,7 @@ public function start_el( &$output, $data_object, $depth = 0, $args = null, $cur // Add title attribute only if it does not match the link text (before or after filtering). if ( ! empty( $menu_item->attr_title ) && trim( strtolower( $menu_item->attr_title ) ) !== trim( strtolower( $menu_item->title ) ) - && trim( strtolower( $menu_item->attr_title ) ) !== trim( strtolower( $title_filtered ) ) + && trim( strtolower( $menu_item->attr_title ) ) !== trim( strtolower( $the_title_filtered ) ) && trim( strtolower( $menu_item->attr_title ) ) !== trim( strtolower( $title ) ) ) { $atts['title'] = $menu_item->attr_title;