/** * Converts a Classic Menu to Block Menu blocks. * * @since 6.3.0 * @access public */ class WP_Classic_To_Block_Menu_Converter {
/** * Converts a Classic Menu to blocks. * * @since 6.3.0 * * @param WP_Term $menu The Menu term object of the menu to convert. * @return string|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. */ public static function convert( $menu ) {
if ( ! is_nav_menu( $menu ) ) { return new WP_Error( 'invalid_menu', __( 'The menu provided is not a valid menu.' ) ); }
// 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 );
/** * Returns an array of menu items grouped by the id of the parent menu item. * * @since 6.3.0 * * @param array $menu_items An array of menu items. * @return array */ private static function group_by_parent_id( $menu_items ) { $menu_items_by_parent_id = array();
/** * Turns menu item data into a nested array of parsed blocks * * @since 6.3.0 * * @param array $menu_items An array of menu items that represent * an individual level of a menu. * @param array $menu_items_by_parent_id An array keyed by the id of the * parent menu where each element is an * array of menu items that belong to * that parent. * @return array An array of parsed block data. */ private static function to_blocks( $menu_items, $menu_items_by_parent_id ) {