wppaste
WordPress

wp_admin_bar_command_palette_menu( WP_Admin_Bar $wp_admin_bar )

Since
7.0.0
Source
wp-includes/admin-bar.php:967
Adds the command palette trigger button.

Description

Displays a button in the admin bar that shows the keyboard shortcut for opening the command palette.

Compatibility

WordPress
since 7.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 2 of the 5 tracked releases, added in 7.0.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$wp_admin_barWP_Admin_Bar
The WP_Admin_Bar instance.

Performance profile

How much work a call to wp_admin_bar_command_palette_menu() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
5–71

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 73.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()one call below wp_admin_bar_command_palette_menu()

Further down the call graph this can also reach option, query, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 3 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_admin_bar_command_palette_menu() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!is_admin()5is_admin()
is_admin() && !wp_script_is()10is_admin(), wp_script_is()
is_admin() && wp_script_is()69–71is_admin(), wp_script_is(), _x(), _x(), __(), wp_json_encode(), wp_json_encode(), wp_get_inline_script_tag(), hide-if-no-js()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev735–714
8.5735–714
8.4735–7144 fewer instructions than PHP 8.3
8.3775–754
8.2775–754
8.1775–7542 fewer instructions than PHP 7.4
7.4795–764

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 6

  • is_admin()Determines whether the current request is for an administrative interface page.
  • wp_script_is()Determines whether a script has been added to the queue.
  • _x()Retrieves translated string with gettext context.
  • __()Retrieves the translation of $text.
  • wp_json_encode()Encodes a variable into JSON, with some confidence checks.
  • wp_get_inline_script_tag()Constructs an inline script tag.

Source code

function wp_admin_bar_command_palette_menu( WP_Admin_Bar $wp_admin_bar ): void {	if ( ! is_admin() || ! wp_script_is( 'wp-core-commands', 'enqueued' ) ) {		return;	} 	$shortcut_labels = array(		'appleOS' => _x( '⌘K', 'keyboard shortcut to open the command palette' ),		'default' => _x( 'Ctrl+K', 'keyboard shortcut to open the command palette' ),	);	$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>',		$shortcut_label,		/* translators: Hidden accessibility text. */		__( 'Open command palette' ),	);	/*	 * Detect Apple OS via JavaScript for sites behind a CDN blocking the UA header.	 *	 * Running the script as the admin bar is rendered avoids a flash of incorrect content	 * for users with Apple OS when the UA header is blocked. It also prevents the need for	 * wp-i18n to be loaded as a dependency.	 */	$function = <<<'JS'		( applePattern, appleOSLabel ) => {			if ( ! ( new RegExp( applePattern, 'i' ) ).test( navigator.userAgent ) ) {				return;			}			const kbd = document.querySelector( '#wp-admin-bar-command-palette .ab-label kbd' );			if ( kbd ) {				kbd.textContent = appleOSLabel;			}		}	JS;	$script   = sprintf(		'( %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__ );	$wp_admin_bar->add_node(		array(			'id'    => 'command-palette',			'title' => $title,			'href'  => '#',			'meta'  => array(				'class'   => 'hide-if-no-js',				'onclick' => 'wp.data.dispatch( "core/commands" ).open(); return false;',				'html'    => wp_get_inline_script_tag( $script ),			),		)	);}

Changelog

Introduced in 7.0.0. Unchanged from 7.0.4 through 7.1.0.

  1. 7.0.4
  2. 7.1.0

Signature, return type and hooks compared across 2 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/admin-bar.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.