| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `core/navigation` 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 $attributes Navigation block attributes. |
| 13 |
* @return array Colors CSS classes and inline styles. |
| 14 |
*/ |
| 15 |
function gutenberg_build_css_colors( $attributes ) { |
| 16 |
$colors = array( |
| 17 |
'css_classes' => array(), |
| 18 |
'inline_styles' => '', |
| 19 |
); |
| 20 |
|
| 21 |
// Text color. |
| 22 |
$has_named_text_color = array_key_exists( 'textColor', $attributes ); |
| 23 |
$has_custom_text_color = array_key_exists( 'customTextColor', $attributes ); |
| 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', $attributes['textColor'] ); |
| 34 |
} elseif ( $has_custom_text_color ) { |
| 35 |
// Add the custom color inline style. |
| 36 |
$colors['inline_styles'] .= sprintf( 'color: %s;', $attributes['customTextColor'] ); |
| 37 |
} |
| 38 |
|
| 39 |
// Background color. |
| 40 |
$has_named_background_color = array_key_exists( 'backgroundColor', $attributes ); |
| 41 |
$has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes ); |
| 42 |
|
| 43 |
// If has background color. |
| 44 |
if ( $has_custom_background_color || $has_named_background_color ) { |
| 45 |
// Add has-background-color class. |
| 46 |
$colors['css_classes'][] = 'has-background-color'; |
| 47 |
} |
| 48 |
|
| 49 |
if ( $has_named_background_color ) { |
| 50 |
// Add the background-color class. |
| 51 |
$colors['css_classes'][] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] ); |
| 52 |
} elseif ( $has_custom_background_color ) { |
| 53 |
// Add the custom background-color inline style. |
| 54 |
$colors['inline_styles'] .= sprintf( 'background-color: %s;', $attributes['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 $attributes Navigation block attributes. |
| 65 |
* @return array Font size CSS classes and inline styles. |
| 66 |
*/ |
| 67 |
function gutenberg_build_css_font_sizes( $attributes ) { |
| 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', $attributes ); |
| 75 |
$has_custom_font_size = array_key_exists( 'customFontSize', $attributes ); |
| 76 |
|
| 77 |
if ( $has_named_font_size ) { |
| 78 |
// Add the font size class. |
| 79 |
$font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $attributes['fontSize'] ); |
| 80 |
} elseif ( $has_custom_font_size ) { |
| 81 |
// Add the custom font size inline style. |
| 82 |
$font_sizes['inline_styles'] = sprintf( 'font-size: %spx;', $attributes['customFontSize'] ); |
| 83 |
} |
| 84 |
|
| 85 |
return $font_sizes; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Recursively filters out links with no labels to build a clean navigation block structure. |
| 90 |
* |
| 91 |
* @param array $blocks Navigation link inner blocks from the Navigation block. |
| 92 |
* @return array Blocks that had valid labels |
| 93 |
*/ |
| 94 |
function gutenberg_gutenberg_remove_empty_navigation_links_recursive( $blocks ) { |
| 95 |
$blocks = array_filter( |
| 96 |
$blocks, |
| 97 |
function( $block ) { |
| 98 |
return ! empty( $block['attrs']['label'] ); |
| 99 |
} |
| 100 |
); |
| 101 |
|
| 102 |
if ( ! empty( $blocks ) ) { |
| 103 |
foreach ( $blocks as $key => $block ) { |
| 104 |
if ( ! empty( $block['innerBlocks'] ) ) { |
| 105 |
$blocks[ $key ]['innerBlocks'] = gutenberg_gutenberg_remove_empty_navigation_links_recursive( $block['innerBlocks'] ); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return $blocks; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Returns the top-level submenu SVG chevron icon. |
| 115 |
* |
| 116 |
* @return string |
| 117 |
*/ |
| 118 |
function gutenberg_render_submenu_icon() { |
| 119 |
return '<svg width="18" height="18" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 18 18" role="img" aria-hidden="true" focusable="false"><polygon points="9,13.5 14.7,7.9 13.2,6.5 9,10.7 4.8,6.5 3.3,7.9 "></polygon></svg>'; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Renders the `core/navigation` block on server. |
| 124 |
* |
| 125 |
* @param array $content The saved content. |
| 126 |
* @param array $block The parsed block. |
| 127 |
* |
| 128 |
* @return string Returns the post content with the legacy widget added. |
| 129 |
*/ |
| 130 |
function gutenberg_render_block_navigation( $content, $block ) { |
| 131 |
|
| 132 |
if ( 'core/navigation' !== $block['blockName'] ) { |
| 133 |
return $content; |
| 134 |
} |
| 135 |
|
| 136 |
$attributes = $block['attrs']; |
| 137 |
$block['innerBlocks'] = gutenberg_gutenberg_remove_empty_navigation_links_recursive( $block['innerBlocks'] ); |
| 138 |
|
| 139 |
if ( empty( $block['innerBlocks'] ) ) { |
| 140 |
return ''; |
| 141 |
} |
| 142 |
|
| 143 |
$colors = gutenberg_build_css_colors( $attributes ); |
| 144 |
$font_sizes = gutenberg_build_css_font_sizes( $attributes ); |
| 145 |
$classes = array_merge( |
| 146 |
$colors['css_classes'], |
| 147 |
$font_sizes['css_classes'], |
| 148 |
array( 'wp-block-navigation' ), |
| 149 |
isset( $attributes['className'] ) ? array( $attributes['className'] ) : array(), |
| 150 |
isset( $attributes['itemsJustification'] ) ? array( 'items-justified-' . $attributes['itemsJustification'] ) : array(), |
| 151 |
isset( $attributes['align'] ) ? array( 'align' . $attributes['align'] ) : array() |
| 152 |
); |
| 153 |
$class_attribute = sprintf( ' class="%s"', esc_attr( implode( ' ', $classes ) ) ); |
| 154 |
$style_attribute = ( $colors['inline_styles'] || $font_sizes['inline_styles'] ) |
| 155 |
? sprintf( ' style="%s"', esc_attr( $colors['inline_styles'] ) . esc_attr( $font_sizes['inline_styles'] ) ) |
| 156 |
: ''; |
| 157 |
|
| 158 |
return sprintf( |
| 159 |
'<nav %1$s %2$s>%3$s</nav>', |
| 160 |
$class_attribute, |
| 161 |
$style_attribute, |
| 162 |
gutenberg_build_navigation_html( $attributes, $block, $colors, $font_sizes, true ) |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Walks the inner block structure and returns an HTML list for it. |
| 168 |
* |
| 169 |
* @param array $attributes The Navigation block attributes. |
| 170 |
* @param array $block The NavigationItem block. |
| 171 |
* @param array $colors Contains inline styles and CSS classes to apply to navigation item. |
| 172 |
* @param array $font_sizes Contains inline styles and CSS classes to apply to navigation item. |
| 173 |
* @param bool $is_level_zero True whether is main menu (level zero). Otherwise, False. |
| 174 |
* |
| 175 |
* @return string Returns an HTML list from innerBlocks. |
| 176 |
*/ |
| 177 |
function gutenberg_build_navigation_html( $attributes, $block, $colors, $font_sizes, $is_level_zero = true ) { |
| 178 |
$html = ''; |
| 179 |
$classes = array_merge( |
| 180 |
$colors['css_classes'], |
| 181 |
$font_sizes['css_classes'] |
| 182 |
); |
| 183 |
$css_classes = implode( ' ', $classes ); |
| 184 |
$class_attribute = sprintf( ' class="wp-block-navigation-link__content %s"', esc_attr( trim( $css_classes ) ) ); |
| 185 |
$style_attribute = ( $colors['inline_styles'] || $font_sizes['inline_styles'] ) |
| 186 |
? sprintf( ' style="%s"', esc_attr( $colors['inline_styles'] ) . esc_attr( $font_sizes['inline_styles'] ) ) |
| 187 |
: ''; |
| 188 |
|
| 189 |
foreach ( (array) $block['innerBlocks'] as $key => $block ) { |
| 190 |
$has_submenu = count( (array) $block['innerBlocks'] ) > 0; |
| 191 |
|
| 192 |
$html .= '<li class="wp-block-navigation-link' . ( $has_submenu ? ' has-submenu' : '' ) . '">' . |
| 193 |
'<a'; |
| 194 |
|
| 195 |
if ( $is_level_zero ) { |
| 196 |
$html .= $class_attribute . $style_attribute; |
| 197 |
} |
| 198 |
|
| 199 |
// Start appending HTML attributes to anchor tag. |
| 200 |
if ( isset( $block['attrs']['url'] ) ) { |
| 201 |
$html .= ' href="' . esc_url( $block['attrs']['url'] ) . '"'; |
| 202 |
} |
| 203 |
if ( isset( $block['attrs']['title'] ) ) { |
| 204 |
$html .= ' title="' . esc_attr( $block['attrs']['title'] ) . '"'; |
| 205 |
} |
| 206 |
|
| 207 |
if ( isset( $block['attrs']['opensInNewTab'] ) && true === $block['attrs']['opensInNewTab'] ) { |
| 208 |
$html .= ' target="_blank" '; |
| 209 |
} |
| 210 |
// End appending HTML attributes to anchor tag. |
| 211 |
|
| 212 |
// Start anchor tag content. |
| 213 |
$html .= '>' . |
| 214 |
// Wrap title with span to isolate it from submenu icon. |
| 215 |
'<span class="wp-block-navigation-link__label">'; |
| 216 |
|
| 217 |
if ( isset( $block['attrs']['label'] ) ) { |
| 218 |
$html .= wp_kses( |
| 219 |
$block['attrs']['label'], |
| 220 |
array( |
| 221 |
'code' => array(), |
| 222 |
'em' => array(), |
| 223 |
'img' => array( |
| 224 |
'scale' => array(), |
| 225 |
'class' => array(), |
| 226 |
'style' => array(), |
| 227 |
'src' => array(), |
| 228 |
'alt' => array(), |
| 229 |
), |
| 230 |
's' => array(), |
| 231 |
'span' => array( |
| 232 |
'style' => array(), |
| 233 |
), |
| 234 |
'strong' => array(), |
| 235 |
) |
| 236 |
); |
| 237 |
} |
| 238 |
|
| 239 |
$html .= '</span>'; |
| 240 |
|
| 241 |
// Append submenu icon to top-level item. |
| 242 |
if ( ! empty( $attributes['showSubmenuIcon'] ) && $is_level_zero && $has_submenu ) { |
| 243 |
$html .= '<span class="wp-block-navigation-link__submenu-icon">' . gutenberg_render_submenu_icon() . '</span>'; |
| 244 |
} |
| 245 |
|
| 246 |
$html .= '</a>'; |
| 247 |
// End anchor tag content. |
| 248 |
|
| 249 |
if ( $has_submenu ) { |
| 250 |
$html .= gutenberg_build_navigation_html( $attributes, $block, $colors, $font_sizes, false ); |
| 251 |
} |
| 252 |
|
| 253 |
$html .= '</li>'; |
| 254 |
} |
| 255 |
return '<ul>' . $html . '</ul>'; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Register the navigation block. |
| 260 |
* |
| 261 |
* @uses gutenberg_render_block_navigation() |
| 262 |
* @throws WP_Error An WP_Error exception parsing the block definition. |
| 263 |
*/ |
| 264 |
function gutenberg_register_block_core_navigation() { |
| 265 |
|
| 266 |
register_block_type( |
| 267 |
'core/navigation', |
| 268 |
array( |
| 269 |
'attributes' => array( |
| 270 |
'className' => array( |
| 271 |
'type' => 'string', |
| 272 |
), |
| 273 |
'textColor' => array( |
| 274 |
'type' => 'string', |
| 275 |
), |
| 276 |
'customTextColor' => array( |
| 277 |
'type' => 'string', |
| 278 |
), |
| 279 |
'backgroundColor' => array( |
| 280 |
'type' => 'string', |
| 281 |
), |
| 282 |
'customBackgroundColor' => array( |
| 283 |
'type' => 'string', |
| 284 |
), |
| 285 |
'fontSize' => array( |
| 286 |
'type' => 'string', |
| 287 |
), |
| 288 |
'customFontSize' => array( |
| 289 |
'type' => 'number', |
| 290 |
), |
| 291 |
'itemsJustification' => array( |
| 292 |
'type' => 'string', |
| 293 |
), |
| 294 |
'showSubmenuIcon' => array( |
| 295 |
'type' => 'boolean', |
| 296 |
'default' => false, |
| 297 |
), |
| 298 |
), |
| 299 |
) |
| 300 |
); |
| 301 |
} |
| 302 |
add_action( 'init', 'gutenberg_register_block_core_navigation', 20 ); |
| 303 |
add_filter( 'render_block', 'gutenberg_render_block_navigation', 10, 2 ); |
| 304 |
|