diff --git a/src/wp-includes/admin-bar.php b/src/wp-includes/admin-bar.php index 1cf33f29566bb..3600b0d814237 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 ''; } /** @@ -149,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' ) . '', @@ -255,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' ) . '', @@ -1016,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' ), @@ -1049,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( @@ -1113,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( @@ -1171,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' ), ) ); @@ -1286,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 a657ec7ff0957..6c8bbefd6fa00 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. @@ -183,68 +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], - ); - } - /** * @since 3.3.0 * @@ -641,24 +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: the icon replaces the first empty `` in the title; without one, the node gets no default icon. - if ( $default_icon && $default_icon['in_title'] ) { - $title = preg_replace( '/]*\bclass=(["\'])(?:[^"\']*\s)?ab-icon(?:\s[^"\']*)?\1[^>]*>\s*<\/span>/', '', (string) $title, 1, $count ); - if ( ! $count ) { - $default_icon = false; - } - } - - if ( ! $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 ); @@ -699,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 0254aa5ee0c2c..f5b5b5faa8082 100644 --- a/src/wp-includes/css/admin-bar.css +++ b/src/wp-includes/css/admin-bar.css @@ -285,6 +285,17 @@ 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, + * 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 .ab-item > .ab-icon.svg-icon ~ .ab-icon.svg-icon { + display: none; +} + #wpadminbar .ab-icon.svg-icon svg { flex: none; width: 24px; diff --git a/tests/phpunit/tests/adminbar.php b/tests/phpunit/tests/adminbar.php index ecdbf0e4174a7..22c3328582616 100644 --- a/tests/phpunit/tests/adminbar.php +++ b/tests/phpunit/tests/adminbar.php @@ -1102,285 +1102,39 @@ static function () { } /** - * 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 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_default_icon_in_place_of_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->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', - '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.' ); - } - - /** - * 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 only the first one is replaced. - * - * @ticket 65089 - * - * @covers WP_Admin_Bar::_render_item - * - * @dataProvider data_empty_icon_elements - * - * @param string $title Node title. - * @param string $expected_rest The title expected after the icon element. - */ - public function test_empty_icon_elements_are_replaced_with_default_icon( $title, $expected_rest ): 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( $expected_rest ), $html ); - } - - /** - * Data provider. - * - * @return array - */ - public static function data_empty_icon_elements(): 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' ), - ); - } - - /** - * Dashicons back-compat: an icon element that has content 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 */ - public function test_icon_element_with_content_is_not_replaced(): void { - $title = 'Label'; + 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 ); + $this->assertStringNotContainsString( 'svg-icon', $html, 'The WordPress logo should not be rendered.' ); + $this->assertStringContainsString( $title, $html, 'The new title should be rendered.' ); } /** - * Dashicons back-compat: only core nodes get a default icon. - * - * @ticket 65089 + * An icon that is not registered counts as no icon, so that a Dashicon drawn for earlier versions still shows. * - * @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 ); - } - - /** * @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( @@ -1392,7 +1146,8 @@ public function test_unregistered_icon_renders_empty_icon_element(): void { ); $html = get_echo( array( $admin_bar, 'render' ) ); - $this->assertStringContainsString( 'Label', $html ); + $this->assertStringNotContainsString( 'ab-icon', $html, 'No icon element should be rendered.' ); + $this->assertStringContainsString( 'Label', $html, 'The title should be rendered.' ); } /** @@ -1402,7 +1157,7 @@ public function test_unregistered_icon_renders_empty_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. */ @@ -1418,8 +1173,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.' ); } /** @@ -1427,7 +1182,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' ), @@ -1437,33 +1192,6 @@ public static function data_values_that_are_not_icon_names(): array { ); } - /** - * @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 * @@ -1489,40 +1217,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' ), - ); - } }