From 6f48332ee0932c9fee0e4d6d79f8d92195b175f5 Mon Sep 17 00:00:00 2001 From: Ashar Fuadi Date: Tue, 27 Oct 2026 19:04:33 +0700 Subject: [PATCH 1/7] Toolbar: Hide `.ab-item::before` icons next to SVG icons. A `content` set on `.ab-item::before` no longer replaces the icon of a node that has an SVG icon. Hide it, so that the two icons are not shown side by side. --- src/wp-includes/css/admin-bar.css | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/wp-includes/css/admin-bar.css b/src/wp-includes/css/admin-bar.css index c609cc9832e7f..1f90dff013597 100644 --- a/src/wp-includes/css/admin-bar.css +++ b/src/wp-includes/css/admin-bar.css @@ -278,6 +278,11 @@ html:lang(he-il) .rtl #wpadminbar * { text-indent: 0; } +/* Dashicons back-compat: hide an icon drawn on `.ab-item::before` so it doesn't show next to the SVG icon. */ +#wpadminbar .ab-item:has(> .ab-icon.svg-icon)::before { + display: none; +} + #wpadminbar .ab-icon.svg-icon svg { flex: none; width: 24px; From 8bcaf879822843888042afc615082cb13bdf0907 Mon Sep 17 00:00:00 2001 From: Ashar Fuadi Date: Wed, 28 Oct 2026 05:54:49 +0700 Subject: [PATCH 2/7] Toolbar: Let the icon of a node replace the empty icon element in its title. A node given the `icon` argument now has the first empty `` in its title replaced by the icon, as core nodes already do. So a node that draws a Dashicon in its title for earlier versions does not also show it next to the SVG icon. --- src/wp-includes/class-wp-admin-bar.php | 10 ++- tests/phpunit/tests/adminbar.php | 87 ++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/class-wp-admin-bar.php b/src/wp-includes/class-wp-admin-bar.php index a657ec7ff0957..819959d4fbf56 100644 --- a/src/wp-includes/class-wp-admin-bar.php +++ b/src/wp-includes/class-wp-admin-bar.php @@ -647,10 +647,14 @@ final protected function _render_item( $node ) { // Dashicons back-compat: a core node that was given no icon gets its default icon. $default_icon = self::get_default_icon( $node ); - // Dashicons back-compat: the icon replaces the first empty `` in the title; without one, the node gets no default icon. - if ( $default_icon && $default_icon['in_title'] ) { + $has_default_icon_in_title = $default_icon && $default_icon['in_title']; + + // Dashicons back-compat: the icon replaces the first empty `` in the title, so that a node given an icon does not also show its Dashicon element. + if ( $has_default_icon_in_title || wp_is_valid_icon_name( $icon_name ) ) { $title = preg_replace( '/]*\bclass=(["\'])(?:[^"\']*\s)?ab-icon(?:\s[^"\']*)?\1[^>]*>\s*<\/span>/', '', (string) $title, 1, $count ); - if ( ! $count ) { + + // Without one, a core node whose icon was drawn in it gets no default icon. + if ( ! $count && $has_default_icon_in_title ) { $default_icon = false; } } diff --git a/tests/phpunit/tests/adminbar.php b/tests/phpunit/tests/adminbar.php index 5cdd37cfdf6e3..86dbf2fa34845 100644 --- a/tests/phpunit/tests/adminbar.php +++ b/tests/phpunit/tests/adminbar.php @@ -1264,6 +1264,93 @@ public function test_other_nodes_get_no_default_icon(): void { $this->assertStringContainsString( $title, $html ); } + /** + * Dashicons back-compat: the icon of a node replaces the empty icon element that the title had for earlier versions. + * + * @ticket 65089 + * + * @covers WP_Admin_Bar::_render_item + * + * @dataProvider data_dashicon_elements + * + * @param string $dashicon_element Empty icon element in the title. + */ + public function test_icon_argument_replaces_empty_icon_element( $dashicon_element ): void { + $admin_bar = new WP_Admin_Bar(); + $admin_bar->add_node( + array( + 'id' => 'my-plugin', + 'icon' => 'core-admin/pin', + 'title' => $dashicon_element . 'Label', + 'href' => 'https://example.org/', + ) + ); + $html = get_echo( array( $admin_bar, 'render' ) ); + + $this->assertMatchesRegularExpression( $this->get_item_with_icon_pattern( 'Label' ), $html ); + $this->assertSame( 1, substr_count( $html, 'ab-icon' ), 'Only the SVG icon element should be rendered.' ); + } + + /** + * Data provider. + * + * @return array + */ + public static function data_dashicon_elements(): array { + return array( + 'empty icon element' => array( '' ), + 'Dashicons class' => array( '' ), + 'Dashicons class and font' => array( '' ), + ); + } + + /** + * Dashicons back-compat: an icon element that has content is kept next to the icon of a node. + * + * @ticket 65089 + * + * @covers WP_Admin_Bar::_render_item + */ + public function test_icon_argument_keeps_icon_element_with_content(): void { + $title = 'Label'; + $admin_bar = new WP_Admin_Bar(); + $admin_bar->add_node( + array( + 'id' => 'my-plugin', + 'icon' => 'core-admin/pin', + 'title' => $title, + 'href' => 'https://example.org/', + ) + ); + $html = get_echo( array( $admin_bar, 'render' ) ); + + $this->assertMatchesRegularExpression( $this->get_item_with_icon_pattern( $title ), $html ); + } + + /** + * Dashicons back-compat: a value that is not an icon name keeps the empty icon element. + * + * @ticket 65089 + * + * @covers WP_Admin_Bar::_render_item + */ + public function test_value_that_is_not_an_icon_name_keeps_empty_icon_element(): void { + $title = 'Label'; + $admin_bar = new WP_Admin_Bar(); + $admin_bar->add_node( + array( + 'id' => 'my-plugin', + 'icon' => 'dashicons-chart-bar', + 'title' => $title, + 'href' => 'https://example.org/', + ) + ); + $html = get_echo( array( $admin_bar, 'render' ) ); + + $this->assertStringNotContainsString( 'svg-icon', $html ); + $this->assertStringContainsString( $title, $html ); + } + /** * @ticket 65089 * From 35a84b4e6560d43b98c63e2acd541d6013f11228 Mon Sep 17 00:00:00 2001 From: Ashar Fuadi Date: Wed, 28 Oct 2026 11:19:23 +0700 Subject: [PATCH 3/7] Address comments --- tests/phpunit/tests/adminbar.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/adminbar.php b/tests/phpunit/tests/adminbar.php index 86dbf2fa34845..7bf5b4e86c8ee 100644 --- a/tests/phpunit/tests/adminbar.php +++ b/tests/phpunit/tests/adminbar.php @@ -1271,7 +1271,7 @@ public function test_other_nodes_get_no_default_icon(): void { * * @covers WP_Admin_Bar::_render_item * - * @dataProvider data_dashicon_elements + * @dataProvider data_icon_argument_replaces_empty_icon_element * * @param string $dashicon_element Empty icon element in the title. */ @@ -1287,16 +1287,16 @@ public function test_icon_argument_replaces_empty_icon_element( $dashicon_elemen ); $html = get_echo( array( $admin_bar, 'render' ) ); - $this->assertMatchesRegularExpression( $this->get_item_with_icon_pattern( 'Label' ), $html ); + $this->assertMatchesRegularExpression( $this->get_item_with_icon_pattern( 'Label' ), $html, 'The SVG icon element should be rendered before the label.' ); $this->assertSame( 1, substr_count( $html, 'ab-icon' ), 'Only the SVG icon element should be rendered.' ); } /** - * Data provider. + * Data provider for test_icon_argument_replaces_empty_icon_element(). * * @return array */ - public static function data_dashicon_elements(): array { + public static function data_icon_argument_replaces_empty_icon_element(): array { return array( 'empty icon element' => array( '' ), 'Dashicons class' => array( '' ), @@ -1347,8 +1347,8 @@ public function test_value_that_is_not_an_icon_name_keeps_empty_icon_element(): ); $html = get_echo( array( $admin_bar, 'render' ) ); - $this->assertStringNotContainsString( 'svg-icon', $html ); - $this->assertStringContainsString( $title, $html ); + $this->assertStringNotContainsString( 'svg-icon', $html, 'No SVG icon element should be rendered.' ); + $this->assertStringContainsString( $title, $html, 'The empty icon element should be kept in the title.' ); } /** From 85a37bf68a1cafea98a7801f09014ecebd36fd16 Mon Sep 17 00:00:00 2001 From: Ashar Fuadi Date: Thu, 29 Oct 2026 10:59:27 +0700 Subject: [PATCH 4/7] Use the HTML API to remove the empty icon element from a node title. --- src/wp-includes/class-wp-admin-bar.php | 55 ++++++++++++++++++++++++-- tests/phpunit/tests/adminbar.php | 6 +-- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/class-wp-admin-bar.php b/src/wp-includes/class-wp-admin-bar.php index 819959d4fbf56..ff4dfed860426 100644 --- a/src/wp-includes/class-wp-admin-bar.php +++ b/src/wp-includes/class-wp-admin-bar.php @@ -245,6 +245,53 @@ private static function get_default_icon( $node ) { ); } + /** + * Dashicons back-compat: removes the first empty `` from a node title. + * + * @since 7.2.0 + * + * @param string $title Node title. + * @return string|false Title without the empty icon element, or false if it has none. + */ + private static function remove_empty_icon_element( $title ) { + $processor = new class( $title ) extends WP_HTML_Tag_Processor { + /** + * Removes the HTML from the given bookmark to the end of the current token. + * + * @param string $bookmark_name Bookmark at the start of the HTML to remove. + */ + public function remove_since_bookmark( $bookmark_name ): void { + $this->set_bookmark( 'here' ); + $start = $this->bookmarks[ $bookmark_name ]->start; + $end = $this->bookmarks['here']->start + $this->bookmarks['here']->length; + $this->seek( $bookmark_name ); + + $this->lexical_updates[] = new WP_HTML_Text_Replacement( $start, $end - $start, '' ); + } + }; + + $query = array( + 'tag_name' => 'SPAN', + 'class_name' => 'ab-icon', + ); + + while ( $processor->next_tag( $query ) ) { + $processor->set_bookmark( 'opener' ); + + $processor->next_token(); + if ( '#text' === $processor->get_token_type() && '' === trim( $processor->get_modifiable_text() ) ) { + $processor->next_token(); + } + + if ( 'SPAN' === $processor->get_tag() && $processor->is_tag_closer() ) { + $processor->remove_since_bookmark( 'opener' ); + return $processor->get_updated_html(); + } + } + + return false; + } + /** * @since 3.3.0 * @@ -651,10 +698,12 @@ final protected function _render_item( $node ) { // Dashicons back-compat: the icon replaces the first empty `` in the title, so that a node given an icon does not also show its Dashicon element. if ( $has_default_icon_in_title || wp_is_valid_icon_name( $icon_name ) ) { - $title = preg_replace( '/]*\bclass=(["\'])(?:[^"\']*\s)?ab-icon(?:\s[^"\']*)?\1[^>]*>\s*<\/span>/', '', (string) $title, 1, $count ); + $title_without_icon_element = self::remove_empty_icon_element( (string) $title ); - // Without one, a core node whose icon was drawn in it gets no default icon. - if ( ! $count && $has_default_icon_in_title ) { + if ( false !== $title_without_icon_element ) { + $title = $title_without_icon_element; + } elseif ( $has_default_icon_in_title ) { + // Without one, a core node whose icon was drawn in it gets no default icon. $default_icon = false; } } diff --git a/tests/phpunit/tests/adminbar.php b/tests/phpunit/tests/adminbar.php index 7bf5b4e86c8ee..e625184795d3a 100644 --- a/tests/phpunit/tests/adminbar.php +++ b/tests/phpunit/tests/adminbar.php @@ -1298,9 +1298,9 @@ public function test_icon_argument_replaces_empty_icon_element( $dashicon_elemen */ public static function data_icon_argument_replaces_empty_icon_element(): array { return array( - 'empty icon element' => array( '' ), - 'Dashicons class' => array( '' ), - 'Dashicons class and font' => array( '' ), + 'empty icon element' => array( '' ), + 'Dashicons class' => array( '' ), + 'whitespace inside' => array( "\n\t" ), ); } From bd11baf04f2f6eadb22f5712ef8d2510b5d05215 Mon Sep 17 00:00:00 2001 From: Ashar Fuadi Date: Thu, 29 Oct 2026 20:45:11 +0700 Subject: [PATCH 5/7] Remove empty span only for registered icons --- src/wp-includes/class-wp-admin-bar.php | 6 ++++++ src/wp-includes/css/admin-bar.css | 2 +- tests/phpunit/tests/adminbar.php | 26 ++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-admin-bar.php b/src/wp-includes/class-wp-admin-bar.php index ff4dfed860426..fa65b7ebe3d9f 100644 --- a/src/wp-includes/class-wp-admin-bar.php +++ b/src/wp-includes/class-wp-admin-bar.php @@ -717,6 +717,12 @@ final protected function _render_item( $node ) { $icon = _wp_admin_bar_icon( $icon_name ); if ( ! WP_Icons_Registry::get_instance()->is_registered( $icon_name ) ) { + // Dashicons back-compat: restore the original title instead of showing an empty icon wrapper. + if ( $title !== $node->title ) { + $title = $node->title; + $icon = ''; + } + _doing_it_wrong( __METHOD__, sprintf( diff --git a/src/wp-includes/css/admin-bar.css b/src/wp-includes/css/admin-bar.css index 1f90dff013597..8f29a7a1cbe6f 100644 --- a/src/wp-includes/css/admin-bar.css +++ b/src/wp-includes/css/admin-bar.css @@ -279,7 +279,7 @@ html:lang(he-il) .rtl #wpadminbar * { } /* Dashicons back-compat: hide an icon drawn on `.ab-item::before` so it doesn't show next to the SVG icon. */ -#wpadminbar .ab-item:has(> .ab-icon.svg-icon)::before { +#wpadminbar .ab-item:has(> .ab-icon.svg-icon > svg)::before { display: none; } diff --git a/tests/phpunit/tests/adminbar.php b/tests/phpunit/tests/adminbar.php index e625184795d3a..fbe126fece754 100644 --- a/tests/phpunit/tests/adminbar.php +++ b/tests/phpunit/tests/adminbar.php @@ -1373,6 +1373,32 @@ public function test_unregistered_icon_renders_empty_icon_element(): void { $this->assertStringContainsString( 'Label', $html ); } + /** + * Dashicons back-compat: an icon that is not registered keeps the empty icon element of the title in its place. + * + * @ticket 65089 + * + * @covers WP_Admin_Bar::_render_item + * + * @expectedIncorrectUsage WP_Admin_Bar::_render_item + */ + public function test_unregistered_icon_keeps_empty_icon_element(): void { + $title = 'Label'; + $admin_bar = new WP_Admin_Bar(); + $admin_bar->add_node( + array( + 'id' => 'my-plugin', + 'icon' => 'my-plugin/not-registered', + 'title' => $title, + 'href' => 'https://example.org/', + ) + ); + $html = get_echo( array( $admin_bar, 'render' ) ); + + $this->assertStringNotContainsString( 'svg-icon', $html, 'No SVG icon element should be rendered.' ); + $this->assertStringContainsString( $title, $html, 'The empty icon element should be kept in the title.' ); + } + /** * Dashicons back-compat: `add_node()` used to ignore an `icon` key, so a value that is not an icon name still renders nothing. * From 65f8900c0cb1375a2972a939216468c6480b3615 Mon Sep 17 00:00:00 2001 From: Ashar Fuadi Date: Fri, 30 Oct 2026 18:47:28 +0700 Subject: [PATCH 6/7] Hide dashicon using CSS instead --- src/wp-includes/admin-bar.php | 10 +- src/wp-includes/class-wp-admin-bar.php | 60 ++----- src/wp-includes/css/admin-bar.css | 6 +- tests/phpunit/tests/adminbar.php | 223 +++++++------------------ 4 files changed, 85 insertions(+), 214 deletions(-) diff --git a/src/wp-includes/admin-bar.php b/src/wp-includes/admin-bar.php index 252c25237e13f..9aec016a278be 100644 --- a/src/wp-includes/admin-bar.php +++ b/src/wp-includes/admin-bar.php @@ -122,10 +122,16 @@ function wp_admin_bar_render() { * @access private * * @param string $icon_name Namespaced icon name, e.g. 'core-admin/wordpress'. - * @return string Admin bar icon markup. The wrapper is empty if the icon is not found. + * @return string Admin bar icon markup, or empty string if the icon is not found. */ function _wp_admin_bar_icon( $icon_name ) { - return ''; + $icon = wp_get_icon( $icon_name ); + + if ( '' === $icon ) { + return ''; + } + + return ''; } /** diff --git a/src/wp-includes/class-wp-admin-bar.php b/src/wp-includes/class-wp-admin-bar.php index fa65b7ebe3d9f..f11c325d85b4b 100644 --- a/src/wp-includes/class-wp-admin-bar.php +++ b/src/wp-includes/class-wp-admin-bar.php @@ -117,7 +117,9 @@ public function remove_menu( $id ) { * @type string $id ID of the item. * @type string $title Title of the node. * @type string $icon Optional. Namespaced icon name in the form "collection/icon-name", - * e.g. 'core-admin/pin'. Rendered before the title. + * e.g. 'core-admin/pin'. Rendered before the title, and hides a + * Dashicon that the node draws in an empty `` + * or on `.ab-item::before`. Ignored if the icon is not registered. * @type string $parent Optional. ID of the parent node. * @type string $href Optional. Link for the item. * @type bool $group Optional. Whether or not the node is a group. Default false. @@ -246,46 +248,25 @@ private static function get_default_icon( $node ) { } /** - * Dashicons back-compat: removes the first empty `` from a node title. + * Dashicons back-compat: checks whether a node title has an empty ``. * * @since 7.2.0 * * @param string $title Node title. - * @return string|false Title without the empty icon element, or false if it has none. + * @return bool True if the title has an empty icon element, else false. */ - private static function remove_empty_icon_element( $title ) { - $processor = new class( $title ) extends WP_HTML_Tag_Processor { - /** - * Removes the HTML from the given bookmark to the end of the current token. - * - * @param string $bookmark_name Bookmark at the start of the HTML to remove. - */ - public function remove_since_bookmark( $bookmark_name ): void { - $this->set_bookmark( 'here' ); - $start = $this->bookmarks[ $bookmark_name ]->start; - $end = $this->bookmarks['here']->start + $this->bookmarks['here']->length; - $this->seek( $bookmark_name ); - - $this->lexical_updates[] = new WP_HTML_Text_Replacement( $start, $end - $start, '' ); - } - }; - - $query = array( + private static function has_empty_icon_element( $title ) { + $processor = new WP_HTML_Tag_Processor( $title ); + $query = array( 'tag_name' => 'SPAN', 'class_name' => 'ab-icon', ); while ( $processor->next_tag( $query ) ) { - $processor->set_bookmark( 'opener' ); - $processor->next_token(); - if ( '#text' === $processor->get_token_type() && '' === trim( $processor->get_modifiable_text() ) ) { - $processor->next_token(); - } if ( 'SPAN' === $processor->get_tag() && $processor->is_tag_closer() ) { - $processor->remove_since_bookmark( 'opener' ); - return $processor->get_updated_html(); + return true; } } @@ -694,21 +675,12 @@ final protected function _render_item( $node ) { // Dashicons back-compat: a core node that was given no icon gets its default icon. $default_icon = self::get_default_icon( $node ); - $has_default_icon_in_title = $default_icon && $default_icon['in_title']; - - // Dashicons back-compat: the icon replaces the first empty `` in the title, so that a node given an icon does not also show its Dashicon element. - if ( $has_default_icon_in_title || wp_is_valid_icon_name( $icon_name ) ) { - $title_without_icon_element = self::remove_empty_icon_element( (string) $title ); - - if ( false !== $title_without_icon_element ) { - $title = $title_without_icon_element; - } elseif ( $has_default_icon_in_title ) { - // Without one, a core node whose icon was drawn in it gets no default icon. - $default_icon = false; - } + // Dashicons back-compat: without an empty `` in the title, the node gets no default icon. + if ( $default_icon && $default_icon['in_title'] && ! self::has_empty_icon_element( (string) $title ) ) { + $default_icon = false; } - if ( ! $icon_name && $default_icon ) { + if ( ! wp_is_valid_icon_name( $icon_name ) && $default_icon ) { $icon_name = $default_icon['icon']; } @@ -717,12 +689,6 @@ final protected function _render_item( $node ) { $icon = _wp_admin_bar_icon( $icon_name ); if ( ! WP_Icons_Registry::get_instance()->is_registered( $icon_name ) ) { - // Dashicons back-compat: restore the original title instead of showing an empty icon wrapper. - if ( $title !== $node->title ) { - $title = $node->title; - $icon = ''; - } - _doing_it_wrong( __METHOD__, sprintf( diff --git a/src/wp-includes/css/admin-bar.css b/src/wp-includes/css/admin-bar.css index 8f29a7a1cbe6f..3a54e3e3a502b 100644 --- a/src/wp-includes/css/admin-bar.css +++ b/src/wp-includes/css/admin-bar.css @@ -278,8 +278,10 @@ html:lang(he-il) .rtl #wpadminbar * { text-indent: 0; } -/* Dashicons back-compat: hide an icon drawn on `.ab-item::before` so it doesn't show next to the SVG icon. */ -#wpadminbar .ab-item:has(> .ab-icon.svg-icon > svg)::before { +/* Dashicons back-compat: hide a Dashicon next to the SVG icon, except `.ab-icon::before` on core nodes. */ +#wpadminbar .ab-item:has(> .ab-icon.svg-icon)::before, +#wpadminbar .ab-item:has(> .ab-icon.svg-icon) .ab-icon:not(.svg-icon):empty, +#wpadminbar li:not(#wp-admin-bar-site-name, #wp-admin-bar-my-sites, #wp-admin-bar-site-editor, #wp-admin-bar-customize, #wp-admin-bar-edit, #wp-admin-bar-my-account, #wp-admin-bar-wp-logo, #wp-admin-bar-menu-toggle, #wp-admin-bar-command-palette, #wp-admin-bar-new-content, #wp-admin-bar-comments, #wp-admin-bar-updates) > .ab-item > .ab-icon.svg-icon::before { display: none; } diff --git a/tests/phpunit/tests/adminbar.php b/tests/phpunit/tests/adminbar.php index fbe126fece754..6b4ba3daa1389 100644 --- a/tests/phpunit/tests/adminbar.php +++ b/tests/phpunit/tests/adminbar.php @@ -1053,7 +1053,7 @@ public function test_my_account_node_with_avatar_gets_no_default_icon(): void { } /** - * Dashicons back-compat: core nodes whose icon used to be on `.ab-icon::before` get the icon in place of the empty icon element. + * Dashicons back-compat: core nodes whose icon used to be on `.ab-icon::before` get the icon if their title has the empty icon element. * * @ticket 65089 * @@ -1063,51 +1063,19 @@ public function test_my_account_node_with_avatar_gets_no_default_icon(): void { * * @param string $id Node ID. */ - public function test_node_with_icon_on_ab_icon_before_gets_default_icon_in_place_of_empty_icon_element( $id ): void { + public function test_node_with_icon_on_ab_icon_before_gets_default_icon_with_empty_icon_element( $id ): void { + $title = 'Label'; $admin_bar = new WP_Admin_Bar(); $admin_bar->add_node( array( 'id' => $id, - 'title' => 'Label', - 'href' => 'https://example.org/', - ) - ); - $html = get_echo( array( $admin_bar, 'render' ) ); - - $this->assertMatchesRegularExpression( $this->get_item_with_icon_pattern( 'Label' ), $html ); - $this->assertStringNotContainsString( '', $html, 'The empty icon element should be removed.' ); - } - - /** - * Dashicons back-compat: core nodes whose icon used to be on `.ab-icon::before` get the given icon in place of the empty icon element. - * - * @ticket 65089 - * - * @covers WP_Admin_Bar::_render_item - * - * @dataProvider data_node_ids_with_icon_on_ab_icon_before - * - * @param string $id Node ID. - */ - public function test_node_with_icon_on_ab_icon_before_gets_given_icon_in_place_of_empty_icon_element( $id ): void { - $admin_bar = new WP_Admin_Bar(); - $admin_bar->add_node( - array( - 'id' => $id, - 'title' => 'Label', + 'title' => $title, 'href' => 'https://example.org/', ) ); - $admin_bar->add_node( - array( - 'id' => $id, - 'icon' => 'core-admin/pencil', - ) - ); $html = get_echo( array( $admin_bar, 'render' ) ); - $this->assertStringContainsString( 'Label', $html ); - $this->assertStringNotContainsString( '', $html, 'The empty icon element should be removed.' ); + $this->assertMatchesRegularExpression( $this->get_item_with_icon_pattern( $title ), $html ); } /** @@ -1178,18 +1146,17 @@ public static function data_core_node_ids(): array { } /** - * Dashicons back-compat: the empty icon element is found whatever its attributes and position, and only the first one is replaced. + * Dashicons back-compat: the empty icon element is found whatever its attributes and position, and the title is left as it is. * * @ticket 65089 * * @covers WP_Admin_Bar::_render_item * - * @dataProvider data_empty_icon_elements + * @dataProvider data_empty_icon_elements_get_default_icon * - * @param string $title Node title. - * @param string $expected_rest The title expected after the icon element. + * @param string $title Node title. */ - public function test_empty_icon_elements_are_replaced_with_default_icon( $title, $expected_rest ): void { + public function test_empty_icon_elements_get_default_icon( $title ): void { $admin_bar = new WP_Admin_Bar(); $admin_bar->add_node( array( @@ -1200,60 +1167,39 @@ public function test_empty_icon_elements_are_replaced_with_default_icon( $title, ); $html = get_echo( array( $admin_bar, 'render' ) ); - $this->assertMatchesRegularExpression( $this->get_item_with_icon_pattern( $expected_rest ), $html ); + $this->assertMatchesRegularExpression( $this->get_item_with_icon_pattern( $title ), $html ); } /** * Data provider. * - * @return array + * @return array */ - public static function data_empty_icon_elements(): array { + public static function data_empty_icon_elements_get_default_icon(): array { return array( - 'extra classes and attributes' => array( 'Label', 'Label' ), - 'single quotes' => array( "Label", 'Label' ), - 'whitespace inside' => array( " \n Label", 'Label' ), - 'not at the start' => array( 'Label ', 'Label ' ), - 'only the first of many' => array( 'Label', 'Label' ), + 'extra classes and attributes' => array( 'Label' ), + 'single quotes' => array( "Label" ), + 'not at the start' => array( 'Label ' ), + 'several' => array( 'Label' ), ); } /** - * Dashicons back-compat: an icon element that has content is left as it is. + * Dashicons back-compat: an icon element that has content, including whitespace as for the CSS `:empty` selector, is left as it is. * * @ticket 65089 * * @covers WP_Admin_Bar::_render_item - */ - public function test_icon_element_with_content_is_not_replaced(): void { - $title = 'Label'; - $admin_bar = new WP_Admin_Bar(); - $admin_bar->add_node( - array( - 'id' => 'wp-logo', - 'title' => $title, - 'href' => 'https://example.org/', - ) - ); - $html = get_echo( array( $admin_bar, 'render' ) ); - - $this->assertStringNotContainsString( 'svg-icon', $html ); - $this->assertStringContainsString( $title, $html ); - } - - /** - * Dashicons back-compat: only core nodes get a default icon. * - * @ticket 65089 + * @dataProvider data_icon_element_with_content_gets_no_default_icon * - * @covers WP_Admin_Bar::_render_item + * @param string $title Node title. */ - public function test_other_nodes_get_no_default_icon(): void { - $title = 'Label'; + public function test_icon_element_with_content_gets_no_default_icon( $title ): void { $admin_bar = new WP_Admin_Bar(); $admin_bar->add_node( array( - 'id' => 'my-plugin', + 'id' => 'wp-logo', 'title' => $title, 'href' => 'https://example.org/', ) @@ -1265,100 +1211,50 @@ public function test_other_nodes_get_no_default_icon(): void { } /** - * Dashicons back-compat: the icon of a node replaces the empty icon element that the title had for earlier versions. - * - * @ticket 65089 - * - * @covers WP_Admin_Bar::_render_item - * - * @dataProvider data_icon_argument_replaces_empty_icon_element - * - * @param string $dashicon_element Empty icon element in the title. - */ - public function test_icon_argument_replaces_empty_icon_element( $dashicon_element ): void { - $admin_bar = new WP_Admin_Bar(); - $admin_bar->add_node( - array( - 'id' => 'my-plugin', - 'icon' => 'core-admin/pin', - 'title' => $dashicon_element . 'Label', - 'href' => 'https://example.org/', - ) - ); - $html = get_echo( array( $admin_bar, 'render' ) ); - - $this->assertMatchesRegularExpression( $this->get_item_with_icon_pattern( 'Label' ), $html, 'The SVG icon element should be rendered before the label.' ); - $this->assertSame( 1, substr_count( $html, 'ab-icon' ), 'Only the SVG icon element should be rendered.' ); - } - - /** - * Data provider for test_icon_argument_replaces_empty_icon_element(). + * Data provider. * * @return array */ - public static function data_icon_argument_replaces_empty_icon_element(): array { + public static function data_icon_element_with_content_gets_no_default_icon(): array { return array( - 'empty icon element' => array( '' ), - 'Dashicons class' => array( '' ), - 'whitespace inside' => array( "\n\t" ), + 'image inside' => array( 'Label' ), + 'whitespace inside' => array( " \n Label" ), ); } /** - * Dashicons back-compat: an icon element that has content is kept next to the icon of a node. + * Dashicons back-compat: only core nodes get a default icon. * * @ticket 65089 * * @covers WP_Admin_Bar::_render_item */ - public function test_icon_argument_keeps_icon_element_with_content(): void { - $title = 'Label'; + public function test_other_nodes_get_no_default_icon(): void { + $title = 'Label'; $admin_bar = new WP_Admin_Bar(); $admin_bar->add_node( array( 'id' => 'my-plugin', - 'icon' => 'core-admin/pin', 'title' => $title, 'href' => 'https://example.org/', ) ); $html = get_echo( array( $admin_bar, 'render' ) ); - $this->assertMatchesRegularExpression( $this->get_item_with_icon_pattern( $title ), $html ); + $this->assertStringNotContainsString( 'svg-icon', $html ); + $this->assertStringContainsString( $title, $html ); } /** - * Dashicons back-compat: a value that is not an icon name keeps the empty icon element. + * An icon that is not registered counts as no icon, so that a Dashicon drawn for earlier versions still shows. * - * @ticket 65089 - * - * @covers WP_Admin_Bar::_render_item - */ - public function test_value_that_is_not_an_icon_name_keeps_empty_icon_element(): void { - $title = 'Label'; - $admin_bar = new WP_Admin_Bar(); - $admin_bar->add_node( - array( - 'id' => 'my-plugin', - 'icon' => 'dashicons-chart-bar', - 'title' => $title, - 'href' => 'https://example.org/', - ) - ); - $html = get_echo( array( $admin_bar, 'render' ) ); - - $this->assertStringNotContainsString( 'svg-icon', $html, 'No SVG icon element should be rendered.' ); - $this->assertStringContainsString( $title, $html, 'The empty icon element should be kept in the title.' ); - } - - /** * @ticket 65089 * * @covers WP_Admin_Bar::_render_item * * @expectedIncorrectUsage WP_Admin_Bar::_render_item */ - public function test_unregistered_icon_renders_empty_icon_element(): void { + public function test_unregistered_icon_renders_no_icon_element(): void { $admin_bar = new WP_Admin_Bar(); $admin_bar->add_node( array( @@ -1370,33 +1266,8 @@ public function test_unregistered_icon_renders_empty_icon_element(): void { ); $html = get_echo( array( $admin_bar, 'render' ) ); - $this->assertStringContainsString( 'Label', $html ); - } - - /** - * Dashicons back-compat: an icon that is not registered keeps the empty icon element of the title in its place. - * - * @ticket 65089 - * - * @covers WP_Admin_Bar::_render_item - * - * @expectedIncorrectUsage WP_Admin_Bar::_render_item - */ - public function test_unregistered_icon_keeps_empty_icon_element(): void { - $title = 'Label'; - $admin_bar = new WP_Admin_Bar(); - $admin_bar->add_node( - array( - 'id' => 'my-plugin', - 'icon' => 'my-plugin/not-registered', - 'title' => $title, - 'href' => 'https://example.org/', - ) - ); - $html = get_echo( array( $admin_bar, 'render' ) ); - - $this->assertStringNotContainsString( 'svg-icon', $html, 'No SVG icon element should be rendered.' ); - $this->assertStringContainsString( $title, $html, 'The empty icon element should be kept in the title.' ); + $this->assertStringNotContainsString( 'ab-icon', $html ); + $this->assertStringContainsString( 'Label', $html ); } /** @@ -1441,6 +1312,32 @@ public static function data_values_that_are_not_icon_names(): array { ); } + /** + * Dashicons back-compat: a value that is not an icon name counts as no icon, so that a core node keeps its default icon. + * + * @ticket 65089 + * + * @covers WP_Admin_Bar::_render_item + * + * @dataProvider data_values_that_are_not_icon_names + * + * @param mixed $icon Value of the `icon` argument. + */ + public function test_value_that_is_not_an_icon_name_keeps_default_icon( $icon ): void { + $admin_bar = new WP_Admin_Bar(); + $admin_bar->add_node( + array( + 'id' => 'new-content', + 'icon' => $icon, + 'title' => 'Label', + 'href' => 'https://example.org/', + ) + ); + $html = get_echo( array( $admin_bar, 'render' ) ); + + $this->assertStringContainsString( wp_get_icon( 'core-admin/plus' ) . 'Label', $html ); + } + /** * @ticket 65089 * From 47a436d1caef85c9abf6d084cd3eb290baac4569 Mon Sep 17 00:00:00 2001 From: Ashar Fuadi Date: Tue, 3 Nov 2026 14:50:34 +0700 Subject: [PATCH 7/7] Apply Aki's patch --- src/wp-includes/admin-bar.php | 23 +- src/wp-includes/class-wp-admin-bar.php | 103 +------- src/wp-includes/css/admin-bar.css | 8 +- tests/phpunit/tests/adminbar.php | 344 +------------------------ 4 files changed, 35 insertions(+), 443 deletions(-) diff --git a/src/wp-includes/admin-bar.php b/src/wp-includes/admin-bar.php index 9aec016a278be..ce1301912e6a4 100644 --- a/src/wp-includes/admin-bar.php +++ b/src/wp-includes/admin-bar.php @@ -155,7 +155,11 @@ function wp_admin_bar_wp_menu( $wp_admin_bar ) { $wp_logo_menu_args = array( 'id' => 'wp-logo', - 'title' => '' . + /* + * Dashicons back-compat: the icon is part of the title, so that a plugin that replaces + * the title with its own logo does not also get the WordPress logo. + */ + 'title' => _wp_admin_bar_icon( 'core-admin/wordpress' ) . '' . /* translators: Hidden accessibility text. */ __( 'About WordPress' ) . '', @@ -261,7 +265,8 @@ function wp_admin_bar_sidebar_toggle( $wp_admin_bar ) { $wp_admin_bar->add_node( array( 'id' => 'menu-toggle', - 'title' => '' . + 'icon' => 'core-admin/menu', + 'title' => '' . /* translators: Hidden accessibility text. */ __( 'Menu' ) . '', @@ -1022,7 +1027,7 @@ function wp_admin_bar_command_palette_menu( WP_Admin_Bar $wp_admin_bar ): void { $is_apple_os = (bool) preg_match( "/{$apple_pattern}/i", $_SERVER['HTTP_USER_AGENT'] ?? '' ); $shortcut_label = $is_apple_os ? $shortcut_labels['appleOS'] : $shortcut_labels['default']; $title = sprintf( - '%s %s', + '%s %s', $shortcut_label, /* translators: Hidden accessibility text. */ __( 'Open command palette' ), @@ -1055,6 +1060,7 @@ function wp_admin_bar_command_palette_menu( WP_Admin_Bar $wp_admin_bar ): void { $wp_admin_bar->add_node( array( 'id' => 'command-palette', + 'icon' => 'core-admin/search', 'title' => $title, 'href' => '#', 'meta' => array( @@ -1119,11 +1125,12 @@ function wp_admin_bar_new_content_menu( $wp_admin_bar ) { return; } - $title = '' . _x( 'New', 'admin bar menu group label' ) . ''; + $title = '' . _x( 'New', 'admin bar menu group label' ) . ''; $wp_admin_bar->add_node( array( 'id' => 'new-content', + 'icon' => 'core-admin/plus', 'title' => $title, 'href' => admin_url( array_key_first( $actions ) ), 'meta' => array( @@ -1177,14 +1184,14 @@ function wp_admin_bar_comments_menu( $wp_admin_bar ) { number_format_i18n( $awaiting_mod ) ); - $icon = ''; $title = ''; $title .= '' . $awaiting_text . ''; $wp_admin_bar->add_node( array( 'id' => 'comments', - 'title' => $icon . $title, + 'icon' => 'core-admin/comment', + 'title' => $title, 'href' => admin_url( 'edit-comments.php' ), ) ); @@ -1292,14 +1299,14 @@ function wp_admin_bar_updates_menu( $wp_admin_bar ) { number_format_i18n( $update_data['counts']['total'] ) ); - $icon = ''; $title = ''; $title .= '' . $updates_text . ''; $wp_admin_bar->add_node( array( 'id' => 'updates', - 'title' => $icon . $title, + 'icon' => 'core-admin/update', + 'title' => $title, 'href' => network_admin_url( 'update-core.php' ), ) ); diff --git a/src/wp-includes/class-wp-admin-bar.php b/src/wp-includes/class-wp-admin-bar.php index f11c325d85b4b..6c8bbefd6fa00 100644 --- a/src/wp-includes/class-wp-admin-bar.php +++ b/src/wp-includes/class-wp-admin-bar.php @@ -185,94 +185,6 @@ public function add_node( $args ) { $this->_set_node( $args ); } - /** - * Dashicons back-compat: returns the default icon for a node ID that core registers. - * - * The icons of these nodes used to be added with CSS rules for the node ID, - * in one of two ways. A node that has no icon of its own gets the icon under - * the same condition as before: - * - * - On `.ab-item::before`: the node gets the icon whatever its title is. The - * `my-account` node only gets it without the `with-avatar` class. - * - On `.ab-icon::before`: the node gets the icon in place of the empty - * `` of its title, and no icon without that element. - * - * @since 7.2.0 - * - * @param object $node The node. - * @return array|false { - * Default icon, or false if the node has none. - * - * @type string $icon Namespaced icon name. - * @type bool $in_title Whether the icon takes the place of the empty icon element of the title. - * } - */ - private static function get_default_icon( $node ) { - $id = $node->id; - $default_icons = array( - // On `.ab-item::before`. - 'site-name' => array( 'core-admin/dashboard', false ), - 'my-sites' => array( 'core-admin/sites', false ), - 'site-editor' => array( 'core-admin/brush', false ), - 'customize' => array( 'core-admin/brush', false ), - 'edit' => array( 'core-admin/pencil', false ), - 'my-account' => array( 'core-admin/people', false ), - // On `.ab-icon::before`, in the title. - 'wp-logo' => array( 'core-admin/wordpress', true ), - 'menu-toggle' => array( 'core-admin/menu', true ), - 'command-palette' => array( 'core-admin/search', true ), - 'new-content' => array( 'core-admin/plus', true ), - 'comments' => array( 'core-admin/comment', true ), - 'updates' => array( 'core-admin/update', true ), - ); - - if ( ! isset( $default_icons[ $id ] ) ) { - return false; - } - - // The account icon is only for a node without an avatar. - if ( 'my-account' === $id && in_array( 'with-avatar', explode( ' ', $node->meta['class'] ?? '' ), true ) ) { - return false; - } - - $icon = $default_icons[ $id ][0]; - - if ( 'site-name' === $id && ( is_admin() || ! current_user_can( 'read' ) ) ) { - $icon = 'core-admin/home'; - } - - return array( - 'icon' => $icon, - 'in_title' => $default_icons[ $id ][1], - ); - } - - /** - * Dashicons back-compat: checks whether a node title has an empty ``. - * - * @since 7.2.0 - * - * @param string $title Node title. - * @return bool True if the title has an empty icon element, else false. - */ - private static function has_empty_icon_element( $title ) { - $processor = new WP_HTML_Tag_Processor( $title ); - $query = array( - 'tag_name' => 'SPAN', - 'class_name' => 'ab-icon', - ); - - while ( $processor->next_tag( $query ) ) { - $processor->next_token(); - - if ( 'SPAN' === $processor->get_tag() && $processor->is_tag_closer() ) { - return true; - } - } - - return false; - } - /** * @since 3.3.0 * @@ -669,21 +581,8 @@ final protected function _render_item( $node ) { $menuclass = ' class="' . esc_attr( trim( $menuclass ) ) . '"'; } - $title = $node->title; $icon_name = $node->icon; - // Dashicons back-compat: a core node that was given no icon gets its default icon. - $default_icon = self::get_default_icon( $node ); - - // Dashicons back-compat: without an empty `` in the title, the node gets no default icon. - if ( $default_icon && $default_icon['in_title'] && ! self::has_empty_icon_element( (string) $title ) ) { - $default_icon = false; - } - - if ( ! wp_is_valid_icon_name( $icon_name ) && $default_icon ) { - $icon_name = $default_icon['icon']; - } - $icon = ''; if ( wp_is_valid_icon_name( $icon_name ) ) { $icon = _wp_admin_bar_icon( $icon_name ); @@ -724,7 +623,7 @@ final protected function _render_item( $node ) { } } - echo ">{$arrow}{$icon}{$title}"; + echo ">{$arrow}{$icon}{$node->title}"; if ( $has_link ) { echo ''; diff --git a/src/wp-includes/css/admin-bar.css b/src/wp-includes/css/admin-bar.css index 3a54e3e3a502b..68c7ca9acc1ef 100644 --- a/src/wp-includes/css/admin-bar.css +++ b/src/wp-includes/css/admin-bar.css @@ -278,10 +278,14 @@ html:lang(he-il) .rtl #wpadminbar * { text-indent: 0; } -/* Dashicons back-compat: hide a Dashicon next to the SVG icon, except `.ab-icon::before` on core nodes. */ +/* + * Dashicons back-compat: hide a Dashicon next to the SVG icon, except `.ab-icon::before` on core nodes, + * and the icon in the title of the `wp-logo` node when the node is given an icon. + */ #wpadminbar .ab-item:has(> .ab-icon.svg-icon)::before, #wpadminbar .ab-item:has(> .ab-icon.svg-icon) .ab-icon:not(.svg-icon):empty, -#wpadminbar li:not(#wp-admin-bar-site-name, #wp-admin-bar-my-sites, #wp-admin-bar-site-editor, #wp-admin-bar-customize, #wp-admin-bar-edit, #wp-admin-bar-my-account, #wp-admin-bar-wp-logo, #wp-admin-bar-menu-toggle, #wp-admin-bar-command-palette, #wp-admin-bar-new-content, #wp-admin-bar-comments, #wp-admin-bar-updates) > .ab-item > .ab-icon.svg-icon::before { +#wpadminbar li:not(#wp-admin-bar-site-name, #wp-admin-bar-my-sites, #wp-admin-bar-site-editor, #wp-admin-bar-customize, #wp-admin-bar-edit, #wp-admin-bar-my-account, #wp-admin-bar-wp-logo, #wp-admin-bar-menu-toggle, #wp-admin-bar-command-palette, #wp-admin-bar-new-content, #wp-admin-bar-comments, #wp-admin-bar-updates) > .ab-item > .ab-icon.svg-icon::before, +#wpadminbar .ab-item > .ab-icon.svg-icon ~ .ab-icon.svg-icon { display: none; } diff --git a/tests/phpunit/tests/adminbar.php b/tests/phpunit/tests/adminbar.php index 6b4ba3daa1389..0f32f04d56cc5 100644 --- a/tests/phpunit/tests/adminbar.php +++ b/tests/phpunit/tests/adminbar.php @@ -993,256 +993,27 @@ public function test_menu_property_is_defined() { } /** - * Returns a pattern that matches a rendered item made of an SVG icon element followed by the given HTML. - * - * @param string $following_html The HTML expected between the icon element and the end of the item. - * @return string Regular expression. - */ - private function get_item_with_icon_pattern( $following_html ): string { - return '#]*>' . preg_quote( $following_html, '#' ) . '#s'; - } - - /** - * Dashicons back-compat: core nodes whose icon used to be on `.ab-item::before` get the icon from their ID alone. - * - * @ticket 65089 - * - * @covers WP_Admin_Bar::_render_item - * - * @dataProvider data_node_ids_with_icon_on_ab_item_before - * - * @param string $id Node ID. - */ - public function test_node_with_icon_on_ab_item_before_gets_default_icon_when_added_without_one( $id ): void { - wp_set_current_user( self::$editor_id ); - - $admin_bar = new WP_Admin_Bar(); - $admin_bar->add_node( - array( - 'id' => $id, - 'title' => 'Custom title', - 'href' => 'https://example.org/', - ) - ); - $html = get_echo( array( $admin_bar, 'render' ) ); - - $this->assertMatchesRegularExpression( $this->get_item_with_icon_pattern( 'Custom title' ), $html ); - } - - /** - * Dashicons back-compat: the `my-account` node gets no default icon when it has an avatar. - * - * @ticket 65089 - * - * @covers WP_Admin_Bar::_render_item - */ - public function test_my_account_node_with_avatar_gets_no_default_icon(): void { - $admin_bar = new WP_Admin_Bar(); - $admin_bar->add_node( - array( - 'id' => 'my-account', - 'title' => 'Custom title', - 'href' => 'https://example.org/', - 'meta' => array( 'class' => 'with-avatar' ), - ) - ); - $html = get_echo( array( $admin_bar, 'render' ) ); - - $this->assertStringNotContainsString( 'ab-icon', $html ); - $this->assertStringContainsString( 'Custom title', $html ); - } - - /** - * Dashicons back-compat: core nodes whose icon used to be on `.ab-icon::before` get the icon if their title has the empty icon element. - * - * @ticket 65089 - * - * @covers WP_Admin_Bar::_render_item - * - * @dataProvider data_node_ids_with_icon_on_ab_icon_before - * - * @param string $id Node ID. - */ - public function test_node_with_icon_on_ab_icon_before_gets_default_icon_with_empty_icon_element( $id ): void { - $title = 'Label'; - $admin_bar = new WP_Admin_Bar(); - $admin_bar->add_node( - array( - 'id' => $id, - 'title' => $title, - 'href' => 'https://example.org/', - ) - ); - $html = get_echo( array( $admin_bar, 'render' ) ); - - $this->assertMatchesRegularExpression( $this->get_item_with_icon_pattern( $title ), $html ); - } - - /** - * Dashicons back-compat: core nodes whose icon used to be on `.ab-icon::before` get no icon when their title has no empty icon element. - * - * @ticket 65089 - * - * @covers WP_Admin_Bar::_render_item - * - * @dataProvider data_node_ids_with_icon_on_ab_icon_before - * - * @param string $id Node ID. - */ - public function test_node_with_icon_on_ab_icon_before_gets_no_icon_without_empty_icon_element( $id ): void { - $admin_bar = new WP_Admin_Bar(); - $admin_bar->add_node( - array( - 'id' => $id, - 'title' => 'Label', - 'href' => 'https://example.org/', - ) - ); - $html = get_echo( array( $admin_bar, 'render' ) ); - - $this->assertStringNotContainsString( 'ab-icon', $html ); - $this->assertStringContainsString( 'Label', $html ); - } - - /** - * Data provider. - * - * @return array - */ - public static function data_node_ids_with_icon_on_ab_item_before(): array { - return array( - 'edit' => array( 'edit' ), - 'customize' => array( 'customize' ), - 'site-editor' => array( 'site-editor' ), - 'my-sites' => array( 'my-sites' ), - 'site-name' => array( 'site-name' ), - 'my-account' => array( 'my-account' ), - ); - } - - /** - * Data provider. - * - * @return array - */ - public static function data_node_ids_with_icon_on_ab_icon_before(): array { - return array( - 'wp-logo' => array( 'wp-logo' ), - 'menu-toggle' => array( 'menu-toggle' ), - 'command-palette' => array( 'command-palette' ), - 'new-content' => array( 'new-content' ), - 'comments' => array( 'comments' ), - 'updates' => array( 'updates' ), - ); - } - - /** - * Data provider. - * - * @return array - */ - public static function data_core_node_ids(): array { - return array_merge( self::data_node_ids_with_icon_on_ab_item_before(), self::data_node_ids_with_icon_on_ab_icon_before() ); - } - - /** - * Dashicons back-compat: the empty icon element is found whatever its attributes and position, and the title is left as it is. - * - * @ticket 65089 - * - * @covers WP_Admin_Bar::_render_item - * - * @dataProvider data_empty_icon_elements_get_default_icon - * - * @param string $title Node title. - */ - public function test_empty_icon_elements_get_default_icon( $title ): void { - $admin_bar = new WP_Admin_Bar(); - $admin_bar->add_node( - array( - 'id' => 'wp-logo', - 'title' => $title, - 'href' => 'https://example.org/', - ) - ); - $html = get_echo( array( $admin_bar, 'render' ) ); - - $this->assertMatchesRegularExpression( $this->get_item_with_icon_pattern( $title ), $html ); - } - - /** - * Data provider. - * - * @return array - */ - public static function data_empty_icon_elements_get_default_icon(): array { - return array( - 'extra classes and attributes' => array( 'Label' ), - 'single quotes' => array( "Label" ), - 'not at the start' => array( 'Label ' ), - 'several' => array( 'Label' ), - ); - } - - /** - * Dashicons back-compat: an icon element that has content, including whitespace as for the CSS `:empty` selector, is left as it is. + * Dashicons back-compat: replacing the title of the `wp-logo` node also replaces the WordPress logo. * * @ticket 65089 * + * @covers ::wp_admin_bar_wp_menu * @covers WP_Admin_Bar::_render_item - * - * @dataProvider data_icon_element_with_content_gets_no_default_icon - * - * @param string $title Node title. */ - public function test_icon_element_with_content_gets_no_default_icon( $title ): void { + public function test_wp_logo_node_title_replaced_without_icon_renders_no_icon(): void { + $title = ' Brand'; $admin_bar = new WP_Admin_Bar(); + wp_admin_bar_wp_menu( $admin_bar ); $admin_bar->add_node( array( 'id' => 'wp-logo', 'title' => $title, - 'href' => 'https://example.org/', ) ); $html = get_echo( array( $admin_bar, 'render' ) ); - $this->assertStringNotContainsString( 'svg-icon', $html ); - $this->assertStringContainsString( $title, $html ); - } - - /** - * Data provider. - * - * @return array - */ - public static function data_icon_element_with_content_gets_no_default_icon(): array { - return array( - 'image inside' => array( 'Label' ), - 'whitespace inside' => array( " \n Label" ), - ); - } - - /** - * Dashicons back-compat: only core nodes get a default icon. - * - * @ticket 65089 - * - * @covers WP_Admin_Bar::_render_item - */ - public function test_other_nodes_get_no_default_icon(): void { - $title = 'Label'; - $admin_bar = new WP_Admin_Bar(); - $admin_bar->add_node( - array( - 'id' => 'my-plugin', - 'title' => $title, - 'href' => 'https://example.org/', - ) - ); - $html = get_echo( array( $admin_bar, 'render' ) ); - - $this->assertStringNotContainsString( 'svg-icon', $html ); - $this->assertStringContainsString( $title, $html ); + $this->assertStringNotContainsString( 'svg-icon', $html, 'The WordPress logo should not be rendered.' ); + $this->assertStringContainsString( $title, $html, 'The new title should be rendered.' ); } /** @@ -1266,8 +1037,8 @@ public function test_unregistered_icon_renders_no_icon_element(): void { ); $html = get_echo( array( $admin_bar, 'render' ) ); - $this->assertStringNotContainsString( 'ab-icon', $html ); - $this->assertStringContainsString( 'Label', $html ); + $this->assertStringNotContainsString( 'ab-icon', $html, 'No icon element should be rendered.' ); + $this->assertStringContainsString( 'Label', $html, 'The title should be rendered.' ); } /** @@ -1277,7 +1048,7 @@ public function test_unregistered_icon_renders_no_icon_element(): void { * * @covers WP_Admin_Bar::_render_item * - * @dataProvider data_values_that_are_not_icon_names + * @dataProvider data_value_that_is_not_an_icon_name_renders_no_icon * * @param mixed $icon Value of the `icon` argument. */ @@ -1293,8 +1064,8 @@ public function test_value_that_is_not_an_icon_name_renders_no_icon( $icon ): vo ); $html = get_echo( array( $admin_bar, 'render' ) ); - $this->assertStringNotContainsString( 'ab-icon', $html ); - $this->assertStringContainsString( 'Label', $html ); + $this->assertStringNotContainsString( 'ab-icon', $html, 'No icon element should be rendered.' ); + $this->assertStringContainsString( 'Label', $html, 'The title should be rendered.' ); } /** @@ -1302,7 +1073,7 @@ public function test_value_that_is_not_an_icon_name_renders_no_icon( $icon ): vo * * @return array */ - public static function data_values_that_are_not_icon_names(): array { + public static function data_value_that_is_not_an_icon_name_renders_no_icon(): array { return array( 'Dashicons class' => array( 'dashicons-admin-site' ), 'URL' => array( 'https://example.org/icon.png' ), @@ -1312,59 +1083,6 @@ public static function data_values_that_are_not_icon_names(): array { ); } - /** - * Dashicons back-compat: a value that is not an icon name counts as no icon, so that a core node keeps its default icon. - * - * @ticket 65089 - * - * @covers WP_Admin_Bar::_render_item - * - * @dataProvider data_values_that_are_not_icon_names - * - * @param mixed $icon Value of the `icon` argument. - */ - public function test_value_that_is_not_an_icon_name_keeps_default_icon( $icon ): void { - $admin_bar = new WP_Admin_Bar(); - $admin_bar->add_node( - array( - 'id' => 'new-content', - 'icon' => $icon, - 'title' => 'Label', - 'href' => 'https://example.org/', - ) - ); - $html = get_echo( array( $admin_bar, 'render' ) ); - - $this->assertStringContainsString( wp_get_icon( 'core-admin/plus' ) . 'Label', $html ); - } - - /** - * @ticket 65089 - * - * @covers WP_Admin_Bar::_render_item - * - * @dataProvider data_core_node_ids - * - * @param string $id Node ID. - */ - public function test_icon_argument_takes_precedence_over_default_icon( $id ): void { - wp_set_current_user( self::$editor_id ); - - $admin_bar = new WP_Admin_Bar(); - $admin_bar->add_node( - array( - 'id' => $id, - 'icon' => 'core-admin/pin', - 'title' => 'Label', - 'href' => 'https://example.org/', - ) - ); - $html = get_echo( array( $admin_bar, 'render' ) ); - - $this->assertStringContainsString( '', $html ); - $this->assertSame( 1, substr_count( $html, 'svg-icon' ) ); - } - /** * @ticket 65089 * @@ -1390,40 +1108,4 @@ public function test_icon_is_kept_when_node_is_updated(): void { $this->assertStringContainsString( 'New label', $html ); } - - /** - * Dashicons back-compat: the titles of these nodes keep their empty icon element. - * - * @ticket 65089 - * - * @covers ::wp_admin_bar_wp_menu - * @covers ::wp_admin_bar_new_content_menu - * @covers ::wp_admin_bar_comments_menu - * - * @dataProvider data_core_node_ids_with_empty_icon_element - * - * @param string $id Node ID. - */ - public function test_core_node_title_starts_with_empty_icon_element( $id ): void { - wp_set_current_user( self::$editor_id ); - - $node = $this->get_standard_admin_bar()->get_node( $id ); - - $this->assertIsObject( $node ); - $this->assertStringStartsWith( '', $node->title ); - $this->assertFalse( $node->icon ); - } - - /** - * Data provider. - * - * @return array - */ - public static function data_core_node_ids_with_empty_icon_element(): array { - return array( - 'wp-logo' => array( 'wp-logo' ), - 'new-content' => array( 'new-content' ), - 'comments' => array( 'comments' ), - ); - } }