Skip to content

Commit 6646b94

Browse files
committed
Determine correct command palette label via JS.
1 parent a95dd0e commit 6646b94

1 file changed

Lines changed: 28 additions & 5 deletions

File tree

src/wp-includes/admin-bar.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -949,11 +949,13 @@ function wp_admin_bar_command_palette_menu( WP_Admin_Bar $wp_admin_bar ): void {
949949
return;
950950
}
951951

952-
$is_apple_os = (bool) preg_match( '/Macintosh|Mac OS X|Mac_PowerPC/i', $_SERVER['HTTP_USER_AGENT'] ?? '' );
953-
$shortcut_label = $is_apple_os
954-
? _x( '⌘K', 'keyboard shortcut to open the command palette' )
955-
: _x( 'Ctrl+K', 'keyboard shortcut to open the command palette' );
956-
$title = sprintf(
952+
$shortcut_labels = array(
953+
'appleOS' => _x( '⌘K', 'keyboard shortcut to open the command palette' ),
954+
'default' => _x( 'Ctrl+K', 'keyboard shortcut to open the command palette' ),
955+
);
956+
$is_apple_os = (bool) preg_match( '/Macintosh|Mac OS X|Mac_PowerPC/i', $_SERVER['HTTP_USER_AGENT'] ?? '' );
957+
$shortcut_label = $is_apple_os ? $shortcut_labels['appleOS'] : $shortcut_labels['default'];
958+
$title = sprintf(
957959
'<span class="ab-icon" aria-hidden="true"></span><span class="ab-label"><kbd>%s</kbd><span class="screen-reader-text"> %s</span></span>',
958960
$shortcut_label,
959961
/* translators: Hidden accessibility text. */
@@ -970,6 +972,27 @@ function wp_admin_bar_command_palette_menu( WP_Admin_Bar $wp_admin_bar ): void {
970972
),
971973
)
972974
);
975+
976+
$script = <<<JS
977+
(( shortCutLabels ) => {
978+
let userAgent = '';
979+
// Assigning agent may error if the HTTP header is blocked at the browser level.
980+
try {
981+
userAgent = navigator.userAgent;
982+
} catch (error) {
983+
// Make no change to the default shortcut label.
984+
return;
985+
}
986+
const isAppleOS = /Macintosh|Mac OS X|Mac_PowerPC/i.test( userAgent );
987+
const shortcutLabel = isAppleOS ? shortCutLabels.appleOS : shortCutLabels.default;
988+
const commandPaletteNode = document.querySelector( '#wp-admin-bar-command-palette .ab-label kbd' );
989+
if ( commandPaletteNode ) {
990+
commandPaletteNode.textContent = shortcutLabel;
991+
}
992+
})
993+
JS;
994+
$script .= '(' . wp_json_encode( $shortcut_labels ) . ');';
995+
wp_add_inline_script( 'admin-bar', $script );
973996
}
974997

975998
/**

0 commit comments

Comments
 (0)