Command Palette: Fix macOs label for sites unable to determine UA via PHP#11636
Command Palette: Fix macOs label for sites unable to determine UA via PHP#11636peterwilsoncc wants to merge 19 commits intoWordPress:trunkfrom
Conversation
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
9630c20 to
6646b94
Compare
😃 |
447a6a6 to
beb1871
Compare
|
@westonruter I've rewritten this based on the discussion on the ticket. I've also switched to |
3c8f6d2 to
10492bc
Compare
47fd950 to
46f0221
Compare
This reverts commit 25015a6.
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: 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>
abcbc2e to
766c557
Compare
westonruter
left a comment
There was a problem hiding this comment.
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__ );
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-AgentHTTP header, on macOS systems this causes the incorrect label to be displayed.See WordPress/gutenberg#77636
Closes Core-65121
Testing Instructions
wp-config.phpfile, add the code$_SERVER['HTTP_USER_AGENT'] = '';to emulate a site behind a CDN stripping the header/wp-adminTrac ticket: https://core.trac.wordpress.org/ticket/65121
Use of AI Tools
N/A