WP_Customize_Nav_Menus::enqueue_scripts()publicWP 4.3.0

Enqueues scripts and styles for Customizer pane.

Method of the class: WP_Customize_Nav_Menus{}

No Hooks.

Return

null. Nothing (null).

Usage

$WP_Customize_Nav_Menus = new WP_Customize_Nav_Menus();
$WP_Customize_Nav_Menus->enqueue_scripts();

Changelog

Since 4.3.0 Introduced.

WP_Customize_Nav_Menus::enqueue_scripts() code WP 6.6.2

public function enqueue_scripts() {
	wp_enqueue_style( 'customize-nav-menus' );
	wp_enqueue_script( 'customize-nav-menus' );

	$temp_nav_menu_setting      = new WP_Customize_Nav_Menu_Setting( $this->manager, 'nav_menu[-1]' );
	$temp_nav_menu_item_setting = new WP_Customize_Nav_Menu_Item_Setting( $this->manager, 'nav_menu_item[-1]' );

	$num_locations = count( get_registered_nav_menus() );

	if ( 1 === $num_locations ) {
		$locations_description = __( 'Your theme can display menus in one location.' );
	} else {
		/* translators: %s: Number of menu locations. */
		$locations_description = sprintf( _n( 'Your theme can display menus in %s location.', 'Your theme can display menus in %s locations.', $num_locations ), number_format_i18n( $num_locations ) );
	}

	// Pass data to JS.
	$settings = array(
		'allMenus'                 => wp_get_nav_menus(),
		'itemTypes'                => $this->available_item_types(),
		'l10n'                     => array(
			'untitled'               => _x( '(no label)', 'missing menu item navigation label' ),
			'unnamed'                => _x( '(unnamed)', 'Missing menu name.' ),
			'custom_label'           => __( 'Custom Link' ),
			'page_label'             => get_post_type_object( 'page' )->labels->singular_name,
			/* translators: %s: Menu location. */
			'menuLocation'           => _x( '(Currently set to: %s)', 'menu' ),
			'locationsTitle'         => 1 === $num_locations ? __( 'Menu Location' ) : __( 'Menu Locations' ),
			'locationsDescription'   => $locations_description,
			'menuNameLabel'          => __( 'Menu Name' ),
			'newMenuNameDescription' => __( 'If your theme has multiple menus, giving them clear names will help you manage them.' ),
			'itemAdded'              => __( 'Menu item added' ),
			'itemDeleted'            => __( 'Menu item deleted' ),
			'menuAdded'              => __( 'Menu created' ),
			'menuDeleted'            => __( 'Menu deleted' ),
			'movedUp'                => __( 'Menu item moved up' ),
			'movedDown'              => __( 'Menu item moved down' ),
			'movedLeft'              => __( 'Menu item moved out of submenu' ),
			'movedRight'             => __( 'Menu item is now a sub-item' ),
			/* translators: ▸ is the unicode right-pointing triangle. %s: Section title in the Customizer. */
			'customizingMenus'       => sprintf( __( 'Customizing ▸ %s' ), esc_html( $this->manager->get_panel( 'nav_menus' )->title ) ),
			/* translators: %s: Title of an invalid menu item. */
			'invalidTitleTpl'        => __( '%s (Invalid)' ),
			/* translators: %s: Title of a menu item in draft status. */
			'pendingTitleTpl'        => __( '%s (Pending)' ),
			/* translators: %d: Number of menu items found. */
			'itemsFound'             => __( 'Number of items found: %d' ),
			/* translators: %d: Number of additional menu items found. */
			'itemsFoundMore'         => __( 'Additional items found: %d' ),
			'itemsLoadingMore'       => __( 'Loading more results... please wait.' ),
			'reorderModeOn'          => __( 'Reorder mode enabled' ),
			'reorderModeOff'         => __( 'Reorder mode closed' ),
			'reorderLabelOn'         => esc_attr__( 'Reorder menu items' ),
			'reorderLabelOff'        => esc_attr__( 'Close reorder mode' ),
		),
		'settingTransport'         => 'postMessage',
		'phpIntMax'                => PHP_INT_MAX,
		'defaultSettingValues'     => array(
			'nav_menu'      => $temp_nav_menu_setting->default,
			'nav_menu_item' => $temp_nav_menu_item_setting->default,
		),
		'locationSlugMappedToName' => get_registered_nav_menus(),
	);

	$data = sprintf( 'var _wpCustomizeNavMenusSettings = %s;', wp_json_encode( $settings ) );
	wp_scripts()->add_data( 'customize-nav-menus', 'data', $data );

	// This is copied from nav-menus.php, and it has an unfortunate object name of `menus`.
	$nav_menus_l10n = array(
		'oneThemeLocationNoMenus' => null,
		'moveUp'                  => __( 'Move up one' ),
		'moveDown'                => __( 'Move down one' ),
		'moveToTop'               => __( 'Move to the top' ),
		/* translators: %s: Previous item name. */
		'moveUnder'               => __( 'Move under %s' ),
		/* translators: %s: Previous item name. */
		'moveOutFrom'             => __( 'Move out from under %s' ),
		/* translators: %s: Previous item name. */
		'under'                   => __( 'Under %s' ),
		/* translators: %s: Previous item name. */
		'outFrom'                 => __( 'Out from under %s' ),
		/* translators: 1: Item name, 2: Item type, 3: Item index, 4: Total items. */
		'menuFocus'               => __( 'Edit %1$s (%2$s, %3$d of %4$d)' ),
		/* translators: 1: Item name, 2: Item type, 3: Item index, 4: Total items, 5: Item parent. */
		'subMenuFocus'            => __( 'Edit %1$s (%2$s, sub-item %3$d of %4$d under %5$s)' ),
		/* translators: 1: Item name, 2: Item type, 3: Item index, 4: Total items, 5: Item parent, 6: Item depth. */
		'subMenuMoreDepthFocus'   => __( 'Edit %1$s (%2$s, sub-item %3$d of %4$d under %5$s, level %6$d)' ),
	);
	wp_localize_script( 'nav-menu', 'menus', $nav_menus_l10n );
}