View source
<?php
function gutenberg_build_css_colors($attributes) {
$colors = array(
'bg_css_classes' => '',
'bg_inline_styles' => '',
'text_css_classes' => '',
'text_inline_styles' => '',
);
if (array_key_exists('backgroundColor', $attributes)) {
$colors['bg_css_classes'] .= ' has-background-color';
}
if (array_key_exists('backgroundColorCSSClass', $attributes)) {
$colors['bg_css_classes'] .= " {$attributes['backgroundColorCSSClass']}";
}
elseif (array_key_exists('customBackgroundColor', $attributes)) {
$colors['bg_inline_styles'] = ' style="background-color: ' . esc_attr($attributes['customBackgroundColor']) . ';"';
}
if (array_key_exists('textColor', $attributes)) {
$colors['text_css_classes'] .= ' has-text-color';
}
if (array_key_exists('textColorCSSClass', $attributes)) {
$colors['text_css_classes'] .= " {$attributes['textColorCSSClass']}";
}
elseif (array_key_exists('customTextColor', $attributes)) {
$colors['text_inline_styles'] = ' style="color: ' . esc_attr($attributes['customTextColor']) . ';"';
}
$colors['bg_css_classes'] = esc_attr(trim($colors['bg_css_classes']));
$colors['text_css_classes'] = esc_attr(trim($colors['text_css_classes']));
return $colors;
}
function gutenberg_render_block_navigation_menu($attributes, $content, $block) {
$comp_inline_styles = '';
if (array_key_exists('backgroundColorValue', $attributes)) {
$comp_inline_styles .= ' background-color: ' . esc_attr($attributes['backgroundColorValue']) . ';';
}
if (array_key_exists('textColorValue', $attributes)) {
$comp_inline_styles .= ' color: ' . esc_attr($attributes['textColorValue']) . ';';
}
$comp_inline_styles = !empty($comp_inline_styles) ? ' style="' . esc_attr(trim($comp_inline_styles)) . '"' : '';
$colors = gutenberg_build_css_colors($attributes);
return "<nav class='wp-block-navigation-menu' {$comp_inline_styles}>" . gutenberg_build_navigation_menu_html($block, $colors) . '</nav>';
}
function gutenberg_build_navigation_menu_html($block, $colors) {
$html = '';
foreach ((array) $block['innerBlocks'] as $key => $menu_item) {
$html .= '<li class="wp-block-navigation-menu-item ' . $colors['bg_css_classes'] . '"' . $colors['bg_inline_styles'] . '>' . '<a
class="wp-block-navigation-menu-item__link ' . $colors['text_css_classes'] . '"
' . $colors['text_inline_styles'];
if (isset($menu_item['attrs']['url'])) {
$html .= ' href="' . $menu_item['attrs']['url'] . '"';
}
if (isset($menu_item['attrs']['title'])) {
$html .= ' title="' . $menu_item['attrs']['title'] . '"';
}
if (isset($menu_item['attrs']['opensInNewTab']) && true === $menu_item['attrs']['opensInNewTab']) {
$html .= ' target="_blank" ';
}
$html .= '>';
if (isset($menu_item['attrs']['label'])) {
$html .= $menu_item['attrs']['label'];
}
$html .= '</a>';
if (count((array) $menu_item['innerBlocks']) > 0) {
$html .= gutenberg_build_navigation_menu_html($menu_item, $colors);
}
$html .= '</li>';
}
return '<ul>' . $html . '</ul>';
}
function gutenberg_register_block_core_navigation_menu() {
register_block_type('core/navigation-menu', array(
'category' => 'layout',
'attributes' => array(
'className' => array(
'type' => 'string',
),
'automaticallyAdd' => array(
'type' => 'boolean',
'default' => false,
),
'backgroundColor' => array(
'type' => 'string',
),
'textColor' => array(
'type' => 'string',
),
'backgroundColorValue' => array(
'type' => 'string',
),
'textColorValue' => array(
'type' => 'string',
),
'customBackgroundColor' => array(
'type' => 'string',
),
'customTextColor' => array(
'type' => 'string',
),
'backgroundColorCSSClass' => array(
'type' => 'string',
),
'textColorCSSClass' => array(
'type' => 'string',
),
),
'render_callback' => 'gutenberg_render_block_navigation_menu',
));
}
add_action('init', 'gutenberg_register_block_core_navigation_menu', 20);