函数文档

wp_enqueue_command_palette_assets()

💡 云策文档标注

概述

wp_enqueue_command_palette_assets() 函数用于为 WordPress 后台的命令面板(Command Palette)加载必要的 JavaScript 和 CSS 资源。该函数会处理菜单和子菜单项,生成命令设置数据,并通过 wp_add_inline_script() 注入到脚本中。

关键要点

  • 函数主要功能是加载命令面板所需的脚本(wp-commands、wp-core-commands)和样式(wp-commands)。
  • 通过全局变量 $menu 和 $submenu 遍历菜单项,过滤用户权限并清理标签,生成命令列表。
  • 使用 wp_add_inline_script() 将命令设置(包括菜单命令和网络管理员状态)以 JSON 格式注入到 wp-core-commands 脚本中。
  • 函数依赖于多个 WordPress 核心函数,如 current_user_can()、menu_page_url()、wp_json_encode() 等。
  • 自 WordPress 6.9.0 版本引入,是命令面板功能的一部分。

代码示例

wp_enqueue_command_palette_assets();

注意事项

  • 函数仅在后台环境中调用,确保 $menu 和 $submenu 全局变量已定义。
  • 命令面板资源加载依赖于用户权限检查,避免向无权限用户暴露菜单项。
  • JSON 编码时使用 JSON_HEX_TAG | JSON_UNESCAPED_SLASHES 选项,确保安全性和正确性。

📄 原文内容

Enqueues the assets required for the Command Palette.

Source

function wp_enqueue_command_palette_assets() {
	global $menu, $submenu;

	$command_palette_settings = array(
		'is_network_admin' => is_network_admin(),
	);

	if ( $menu ) {
		$menu_commands = array();
		foreach ( $menu as $menu_item ) {
			if ( empty( $menu_item[0] ) || ! empty( $menu_item[1] ) && ! current_user_can( $menu_item[1] ) ) {
				continue;
			}

			// Remove all HTML tags and their contents.
			$menu_label = $menu_item[0];
			while ( preg_match( '/<[^>]*>/', $menu_label ) ) {
				$menu_label = preg_replace( '/<[^>]*>.*?</[^>]*>|<[^>]*/>|<[^>]*>/s', '', $menu_label );
			}
			$menu_label = trim( $menu_label );
			$menu_url   = '';
			$menu_slug  = $menu_item[2];

			if ( preg_match( '/.php($|?)/', $menu_slug ) || wp_http_validate_url( $menu_slug ) ) {
				$menu_url = $menu_slug;
			} elseif ( ! empty( menu_page_url( $menu_slug, false ) ) ) {
				$menu_url = html_entity_decode( menu_page_url( $menu_slug, false ), ENT_QUOTES, get_bloginfo( 'charset' ) );
			}

			if ( $menu_url ) {
				$menu_commands[] = array(
					'label' => $menu_label,
					'url'   => $menu_url,
					'name'  => $menu_slug,
				);
			}

			if ( array_key_exists( $menu_slug, $submenu ) ) {
				foreach ( $submenu[ $menu_slug ] as $submenu_item ) {
					if ( empty( $submenu_item[0] ) || ! empty( $submenu_item[1] ) && ! current_user_can( $submenu_item[1] ) ) {
						continue;
					}

					// Remove all HTML tags and their contents.
					$submenu_label = $submenu_item[0];
					while ( preg_match( '/<[^>]*>/', $submenu_label ) ) {
						$submenu_label = preg_replace( '/<[^>]*>.*?</[^>]*>|<[^>]*/>|<[^>]*>/s', '', $submenu_label );
					}
					$submenu_label = trim( $submenu_label );
					$submenu_url   = '';
					$submenu_slug  = $submenu_item[2];

					if ( preg_match( '/.php($|?)/', $submenu_slug ) || wp_http_validate_url( $submenu_slug ) ) {
						$submenu_url = $submenu_slug;
					} elseif ( ! empty( menu_page_url( $submenu_slug, false ) ) ) {
						$submenu_url = html_entity_decode( menu_page_url( $submenu_slug, false ), ENT_QUOTES, get_bloginfo( 'charset' ) );
					}

					if ( $submenu_url ) {
						$menu_commands[] = array(
							'label' => sprintf(
								/* translators: 1: Menu label, 2: Submenu label. */
								__( '%1$s > %2$s' ),
								$menu_label,
								$submenu_label
							),
							'url'   => $submenu_url,
							'name'  => $menu_slug . '-' . $submenu_item[2],
						);
					}
				}
			}
		}
		$command_palette_settings['menu_commands'] = $menu_commands;
	}

	wp_enqueue_script( 'wp-commands' );
	wp_enqueue_style( 'wp-commands' );
	wp_enqueue_script( 'wp-core-commands' );

	wp_add_inline_script(
		'wp-core-commands',
		sprintf(
			'wp.coreCommands.initializeCommandPalette( %s );',
			wp_json_encode( $command_palette_settings, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
		)
	);
}

Changelog

Version Description
6.9.0 Introduced.