-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.php
74 lines (68 loc) · 1.94 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/**
* Server-side rendering of the `core/navigation-menu` block.
*
* @package gutenberg
*/
/**
* Renders the `core/navigation-menu` block on server.
*
* @param array $attributes The block attributes.
* @param array $content The saved content.
* @param array $block The parsed block.
*
* @return string Returns the post content with the legacy widget added.
*/
function render_block_navigation_menu( $attributes, $content, $block ) {
return '<nav class="wp-block-navigation-menu">' . build_navigation_menu_html( $block ) . '</nav>';
}
/**
* Walks the inner block structure and returns an HTML list for it.
*
* @param array $block The block.
*
* @return string Returns an HTML list from innerBlocks.
*/
function build_navigation_menu_html( $block ) {
$html = '';
foreach ( (array) $block['innerBlocks'] as $key => $menu_item ) {
$html .= '<li class="wp-block-navigation-menu-item"><a class="wp-block-navigation-menu-item"';
if ( isset( $menu_item['attrs']['destination'] ) ) {
$html .= ' href="' . $menu_item['attrs']['destination'] . '"';
}
if ( isset( $menu_item['attrs']['title'] ) ) {
$html .= ' title="' . $menu_item['attrs']['title'] . '"';
}
$html .= '>';
if ( isset( $menu_item['attrs']['label'] ) ) {
$html .= $menu_item['attrs']['label'];
}
$html .= '</a>';
if ( count( (array) $menu_item['innerBlocks'] ) > 0 ) {
$html .= build_navigation_menu_html( $menu_item );
}
$html .= '</li>';
}
return '<ul>' . $html . '</ul>';
}
/**
* Register the navigation menu block.
*
* @uses render_block_navigation_menu()
*/
function register_block_core_navigation_menu() {
register_block_type(
'core/navigation-menu',
array(
'category' => 'layout',
'attributes' => array(
'automaticallyAdd' => array(
'type' => 'boolean',
'default' => false,
),
),
'render_callback' => 'render_block_navigation_menu',
)
);
}
add_action( 'init', 'register_block_core_navigation_menu' );