1<?php
2/**
3 * WP_Classic_To_Block_Menu_Converter class
4 *
5 * @package WordPress
6 * @since 6.3.0
7 */
8
9/**
10 * Converts a Classic Menu to Block Menu blocks.
11 *
12 * @since 6.3.0
13 */
14class WP_Classic_To_Block_Menu_Converter {
15
16 /**
17 * Converts a Classic Menu to blocks.
18 *
19 * @since 6.3.0
20 *
21 * @param WP_Term $menu The Menu term object of the menu to convert.
22 * @return string|WP_Error The serialized and normalized parsed blocks on success,
23 * an empty string when there are no menus to convert,
24 * or WP_Error on invalid menu.
25 */
26 public static function convert( $menu ) {
27
28 if ( ! is_nav_menu( $menu ) ) {
29 return new WP_Error(
30 'invalid_menu',
31 __( 'The menu provided is not a valid menu.' )
32 );
33 }
34
35 $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
36
37 if ( empty( $menu_items ) ) {
38 return '';
39 }
40
41 // Set up the $menu_item variables.
42 // Adds the class property classes for the current context, if applicable.
43 _wp_menu_item_classes_by_context( $menu_items );
44
45 $menu_items_by_parent_id = static::group_by_parent_id( $menu_items );
46
47 $first_menu_item = isset( $menu_items_by_parent_id[0] )
48 ? $menu_items_by_parent_id[0]
49 : array();
50
51 $inner_blocks = static::to_blocks(
52 $first_menu_item,
53 $menu_items_by_parent_id
54 );
55
56 return serialize_blocks( $inner_blocks );
57 }
58
59 /**
60 * Returns an array of menu items grouped by the id of the parent menu item.
61 *
62 * @since 6.3.0
63 *
64 * @param array $menu_items An array of menu items.
65 * @return array
66 */
67 private static function group_by_parent_id( $menu_items ) {
68 $menu_items_by_parent_id = array();
69
70 foreach ( $menu_items as $menu_item ) {
71 $menu_items_by_parent_id[ $menu_item->menu_item_parent ][] = $menu_item;
72 }
73
74 return $menu_items_by_parent_id;
75 }
76
77 /**
78 * Turns menu item data into a nested array of parsed blocks
79 *
80 * @since 6.3.0
81 *
82 * @param array $menu_items An array of menu items that represent
83 * an individual level of a menu.
84 * @param array $menu_items_by_parent_id An array keyed by the id of the
85 * parent menu where each element is an
86 * array of menu items that belong to
87 * that parent.
88 * @return array An array of parsed block data.
89 */
90 private static function to_blocks( $menu_items, $menu_items_by_parent_id ) {
91
92 if ( empty( $menu_items ) ) {
93 return array();
94 }
95
96 $blocks = array();
97
98 foreach ( $menu_items as $menu_item ) {
99 $class_name = ! empty( $menu_item->classes ) ? implode( ' ', (array) $menu_item->classes ) : null;
100 $id = ( null !== $menu_item->object_id && 'custom' !== $menu_item->object ) ? $menu_item->object_id : null;
101 $opens_in_new_tab = null !== $menu_item->target && '_blank' === $menu_item->target;
102 $rel = ( null !== $menu_item->xfn && '' !== $menu_item->xfn ) ? $menu_item->xfn : null;
103 $kind = null !== $menu_item->type ? str_replace( '_', '-', $menu_item->type ) : 'custom';
104
105 $block = array(
106 'blockName' => isset( $menu_items_by_parent_id[ $menu_item->ID ] ) ? 'core/navigation-submenu' : 'core/navigation-link',
107 'attrs' => array(
108 'className' => $class_name,
109 'description' => $menu_item->description,
110 'id' => $id,
111 'kind' => $kind,
112 'label' => $menu_item->title,
113 'opensInNewTab' => $opens_in_new_tab,
114 'rel' => $rel,
115 'title' => $menu_item->attr_title,
116 'type' => $menu_item->object,
117 'url' => $menu_item->url,
118 ),
119 );
120
121 $block['innerBlocks'] = isset( $menu_items_by_parent_id[ $menu_item->ID ] )
122 ? static::to_blocks( $menu_items_by_parent_id[ $menu_item->ID ], $menu_items_by_parent_id )
123 : array();
124 $block['innerContent'] = array_map( 'serialize_block', $block['innerBlocks'] );
125
126 $blocks[] = $block;
127 }
128
129 return $blocks;
130 }
131}
132