Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions src/wp-includes/admin-bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<span class="ab-icon svg-icon" aria-hidden="true">' . wp_get_icon( $icon_name ) . '</span>';
$icon = wp_get_icon( $icon_name );

if ( '' === $icon ) {
return '';
}

return '<span class="ab-icon svg-icon" aria-hidden="true">' . $icon . '</span>';
}

/**
Expand All @@ -149,7 +155,11 @@ function wp_admin_bar_wp_menu( $wp_admin_bar ) {

$wp_logo_menu_args = array(
'id' => 'wp-logo',
'title' => '<span class="ab-icon" aria-hidden="true"></span><span class="screen-reader-text">' .
/*
* 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' ) . '<span class="screen-reader-text">' .
/* translators: Hidden accessibility text. */
__( 'About WordPress' ) .
'</span>',
Expand Down Expand Up @@ -255,7 +265,8 @@ function wp_admin_bar_sidebar_toggle( $wp_admin_bar ) {
$wp_admin_bar->add_node(
array(
'id' => 'menu-toggle',
'title' => '<span class="ab-icon" aria-hidden="true"></span><span class="screen-reader-text">' .
'icon' => 'core-admin/menu',
'title' => '<span class="screen-reader-text">' .
/* translators: Hidden accessibility text. */
__( 'Menu' ) .
'</span>',
Expand Down Expand Up @@ -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(
'<span class="ab-icon" aria-hidden="true"></span><span class="ab-label"><kbd>%s</kbd><span class="screen-reader-text"> %s</span></span>',
'<span class="ab-label"><kbd>%s</kbd><span class="screen-reader-text"> %s</span></span>',
$shortcut_label,
/* translators: Hidden accessibility text. */
__( 'Open command palette' ),
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -1113,11 +1125,12 @@ function wp_admin_bar_new_content_menu( $wp_admin_bar ) {
return;
}

$title = '<span class="ab-icon" aria-hidden="true"></span><span class="ab-label">' . _x( 'New', 'admin bar menu group label' ) . '</span>';
$title = '<span class="ab-label">' . _x( 'New', 'admin bar menu group label' ) . '</span>';

$wp_admin_bar->add_node(
array(
'id' => 'new-content',
'icon' => 'core-admin/plus',
'title' => $title,
'href' => admin_url( array_key_first( $actions ) ),
'meta' => array(
Expand Down Expand Up @@ -1171,14 +1184,14 @@ function wp_admin_bar_comments_menu( $wp_admin_bar ) {
number_format_i18n( $awaiting_mod )
);

$icon = '<span class="ab-icon" aria-hidden="true"></span>';
$title = '<span class="ab-label awaiting-mod pending-count count-' . $awaiting_mod . '" aria-hidden="true">' . number_format_i18n( $awaiting_mod ) . '</span>';
$title .= '<span class="screen-reader-text comments-in-moderation-text">' . $awaiting_text . '</span>';

$wp_admin_bar->add_node(
array(
'id' => 'comments',
'title' => $icon . $title,
'icon' => 'core-admin/comment',
'title' => $title,
'href' => admin_url( 'edit-comments.php' ),
)
);
Expand Down Expand Up @@ -1286,14 +1299,14 @@ function wp_admin_bar_updates_menu( $wp_admin_bar ) {
number_format_i18n( $update_data['counts']['total'] )
);

$icon = '<span class="ab-icon" aria-hidden="true"></span>';
$title = '<span class="ab-label" aria-hidden="true">' . number_format_i18n( $update_data['counts']['total'] ) . '</span>';
$title .= '<span class="screen-reader-text updates-available-text">' . $updates_text . '</span>';

$wp_admin_bar->add_node(
array(
'id' => 'updates',
'title' => $icon . $title,
'icon' => 'core-admin/update',
'title' => $title,
'href' => network_admin_url( 'update-core.php' ),
)
);
Expand Down
84 changes: 4 additions & 80 deletions src/wp-includes/class-wp-admin-bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<span class="ab-icon">`
* 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.
Expand Down Expand Up @@ -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
* `<span class="ab-icon">` 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
*
Expand Down Expand Up @@ -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 `<span class="ab-icon">` in the title; without one, the node gets no default icon.
if ( $default_icon && $default_icon['in_title'] ) {
$title = preg_replace( '/<span\b[^>]*\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 );
Expand Down Expand Up @@ -699,7 +623,7 @@ final protected function _render_item( $node ) {
}
}

echo ">{$arrow}{$icon}{$title}";
echo ">{$arrow}{$icon}{$node->title}";

if ( $has_link ) {
echo '</a>';
Expand Down
11 changes: 11 additions & 0 deletions src/wp-includes/css/admin-bar.css
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Comment on lines +289 to +290

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can improve this comment, because I think future readers will have a hard time understanding this without spending some time reading this very PR:

  • what does "hide Dashicon next to the SVG icon" mean? -> explain that, for compat, we render both these things, and the new one is X, and we hide the element of Y, etc.
  • why is wp-logo special? -> explain something along the lines of: we don't want to break plugins that replace the WP logo, so we kept its rendering in the old style

*/
#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;
Expand Down
Loading
Loading