Skip to content

Command Palette: Fix macOs label for sites unable to determine UA via PHP#11636

Open
peterwilsoncc wants to merge 19 commits intoWordPress:trunkfrom
peterwilsoncc:fix/65121-command-pallete-shortcut-js-fallback
Open

Command Palette: Fix macOs label for sites unable to determine UA via PHP#11636
peterwilsoncc wants to merge 19 commits intoWordPress:trunkfrom
peterwilsoncc:fix/65121-command-pallete-shortcut-js-fallback

Conversation

@peterwilsoncc
Copy link
Copy Markdown
Contributor

Adds a JavaScript check of the User-Agent for displaying the text in the admin bar.

On sites served over a CDN is it relatively common for the CDN to strip the User-Agent HTTP header, on macOS systems this causes the incorrect label to be displayed.

See WordPress/gutenberg#77636
Closes Core-65121

Testing Instructions

  1. Buy a macOS computer/use BrowserStack on macOS.
  2. In your wp-config.php file, add the code $_SERVER['HTTP_USER_AGENT'] = ''; to emulate a site behind a CDN stripping the header
  3. Visit /wp-admin
  4. Ensure the label is correct on this branch.
  5. Ensure the label is incorrect on trunk
  6. Remove the code added to the wp-config file to avoid confusion later.

Trac ticket: https://core.trac.wordpress.org/ticket/65121

Use of AI Tools

N/A

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 24, 2026

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props peterwilsoncc, westonruter, mukesh27.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@peterwilsoncc peterwilsoncc force-pushed the fix/65121-command-pallete-shortcut-js-fallback branch from 9630c20 to 6646b94 Compare April 24, 2026 01:48
Comment thread src/wp-includes/admin-bar.php Outdated
Comment thread src/wp-includes/admin-bar.php Outdated
@mukeshpanchal27
Copy link
Copy Markdown
Member

  1. Buy a macOS computer

😃

Comment thread src/wp-includes/admin-bar.php Outdated
@peterwilsoncc peterwilsoncc force-pushed the fix/65121-command-pallete-shortcut-js-fallback branch from 447a6a6 to beb1871 Compare April 25, 2026 23:18
Comment thread src/wp-includes/admin-bar.php Outdated
@peterwilsoncc
Copy link
Copy Markdown
Contributor Author

@westonruter I've rewritten this based on the discussion on the ticket. I've also switched to navigator.platform for increased reliability over the UA sniffing.

Comment thread src/wp-includes/admin-bar.php Outdated
Comment thread src/wp-includes/admin-bar.php Outdated
@peterwilsoncc peterwilsoncc force-pushed the fix/65121-command-pallete-shortcut-js-fallback branch from 3c8f6d2 to 10492bc Compare April 26, 2026 21:49
Comment thread src/wp-includes/admin-bar.php
Comment thread src/wp-includes/admin-bar.php Outdated
@peterwilsoncc peterwilsoncc force-pushed the fix/65121-command-pallete-shortcut-js-fallback branch from 47fd950 to 46f0221 Compare April 27, 2026 00:28
Comment thread src/wp-includes/admin-bar.php Outdated
Comment thread src/wp-includes/admin-bar.php Outdated
peterwilsoncc and others added 10 commits April 27, 2026 12:20
Co-authored-by: Weston Ruter <westonruter@gmail.com>
Co-authored-by: Weston Ruter <westonruter@gmail.com>
Co-authored-by: Weston Ruter <westonruter@gmail.com>
Co-Authored-By: westonruter <westonruter@git.wordpress.org>
Co-authored-by: Weston Ruter <westonruter@gmail.com>
Co-authored-by: Weston Ruter <westonruter@gmail.com>
Co-Authored-By: westonruter <westonruter@git.wordpress.org>
@peterwilsoncc peterwilsoncc force-pushed the fix/65121-command-pallete-shortcut-js-fallback branch from abcbc2e to 766c557 Compare April 27, 2026 02:20
Copy link
Copy Markdown
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

This is working well and I'm approving.

I did want to point out one thing which may improve maintenance going forward. Namely, a regular expression is being used on the User-Agent in PHP. This is not being used client-side. We could take that pattern from PHP and export it to JS for re-use, which would reduce the maintenance burden:

diff --git a/src/wp-includes/admin-bar.php b/src/wp-includes/admin-bar.php
index 1e96ac794d..c5e449a23c 100644
--- a/src/wp-includes/admin-bar.php
+++ b/src/wp-includes/admin-bar.php
@@ -953,7 +953,8 @@ function wp_admin_bar_command_palette_menu( WP_Admin_Bar $wp_admin_bar ): void {
 		'appleOS' => _x( '⌘K', 'keyboard shortcut to open the command palette' ),
 		'default' => _x( 'Ctrl+K', 'keyboard shortcut to open the command palette' ),
 	);
-	$is_apple_os     = (bool) preg_match( '/Macintosh|Mac OS X|Mac_PowerPC/i', $_SERVER['HTTP_USER_AGENT'] ?? '' );
+	$apple_pattern   = 'Macintosh|Mac OS X|Mac_PowerPC';
+	$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>',
@@ -969,15 +970,16 @@ function wp_admin_bar_command_palette_menu( WP_Admin_Bar $wp_admin_bar ): void {
 	 * wp-i18n to be loaded as a dependency.
 	 */
 	$function = <<<'JS'
-		( appleOSLabel ) => {
-			if ( navigator.platform.startsWith("Mac") || navigator.platform === "iPhone" || navigator.platform === "iPad" ) {
+		( applePattern, appleOSLabel ) => {
+			if ( ( new RegExp( applePattern ) ).test( navigator.userAgent ) ) {
 				document.querySelector( '#wp-admin-bar-command-palette .ab-label kbd' ).textContent = appleOSLabel;
 			}
 		}
 	JS;
 	$script   = sprintf(
-		'( %s )( %s );',
+		'( %s )( %s, %s );',
 		$function,
+		wp_json_encode( $apple_pattern, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),
 		wp_json_encode( $shortcut_labels['appleOS'], JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
 	);
 	$script  .= "\n//# sourceURL=" . rawurlencode( __FUNCTION__ );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants