View source
<?php
function gutenberg_block_core_navigation_build_css_colors($attributes) {
$colors = array(
'css_classes' => array(),
'inline_styles' => '',
);
$has_named_text_color = array_key_exists('textColor', $attributes);
$has_custom_text_color = array_key_exists('customTextColor', $attributes);
if ($has_custom_text_color || $has_named_text_color) {
$colors['css_classes'][] = 'has-text-color';
}
if ($has_named_text_color) {
$colors['css_classes'][] = sprintf('has-%s-color', $attributes['textColor']);
}
elseif ($has_custom_text_color) {
$colors['inline_styles'] .= sprintf('color: %s;', $attributes['customTextColor']);
}
$has_named_background_color = array_key_exists('backgroundColor', $attributes);
$has_custom_background_color = array_key_exists('customBackgroundColor', $attributes);
if ($has_custom_background_color || $has_named_background_color) {
$colors['css_classes'][] = 'has-background';
}
if ($has_named_background_color) {
$colors['css_classes'][] = sprintf('has-%s-background-color', $attributes['backgroundColor']);
}
elseif ($has_custom_background_color) {
$colors['inline_styles'] .= sprintf('background-color: %s;', $attributes['customBackgroundColor']);
}
return $colors;
}
function gutenberg_block_core_navigation_build_css_font_sizes($attributes) {
$font_sizes = array(
'css_classes' => array(),
'inline_styles' => '',
);
$has_named_font_size = array_key_exists('fontSize', $attributes);
$has_custom_font_size = array_key_exists('customFontSize', $attributes);
if ($has_named_font_size) {
$font_sizes['css_classes'][] = sprintf('has-%s-font-size', $attributes['fontSize']);
}
elseif ($has_custom_font_size) {
$font_sizes['inline_styles'] = sprintf('font-size: %spx;', $attributes['customFontSize']);
}
return $font_sizes;
}
function gutenberg_block_core_navigation_empty_navigation_links_recursive($blocks) {
$blocks = array_filter($blocks, function ($block) {
return !empty($block['attrs']['label']);
});
if (!empty($blocks)) {
foreach ($blocks as $key => $block) {
if (!empty($block['innerBlocks'])) {
$blocks[$key]['innerBlocks'] = gutenberg_block_core_navigation_empty_navigation_links_recursive($block['innerBlocks']);
}
}
}
return $blocks;
}
function gutenberg_block_core_navigation_render_submenu_icon() {
return '<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 24 24" transform="rotate(90)"><path d="M8 5v14l11-7z"/><path d="M0 0h24v24H0z" fill="none"/></svg>';
}
function gutenberg_render_block_core_navigation($content, $block) {
if ('core/navigation' !== $block['blockName']) {
return $content;
}
$block['innerBlocks'] = gutenberg_block_core_navigation_empty_navigation_links_recursive($block['innerBlocks']);
$attributes = $block['attrs'];
if (isset($attributes['rgbTextColor']) && empty($attributes['textColor'])) {
$attributes['customTextColor'] = $attributes['rgbTextColor'];
}
if (isset($attributes['rgbBackgroundColor']) && empty($attributes['backgroundColor'])) {
$attributes['customBackgroundColor'] = $attributes['rgbBackgroundColor'];
}
unset($attributes['rgbTextColor'], $attributes['rgbBackgroundColor']);
if (empty($block['innerBlocks'])) {
return '';
}
$colors = gutenberg_block_core_navigation_build_css_colors($attributes);
$font_sizes = gutenberg_block_core_navigation_build_css_font_sizes($attributes);
$classes = array_merge($colors['css_classes'], $font_sizes['css_classes'], array(
'wp-block-navigation',
), isset($attributes['className']) ? array(
$attributes['className'],
) : array(), isset($attributes['orientation']) && 'vertical' === $attributes['orientation'] ? array(
'is-vertical',
) : array(), isset($attributes['itemsJustification']) ? array(
'items-justified-' . $attributes['itemsJustification'],
) : array(), isset($attributes['align']) ? array(
'align' . $attributes['align'],
) : array());
$class_attribute = sprintf(' class="%s"', esc_attr(implode(' ', $classes)));
$style_attribute = $colors['inline_styles'] || $font_sizes['inline_styles'] ? sprintf(' style="%s"', esc_attr($colors['inline_styles']) . esc_attr($font_sizes['inline_styles'])) : '';
return sprintf('<nav %1$s %2$s>%3$s</nav>', $class_attribute, $style_attribute, gutenberg_block_core_navigation_build_html($attributes, $block, $colors, $font_sizes, true));
}
function gutenberg_block_core_navigation_build_html($attributes, $block, $colors, $font_sizes) {
$html = '';
$classes = array_merge($colors['css_classes'], $font_sizes['css_classes']);
$classes[] = 'wp-block-navigation-link';
$style_attribute = $colors['inline_styles'] || $font_sizes['inline_styles'] ? sprintf(' style="%s"', esc_attr($colors['inline_styles']) . esc_attr($font_sizes['inline_styles'])) : '';
foreach ((array) $block['innerBlocks'] as $key => $block) {
$css_classes = trim(implode(' ', $classes));
$has_submenu = count((array) $block['innerBlocks']) > 0;
$is_active = !empty($block['attrs']['id']) && get_the_ID() === $block['attrs']['id'];
$class_name = !empty($block['attrs']['className']) ? implode(' ', (array) $block['attrs']['className']) : false;
if (false !== $class_name) {
$css_classes .= ' ' . $class_name;
}
$html .= '<li class="' . esc_attr($css_classes . ($has_submenu ? ' has-child' : '')) . ($is_active ? ' current-menu-item' : '') . '"' . $style_attribute . '>' . '<a class="wp-block-navigation-link__content"';
if (isset($block['attrs']['url'])) {
$html .= ' href="' . esc_url($block['attrs']['url']) . '"';
}
if (isset($block['attrs']['opensInNewTab']) && true === $block['attrs']['opensInNewTab']) {
$html .= ' target="_blank" ';
}
$html .= '>' . '<span class="wp-block-navigation-link__label">';
if (isset($block['attrs']['label'])) {
$html .= wp_kses($block['attrs']['label'], array(
'code' => array(),
'em' => array(),
'img' => array(
'scale' => array(),
'class' => array(),
'style' => array(),
'src' => array(),
'alt' => array(),
),
's' => array(),
'span' => array(
'style' => array(),
),
'strong' => array(),
));
}
$html .= '</span>';
$html .= '</a>';
if ((isset($attributes['showSubmenuIcon']) && false !== $attributes['showSubmenuIcon'] || !isset($attributes['showSubmenuIcon'])) && $has_submenu) {
$html .= '<span class="wp-block-navigation-link__submenu-icon">' . gutenberg_block_core_navigation_render_submenu_icon() . '</span>';
}
if ($has_submenu) {
$html .= gutenberg_block_core_navigation_build_html($attributes, $block, $colors, $font_sizes);
}
$html .= '</li>';
}
return '<ul class="wp-block-navigation__container">' . $html . '</ul>';
}
function gutenberg_register_block_core_navigation() {
register_block_type_from_metadata(__DIR__ . '/navigation');
}
add_action('init', 'gutenberg_register_block_core_navigation', 20);
add_filter('render_block', 'gutenberg_render_block_core_navigation', 10, 2);