wppaste
WordPress

WP_Classic_To_Block_Menu_Converter

Since
6.3.0
Source
wp-includes/class-wp-classic-to-block-menu-converter.php:14
Converts a Classic Menu to Block Menu blocks.

Compatibility

WordPress
since 6.3.0
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0).

Methods · 3

  • convert()Converts a Classic Menu to blocks.
  • group_by_parent_id()Returns an array of menu items grouped by the id of the parent menu item.
  • to_blocks()Turns menu item data into a nested array of parsed blocks

Source code

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.' )			);		} 		$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 = $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 );	} 	/**	 * 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(); 		foreach ( $menu_items as $menu_item ) {			$menu_items_by_parent_id[ $menu_item->menu_item_parent ][] = $menu_item;		} 		return $menu_items_by_parent_id;	} 	/**	 * 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 ) { 		if ( empty( $menu_items ) ) {			return array();		}

Changelog

Introduced in 6.3.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

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

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-wp-classic-to-block-menu-converter.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.