| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `core/navigation-link` block. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Build an array with CSS classes and inline styles defining the colors |
| 10 |
* which will be applied to the navigation markup in the front-end. |
| 11 |
* |
| 12 |
* @param array $context Navigation block context. |
| 13 |
* @return array Colors CSS classes and inline styles. |
| 14 |
*/ |
| 15 |
function gutenberg_block_core_navigation_link_build_css_colors( $context ) { |
| 16 |
$colors = array( |
| 17 |
'css_classes' => array(), |
| 18 |
'inline_styles' => '', |
| 19 |
); |
| 20 |
|
| 21 |
// Text color. |
| 22 |
$has_named_text_color = array_key_exists( 'textColor', $context ); |
| 23 |
$has_custom_text_color = array_key_exists( 'customTextColor', $context ); |
| 24 |
|
| 25 |
// If has text color. |
| 26 |
if ( $has_custom_text_color || $has_named_text_color ) { |
| 27 |
// Add has-text-color class. |
| 28 |
$colors['css_classes'][] = 'has-text-color'; |
| 29 |
} |
| 30 |
|
| 31 |
if ( $has_named_text_color ) { |
| 32 |
// Add the color class. |
| 33 |
$colors['css_classes'][] = sprintf( 'has-%s-color', $context['textColor'] ); |
| 34 |
} elseif ( $has_custom_text_color ) { |
| 35 |
// Add the custom color inline style. |
| 36 |
$colors['inline_styles'] .= sprintf( 'color: %s;', $context['customTextColor'] ); |
| 37 |
} |
| 38 |
|
| 39 |
// Background color. |
| 40 |
$has_named_background_color = array_key_exists( 'backgroundColor', $context ); |
| 41 |
$has_custom_background_color = array_key_exists( 'customBackgroundColor', $context ); |
| 42 |
|
| 43 |
// If has background color. |
| 44 |
if ( $has_custom_background_color || $has_named_background_color ) { |
| 45 |
// Add has-background class. |
| 46 |
$colors['css_classes'][] = 'has-background'; |
| 47 |
} |
| 48 |
|
| 49 |
if ( $has_named_background_color ) { |
| 50 |
// Add the background-color class. |
| 51 |
$colors['css_classes'][] = sprintf( 'has-%s-background-color', $context['backgroundColor'] ); |
| 52 |
} elseif ( $has_custom_background_color ) { |
| 53 |
// Add the custom background-color inline style. |
| 54 |
$colors['inline_styles'] .= sprintf( 'background-color: %s;', $context['customBackgroundColor'] ); |
| 55 |
} |
| 56 |
|
| 57 |
return $colors; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Build an array with CSS classes and inline styles defining the font sizes |
| 62 |
* which will be applied to the navigation markup in the front-end. |
| 63 |
* |
| 64 |
* @param array $context Navigation block context. |
| 65 |
* @return array Font size CSS classes and inline styles. |
| 66 |
*/ |
| 67 |
function gutenberg_block_core_navigation_link_build_css_font_sizes( $context ) { |
| 68 |
// CSS classes. |
| 69 |
$font_sizes = array( |
| 70 |
'css_classes' => array(), |
| 71 |
'inline_styles' => '', |
| 72 |
); |
| 73 |
|
| 74 |
$has_named_font_size = array_key_exists( 'fontSize', $context ); |
| 75 |
$has_custom_font_size = array_key_exists( 'customFontSize', $context ); |
| 76 |
|
| 77 |
if ( $has_named_font_size ) { |
| 78 |
// Add the font size class. |
| 79 |
$font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $context['fontSize'] ); |
| 80 |
} elseif ( $has_custom_font_size ) { |
| 81 |
// Add the custom font size inline style. |
| 82 |
$font_sizes['inline_styles'] = sprintf( 'font-size: %spx;', $context['customFontSize'] ); |
| 83 |
} |
| 84 |
|
| 85 |
return $font_sizes; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Returns the top-level submenu SVG chevron icon. |
| 90 |
* |
| 91 |
* @return string |
| 92 |
*/ |
| 93 |
function gutenberg_block_core_navigation_link_render_submenu_icon() { |
| 94 |
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>'; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Renders the `core/navigation-link` block. |
| 99 |
* |
| 100 |
* @param array $attributes The block attributes. |
| 101 |
* @param array $content The saved content. |
| 102 |
* @param array $block The parsed block. |
| 103 |
* |
| 104 |
* @return string Returns the post content with the legacy widget added. |
| 105 |
*/ |
| 106 |
function gutenberg_render_block_core_navigation_link( $attributes, $content, $block ) { |
| 107 |
// Don't render the block's subtree if it has no label. |
| 108 |
if ( empty( $attributes['label'] ) ) { |
| 109 |
return ''; |
| 110 |
} |
| 111 |
|
| 112 |
$colors = gutenberg_block_core_navigation_link_build_css_colors( $block->context ); |
| 113 |
$font_sizes = gutenberg_block_core_navigation_link_build_css_font_sizes( $block->context ); |
| 114 |
$classes = array_merge( |
| 115 |
$colors['css_classes'], |
| 116 |
$font_sizes['css_classes'] |
| 117 |
); |
| 118 |
$classes[] = 'wp-block-navigation-link'; |
| 119 |
$style_attribute = ( $colors['inline_styles'] || $font_sizes['inline_styles'] ); |
| 120 |
|
| 121 |
$css_classes = trim( implode( ' ', $classes ) ); |
| 122 |
$has_submenu = count( $block->inner_blocks ) > 0; |
| 123 |
$is_active = ! empty( $attributes['id'] ) && ( get_the_ID() === $attributes['id'] ); |
| 124 |
|
| 125 |
$class_name = ! empty( $attributes['className'] ) ? implode( ' ', (array) $attributes['className'] ) : false; |
| 126 |
|
| 127 |
if ( false !== $class_name ) { |
| 128 |
$css_classes .= ' ' . $class_name; |
| 129 |
}; |
| 130 |
|
| 131 |
$html = '<li class="' . esc_attr( $css_classes . ( $has_submenu ? ' has-child' : '' ) ) . |
| 132 |
( $is_active ? ' current-menu-item' : '' ) . '"' . $style_attribute . '>' . |
| 133 |
'<a class="wp-block-navigation-link__content"'; |
| 134 |
|
| 135 |
// Start appending HTML attributes to anchor tag. |
| 136 |
if ( isset( $attributes['url'] ) ) { |
| 137 |
$html .= ' href="' . esc_url( $attributes['url'] ) . '"'; |
| 138 |
} |
| 139 |
|
| 140 |
if ( isset( $attributes['opensInNewTab'] ) && true === $attributes['opensInNewTab'] ) { |
| 141 |
$html .= ' target="_blank" '; |
| 142 |
} |
| 143 |
// End appending HTML attributes to anchor tag. |
| 144 |
|
| 145 |
// Start anchor tag content. |
| 146 |
$html .= '>' . |
| 147 |
// Wrap title with span to isolate it from submenu icon. |
| 148 |
'<span class="wp-block-navigation-link__label">'; |
| 149 |
|
| 150 |
if ( isset( $attributes['label'] ) ) { |
| 151 |
$html .= wp_kses( |
| 152 |
$attributes['label'], |
| 153 |
array( |
| 154 |
'code' => array(), |
| 155 |
'em' => array(), |
| 156 |
'img' => array( |
| 157 |
'scale' => array(), |
| 158 |
'class' => array(), |
| 159 |
'style' => array(), |
| 160 |
'src' => array(), |
| 161 |
'alt' => array(), |
| 162 |
), |
| 163 |
's' => array(), |
| 164 |
'span' => array( |
| 165 |
'style' => array(), |
| 166 |
), |
| 167 |
'strong' => array(), |
| 168 |
) |
| 169 |
); |
| 170 |
} |
| 171 |
|
| 172 |
$html .= '</span>'; |
| 173 |
|
| 174 |
$html .= '</a>'; |
| 175 |
// End anchor tag content. |
| 176 |
|
| 177 |
if ( $block->context['showSubmenuIcon'] && $has_submenu ) { |
| 178 |
// The submenu icon can be hidden by a CSS rule on the Navigation Block. |
| 179 |
$html .= '<span class="wp-block-navigation-link__submenu-icon">' . gutenberg_block_core_navigation_link_render_submenu_icon() . '</span>'; |
| 180 |
} |
| 181 |
|
| 182 |
if ( $has_submenu ) { |
| 183 |
$inner_blocks_html = ''; |
| 184 |
foreach ( $block->inner_blocks as $inner_block ) { |
| 185 |
$inner_blocks_html .= $inner_block->render(); |
| 186 |
} |
| 187 |
|
| 188 |
// TODO - classname is wrong! |
| 189 |
$html .= sprintf( |
| 190 |
'<ul class="wp-block-navigation__container">%s</ul>', |
| 191 |
$inner_blocks_html |
| 192 |
); |
| 193 |
} |
| 194 |
|
| 195 |
$html .= '</li>'; |
| 196 |
|
| 197 |
return $html; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Register the navigation link block. |
| 202 |
* |
| 203 |
* @uses render_block_core_navigation() |
| 204 |
* @throws WP_Error An WP_Error exception parsing the block definition. |
| 205 |
*/ |
| 206 |
function gutenberg_register_block_core_navigation_link() { |
| 207 |
register_block_type_from_metadata( |
| 208 |
__DIR__ . '/navigation-link', |
| 209 |
array( |
| 210 |
'render_callback' => 'gutenberg_render_block_core_navigation_link', |
| 211 |
) |
| 212 |
); |
| 213 |
} |
| 214 |
add_action( 'init', 'gutenberg_register_block_core_navigation_link', 20 ); |
| 215 |
|