Skip to content

Commit beb1871

Browse files
committed
MIsc changes.
1 parent df07b73 commit beb1871

1 file changed

Lines changed: 11 additions & 19 deletions

File tree

src/wp-includes/admin-bar.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -962,33 +962,24 @@ function wp_admin_bar_command_palette_menu( WP_Admin_Bar $wp_admin_bar ): void {
962962
__( 'Open command palette' ),
963963
);
964964
/*
965-
* The script is added as an inline script to avoid an additional dependency.
965+
* Detect Apple OS via JavaScript for users behind a CDN blocking the UA header.
966966
*
967-
* Adding the code within the admin-bar script would require the wp-i18n script to be loaded
968-
* as a dependency. While this is widely available in the admin, the wp-i18n script is not
969-
* commonly required on the front end of the site. As the command palette is not available
970-
* on the front-end, the script would be loaded but remain unused on the front-end of
971-
* a website.
967+
* Running the script as the admin bar is rendered avoids a flash of incorrect content
968+
* for users with Apple OS when the UA header is blocked. It also prevents the need for
969+
* wp-i18n to be loaded as a dependency as it is most likely not included on the front
970+
* end of a site.
972971
*/
973972
$script = <<<'JS'
974973
(( shortcutLabels ) => {
975-
let userAgent = '';
976-
// Assigning agent may error if the HTTP header is blocked at the browser level.
977-
try {
978-
userAgent = navigator.userAgent;
979-
} catch (error) {
980-
// Make no change to the default shortcut label.
974+
const isAppleOS = navigator.platform.startsWith("Mac") || navigator.platform === "iPhone" || navigator.platform === "iPad";
975+
if ( ! isAppleOS ) {
981976
return;
982977
}
983-
const isAppleOS = /Macintosh|Mac OS X|Mac_PowerPC/i.test( userAgent );
984-
const shortcutLabel = isAppleOS ? shortcutLabels.appleOS : shortcutLabels.default;
985-
const commandPaletteNode = document.querySelector( '#wp-admin-bar-command-palette .ab-label kbd' );
986-
if ( commandPaletteNode ) {
987-
commandPaletteNode.textContent = shortcutLabel;
988-
}
978+
document.querySelector( '#wp-admin-bar-command-palette .ab-label kbd' ).textContent = shortcutLabels.appleOS;
989979
})
990980
JS;
991-
$script .= '(' . wp_json_encode( $shortcut_labels ) . ');';
981+
$script .= '(' . wp_json_encode( $shortcut_labels, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) . ');';
982+
$script .= "\n//# sourceURL=wp_admin_bar_command_palette_menu";
992983
$wp_admin_bar->add_node(
993984
array(
994985
'id' => 'command-palette',
@@ -997,6 +988,7 @@ function wp_admin_bar_command_palette_menu( WP_Admin_Bar $wp_admin_bar ): void {
997988
'meta' => array(
998989
'class' => 'hide-if-no-js',
999990
'onclick' => 'wp.data.dispatch( "core/commands" ).open(); return false;',
991+
'html' => '<script>' . $script . '</script>',
1000992
),
1001993
)
1002994
);

0 commit comments

Comments
 (0)