@@ -9,16 +9,41 @@ function gutenberg_admin_bar_command_palette_menu( WP_Admin_Bar $wp_admin_bar ):
99 return ;
1010 }
1111
12- $ is_apple_os = (bool ) preg_match ( '/Macintosh|Mac OS X|Mac_PowerPC/i ' , $ _SERVER ['HTTP_USER_AGENT ' ] ?? '' );
13- $ shortcut_label = $ is_apple_os
14- ? _x ( '⌘K ' , 'keyboard shortcut to open the command palette ' )
15- : _x ( 'Ctrl+K ' , 'keyboard shortcut to open the command palette ' );
16- $ title = sprintf (
12+ $ shortcut_labels = array (
13+ 'appleOS ' => _x ( '⌘K ' , 'keyboard shortcut to open the command palette ' ),
14+ 'default ' => _x ( 'Ctrl+K ' , 'keyboard shortcut to open the command palette ' ),
15+ );
16+ $ apple_pattern = 'Macintosh|Mac OS X|Mac_PowerPC ' ;
17+ $ is_apple_os = (bool ) preg_match ( "/ {$ apple_pattern }/i " , $ _SERVER ['HTTP_USER_AGENT ' ] ?? '' );
18+ $ shortcut_label = $ is_apple_os ? $ shortcut_labels ['appleOS ' ] : $ shortcut_labels ['default ' ];
19+ $ title = sprintf (
1720 '<span class="ab-icon" aria-hidden="true"></span><span class="ab-label"><kbd>%s</kbd><span class="screen-reader-text"> %s</span></span> ' ,
1821 $ shortcut_label ,
1922 /* translators: Hidden accessibility text. */
2023 __ ( 'Open command palette ' ),
2124 );
25+
26+ /*
27+ * Detect Apple OS via JavaScript for sites behind a CDN blocking the UA header.
28+ *
29+ * Running the script as the admin bar is rendered avoids a flash of incorrect content
30+ * for users with Apple OS when the UA header is blocked. It also prevents the need for
31+ * wp-i18n to be loaded as a dependency.
32+ */
33+ $ function = <<<'JS'
34+ ( applePattern, appleOSLabel ) => {
35+ if ( ( new RegExp( applePattern ) ).test( navigator.userAgent ) ) {
36+ document.querySelector( '#wp-admin-bar-command-palette .ab-label kbd' ).textContent = appleOSLabel;
37+ }
38+ }
39+ JS;
40+ $ script = sprintf (
41+ '( %s )( %s, %s ); ' ,
42+ $ function ,
43+ wp_json_encode ( $ apple_pattern , JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),
44+ wp_json_encode ( $ shortcut_labels ['appleOS ' ], JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
45+ );
46+ $ script .= "\n//# sourceURL= " . rawurlencode ( __FUNCTION__ );
2247 $ wp_admin_bar ->add_node (
2348 array (
2449 'id ' => 'command-palette ' ,
@@ -27,6 +52,7 @@ function gutenberg_admin_bar_command_palette_menu( WP_Admin_Bar $wp_admin_bar ):
2752 'meta ' => array (
2853 'class ' => 'hide-if-no-js ' ,
2954 'onclick ' => 'wp.data.dispatch( "core/commands" ).open(); return false; ' ,
55+ 'html ' => wp_get_inline_script_tag ( $ script ),
3056 ),
3157 )
3258 );
0 commit comments