WP_Classic_To_Block_Menu_Converter::convert()public staticWP 6.3.0

Converts a Classic Menu to blocks.

Метод класса: WP_Classic_To_Block_Menu_Converter{}

Хуков нет.

Возвращает

Строку|WP_Error. The serialized and normalized parsed blocks on success, an empty string when there are no menus to convert, or WP_Error on invalid menu.

Использование

$result = WP_Classic_To_Block_Menu_Converter::convert( $menu );
$menu(WP_Term) (обязательный)
The Menu term object of the menu to convert.

Список изменений

С версии 6.3.0 Введена.

Код WP_Classic_To_Block_Menu_Converter::convert() WP 6.6.2

public static function convert( $menu ) {

	if ( ! is_nav_menu( $menu ) ) {
		return new WP_Error(
			'invalid_menu',
			__( 'The menu provided is not a valid menu.' )
		);
	}

	$menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );

	if ( empty( $menu_items ) ) {
		return '';
	}

	// Set up the $menu_item variables.
	// Adds the class property classes for the current context, if applicable.
	_wp_menu_item_classes_by_context( $menu_items );

	$menu_items_by_parent_id = static::group_by_parent_id( $menu_items );

	$first_menu_item = isset( $menu_items_by_parent_id[0] )
		? $menu_items_by_parent_id[0]
		: array();

	$inner_blocks = static::to_blocks(
		$first_menu_item,
		$menu_items_by_parent_id
	);

	return serialize_blocks( $inner_blocks );
}