From 2ab6f897d523a73b9dd1d30377ebdf1306e08224 Mon Sep 17 00:00:00 2001 From: sanketio Date: Wed, 5 Aug 2026 11:39:24 +0530 Subject: [PATCH 1/9] Administration: Clean up counter item accessible names --- src/wp-admin/menu-header.php | 39 ++++-- src/wp-admin/menu.php | 69 ++++++++--- src/wp-includes/admin-bar.php | 12 +- src/wp-includes/class-wp-admin-bar.php | 7 +- tests/phpunit/tests/admin/wpMenuOutput.php | 133 +++++++++++++++++++++ tests/phpunit/tests/adminbar.php | 67 +++++++++++ 6 files changed, 300 insertions(+), 27 deletions(-) create mode 100644 tests/phpunit/tests/admin/wpMenuOutput.php diff --git a/src/wp-admin/menu-header.php b/src/wp-admin/menu-header.php index 5cced0ceaed96..1a4936d4ee184 100644 --- a/src/wp-admin/menu-header.php +++ b/src/wp-admin/menu-header.php @@ -145,6 +145,18 @@ function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true ) { $title = wptexturize( $item[0] ); + /* + * When the menu title carries a hidden count description (e.g. pending + * updates or comments awaiting moderation), associate it with the link + * via aria-describedby. This keeps the count out of the link's accessible + * name so voice control users can operate it by its visible label, while + * the count is still announced by assistive technologies. + */ + $describedby = ''; + if ( preg_match( '/id="([^"]+)" class="wp-menu-count-description/', $item[0], $matches ) ) { + $describedby = ' aria-describedby="' . esc_attr( $matches[1] ) . '"'; + } + // Hide separators from screen readers. if ( $is_separator ) { $aria_hidden = ' aria-hidden="true"'; @@ -170,9 +182,9 @@ function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true ) { && ! file_exists( ABSPATH . "/wp-admin/$menu_file" ) ) ) { $admin_is_parent = true; - echo ""; + echo ""; } else { - echo "\n\t"; + echo "\n\t"; } } elseif ( ! empty( $item[2] ) && current_user_can( $item[1] ) ) { $menu_hook = get_plugin_page_hook( $item[2], 'admin.php' ); @@ -189,15 +201,22 @@ function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true ) { && ! file_exists( ABSPATH . "/wp-admin/$menu_file" ) ) ) { $admin_is_parent = true; - echo "\n\t"; + echo "\n\t"; } else { - echo "\n\t"; + echo "\n\t"; } } if ( ! empty( $submenu_items ) ) { echo "\n\t'; diff --git a/src/wp-admin/menu.php b/src/wp-admin/menu.php index 2a3430ebc3df8..79c0a7731a8b3 100644 --- a/src/wp-admin/menu.php +++ b/src/wp-admin/menu.php @@ -49,16 +49,26 @@ $capability = 'update_languages'; } + $updates_count = sprintf( + '', + $update_data['counts']['total'], + number_format_i18n( $update_data['counts']['total'] ) + ); + + $updates_text = sprintf( + /* translators: Hidden accessibility text. %s: Number of updates available. */ + _n( '%s update available', '%s updates available', $update_data['counts']['total'] ), + number_format_i18n( $update_data['counts']['total'] ) + ); + + $updates_description = ''; + $submenu['index.php'][10] = array( sprintf( /* translators: %s: Number of pending updates. */ __( 'Updates %s' ), - sprintf( - '%s', - $update_data['counts']['total'], - number_format_i18n( $update_data['counts']['total'] ) - ) - ), + $updates_count + ) . $updates_description, $capability, 'update-core.php', ); @@ -103,8 +113,11 @@ $awaiting_moderation_text = sprintf( _n( '%s Comment in moderation', '%s Comments in moderation', $awaiting_moderation ), $awaiting_moderation_i18n ); $menu[25] = array( - /* translators: %s: Number of comments. */ - sprintf( __( 'Comments %s' ), '' . $awaiting_moderation_text . '' ), + sprintf( + /* translators: %s: Number of comments. */ + __( 'Comments %s' ), + '' + ) . '', 'edit_posts', 'edit-comments.php', '', @@ -208,21 +221,30 @@ $menu[60] = array( __( 'Appearance' ), $appearance_capability, 'themes.php', '', 'menu-top menu-icon-appearance', 'menu-appearance', 'dashicons-admin-appearance' ); -$count = ''; +$count = ''; +$description = ''; if ( ! is_multisite() && current_user_can( 'update_themes' ) ) { if ( ! isset( $update_data ) ) { $update_data = wp_get_update_data(); } $count = sprintf( - '%s', + '', $update_data['counts']['themes'], number_format_i18n( $update_data['counts']['themes'] ) ); + + $themes_text = sprintf( + /* translators: Hidden accessibility text. %s: Number of available theme updates. */ + _n( '%s theme update available', '%s theme updates available', $update_data['counts']['themes'] ), + number_format_i18n( $update_data['counts']['themes'] ) + ); + + $description = ''; } /* translators: %s: Number of available theme updates. */ - $submenu['themes.php'][5] = array( sprintf( __( 'Themes %s' ), $count ), $appearance_capability, 'themes.php' ); + $submenu['themes.php'][5] = array( sprintf( __( 'Themes %s' ), $count ) . $description, $appearance_capability, 'themes.php' ); if ( wp_is_block_theme() ) { $submenu['themes.php'][6] = array( _x( 'Editor', 'site editor menu item' ), 'edit_theme_options', 'site-editor.php' ); @@ -307,20 +329,29 @@ function _add_plugin_file_editor_to_tools() { ); } -$count = ''; +$count = ''; +$description = ''; if ( ! is_multisite() && current_user_can( 'update_plugins' ) ) { if ( ! isset( $update_data ) ) { $update_data = wp_get_update_data(); } $count = sprintf( - '%s', + '', $update_data['counts']['plugins'], number_format_i18n( $update_data['counts']['plugins'] ) ); + + $plugins_text = sprintf( + /* translators: Hidden accessibility text. %s: Number of available plugin updates. */ + _n( '%s plugin update available', '%s plugin updates available', $update_data['counts']['plugins'] ), + number_format_i18n( $update_data['counts']['plugins'] ) + ); + + $description = ''; } /* translators: %s: Number of available plugin updates. */ -$menu[65] = array( sprintf( __( 'Plugins %s' ), $count ), 'activate_plugins', 'plugins.php', '', 'menu-top menu-icon-plugins', 'menu-plugins', 'dashicons-admin-plugins' ); +$menu[65] = array( sprintf( __( 'Plugins %s' ), $count ) . $description, 'activate_plugins', 'plugins.php', '', 'menu-top menu-icon-plugins', 'menu-plugins', 'dashicons-admin-plugins' ); $submenu['plugins.php'][5] = array( __( 'Installed Plugins' ), 'activate_plugins', 'plugins.php' ); @@ -381,10 +412,18 @@ function _add_plugin_file_editor_to_tools() { } $site_health_count = sprintf( - '%s', + '', $issue_counts['critical'], number_format_i18n( $issue_counts['critical'] ) ); + + $site_health_text = sprintf( + /* translators: Hidden accessibility text. %s: Number of critical Site Health checks. */ + _n( '%s critical issue', '%s critical issues', $issue_counts['critical'] ), + number_format_i18n( $issue_counts['critical'] ) + ); + + $site_health_count .= ''; } $menu[75] = array( __( 'Tools' ), 'edit_posts', 'tools.php', '', 'menu-top menu-icon-tools', 'menu-tools', 'dashicons-admin-tools' ); diff --git a/src/wp-includes/admin-bar.php b/src/wp-includes/admin-bar.php index 5ba568792209c..65d7e10d8d5c7 100644 --- a/src/wp-includes/admin-bar.php +++ b/src/wp-includes/admin-bar.php @@ -1134,13 +1134,17 @@ function wp_admin_bar_comments_menu( $wp_admin_bar ) { $icon = ''; $title = ''; - $title .= '' . $awaiting_text . ''; + $title .= '' . __( 'Comments' ) . ''; + $title .= ''; $wp_admin_bar->add_node( array( 'id' => 'comments', 'title' => $icon . $title, 'href' => admin_url( 'edit-comments.php' ), + 'meta' => array( + 'aria-describedby' => 'wp-admin-bar-comments-count-description', + ), ) ); } @@ -1249,13 +1253,17 @@ function wp_admin_bar_updates_menu( $wp_admin_bar ) { $icon = ''; $title = ''; - $title .= '' . $updates_text . ''; + $title .= '' . __( 'Updates' ) . ''; + $title .= ''; $wp_admin_bar->add_node( array( 'id' => 'updates', 'title' => $icon . $title, 'href' => network_admin_url( 'update-core.php' ), + 'meta' => array( + 'aria-describedby' => 'wp-admin-bar-updates-count-description', + ), ) ); } diff --git a/src/wp-includes/class-wp-admin-bar.php b/src/wp-includes/class-wp-admin-bar.php index 200d9152002f1..d72178fb8edcc 100644 --- a/src/wp-includes/class-wp-admin-bar.php +++ b/src/wp-includes/class-wp-admin-bar.php @@ -118,7 +118,8 @@ public function remove_menu( $id ) { * @type string $href Optional. Link for the item. * @type bool $group Optional. Whether or not the node is a group. Default false. * @type array $meta Meta data including the following keys: 'html', 'class', 'rel', 'lang', 'dir', - * 'onclick', 'target', 'title', 'tabindex', 'menu_title'. Default empty. + * 'onclick', 'target', 'title', 'tabindex', 'menu_title', 'aria-describedby'. + * Default empty. * } */ public function add_node( $args ) { @@ -577,10 +578,10 @@ final protected function _render_item( $node ) { echo "
  • id ) . "'$menuclass>"; if ( $has_link ) { - $attributes = array( 'onclick', 'target', 'title', 'rel', 'lang', 'dir' ); + $attributes = array( 'onclick', 'target', 'title', 'rel', 'lang', 'dir', 'aria-describedby' ); echo "user->create( array( 'role' => 'administrator' ) ); + } + + public function set_up() { + parent::set_up(); + wp_set_current_user( self::$admin_id ); + } + + /** + * A hidden count description on a top-level menu title is associated with the + * link via aria-describedby, keeping the count out of the accessible name. + * + * @ticket 65793 + */ + public function test_top_level_count_description_is_associated_via_aria_describedby() { + $menu = array( + array( + 'Plugins ', + 'activate_plugins', + 'plugins.php', + '', + 'menu-top menu-icon-plugins', + 'menu-plugins', + 'dashicons-admin-plugins', + ), + ); + + $output = get_echo( '_wp_menu_output', array( $menu, array() ) ); + + $this->assertStringContainsString( 'aria-describedby="wp-menu-plugins-count-description"', $output ); + $this->assertStringContainsString( 'id="wp-menu-plugins-count-description"', $output ); + } + + /** + * A hidden count description on a submenu title is associated with the submenu link. + * + * @ticket 65793 + */ + public function test_submenu_count_description_is_associated_via_aria_describedby() { + $menu = array( + array( 'Dashboard', 'read', 'index.php', '', 'menu-top', 'menu-dashboard', 'dashicons-dashboard' ), + ); + + $submenu = array( + 'index.php' => array( + 10 => array( + 'Updates ', + 'read', + 'update-core.php', + ), + ), + ); + + $output = get_echo( '_wp_menu_output', array( $menu, $submenu ) ); + + $this->assertStringContainsString( 'aria-describedby="wp-menu-updates-count-description"', $output ); + } + + /** + * The count description id must appear once even when the top-level item has a + * submenu, since the submenu head repeats the top-level title. + * + * @ticket 65793 + */ + public function test_count_description_id_is_not_duplicated_by_submenu_head() { + $menu = array( + array( + 'Plugins ', + 'activate_plugins', + 'plugins.php', + '', + 'menu-top menu-icon-plugins', + 'menu-plugins', + 'dashicons-admin-plugins', + ), + ); + + $submenu = array( + 'plugins.php' => array( + 5 => array( 'Installed Plugins', 'activate_plugins', 'plugins.php' ), + 10 => array( 'Add New Plugin', 'install_plugins', 'plugin-install.php' ), + ), + ); + + $output = get_echo( '_wp_menu_output', array( $menu, $submenu ) ); + + $this->assertSame( 1, substr_count( $output, 'id="wp-menu-plugins-count-description"' ) ); + $this->assertStringContainsString( 'aria-describedby="wp-menu-plugins-count-description"', $output ); + } + + /** + * Menu items without a count description do not receive an aria-describedby attribute. + * + * @ticket 65793 + */ + public function test_menu_without_count_description_has_no_aria_describedby() { + $menu = array( + array( 'Media', 'upload_files', 'upload.php', '', 'menu-top menu-icon-media', 'menu-media', 'dashicons-admin-media' ), + ); + + $output = get_echo( '_wp_menu_output', array( $menu, array() ) ); + + $this->assertStringNotContainsString( 'aria-describedby', $output ); + } +} diff --git a/tests/phpunit/tests/adminbar.php b/tests/phpunit/tests/adminbar.php index fb29eaf39af42..e4f03fc3f3ab1 100644 --- a/tests/phpunit/tests/adminbar.php +++ b/tests/phpunit/tests/adminbar.php @@ -337,6 +337,73 @@ public function data_admin_bar_nodes_with_tabindex_meta() { ); } + /** + * A node can render an aria-describedby attribute on its link via meta. + * + * @ticket 65793 + */ + public function test_admin_bar_meta_renders_aria_describedby() { + $admin_bar = new WP_Admin_Bar(); + $admin_bar->add_node( + array( + 'id' => 'test-node', + 'href' => 'https://example.org/', + 'title' => 'Test Extra description', + 'meta' => array( 'aria-describedby' => 'test-node-description' ), + ) + ); + + $html = get_echo( array( $admin_bar, 'render' ) ); + + $this->assertStringContainsString( "aria-describedby='test-node-description'", $html ); + } + + /** + * The Comments toolbar item exposes its count as a description, keeping the + * link's accessible name free of the count so voice control can operate it. + * + * @ticket 65793 + */ + public function test_admin_bar_comments_menu_associates_count_via_describedby() { + wp_set_current_user( self::$editor_id ); + + $admin_bar = new WP_Admin_Bar(); + wp_admin_bar_comments_menu( $admin_bar ); + $html = get_echo( array( $admin_bar, 'render' ) ); + + $this->assertStringContainsString( "aria-describedby='wp-admin-bar-comments-count-description'", $html ); + $this->assertStringContainsString( 'id="wp-admin-bar-comments-count-description"', $html ); + $this->assertStringContainsString( 'Comments', $html ); + } + + /** + * The Updates toolbar item exposes its count as a description. + * + * @ticket 65793 + */ + public function test_admin_bar_updates_menu_associates_count_via_describedby() { + wp_set_current_user( self::$admin_id ); + + // Seed a pending plugin update so the Updates node is added with a count. + set_site_transient( + 'update_plugins', + (object) array( + 'response' => array( + 'fake/fake.php' => (object) array( 'new_version' => '2.0' ), + ), + ) + ); + + $admin_bar = new WP_Admin_Bar(); + wp_admin_bar_updates_menu( $admin_bar ); + $html = get_echo( array( $admin_bar, 'render' ) ); + + delete_site_transient( 'update_plugins' ); + + $this->assertStringContainsString( "aria-describedby='wp-admin-bar-updates-count-description'", $html ); + $this->assertStringContainsString( 'Updates', $html ); + } + /** * @ticket 22247 */ From fa07abaf57b333e4c16fcf8edac36a35cf137eb8 Mon Sep 17 00:00:00 2001 From: sanketio Date: Wed, 5 Aug 2026 12:10:57 +0530 Subject: [PATCH 2/9] Administration: Make counter a11y tests multisite-safe --- tests/phpunit/tests/admin/wpMenuOutput.php | 4 ++-- tests/phpunit/tests/adminbar.php | 23 +++++++++++++--------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/tests/phpunit/tests/admin/wpMenuOutput.php b/tests/phpunit/tests/admin/wpMenuOutput.php index 5aecdac9123e7..497fbd5476b42 100644 --- a/tests/phpunit/tests/admin/wpMenuOutput.php +++ b/tests/phpunit/tests/admin/wpMenuOutput.php @@ -44,7 +44,7 @@ public function test_top_level_count_description_is_associated_via_aria_describe $menu = array( array( 'Plugins ', - 'activate_plugins', + 'read', 'plugins.php', '', 'menu-top menu-icon-plugins', @@ -94,7 +94,7 @@ public function test_count_description_id_is_not_duplicated_by_submenu_head() { $menu = array( array( 'Plugins ', - 'activate_plugins', + 'read', 'plugins.php', '', 'menu-top menu-icon-plugins', diff --git a/tests/phpunit/tests/adminbar.php b/tests/phpunit/tests/adminbar.php index e4f03fc3f3ab1..0545da09be1cb 100644 --- a/tests/phpunit/tests/adminbar.php +++ b/tests/phpunit/tests/adminbar.php @@ -384,21 +384,26 @@ public function test_admin_bar_comments_menu_associates_count_via_describedby() public function test_admin_bar_updates_menu_associates_count_via_describedby() { wp_set_current_user( self::$admin_id ); - // Seed a pending plugin update so the Updates node is added with a count. - set_site_transient( - 'update_plugins', - (object) array( - 'response' => array( - 'fake/fake.php' => (object) array( 'new_version' => '2.0' ), + // Force a pending update so the node is added regardless of environment or capabilities. + $force_update_data = static function () { + return array( + 'counts' => array( + 'plugins' => 1, + 'themes' => 0, + 'wordpress' => 0, + 'translations' => 0, + 'total' => 1, ), - ) - ); + 'title' => '', + ); + }; + add_filter( 'wp_get_update_data', $force_update_data ); $admin_bar = new WP_Admin_Bar(); wp_admin_bar_updates_menu( $admin_bar ); $html = get_echo( array( $admin_bar, 'render' ) ); - delete_site_transient( 'update_plugins' ); + remove_filter( 'wp_get_update_data', $force_update_data ); $this->assertStringContainsString( "aria-describedby='wp-admin-bar-updates-count-description'", $html ); $this->assertStringContainsString( 'Updates', $html ); From 5d76e7e8cfcda94d10d6c718344971d39de11b22 Mon Sep 17 00:00:00 2001 From: sanketio Date: Thu, 6 Aug 2026 11:16:38 +0530 Subject: [PATCH 3/9] Administration: Wire counter descriptions without title parsing --- src/wp-admin/menu-header.php | 50 +++++++++++--------- src/wp-admin/menu.php | 49 +++++++++++++++++--- tests/phpunit/tests/admin/wpMenuOutput.php | 54 ++++++++++++++++++++-- 3 files changed, 123 insertions(+), 30 deletions(-) diff --git a/src/wp-admin/menu-header.php b/src/wp-admin/menu-header.php index 1a4936d4ee184..a639e9e1824ac 100644 --- a/src/wp-admin/menu-header.php +++ b/src/wp-admin/menu-header.php @@ -75,6 +75,7 @@ function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true ) { $first = true; // 0 = menu_title, 1 = capability, 2 = menu_slug, 3 = page_title, 4 = classes, 5 = hookname, 6 = icon_url. + // Optional 'count_description' = array( 'id' => string, 'html' => string ) for a counter's hidden description. foreach ( $menu as $key => $item ) { $admin_is_parent = false; $class = array(); @@ -146,15 +147,20 @@ function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true ) { $title = wptexturize( $item[0] ); /* - * When the menu title carries a hidden count description (e.g. pending + * When a menu item carries a hidden count description (e.g. pending * updates or comments awaiting moderation), associate it with the link - * via aria-describedby. This keeps the count out of the link's accessible - * name so voice control users can operate it by its visible label, while - * the count is still announced by assistive technologies. + * via aria-describedby and render it inside the link. This keeps the + * count out of the link's accessible name so voice control users can + * operate it by its visible label, while the count is still announced by + * assistive technologies. The id and markup are supplied explicitly by + * the menu item's 'count_description' entry rather than parsed from the + * title, so the association does not depend on the title's HTML shape. */ - $describedby = ''; - if ( preg_match( '/id="([^"]+)" class="wp-menu-count-description/', $item[0], $matches ) ) { - $describedby = ' aria-describedby="' . esc_attr( $matches[1] ) . '"'; + $describedby = ''; + $count_description = ''; + if ( ! empty( $item['count_description']['id'] ) ) { + $describedby = ' aria-describedby="' . esc_attr( $item['count_description']['id'] ) . '"'; + $count_description = $item['count_description']['html']; } // Hide separators from screen readers. @@ -182,9 +188,9 @@ function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true ) { && ! file_exists( ABSPATH . "/wp-admin/$menu_file" ) ) ) { $admin_is_parent = true; - echo ""; + echo ""; } else { - echo "\n\t"; + echo "\n\t"; } } elseif ( ! empty( $item[2] ) && current_user_can( $item[1] ) ) { $menu_hook = get_plugin_page_hook( $item[2], 'admin.php' ); @@ -201,9 +207,9 @@ function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true ) { && ! file_exists( ABSPATH . "/wp-admin/$menu_file" ) ) ) { $admin_is_parent = true; - echo "\n\t"; + echo "\n\t"; } else { - echo "\n\t"; + echo "\n\t"; } } @@ -211,16 +217,16 @@ function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true ) { echo "\n\t'; diff --git a/src/wp-admin/menu.php b/src/wp-admin/menu.php index 79c0a7731a8b3..45c345340737d 100644 --- a/src/wp-admin/menu.php +++ b/src/wp-admin/menu.php @@ -68,11 +68,17 @@ /* translators: %s: Number of pending updates. */ __( 'Updates %s' ), $updates_count - ) . $updates_description, + ), $capability, 'update-core.php', ); + // Associate the hidden count description with the link. See _wp_menu_output(). + $submenu['index.php'][10]['count_description'] = array( + 'id' => 'wp-menu-updates-count-description', + 'html' => $updates_description, + ); + unset( $capability ); } @@ -117,7 +123,7 @@ /* translators: %s: Number of comments. */ __( 'Comments %s' ), '' - ) . '', + ), 'edit_posts', 'edit-comments.php', '', @@ -126,6 +132,12 @@ 'dashicons-admin-comments', ); + // Associate the hidden count description with the link. See _wp_menu_output(). + $menu[25]['count_description'] = array( + 'id' => 'wp-menu-comments-count-description', + 'html' => '', + ); + unset( $awaiting_moderation ); } @@ -244,7 +256,15 @@ } /* translators: %s: Number of available theme updates. */ - $submenu['themes.php'][5] = array( sprintf( __( 'Themes %s' ), $count ) . $description, $appearance_capability, 'themes.php' ); + $submenu['themes.php'][5] = array( sprintf( __( 'Themes %s' ), $count ), $appearance_capability, 'themes.php' ); + + // Associate the hidden count description with the link. See _wp_menu_output(). +if ( '' !== $description ) { + $submenu['themes.php'][5]['count_description'] = array( + 'id' => 'wp-menu-themes-count-description', + 'html' => $description, + ); +} if ( wp_is_block_theme() ) { $submenu['themes.php'][6] = array( _x( 'Editor', 'site editor menu item' ), 'edit_theme_options', 'site-editor.php' ); @@ -351,7 +371,15 @@ function _add_plugin_file_editor_to_tools() { } /* translators: %s: Number of available plugin updates. */ -$menu[65] = array( sprintf( __( 'Plugins %s' ), $count ) . $description, 'activate_plugins', 'plugins.php', '', 'menu-top menu-icon-plugins', 'menu-plugins', 'dashicons-admin-plugins' ); +$menu[65] = array( sprintf( __( 'Plugins %s' ), $count ), 'activate_plugins', 'plugins.php', '', 'menu-top menu-icon-plugins', 'menu-plugins', 'dashicons-admin-plugins' ); + +// Associate the hidden count description with the link. See _wp_menu_output(). +if ( '' !== $description ) { + $menu[65]['count_description'] = array( + 'id' => 'wp-menu-plugins-count-description', + 'html' => $description, + ); +} $submenu['plugins.php'][5] = array( __( 'Installed Plugins' ), 'activate_plugins', 'plugins.php' ); @@ -393,7 +421,8 @@ function _add_plugin_file_editor_to_tools() { } } -$site_health_count = ''; +$site_health_count = ''; +$site_health_description = ''; if ( ! is_multisite() && current_user_can( 'view_site_health_checks' ) ) { $get_issues = get_transient( 'health-check-site-status-result' ); @@ -423,7 +452,7 @@ function _add_plugin_file_editor_to_tools() { number_format_i18n( $issue_counts['critical'] ) ); - $site_health_count .= ''; + $site_health_description = ''; } $menu[75] = array( __( 'Tools' ), 'edit_posts', 'tools.php', '', 'menu-top menu-icon-tools', 'menu-tools', 'dashicons-admin-tools' ); @@ -432,6 +461,14 @@ function _add_plugin_file_editor_to_tools() { $submenu['tools.php'][15] = array( __( 'Export' ), 'export', 'export.php' ); /* translators: %s: Number of critical Site Health checks. */ $submenu['tools.php'][20] = array( sprintf( __( 'Site Health %s' ), $site_health_count ), 'view_site_health_checks', 'site-health.php' ); + + // Associate the hidden count description with the link. See _wp_menu_output(). +if ( '' !== $site_health_description ) { + $submenu['tools.php'][20]['count_description'] = array( + 'id' => 'wp-menu-site-health-count-description', + 'html' => $site_health_description, + ); +} $submenu['tools.php'][25] = array( __( 'Export Personal Data' ), 'export_others_personal_data', 'export-personal-data.php' ); $submenu['tools.php'][30] = array( __( 'Erase Personal Data' ), 'erase_others_personal_data', 'erase-personal-data.php' ); if ( is_multisite() && ! is_main_site() && '1' !== get_site()->deleted ) { diff --git a/tests/phpunit/tests/admin/wpMenuOutput.php b/tests/phpunit/tests/admin/wpMenuOutput.php index 497fbd5476b42..b8119ffb51e29 100644 --- a/tests/phpunit/tests/admin/wpMenuOutput.php +++ b/tests/phpunit/tests/admin/wpMenuOutput.php @@ -43,7 +43,7 @@ public function set_up() { public function test_top_level_count_description_is_associated_via_aria_describedby() { $menu = array( array( - 'Plugins ', + 'Plugins ', 'read', 'plugins.php', '', @@ -53,10 +53,16 @@ public function test_top_level_count_description_is_associated_via_aria_describe ), ); + $menu[0]['count_description'] = array( + 'id' => 'wp-menu-plugins-count-description', + 'html' => '', + ); + $output = get_echo( '_wp_menu_output', array( $menu, array() ) ); $this->assertStringContainsString( 'aria-describedby="wp-menu-plugins-count-description"', $output ); $this->assertStringContainsString( 'id="wp-menu-plugins-count-description"', $output ); + $this->assertStringContainsString( '2 plugin updates available', $output ); } /** @@ -72,16 +78,22 @@ public function test_submenu_count_description_is_associated_via_aria_describedb $submenu = array( 'index.php' => array( 10 => array( - 'Updates ', + 'Updates ', 'read', 'update-core.php', ), ), ); + $submenu['index.php'][10]['count_description'] = array( + 'id' => 'wp-menu-updates-count-description', + 'html' => '', + ); + $output = get_echo( '_wp_menu_output', array( $menu, $submenu ) ); $this->assertStringContainsString( 'aria-describedby="wp-menu-updates-count-description"', $output ); + $this->assertStringContainsString( '2 updates available', $output ); } /** @@ -93,7 +105,7 @@ public function test_submenu_count_description_is_associated_via_aria_describedb public function test_count_description_id_is_not_duplicated_by_submenu_head() { $menu = array( array( - 'Plugins ', + 'Plugins ', 'read', 'plugins.php', '', @@ -103,6 +115,11 @@ public function test_count_description_id_is_not_duplicated_by_submenu_head() { ), ); + $menu[0]['count_description'] = array( + 'id' => 'wp-menu-plugins-count-description', + 'html' => '', + ); + $submenu = array( 'plugins.php' => array( 5 => array( 'Installed Plugins', 'activate_plugins', 'plugins.php' ), @@ -116,6 +133,37 @@ public function test_count_description_id_is_not_duplicated_by_submenu_head() { $this->assertStringContainsString( 'aria-describedby="wp-menu-plugins-count-description"', $output ); } + /** + * The count description association is driven by the item's explicit + * 'count_description' entry, not by parsing the title HTML, so it works + * regardless of the markup shape of the title. + * + * @ticket 65793 + */ + public function test_count_description_is_independent_of_title_html_shape() { + $menu = array( + array( + // Title markup that would not match a fixed id-then-class regex. + 'Plugins 2', + 'read', + 'plugins.php', + '', + 'menu-top menu-icon-plugins', + 'menu-plugins', + 'dashicons-admin-plugins', + ), + ); + + $menu[0]['count_description'] = array( + 'id' => 'wp-menu-plugins-count-description', + 'html' => '', + ); + + $output = get_echo( '_wp_menu_output', array( $menu, array() ) ); + + $this->assertStringContainsString( 'aria-describedby="wp-menu-plugins-count-description"', $output ); + } + /** * Menu items without a count description do not receive an aria-describedby attribute. * From 53c15278c7b07ce6992b5c0ea7d6b64d5cb01f35 Mon Sep 17 00:00:00 2001 From: sanketio Date: Thu, 6 Aug 2026 11:16:38 +0530 Subject: [PATCH 4/9] Administration: Sync menu counter descriptions on live updates --- src/js/_enqueues/wp/updates.js | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/js/_enqueues/wp/updates.js b/src/js/_enqueues/wp/updates.js index 59d0ef2e6b1cb..bff346189d47a 100644 --- a/src/js/_enqueues/wp/updates.js +++ b/src/js/_enqueues/wp/updates.js @@ -389,6 +389,21 @@ $dashboardNavMenuUpdateCount.remove(); } + /* + * Keep the menu item's hidden count description in sync. The visible + * count bubble is aria-hidden, so this description is what assistive + * technologies announce via aria-describedby. + */ + $( '#wp-menu-updates-count-description' ).text( + settings.totals.counts.total > 0 + ? sprintf( + /* translators: %s: Number of updates available. */ + _n( '%s update available', '%s updates available', settings.totals.counts.total ), + settings.totals.counts.total + ) + : '' + ); + // Update the "Plugins" menu item. $pluginsNavMenuUpdateCount.each( function( index, element ) { element.className = element.className.replace( /count-\d+/, 'count-' + settings.totals.counts.plugins ); @@ -399,6 +414,17 @@ $pluginsNavMenuUpdateCount.remove(); } + // Keep the menu item's hidden count description in sync. See above. + $( '#wp-menu-plugins-count-description' ).text( + settings.totals.counts.plugins > 0 + ? sprintf( + /* translators: %s: Number of available plugin updates. */ + _n( '%s plugin update available', '%s plugin updates available', settings.totals.counts.plugins ), + settings.totals.counts.plugins + ) + : '' + ); + // Update the "Appearance" menu item. $appearanceNavMenuUpdateCount.each( function( index, element ) { element.className = element.className.replace( /count-\d+/, 'count-' + settings.totals.counts.themes ); @@ -409,6 +435,17 @@ $appearanceNavMenuUpdateCount.remove(); } + // Keep the menu item's hidden count description in sync. See above. + $( '#wp-menu-themes-count-description' ).text( + settings.totals.counts.themes > 0 + ? sprintf( + /* translators: %s: Number of available theme updates. */ + _n( '%s theme update available', '%s theme updates available', settings.totals.counts.themes ), + settings.totals.counts.themes + ) + : '' + ); + // Update list table filter navigation. if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) { itemCount = settings.totals.counts.plugins; From 811f1a94ca215aab8109b5756b74abb47a4045fc Mon Sep 17 00:00:00 2001 From: sanketio Date: Thu, 6 Aug 2026 11:21:02 +0530 Subject: [PATCH 5/9] Administration: Satisfy JSHint in counter description sync --- src/js/_enqueues/wp/updates.js | 36 ++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/js/_enqueues/wp/updates.js b/src/js/_enqueues/wp/updates.js index bff346189d47a..eba4b068d280f 100644 --- a/src/js/_enqueues/wp/updates.js +++ b/src/js/_enqueues/wp/updates.js @@ -394,15 +394,17 @@ * count bubble is aria-hidden, so this description is what assistive * technologies announce via aria-describedby. */ - $( '#wp-menu-updates-count-description' ).text( - settings.totals.counts.total > 0 - ? sprintf( + if ( settings.totals.counts.total > 0 ) { + $( '#wp-menu-updates-count-description' ).text( + sprintf( /* translators: %s: Number of updates available. */ _n( '%s update available', '%s updates available', settings.totals.counts.total ), settings.totals.counts.total ) - : '' - ); + ); + } else { + $( '#wp-menu-updates-count-description' ).text( '' ); + } // Update the "Plugins" menu item. $pluginsNavMenuUpdateCount.each( function( index, element ) { @@ -415,15 +417,17 @@ } // Keep the menu item's hidden count description in sync. See above. - $( '#wp-menu-plugins-count-description' ).text( - settings.totals.counts.plugins > 0 - ? sprintf( + if ( settings.totals.counts.plugins > 0 ) { + $( '#wp-menu-plugins-count-description' ).text( + sprintf( /* translators: %s: Number of available plugin updates. */ _n( '%s plugin update available', '%s plugin updates available', settings.totals.counts.plugins ), settings.totals.counts.plugins ) - : '' - ); + ); + } else { + $( '#wp-menu-plugins-count-description' ).text( '' ); + } // Update the "Appearance" menu item. $appearanceNavMenuUpdateCount.each( function( index, element ) { @@ -436,15 +440,17 @@ } // Keep the menu item's hidden count description in sync. See above. - $( '#wp-menu-themes-count-description' ).text( - settings.totals.counts.themes > 0 - ? sprintf( + if ( settings.totals.counts.themes > 0 ) { + $( '#wp-menu-themes-count-description' ).text( + sprintf( /* translators: %s: Number of available theme updates. */ _n( '%s theme update available', '%s theme updates available', settings.totals.counts.themes ), settings.totals.counts.themes ) - : '' - ); + ); + } else { + $( '#wp-menu-themes-count-description' ).text( '' ); + } // Update list table filter navigation. if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) { From 100b69b80e5927cb1f2c277dd039f0a87f7629e5 Mon Sep 17 00:00:00 2001 From: sanketio Date: Wed, 16 Sep 2026 11:59:31 +0530 Subject: [PATCH 6/9] Administration: Use hidden attribute for count descriptions --- src/wp-admin/menu.php | 15 ++++++++++----- src/wp-includes/admin-bar.php | 6 ++++-- tests/phpunit/tests/admin/wpMenuOutput.php | 8 ++++---- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/wp-admin/menu.php b/src/wp-admin/menu.php index 45c345340737d..f42e34dddb4e5 100644 --- a/src/wp-admin/menu.php +++ b/src/wp-admin/menu.php @@ -61,7 +61,8 @@ number_format_i18n( $update_data['counts']['total'] ) ); - $updates_description = ''; + // Hidden count description; exposed to assistive tech via aria-describedby on the link. + $updates_description = ''; $submenu['index.php'][10] = array( sprintf( @@ -135,7 +136,8 @@ // Associate the hidden count description with the link. See _wp_menu_output(). $menu[25]['count_description'] = array( 'id' => 'wp-menu-comments-count-description', - 'html' => '', + // Hidden count description; exposed to assistive tech via aria-describedby on the link. + 'html' => '', ); unset( $awaiting_moderation ); @@ -252,7 +254,8 @@ number_format_i18n( $update_data['counts']['themes'] ) ); - $description = ''; + // Hidden count description; exposed to assistive tech via aria-describedby on the link. + $description = ''; } /* translators: %s: Number of available theme updates. */ @@ -367,7 +370,8 @@ function _add_plugin_file_editor_to_tools() { number_format_i18n( $update_data['counts']['plugins'] ) ); - $description = ''; + // Hidden count description; exposed to assistive tech via aria-describedby on the link. + $description = ''; } /* translators: %s: Number of available plugin updates. */ @@ -452,7 +456,8 @@ function _add_plugin_file_editor_to_tools() { number_format_i18n( $issue_counts['critical'] ) ); - $site_health_description = ''; + // Hidden count description; exposed to assistive tech via aria-describedby on the link. + $site_health_description = ''; } $menu[75] = array( __( 'Tools' ), 'edit_posts', 'tools.php', '', 'menu-top menu-icon-tools', 'menu-tools', 'dashicons-admin-tools' ); diff --git a/src/wp-includes/admin-bar.php b/src/wp-includes/admin-bar.php index 65d7e10d8d5c7..8520010f3bf32 100644 --- a/src/wp-includes/admin-bar.php +++ b/src/wp-includes/admin-bar.php @@ -1135,7 +1135,8 @@ function wp_admin_bar_comments_menu( $wp_admin_bar ) { $icon = ''; $title = ''; $title .= '' . __( 'Comments' ) . ''; - $title .= ''; + // Hidden count description; exposed to assistive tech via aria-describedby on the link. + $title .= ''; $wp_admin_bar->add_node( array( @@ -1254,7 +1255,8 @@ function wp_admin_bar_updates_menu( $wp_admin_bar ) { $icon = ''; $title = ''; $title .= '' . __( 'Updates' ) . ''; - $title .= ''; + // Hidden count description; exposed to assistive tech via aria-describedby on the link. + $title .= ''; $wp_admin_bar->add_node( array( diff --git a/tests/phpunit/tests/admin/wpMenuOutput.php b/tests/phpunit/tests/admin/wpMenuOutput.php index b8119ffb51e29..a45bbe6596ab4 100644 --- a/tests/phpunit/tests/admin/wpMenuOutput.php +++ b/tests/phpunit/tests/admin/wpMenuOutput.php @@ -55,7 +55,7 @@ public function test_top_level_count_description_is_associated_via_aria_describe $menu[0]['count_description'] = array( 'id' => 'wp-menu-plugins-count-description', - 'html' => '', + 'html' => '', ); $output = get_echo( '_wp_menu_output', array( $menu, array() ) ); @@ -87,7 +87,7 @@ public function test_submenu_count_description_is_associated_via_aria_describedb $submenu['index.php'][10]['count_description'] = array( 'id' => 'wp-menu-updates-count-description', - 'html' => '', + 'html' => '', ); $output = get_echo( '_wp_menu_output', array( $menu, $submenu ) ); @@ -117,7 +117,7 @@ public function test_count_description_id_is_not_duplicated_by_submenu_head() { $menu[0]['count_description'] = array( 'id' => 'wp-menu-plugins-count-description', - 'html' => '', + 'html' => '', ); $submenu = array( @@ -156,7 +156,7 @@ public function test_count_description_is_independent_of_title_html_shape() { $menu[0]['count_description'] = array( 'id' => 'wp-menu-plugins-count-description', - 'html' => '', + 'html' => '', ); $output = get_echo( '_wp_menu_output', array( $menu, array() ) ); From d6cb4461cf45862050ac2dc8ef088b622f6f903b Mon Sep 17 00:00:00 2001 From: sanketio Date: Wed, 16 Sep 2026 12:36:17 +0530 Subject: [PATCH 7/9] Code Quality: Guard update_data in the admin menu --- src/wp-admin/menu.php | 4 ++++ tests/phpstan/baselines/variable.undefined.neon | 5 ----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/wp-admin/menu.php b/src/wp-admin/menu.php index f42e34dddb4e5..45250abab954a 100644 --- a/src/wp-admin/menu.php +++ b/src/wp-admin/menu.php @@ -49,6 +49,10 @@ $capability = 'update_languages'; } + if ( ! isset( $update_data ) ) { + $update_data = wp_get_update_data(); + } + $updates_count = sprintf( '', $update_data['counts']['total'], diff --git a/tests/phpstan/baselines/variable.undefined.neon b/tests/phpstan/baselines/variable.undefined.neon index 4fd47e0948b73..abb269e4a215b 100644 --- a/tests/phpstan/baselines/variable.undefined.neon +++ b/tests/phpstan/baselines/variable.undefined.neon @@ -318,11 +318,6 @@ parameters: identifier: variable.undefined count: 9 path: ../../../src/wp-admin/maint/repair.php - - - message: '#^Variable \$update_data might not be defined\.$#' - identifier: variable.undefined - count: 2 - path: ../../../src/wp-admin/menu.php - message: '#^Variable \$current_user might not be defined\.$#' identifier: variable.undefined From ab8cca8739674c8128aa5fd10417dff46f51b333 Mon Sep 17 00:00:00 2001 From: sanketio Date: Fri, 2 Oct 2026 10:26:58 +0530 Subject: [PATCH 8/9] Administration: Concatenate menu counts instead of sprintf --- src/wp-admin/css/admin-menu.css | 2 +- src/wp-admin/menu.php | 21 +++++---------------- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/src/wp-admin/css/admin-menu.css b/src/wp-admin/css/admin-menu.css index 7889a3e76008c..9e2851a13542f 100644 --- a/src/wp-admin/css/admin-menu.css +++ b/src/wp-admin/css/admin-menu.css @@ -425,7 +425,7 @@ ul#adminmenu > li.current > a.current::after { display: inline-block; vertical-align: top; box-sizing: border-box; - margin: 1px 0 -1px 2px; + margin: 1px 0 -1px 6px; padding: 0 5px; min-width: 18px; height: 18px; diff --git a/src/wp-admin/menu.php b/src/wp-admin/menu.php index 45250abab954a..2c2ff1aac4465 100644 --- a/src/wp-admin/menu.php +++ b/src/wp-admin/menu.php @@ -69,11 +69,7 @@ $updates_description = ''; $submenu['index.php'][10] = array( - sprintf( - /* translators: %s: Number of pending updates. */ - __( 'Updates %s' ), - $updates_count - ), + __( 'Updates' ) . $updates_count, $capability, 'update-core.php', ); @@ -124,11 +120,7 @@ $awaiting_moderation_text = sprintf( _n( '%s Comment in moderation', '%s Comments in moderation', $awaiting_moderation ), $awaiting_moderation_i18n ); $menu[25] = array( - sprintf( - /* translators: %s: Number of comments. */ - __( 'Comments %s' ), - '' - ), + __( 'Comments' ) . '', 'edit_posts', 'edit-comments.php', '', @@ -262,8 +254,7 @@ $description = ''; } - /* translators: %s: Number of available theme updates. */ - $submenu['themes.php'][5] = array( sprintf( __( 'Themes %s' ), $count ), $appearance_capability, 'themes.php' ); + $submenu['themes.php'][5] = array( __( 'Themes' ) . $count, $appearance_capability, 'themes.php' ); // Associate the hidden count description with the link. See _wp_menu_output(). if ( '' !== $description ) { @@ -378,8 +369,7 @@ function _add_plugin_file_editor_to_tools() { $description = ''; } -/* translators: %s: Number of available plugin updates. */ -$menu[65] = array( sprintf( __( 'Plugins %s' ), $count ), 'activate_plugins', 'plugins.php', '', 'menu-top menu-icon-plugins', 'menu-plugins', 'dashicons-admin-plugins' ); +$menu[65] = array( __( 'Plugins' ) . $count, 'activate_plugins', 'plugins.php', '', 'menu-top menu-icon-plugins', 'menu-plugins', 'dashicons-admin-plugins' ); // Associate the hidden count description with the link. See _wp_menu_output(). if ( '' !== $description ) { @@ -468,8 +458,7 @@ function _add_plugin_file_editor_to_tools() { $submenu['tools.php'][5] = array( __( 'Available Tools' ), 'edit_posts', 'tools.php' ); $submenu['tools.php'][10] = array( __( 'Import' ), 'import', 'import.php' ); $submenu['tools.php'][15] = array( __( 'Export' ), 'export', 'export.php' ); - /* translators: %s: Number of critical Site Health checks. */ - $submenu['tools.php'][20] = array( sprintf( __( 'Site Health %s' ), $site_health_count ), 'view_site_health_checks', 'site-health.php' ); + $submenu['tools.php'][20] = array( __( 'Site Health' ) . $site_health_count, 'view_site_health_checks', 'site-health.php' ); // Associate the hidden count description with the link. See _wp_menu_output(). if ( '' !== $site_health_description ) { From 694d0f020976f884ef0fee882ddd20058ab129b6 Mon Sep 17 00:00:00 2001 From: sanketio Date: Fri, 9 Oct 2026 11:29:26 +0530 Subject: [PATCH 9/9] Administration: Address menu counter review feedback --- src/js/_enqueues/admin/edit-comments.js | 5 + src/js/_enqueues/admin/site-health.js | 11 ++ src/wp-admin/css/admin-menu.css | 2 +- src/wp-admin/menu-header.php | 4 +- src/wp-admin/menu.php | 84 +++++----- src/wp-includes/admin-bar.php | 8 +- .../phpunit/tests/admin/wpAdminMenuCounts.php | 150 ++++++++++++++++++ tests/phpunit/tests/admin/wpMenuOutput.php | 30 ++++ tests/phpunit/tests/adminbar.php | 10 ++ 9 files changed, 261 insertions(+), 43 deletions(-) create mode 100644 tests/phpunit/tests/admin/wpAdminMenuCounts.php diff --git a/src/js/_enqueues/admin/edit-comments.js b/src/js/_enqueues/admin/edit-comments.js index 7e89f1c0dd7cb..77b10c0d7fee7 100644 --- a/src/js/_enqueues/admin/edit-comments.js +++ b/src/js/_enqueues/admin/edit-comments.js @@ -172,6 +172,11 @@ var getCount, updateCount, updateCountText, updatePending, updateApproved, // Update the "comment in moderation" text across the UI. $( '.comments-in-moderation-text' ).text( response.i18n_moderation_text ); + + // The menu's count description is announced via aria-describedby; keep it empty at zero. + if ( 0 === parseInt( response.in_moderation, 10 ) ) { + $( '#wp-menu-comments-count-description' ).text( '' ); + } // Hide the "comment in moderation" text in the Dashboard "At a Glance" widget. if ( isDashboard && response.in_moderation ) { $( '.comment-mod-count', '#dashboard_right_now' ) diff --git a/src/js/_enqueues/admin/site-health.js b/src/js/_enqueues/admin/site-health.js index 0a1b052e56f3b..7c644b637a789 100644 --- a/src/js/_enqueues/admin/site-health.js +++ b/src/js/_enqueues/admin/site-health.js @@ -192,8 +192,19 @@ jQuery( function( $ ) { $( '#health-check-issues-critical' ).removeClass( 'hidden' ); menuCounterWrapper.removeClass( 'count-0' ); + + // Keep the menu item's hidden count description in sync. + $( '#wp-menu-site-health-count-description' ).text( + sprintf( + /* translators: %s: Number of critical Site Health checks. */ + _n( '%s critical issue', '%s critical issues', SiteHealth.site_status.issues.critical ), + SiteHealth.site_status.issues.critical + ) + ); } else { menuCounterWrapper.addClass( 'count-0' ); + + $( '#wp-menu-site-health-count-description' ).text( '' ); } if ( 0 < parseInt( SiteHealth.site_status.issues.recommended, 0 ) ) { $( '#health-check-issues-recommended' ).removeClass( 'hidden' ); diff --git a/src/wp-admin/css/admin-menu.css b/src/wp-admin/css/admin-menu.css index 9e2851a13542f..7889a3e76008c 100644 --- a/src/wp-admin/css/admin-menu.css +++ b/src/wp-admin/css/admin-menu.css @@ -425,7 +425,7 @@ ul#adminmenu > li.current > a.current::after { display: inline-block; vertical-align: top; box-sizing: border-box; - margin: 1px 0 -1px 6px; + margin: 1px 0 -1px 2px; padding: 0 5px; min-width: 18px; height: 18px; diff --git a/src/wp-admin/menu-header.php b/src/wp-admin/menu-header.php index a639e9e1824ac..db0aefc3761b8 100644 --- a/src/wp-admin/menu-header.php +++ b/src/wp-admin/menu-header.php @@ -160,7 +160,7 @@ function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true ) { $count_description = ''; if ( ! empty( $item['count_description']['id'] ) ) { $describedby = ' aria-describedby="' . esc_attr( $item['count_description']['id'] ) . '"'; - $count_description = $item['count_description']['html']; + $count_description = $item['count_description']['html'] ?? ''; } // Hide separators from screen readers. @@ -288,7 +288,7 @@ function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true ) { $sub_count_description = ''; if ( ! empty( $sub_item['count_description']['id'] ) ) { $sub_describedby = ' aria-describedby="' . esc_attr( $sub_item['count_description']['id'] ) . '"'; - $sub_count_description = $sub_item['count_description']['html']; + $sub_count_description = $sub_item['count_description']['html'] ?? ''; } if ( ! empty( $menu_hook ) diff --git a/src/wp-admin/menu.php b/src/wp-admin/menu.php index 2c2ff1aac4465..d947392940775 100644 --- a/src/wp-admin/menu.php +++ b/src/wp-admin/menu.php @@ -54,16 +54,19 @@ } $updates_count = sprintf( - '', + '', $update_data['counts']['total'], number_format_i18n( $update_data['counts']['total'] ) ); - $updates_text = sprintf( - /* translators: Hidden accessibility text. %s: Number of updates available. */ - _n( '%s update available', '%s updates available', $update_data['counts']['total'] ), - number_format_i18n( $update_data['counts']['total'] ) - ); + $updates_text = ''; + if ( $update_data['counts']['total'] > 0 ) { + $updates_text = sprintf( + /* translators: Hidden accessibility text. %s: Number of updates available. */ + _n( '%s update available', '%s updates available', $update_data['counts']['total'] ), + number_format_i18n( $update_data['counts']['total'] ) + ); + } // Hidden count description; exposed to assistive tech via aria-describedby on the link. $updates_description = ''; @@ -120,7 +123,7 @@ $awaiting_moderation_text = sprintf( _n( '%s Comment in moderation', '%s Comments in moderation', $awaiting_moderation ), $awaiting_moderation_i18n ); $menu[25] = array( - __( 'Comments' ) . '', + __( 'Comments' ) . '', 'edit_posts', 'edit-comments.php', '', @@ -133,7 +136,7 @@ $menu[25]['count_description'] = array( 'id' => 'wp-menu-comments-count-description', // Hidden count description; exposed to assistive tech via aria-describedby on the link. - 'html' => '', + 'html' => '', ); unset( $awaiting_moderation ); @@ -231,36 +234,39 @@ $menu[60] = array( __( 'Appearance' ), $appearance_capability, 'themes.php', '', 'menu-top menu-icon-appearance', 'menu-appearance', 'dashicons-admin-appearance' ); -$count = ''; -$description = ''; +$count = ''; +$themes_description = ''; if ( ! is_multisite() && current_user_can( 'update_themes' ) ) { if ( ! isset( $update_data ) ) { $update_data = wp_get_update_data(); } $count = sprintf( - '', + '', $update_data['counts']['themes'], number_format_i18n( $update_data['counts']['themes'] ) ); - $themes_text = sprintf( - /* translators: Hidden accessibility text. %s: Number of available theme updates. */ - _n( '%s theme update available', '%s theme updates available', $update_data['counts']['themes'] ), - number_format_i18n( $update_data['counts']['themes'] ) - ); + $themes_text = ''; + if ( $update_data['counts']['themes'] > 0 ) { + $themes_text = sprintf( + /* translators: Hidden accessibility text. %s: Number of available theme updates. */ + _n( '%s theme update available', '%s theme updates available', $update_data['counts']['themes'] ), + number_format_i18n( $update_data['counts']['themes'] ) + ); + } // Hidden count description; exposed to assistive tech via aria-describedby on the link. - $description = ''; + $themes_description = ''; } $submenu['themes.php'][5] = array( __( 'Themes' ) . $count, $appearance_capability, 'themes.php' ); // Associate the hidden count description with the link. See _wp_menu_output(). -if ( '' !== $description ) { +if ( '' !== $themes_description ) { $submenu['themes.php'][5]['count_description'] = array( 'id' => 'wp-menu-themes-count-description', - 'html' => $description, + 'html' => $themes_description, ); } @@ -347,35 +353,38 @@ function _add_plugin_file_editor_to_tools() { ); } -$count = ''; -$description = ''; +$count = ''; +$plugins_description = ''; if ( ! is_multisite() && current_user_can( 'update_plugins' ) ) { if ( ! isset( $update_data ) ) { $update_data = wp_get_update_data(); } $count = sprintf( - '', + '', $update_data['counts']['plugins'], number_format_i18n( $update_data['counts']['plugins'] ) ); - $plugins_text = sprintf( - /* translators: Hidden accessibility text. %s: Number of available plugin updates. */ - _n( '%s plugin update available', '%s plugin updates available', $update_data['counts']['plugins'] ), - number_format_i18n( $update_data['counts']['plugins'] ) - ); + $plugins_text = ''; + if ( $update_data['counts']['plugins'] > 0 ) { + $plugins_text = sprintf( + /* translators: Hidden accessibility text. %s: Number of available plugin updates. */ + _n( '%s plugin update available', '%s plugin updates available', $update_data['counts']['plugins'] ), + number_format_i18n( $update_data['counts']['plugins'] ) + ); + } // Hidden count description; exposed to assistive tech via aria-describedby on the link. - $description = ''; + $plugins_description = ''; } $menu[65] = array( __( 'Plugins' ) . $count, 'activate_plugins', 'plugins.php', '', 'menu-top menu-icon-plugins', 'menu-plugins', 'dashicons-admin-plugins' ); // Associate the hidden count description with the link. See _wp_menu_output(). -if ( '' !== $description ) { +if ( '' !== $plugins_description ) { $menu[65]['count_description'] = array( 'id' => 'wp-menu-plugins-count-description', - 'html' => $description, + 'html' => $plugins_description, ); } @@ -439,16 +448,19 @@ function _add_plugin_file_editor_to_tools() { } $site_health_count = sprintf( - '', + '', $issue_counts['critical'], number_format_i18n( $issue_counts['critical'] ) ); - $site_health_text = sprintf( - /* translators: Hidden accessibility text. %s: Number of critical Site Health checks. */ - _n( '%s critical issue', '%s critical issues', $issue_counts['critical'] ), - number_format_i18n( $issue_counts['critical'] ) - ); + $site_health_text = ''; + if ( $issue_counts['critical'] > 0 ) { + $site_health_text = sprintf( + /* translators: Hidden accessibility text. %s: Number of critical Site Health checks. */ + _n( '%s critical issue', '%s critical issues', $issue_counts['critical'] ), + number_format_i18n( $issue_counts['critical'] ) + ); + } // Hidden count description; exposed to assistive tech via aria-describedby on the link. $site_health_description = ''; diff --git a/src/wp-includes/admin-bar.php b/src/wp-includes/admin-bar.php index 8520010f3bf32..4762cbf68dcc2 100644 --- a/src/wp-includes/admin-bar.php +++ b/src/wp-includes/admin-bar.php @@ -1133,8 +1133,8 @@ function wp_admin_bar_comments_menu( $wp_admin_bar ) { ); $icon = ''; - $title = ''; - $title .= '' . __( 'Comments' ) . ''; + $title = '' . __( 'Comments' ) . ''; + $title .= ''; // Hidden count description; exposed to assistive tech via aria-describedby on the link. $title .= ''; @@ -1253,8 +1253,8 @@ function wp_admin_bar_updates_menu( $wp_admin_bar ) { ); $icon = ''; - $title = ''; - $title .= '' . __( 'Updates' ) . ''; + $title = '' . __( 'Updates' ) . ''; + $title .= ''; // Hidden count description; exposed to assistive tech via aria-describedby on the link. $title .= ''; diff --git a/tests/phpunit/tests/admin/wpAdminMenuCounts.php b/tests/phpunit/tests/admin/wpAdminMenuCounts.php new file mode 100644 index 0000000000000..a66d473bd5d06 --- /dev/null +++ b/tests/phpunit/tests/admin/wpAdminMenuCounts.php @@ -0,0 +1,150 @@ +user->create( array( 'role' => 'administrator' ) ) ); + + add_filter( + 'wp_get_update_data', + static function () use ( $count ) { + return array( + 'counts' => array( + 'plugins' => $count, + 'themes' => $count, + 'wordpress' => 0, + 'translations' => 0, + 'total' => 2 * $count, + ), + 'title' => '', + ); + } + ); + + set_transient( + 'health-check-site-status-result', + wp_json_encode( + array( + 'good' => 0, + 'recommended' => 0, + 'critical' => $count, + ) + ) + ); + + if ( $count > 0 ) { + self::factory()->comment->create_many( + $count, + array( + 'comment_post_ID' => self::factory()->post->create(), + 'comment_approved' => '0', + ) + ); + } + + $pagenow = 'index.php'; + $menu = array(); + $submenu = array(); + + require_once ABSPATH . 'wp-admin/includes/admin.php'; + require ABSPATH . 'wp-admin/menu.php'; + + return array( $menu, $submenu ); + } + + /** + * Returns the menu item whose slug matches. + * + * @param array $items Menu or submenu items. + * @param string $slug Menu slug. + * @return array The item, or an empty array. + */ + private function find_item( $items, $slug ) { + foreach ( $items as $item ) { + if ( $slug === $item[2] ) { + return $item; + } + } + + return array(); + } + + /** + * Returns the five counter items keyed by description id. + * + * @param int $count Count to build the menu with. + * @return array[] + */ + private function counter_items( $count ) { + list( $menu, $submenu ) = $this->build_menu( $count ); + + return array( + 'wp-menu-updates-count-description' => $this->find_item( $submenu['index.php'], 'update-core.php' ), + 'wp-menu-comments-count-description' => $this->find_item( $menu, 'edit-comments.php' ), + 'wp-menu-themes-count-description' => $this->find_item( $submenu['themes.php'], 'themes.php' ), + 'wp-menu-plugins-count-description' => $this->find_item( $menu, 'plugins.php' ), + 'wp-menu-site-health-count-description' => $this->find_item( $submenu['tools.php'], 'site-health.php' ), + ); + } + + /** + * At zero, each counter keeps its description element but with no text. + * + * @ticket 65793 + * + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function test_zero_counts_have_empty_descriptions() { + foreach ( $this->counter_items( 0 ) as $id => $item ) { + $this->assertSame( $id, $item['count_description']['id'], "$id should still be referenced." ); + $this->assertMatchesRegularExpression( + '#^]* hidden>$#', + $item['count_description']['html'], + "$id should have no text at zero." + ); + } + } + + /** + * With a count, each description has text and each bubble sits in an + * aria-hidden wrapper that starts with a space. + * + * @ticket 65793 + * + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function test_nonzero_counts_have_text_and_wrapped_bubbles() { + foreach ( $this->counter_items( 2 ) as $id => $item ) { + $this->assertMatchesRegularExpression( + '#^]* hidden>[^<]+$#', + $item['count_description']['html'], + "$id should have text." + ); + $this->assertMatchesRegularExpression( + '#^[^<]+