diff --git a/lib/blocks.php b/lib/blocks.php index 842ff6a6ec093e..e55f8e0a8adec7 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -29,6 +29,7 @@ function gutenberg_reregister_core_block_types() { 'media-text', 'missing', 'more', + 'navigation', 'nextpage', 'paragraph', 'preformatted', @@ -71,7 +72,6 @@ function gutenberg_reregister_core_block_types() { 'latest-comments.php' => 'core/latest-comments', 'latest-posts.php' => 'core/latest-posts', 'loginout.php' => 'core/loginout', - 'navigation.php' => 'core/navigation', 'navigation-link.php' => 'core/navigation-link', 'navigation-submenu.php' => 'core/navigation-submenu', 'page-list.php' => 'core/page-list', diff --git a/packages/block-library/src/navigation/block.json b/packages/block-library/src/navigation/block.json index 781251b101aa8d..ae0edfd902fe47 100644 --- a/packages/block-library/src/navigation/block.json +++ b/packages/block-library/src/navigation/block.json @@ -132,5 +132,6 @@ }, "viewScript": [ "file:./view.min.js" ], "editorStyle": "wp-block-navigation-editor", - "style": "wp-block-navigation" + "style": "wp-block-navigation", + "render": "../navigation.php" } diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 9c594696f94463..fbeaa6902cebc5 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -1,10 +1,12 @@ term_id, array( 'update_post_term_cache' => false ) ); - _wp_menu_item_classes_by_context( $menu_items ); + $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) ); + _wp_menu_item_classes_by_context( $menu_items ); - return $menu_items; + return $menu_items; + } } @@ -51,19 +55,21 @@ function block_core_navigation_get_menu_items_at_location( $location ) { * @return array An array keyed by the id of the parent menu where each element * is an array of menu items that belong to that parent. */ - function block_core_navigation_sort_menu_items_by_parent_id( $menu_items ) { - $sorted_menu_items = array(); - foreach ( (array) $menu_items as $menu_item ) { - $sorted_menu_items[ $menu_item->menu_order ] = $menu_item; - } - unset( $menu_items, $menu_item ); + if ( ! function_exists( 'block_core_navigation_sort_menu_items_by_parent_id' ) ) { + function block_core_navigation_sort_menu_items_by_parent_id( $menu_items ) { + $sorted_menu_items = array(); + foreach ( (array) $menu_items as $menu_item ) { + $sorted_menu_items[ $menu_item->menu_order ] = $menu_item; + } + unset( $menu_items, $menu_item ); - $menu_items_by_parent_id = array(); - foreach ( $sorted_menu_items as $menu_item ) { - $menu_items_by_parent_id[ $menu_item->menu_item_parent ][] = $menu_item; - } + $menu_items_by_parent_id = array(); + foreach ( $sorted_menu_items as $menu_item ) { + $menu_items_by_parent_id[ $menu_item->menu_item_parent ][] = $menu_item; + } - return $menu_items_by_parent_id; + return $menu_items_by_parent_id; + } } /** @@ -77,45 +83,47 @@ function block_core_navigation_sort_menu_items_by_parent_id( $menu_items ) { * that parent. * @return array An array of parsed block data. */ - function block_core_navigation_parse_blocks_from_menu_items( $menu_items, $menu_items_by_parent_id ) { - if ( empty( $menu_items ) ) { - return array(); - } - - $blocks = array(); - - foreach ( $menu_items as $menu_item ) { - $class_name = ! empty( $menu_item->classes ) ? implode( ' ', (array) $menu_item->classes ) : null; - $id = ( null !== $menu_item->object_id && 'custom' !== $menu_item->object ) ? $menu_item->object_id : null; - $opens_in_new_tab = null !== $menu_item->target && '_blank' === $menu_item->target; - $rel = ( null !== $menu_item->xfn && '' !== $menu_item->xfn ) ? $menu_item->xfn : null; - $kind = null !== $menu_item->type ? str_replace( '_', '-', $menu_item->type ) : 'custom'; - - $block = array( - 'blockName' => isset( $menu_items_by_parent_id[ $menu_item->ID ] ) ? 'core/navigation-submenu' : 'core/navigation-link', - 'attrs' => array( - 'className' => $class_name, - 'description' => $menu_item->description, - 'id' => $id, - 'kind' => $kind, - 'label' => $menu_item->title, - 'opensInNewTab' => $opens_in_new_tab, - 'rel' => $rel, - 'title' => $menu_item->attr_title, - 'type' => $menu_item->object, - 'url' => $menu_item->url, - ), - ); + if ( ! function_exists( 'block_core_navigation_parse_blocks_from_menu_items' ) ) { + function block_core_navigation_parse_blocks_from_menu_items( $menu_items, $menu_items_by_parent_id ) { + if ( empty( $menu_items ) ) { + return array(); + } - $block['innerBlocks'] = isset( $menu_items_by_parent_id[ $menu_item->ID ] ) - ? block_core_navigation_parse_blocks_from_menu_items( $menu_items_by_parent_id[ $menu_item->ID ], $menu_items_by_parent_id ) - : array(); - $block['innerContent'] = array_map( 'serialize_block', $block['innerBlocks'] ); + $blocks = array(); + + foreach ( $menu_items as $menu_item ) { + $class_name = ! empty( $menu_item->classes ) ? implode( ' ', (array) $menu_item->classes ) : null; + $id = ( null !== $menu_item->object_id && 'custom' !== $menu_item->object ) ? $menu_item->object_id : null; + $opens_in_new_tab = null !== $menu_item->target && '_blank' === $menu_item->target; + $rel = ( null !== $menu_item->xfn && '' !== $menu_item->xfn ) ? $menu_item->xfn : null; + $kind = null !== $menu_item->type ? str_replace( '_', '-', $menu_item->type ) : 'custom'; + + $block = array( + 'blockName' => isset( $menu_items_by_parent_id[ $menu_item->ID ] ) ? 'core/navigation-submenu' : 'core/navigation-link', + 'attrs' => array( + 'className' => $class_name, + 'description' => $menu_item->description, + 'id' => $id, + 'kind' => $kind, + 'label' => $menu_item->title, + 'opensInNewTab' => $opens_in_new_tab, + 'rel' => $rel, + 'title' => $menu_item->attr_title, + 'type' => $menu_item->object, + 'url' => $menu_item->url, + ), + ); + + $block['innerBlocks'] = isset( $menu_items_by_parent_id[ $menu_item->ID ] ) + ? block_core_navigation_parse_blocks_from_menu_items( $menu_items_by_parent_id[ $menu_item->ID ], $menu_items_by_parent_id ) + : array(); + $block['innerContent'] = array_map( 'serialize_block', $block['innerBlocks'] ); + + $blocks[] = $block; + } - $blocks[] = $block; + return $blocks; } - - return $blocks; } } @@ -127,87 +135,89 @@ function block_core_navigation_parse_blocks_from_menu_items( $menu_items, $menu_ * * @return array Colors CSS classes and inline styles. */ -function block_core_navigation_build_css_colors( $attributes ) { - $colors = array( - 'css_classes' => array(), - 'inline_styles' => '', - 'overlay_css_classes' => array(), - 'overlay_inline_styles' => '', - ); +if ( ! function_exists( 'block_core_navigation_build_css_colors' ) ) { + function block_core_navigation_build_css_colors( $attributes ) { + $colors = array( + 'css_classes' => array(), + 'inline_styles' => '', + 'overlay_css_classes' => array(), + 'overlay_inline_styles' => '', + ); - // Text color. - $has_named_text_color = array_key_exists( 'textColor', $attributes ); - $has_custom_text_color = array_key_exists( 'customTextColor', $attributes ); + // Text color. + $has_named_text_color = array_key_exists( 'textColor', $attributes ); + $has_custom_text_color = array_key_exists( 'customTextColor', $attributes ); - // If has text color. - if ( $has_custom_text_color || $has_named_text_color ) { - // Add has-text-color class. - $colors['css_classes'][] = 'has-text-color'; - } + // If has text color. + if ( $has_custom_text_color || $has_named_text_color ) { + // Add has-text-color class. + $colors['css_classes'][] = 'has-text-color'; + } - if ( $has_named_text_color ) { - // Add the color class. - $colors['css_classes'][] = sprintf( 'has-%s-color', $attributes['textColor'] ); - } elseif ( $has_custom_text_color ) { - // Add the custom color inline style. - $colors['inline_styles'] .= sprintf( 'color: %s;', $attributes['customTextColor'] ); - } + if ( $has_named_text_color ) { + // Add the color class. + $colors['css_classes'][] = sprintf( 'has-%s-color', $attributes['textColor'] ); + } elseif ( $has_custom_text_color ) { + // Add the custom color inline style. + $colors['inline_styles'] .= sprintf( 'color: %s;', $attributes['customTextColor'] ); + } - // Background color. - $has_named_background_color = array_key_exists( 'backgroundColor', $attributes ); - $has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes ); + // Background color. + $has_named_background_color = array_key_exists( 'backgroundColor', $attributes ); + $has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes ); - // If has background color. - if ( $has_custom_background_color || $has_named_background_color ) { - // Add has-background class. - $colors['css_classes'][] = 'has-background'; - } + // If has background color. + if ( $has_custom_background_color || $has_named_background_color ) { + // Add has-background class. + $colors['css_classes'][] = 'has-background'; + } - if ( $has_named_background_color ) { - // Add the background-color class. - $colors['css_classes'][] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] ); - } elseif ( $has_custom_background_color ) { - // Add the custom background-color inline style. - $colors['inline_styles'] .= sprintf( 'background-color: %s;', $attributes['customBackgroundColor'] ); - } + if ( $has_named_background_color ) { + // Add the background-color class. + $colors['css_classes'][] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] ); + } elseif ( $has_custom_background_color ) { + // Add the custom background-color inline style. + $colors['inline_styles'] .= sprintf( 'background-color: %s;', $attributes['customBackgroundColor'] ); + } - // Overlay text color. - $has_named_overlay_text_color = array_key_exists( 'overlayTextColor', $attributes ); - $has_custom_overlay_text_color = array_key_exists( 'customOverlayTextColor', $attributes ); + // Overlay text color. + $has_named_overlay_text_color = array_key_exists( 'overlayTextColor', $attributes ); + $has_custom_overlay_text_color = array_key_exists( 'customOverlayTextColor', $attributes ); - // If has overlay text color. - if ( $has_custom_overlay_text_color || $has_named_overlay_text_color ) { - // Add has-text-color class. - $colors['overlay_css_classes'][] = 'has-text-color'; - } + // If has overlay text color. + if ( $has_custom_overlay_text_color || $has_named_overlay_text_color ) { + // Add has-text-color class. + $colors['overlay_css_classes'][] = 'has-text-color'; + } - if ( $has_named_overlay_text_color ) { - // Add the overlay color class. - $colors['overlay_css_classes'][] = sprintf( 'has-%s-color', $attributes['overlayTextColor'] ); - } elseif ( $has_custom_overlay_text_color ) { - // Add the custom overlay color inline style. - $colors['overlay_inline_styles'] .= sprintf( 'color: %s;', $attributes['customOverlayTextColor'] ); - } + if ( $has_named_overlay_text_color ) { + // Add the overlay color class. + $colors['overlay_css_classes'][] = sprintf( 'has-%s-color', $attributes['overlayTextColor'] ); + } elseif ( $has_custom_overlay_text_color ) { + // Add the custom overlay color inline style. + $colors['overlay_inline_styles'] .= sprintf( 'color: %s;', $attributes['customOverlayTextColor'] ); + } - // Overlay background color. - $has_named_overlay_background_color = array_key_exists( 'overlayBackgroundColor', $attributes ); - $has_custom_overlay_background_color = array_key_exists( 'customOverlayBackgroundColor', $attributes ); + // Overlay background color. + $has_named_overlay_background_color = array_key_exists( 'overlayBackgroundColor', $attributes ); + $has_custom_overlay_background_color = array_key_exists( 'customOverlayBackgroundColor', $attributes ); - // If has overlay background color. - if ( $has_custom_overlay_background_color || $has_named_overlay_background_color ) { - // Add has-background class. - $colors['overlay_css_classes'][] = 'has-background'; - } + // If has overlay background color. + if ( $has_custom_overlay_background_color || $has_named_overlay_background_color ) { + // Add has-background class. + $colors['overlay_css_classes'][] = 'has-background'; + } - if ( $has_named_overlay_background_color ) { - // Add the overlay background-color class. - $colors['overlay_css_classes'][] = sprintf( 'has-%s-background-color', $attributes['overlayBackgroundColor'] ); - } elseif ( $has_custom_overlay_background_color ) { - // Add the custom overlay background-color inline style. - $colors['overlay_inline_styles'] .= sprintf( 'background-color: %s;', $attributes['customOverlayBackgroundColor'] ); - } + if ( $has_named_overlay_background_color ) { + // Add the overlay background-color class. + $colors['overlay_css_classes'][] = sprintf( 'has-%s-background-color', $attributes['overlayBackgroundColor'] ); + } elseif ( $has_custom_overlay_background_color ) { + // Add the custom overlay background-color inline style. + $colors['overlay_inline_styles'] .= sprintf( 'background-color: %s;', $attributes['customOverlayBackgroundColor'] ); + } - return $colors; + return $colors; + } } /** @@ -218,25 +228,27 @@ function block_core_navigation_build_css_colors( $attributes ) { * * @return array Font size CSS classes and inline styles. */ -function block_core_navigation_build_css_font_sizes( $attributes ) { - // CSS classes. - $font_sizes = array( - 'css_classes' => array(), - 'inline_styles' => '', - ); +if ( ! function_exists( 'block_core_navigation_build_css_font_sizes' ) ) { + function block_core_navigation_build_css_font_sizes( $attributes ) { + // CSS classes. + $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 ); + $has_named_font_size = array_key_exists( 'fontSize', $attributes ); + $has_custom_font_size = array_key_exists( 'customFontSize', $attributes ); - if ( $has_named_font_size ) { - // Add the font size class. - $font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $attributes['fontSize'] ); - } elseif ( $has_custom_font_size ) { - // Add the custom font size inline style. - $font_sizes['inline_styles'] = sprintf( 'font-size: %spx;', $attributes['customFontSize'] ); - } + if ( $has_named_font_size ) { + // Add the font size class. + $font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $attributes['fontSize'] ); + } elseif ( $has_custom_font_size ) { + // Add the custom font size inline style. + $font_sizes['inline_styles'] = sprintf( 'font-size: %spx;', $attributes['customFontSize'] ); + } - return $font_sizes; + return $font_sizes; + } } /** @@ -244,8 +256,10 @@ function block_core_navigation_build_css_font_sizes( $attributes ) { * * @return string */ -function block_core_navigation_render_submenu_icon() { - return ''; +if ( ! function_exists( 'block_core_navigation_render_submenu_icon' ) ) { + function block_core_navigation_render_submenu_icon() { + return ''; + } } @@ -254,23 +268,25 @@ function block_core_navigation_render_submenu_icon() { * * @return WP_Post|null the first non-empty Navigation or null. */ -function block_core_navigation_get_most_recently_published_navigation() { - // We default to the most recently created menu. - $parsed_args = array( - 'post_type' => 'wp_navigation', - 'no_found_rows' => true, - 'order' => 'DESC', - 'orderby' => 'date', - 'post_status' => 'publish', - 'posts_per_page' => 1, // get only the most recent. - ); +if ( ! function_exists( 'block_core_navigation_get_most_recently_published_navigation' ) ) { + function block_core_navigation_get_most_recently_published_navigation() { + // We default to the most recently created menu. + $parsed_args = array( + 'post_type' => 'wp_navigation', + 'no_found_rows' => true, + 'order' => 'DESC', + 'orderby' => 'date', + 'post_status' => 'publish', + 'posts_per_page' => 1, // get only the most recent. + ); - $navigation_post = new WP_Query( $parsed_args ); - if ( count( $navigation_post->posts ) > 0 ) { - return $navigation_post->posts[0]; - } + $navigation_post = new WP_Query( $parsed_args ); + if ( count( $navigation_post->posts ) > 0 ) { + return $navigation_post->posts[0]; + } - return null; + return null; + } } /** @@ -282,16 +298,18 @@ function block_core_navigation_get_most_recently_published_navigation() { * @param array $parsed_blocks the parsed blocks to be normalized. * @return array the normalized parsed blocks. */ -function block_core_navigation_filter_out_empty_blocks( $parsed_blocks ) { - $filtered = array_filter( - $parsed_blocks, - function( $block ) { - return isset( $block['blockName'] ); - } - ); +if ( ! function_exists( 'block_core_navigation_filter_out_empty_blocks' ) ) { + function block_core_navigation_filter_out_empty_blocks( $parsed_blocks ) { + $filtered = array_filter( + $parsed_blocks, + function ( $block ) { + return isset( $block['blockName'] ); + } + ); - // Reset keys. - return array_values( $filtered ); + // Reset keys. + return array_values( $filtered ); + } } /** @@ -303,46 +321,48 @@ function( $block ) { * * @return array the array of blocks to be used as a fallback. */ -function block_core_navigation_get_fallback_blocks() { - $page_list_fallback = array( - array( - 'blockName' => 'core/page-list', - 'attrs' => array( - '__unstableMaxPages' => 4, +if ( ! function_exists( 'block_core_navigation_get_fallback_blocks' ) ) { + function block_core_navigation_get_fallback_blocks() { + $page_list_fallback = array( + array( + 'blockName' => 'core/page-list', + 'attrs' => array( + '__unstableMaxPages' => 4, + ), ), - ), - ); + ); - $registry = WP_Block_Type_Registry::get_instance(); + $registry = WP_Block_Type_Registry::get_instance(); - // If `core/page-list` is not registered then return empty blocks. - $fallback_blocks = $registry->is_registered( 'core/page-list' ) ? $page_list_fallback : array(); + // If `core/page-list` is not registered then return empty blocks. + $fallback_blocks = $registry->is_registered( 'core/page-list' ) ? $page_list_fallback : array(); - // Default to a list of Pages. + // Default to a list of Pages. - $navigation_post = block_core_navigation_get_most_recently_published_navigation(); + $navigation_post = block_core_navigation_get_most_recently_published_navigation(); - // Prefer using the first non-empty Navigation as fallback if available. - if ( $navigation_post ) { - $maybe_fallback = block_core_navigation_filter_out_empty_blocks( parse_blocks( $navigation_post->post_content ) ); + // Prefer using the first non-empty Navigation as fallback if available. + if ( $navigation_post ) { + $maybe_fallback = block_core_navigation_filter_out_empty_blocks( parse_blocks( $navigation_post->post_content ) ); - // Normalizing blocks may result in an empty array of blocks if they were all `null` blocks. - // In this case default to the (Page List) fallback. - $fallback_blocks = ! empty( $maybe_fallback ) ? $maybe_fallback : $fallback_blocks; - } + // Normalizing blocks may result in an empty array of blocks if they were all `null` blocks. + // In this case default to the (Page List) fallback. + $fallback_blocks = ! empty( $maybe_fallback ) ? $maybe_fallback : $fallback_blocks; + } - /** - * Filters the fallback experience for the Navigation block. - * - * Returning a falsey value will opt out of the fallback and cause the block not to render. - * To customise the blocks provided return an array of blocks - these should be valid - * children of the `core/navigation` block. - * - * @since 5.9.0 - * - * @param array[] default fallback blocks provided by the default block mechanic. - */ - return apply_filters( 'block_core_navigation_render_fallback', $fallback_blocks ); + /** + * Filters the fallback experience for the Navigation block. + * + * Returning a falsey value will opt out of the fallback and cause the block not to render. + * To customise the blocks provided return an array of blocks - these should be valid + * children of the `core/navigation` block. + * + * @since 5.9.0 + * + * @param array[] default fallback blocks provided by the default block mechanic. + */ + return apply_filters( 'block_core_navigation_render_fallback', $fallback_blocks ); + } } /** @@ -352,9 +372,11 @@ function block_core_navigation_get_fallback_blocks() { * * @return array Array of post IDs. */ -function block_core_navigation_get_post_ids( $inner_blocks ) { - $post_ids = array_map( 'block_core_navigation_from_block_get_post_ids', iterator_to_array( $inner_blocks ) ); - return array_unique( array_merge( ...$post_ids ) ); +if ( ! function_exists( 'block_core_navigation_get_post_ids' ) ) { + function block_core_navigation_get_post_ids( $inner_blocks ) { + $post_ids = array_map( 'block_core_navigation_from_block_get_post_ids', iterator_to_array( $inner_blocks ) ); + return array_unique( array_merge( ...$post_ids ) ); + } } /** @@ -364,375 +386,330 @@ function block_core_navigation_get_post_ids( $inner_blocks ) { * * @return array Array of post IDs. */ -function block_core_navigation_from_block_get_post_ids( $block ) { - $post_ids = array(); +if ( ! function_exists( 'block_core_navigation_from_block_get_post_ids' ) ) { + function block_core_navigation_from_block_get_post_ids( $block ) { + $post_ids = array(); - if ( $block->inner_blocks ) { - $post_ids = block_core_navigation_get_post_ids( $block->inner_blocks ); - } + if ( $block->inner_blocks ) { + $post_ids = block_core_navigation_get_post_ids( $block->inner_blocks ); + } - if ( 'core/navigation-link' === $block->name || 'core/navigation-submenu' === $block->name ) { - if ( $block->attributes && isset( $block->attributes['kind'] ) && 'post-type' === $block->attributes['kind'] && isset( $block->attributes['id'] ) ) { - $post_ids[] = $block->attributes['id']; + if ( 'core/navigation-link' === $block->name || 'core/navigation-submenu' === $block->name ) { + if ( $block->attributes && isset( $block->attributes['kind'] ) && 'post-type' === $block->attributes['kind'] && isset( $block->attributes['id'] ) ) { + $post_ids[] = $block->attributes['id']; + } } - } - return $post_ids; + return $post_ids; + } } /** - * Renders the `core/navigation` block on server. + * Filter that changes the parsed attribute values of navigation blocks contain typographic presets to contain the values directly. * - * @param array $attributes The block attributes. - * @param string $content The saved content. - * @param WP_Block $block The parsed block. + * @param array $parsed_block The block being rendered. * - * @return string Returns the post content with the legacy widget added. + * @return array The block being rendered without typographic presets. */ -function render_block_core_navigation( $attributes, $content, $block ) { - - static $seen_menu_names = array(); - - // Flag used to indicate whether the rendered output is considered to be - // a fallback (i.e. the block has no menu associated with it). - $is_fallback = false; - - $nav_menu_name = ''; +if ( ! function_exists( 'block_core_navigation_typographic_presets_backcompatibility' ) ) { + function block_core_navigation_typographic_presets_backcompatibility( $parsed_block ) { + if ( 'core/navigation' === $parsed_block['blockName'] ) { + $attribute_to_prefix_map = array( + 'fontStyle' => 'var:preset|font-style|', + 'fontWeight' => 'var:preset|font-weight|', + 'textDecoration' => 'var:preset|text-decoration|', + 'textTransform' => 'var:preset|text-transform|', + ); + foreach ( $attribute_to_prefix_map as $style_attribute => $prefix ) { + if ( ! empty( $parsed_block['attrs']['style']['typography'][ $style_attribute ] ) ) { + $prefix_len = strlen( $prefix ); + $attribute_value = &$parsed_block['attrs']['style']['typography'][ $style_attribute ]; + if ( 0 === strncmp( $attribute_value, $prefix, $prefix_len ) ) { + $attribute_value = substr( $attribute_value, $prefix_len ); + } + if ( 'textDecoration' === $style_attribute && 'strikethrough' === $attribute_value ) { + $attribute_value = 'line-through'; + } + } + } + } - /** - * Deprecated: - * The rgbTextColor and rgbBackgroundColor attributes - * have been deprecated in favor of - * customTextColor and customBackgroundColor ones. - * Move the values from old attrs to the new ones. - */ - if ( isset( $attributes['rgbTextColor'] ) && empty( $attributes['textColor'] ) ) { - $attributes['customTextColor'] = $attributes['rgbTextColor']; + return $parsed_block; } - if ( isset( $attributes['rgbBackgroundColor'] ) && empty( $attributes['backgroundColor'] ) ) { - $attributes['customBackgroundColor'] = $attributes['rgbBackgroundColor']; - } + add_filter( 'render_block_data', 'block_core_navigation_typographic_presets_backcompatibility' ); +} - unset( $attributes['rgbTextColor'], $attributes['rgbBackgroundColor'] ); +/** + * From here it is included included what was supposed to go in the `render_block_core_navigation` function + */ - /** - * This is for backwards compatibility after `isResponsive` attribute has been removed. - */ - $has_old_responsive_attribute = ! empty( $attributes['isResponsive'] ) && $attributes['isResponsive']; - $is_responsive_menu = isset( $attributes['overlayMenu'] ) && 'never' !== $attributes['overlayMenu'] || $has_old_responsive_attribute; - $should_load_view_script = ! wp_script_is( 'wp-block-navigation-view' ) && ( $is_responsive_menu || $attributes['openSubmenusOnClick'] || $attributes['showSubmenuIcon'] ); - if ( $should_load_view_script ) { - wp_enqueue_script( 'wp-block-navigation-view' ); - } +static $seen_menu_names = array(); - $should_load_modal_view_script = isset( $attributes['overlayMenu'] ) && 'never' !== $attributes['overlayMenu']; - if ( $should_load_modal_view_script ) { - // wp_enqueue_script( 'wp-block-navigation-view-modal' ); - } +// Flag used to indicate whether the rendered output is considered to be +// a fallback (i.e. the block has no menu associated with it). +$is_fallback = false; - $inner_blocks = $block->inner_blocks; +$nav_menu_name = ''; - // Ensure that blocks saved with the legacy ref attribute name (navigationMenuId) continue to render. - if ( array_key_exists( 'navigationMenuId', $attributes ) ) { - $attributes['ref'] = $attributes['navigationMenuId']; - } +/** + * Deprecated: + * The rgbTextColor and rgbBackgroundColor attributes + * have been deprecated in favor of + * customTextColor and customBackgroundColor ones. + * Move the values from old attrs to the new ones. + */ +if ( isset( $attributes['rgbTextColor'] ) && empty( $attributes['textColor'] ) ) { + $attributes['customTextColor'] = $attributes['rgbTextColor']; +} - // If: - // - the gutenberg plugin is active - // - `__unstableLocation` is defined - // - we have menu items at the defined location - // - we don't have a relationship to a `wp_navigation` Post (via `ref`). - // ...then create inner blocks from the classic menu assigned to that location. - if ( - defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN && - array_key_exists( '__unstableLocation', $attributes ) && - ! array_key_exists( 'ref', $attributes ) && - ! empty( block_core_navigation_get_menu_items_at_location( $attributes['__unstableLocation'] ) ) - ) { - $menu_items = block_core_navigation_get_menu_items_at_location( $attributes['__unstableLocation'] ); - if ( empty( $menu_items ) ) { - return ''; - } +if ( isset( $attributes['rgbBackgroundColor'] ) && empty( $attributes['backgroundColor'] ) ) { + $attributes['customBackgroundColor'] = $attributes['rgbBackgroundColor']; +} - $menu_items_by_parent_id = block_core_navigation_sort_menu_items_by_parent_id( $menu_items ); - $parsed_blocks = block_core_navigation_parse_blocks_from_menu_items( $menu_items_by_parent_id[0], $menu_items_by_parent_id ); - $inner_blocks = new WP_Block_List( $parsed_blocks, $attributes ); - } +unset( $attributes['rgbTextColor'], $attributes['rgbBackgroundColor'] ); - // Load inner blocks from the navigation post. - if ( array_key_exists( 'ref', $attributes ) ) { - $navigation_post = get_post( $attributes['ref'] ); - if ( ! isset( $navigation_post ) ) { - return ''; - } +/** + * This is for backwards compatibility after `isResponsive` attribute has been removed. + */ +$has_old_responsive_attribute = ! empty( $attributes['isResponsive'] ) && $attributes['isResponsive']; +$is_responsive_menu = isset( $attributes['overlayMenu'] ) && 'never' !== $attributes['overlayMenu'] || $has_old_responsive_attribute; +$should_load_view_script = ! wp_script_is( 'wp-block-navigation-view' ) && ( $is_responsive_menu || $attributes['openSubmenusOnClick'] || $attributes['showSubmenuIcon'] ); +if ( $should_load_view_script ) { + wp_enqueue_script( 'wp-block-navigation-view' ); +} - // Only published posts are valid. If this is changed then a corresponding change - // must also be implemented in `use-navigation-menu.js`. - if ( 'publish' === $navigation_post->post_status ) { - $nav_menu_name = $navigation_post->post_title; +$should_load_modal_view_script = isset( $attributes['overlayMenu'] ) && 'never' !== $attributes['overlayMenu']; +if ( $should_load_modal_view_script ) { + // wp_enqueue_script( 'wp-block-navigation-view-modal' ); +} - if ( isset( $seen_menu_names[ $nav_menu_name ] ) ) { - ++$seen_menu_names[ $nav_menu_name ]; - } else { - $seen_menu_names[ $nav_menu_name ] = 1; - } +$inner_blocks = $block->inner_blocks; - $parsed_blocks = parse_blocks( $navigation_post->post_content ); +// Ensure that blocks saved with the legacy ref attribute name (navigationMenuId) continue to render. +if ( array_key_exists( 'navigationMenuId', $attributes ) ) { + $attributes['ref'] = $attributes['navigationMenuId']; +} - // 'parse_blocks' includes a null block with '\n\n' as the content when - // it encounters whitespace. This code strips it. - $compacted_blocks = block_core_navigation_filter_out_empty_blocks( $parsed_blocks ); +// If: +// - the gutenberg plugin is active +// - `__unstableLocation` is defined +// - we have menu items at the defined location +// - we don't have a relationship to a `wp_navigation` Post (via `ref`). +// ...then create inner blocks from the classic menu assigned to that location. +if ( + defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN && + array_key_exists( '__unstableLocation', $attributes ) && + ! array_key_exists( 'ref', $attributes ) && + ! empty( block_core_navigation_get_menu_items_at_location( $attributes['__unstableLocation'] ) ) +) { + $menu_items = block_core_navigation_get_menu_items_at_location( $attributes['__unstableLocation'] ); + if ( empty( $menu_items ) ) { + return ''; + } + + $menu_items_by_parent_id = block_core_navigation_sort_menu_items_by_parent_id( $menu_items ); + $parsed_blocks = block_core_navigation_parse_blocks_from_menu_items( $menu_items_by_parent_id[0], $menu_items_by_parent_id ); + $inner_blocks = new WP_Block_List( $parsed_blocks, $attributes ); +} - // TODO - this uses the full navigation block attributes for the - // context which could be refined. - $inner_blocks = new WP_Block_List( $compacted_blocks, $attributes ); - } +// Load inner blocks from the navigation post. +if ( array_key_exists( 'ref', $attributes ) ) { + $navigation_post = get_post( $attributes['ref'] ); + if ( ! isset( $navigation_post ) ) { + return ''; } - // If there are no inner blocks then fallback to rendering an appropriate fallback. - if ( empty( $inner_blocks ) ) { - $is_fallback = true; // indicate we are rendering the fallback. - - $fallback_blocks = block_core_navigation_get_fallback_blocks(); + // Only published posts are valid. If this is changed then a corresponding change + // must also be implemented in `use-navigation-menu.js`. + if ( 'publish' === $navigation_post->post_status ) { + $nav_menu_name = $navigation_post->post_title; - // Fallback my have been filtered so do basic test for validity. - if ( empty( $fallback_blocks ) || ! is_array( $fallback_blocks ) ) { - return ''; + if ( isset( $seen_menu_names[ $nav_menu_name ] ) ) { + ++$seen_menu_names[ $nav_menu_name ]; + } else { + $seen_menu_names[ $nav_menu_name ] = 1; } - $inner_blocks = new WP_Block_List( $fallback_blocks, $attributes ); + $parsed_blocks = parse_blocks( $navigation_post->post_content ); + + // 'parse_blocks' includes a null block with '\n\n' as the content when + // it encounters whitespace. This code strips it. + $compacted_blocks = block_core_navigation_filter_out_empty_blocks( $parsed_blocks ); + + // TODO - this uses the full navigation block attributes for the + // context which could be refined. + $inner_blocks = new WP_Block_List( $compacted_blocks, $attributes ); } +} - /** - * Filter navigation block $inner_blocks. - * Allows modification of a navigation block menu items. - * - * @since 6.1.0 - * - * @param \WP_Block_List $inner_blocks - */ - $inner_blocks = apply_filters( 'block_core_navigation_render_inner_blocks', $inner_blocks ); +// If there are no inner blocks then fallback to rendering an appropriate fallback. +if ( empty( $inner_blocks ) ) { + $is_fallback = true; // indicate we are rendering the fallback. - $layout_justification = array( - 'left' => 'items-justified-left', - 'right' => 'items-justified-right', - 'center' => 'items-justified-center', - 'space-between' => 'items-justified-space-between', - ); + $fallback_blocks = block_core_navigation_get_fallback_blocks(); - // Restore legacy classnames for submenu positioning. - $layout_class = ''; - if ( isset( $attributes['layout']['justifyContent'] ) ) { - $layout_class .= $layout_justification[ $attributes['layout']['justifyContent'] ]; - } - if ( isset( $attributes['layout']['orientation'] ) && 'vertical' === $attributes['layout']['orientation'] ) { - $layout_class .= ' is-vertical'; + // Fallback my have been filtered so do basic test for validity. + if ( empty( $fallback_blocks ) || ! is_array( $fallback_blocks ) ) { + return ''; } - if ( isset( $attributes['layout']['flexWrap'] ) && 'nowrap' === $attributes['layout']['flexWrap'] ) { - $layout_class .= ' no-wrap'; - } + $inner_blocks = new WP_Block_List( $fallback_blocks, $attributes ); +} - // Manually add block support text decoration as CSS class. - $text_decoration = _wp_array_get( $attributes, array( 'style', 'typography', 'textDecoration' ), null ); - $text_decoration_class = sprintf( 'has-text-decoration-%s', $text_decoration ); - - $colors = block_core_navigation_build_css_colors( $attributes ); - $font_sizes = block_core_navigation_build_css_font_sizes( $attributes ); - $classes = array_merge( - $colors['css_classes'], - $font_sizes['css_classes'], - $is_responsive_menu ? array( 'is-responsive' ) : array(), - $layout_class ? array( $layout_class ) : array(), - $is_fallback ? array( 'is-fallback' ) : array(), - $text_decoration ? array( $text_decoration_class ) : array() - ); +/** + * Filter navigation block $inner_blocks. + * Allows modification of a navigation block menu items. + * + * @since 6.1.0 + * + * @param \WP_Block_List $inner_blocks + */ +$inner_blocks = apply_filters( 'block_core_navigation_render_inner_blocks', $inner_blocks ); + +$layout_justification = array( + 'left' => 'items-justified-left', + 'right' => 'items-justified-right', + 'center' => 'items-justified-center', + 'space-between' => 'items-justified-space-between', +); + +// Restore legacy classnames for submenu positioning. +$layout_class = ''; +if ( isset( $attributes['layout']['justifyContent'] ) ) { + $layout_class .= $layout_justification[ $attributes['layout']['justifyContent'] ]; +} +if ( isset( $attributes['layout']['orientation'] ) && 'vertical' === $attributes['layout']['orientation'] ) { + $layout_class .= ' is-vertical'; +} - $post_ids = block_core_navigation_get_post_ids( $inner_blocks ); - if ( $post_ids ) { - _prime_post_caches( $post_ids, false, false ); - } +if ( isset( $attributes['layout']['flexWrap'] ) && 'nowrap' === $attributes['layout']['flexWrap'] ) { + $layout_class .= ' no-wrap'; +} - $inner_blocks_html = ''; - $is_list_open = false; - $is_first_item = false; - foreach ( $inner_blocks as $inner_block ) { - if ( ( 'core/navigation-link' === $inner_block->name || 'core/home-link' === $inner_block->name || 'core/site-title' === $inner_block->name || 'core/site-logo' === $inner_block->name || 'core/navigation-submenu' === $inner_block->name ) && ! $is_list_open ) { - $is_list_open = true; - $inner_blocks_html .= '
'; - } - $inner_block_content = $inner_block->render(); - if (!$is_first_item) { - $w = new WP_HTML_Tag_Processor($inner_block_content); - $w->next_tag('a'); - $w->set_attribute('wp-effect', 'core.navigation.addFirstElementToContext'); - $inner_block_content = (string) $w; - $is_first_item = true; - } - if ( 'core/site-title' === $inner_block->name || ( 'core/site-logo' === $inner_block->name && $inner_block_content ) ) { - $inner_blocks_html .= '